Now I complete extension that provides facility to see the current state of query execution working on external session in form of EXPLAIN ANALYZE output. This extension works on 9.5 version, for 9.6 and later it doesn't support detailed statistics for parallel nodes yet.
I want to present patches to the latest version of PostgreSQL core to enable this extension.
My patch *custom_signal.patch* resolves the problem of «heavy» signal handlers. In essence, I follow the course offered in *procsignal.c* file. They define *ProcSignalReason* values on which the SUGUSR1 is multiplexed. Signal recent causes setting flags for *ProcessInterrupt* actuating, i.e. procsignal_sigusr1_handler() only sets specific flags. When CHECK_FOR_INTERRUPTS appears later on query execution *ProcessInterrupt* is called. Then triggered user defined signal handler is executed. As a result, we have a deferred signal handling.
Yes, but the problem is that nothing gives you the guarantee that at the moment you decide to handle the interrupt, the QueryDesc structures you're looking at are in a good shape for Explain* functions to run on them. Even if that appears to be the case in your testing now, there's no way to tell if that will be the case at any future point in time.
Another problem is use if shm_mq facility, because it manipulates the state of process latch. This is not supposed to happen to a backend happily performing its tasks, at random due to external factors, and this is what the proposed approach introduces.