Hi
I am sending a prototype with basic implementation with explain of running query specified by pid.
It can show more than the execution plan. There is a examples of full query text and running completion tag.
This patch is in early stage - I know, so there is one race condition.
I hoped so I can use new shm_mq API, but it is not prepared for usage where receiver and sender are mutable.
How it works:
postgres=# select pg_cmdstatus(pid,1) from pg_stat_activity where pid <> pg_backend_pid();
pg_cmdstatus
-------------------------------------------------------------------------------
Query Text: select * from pg_class, pg_attribute limit 4000000;
Limit (cost=0.00..8795.58 rows=697380 width=403)
-> Nested Loop (cost=0.00..8795.58 rows=697380 width=403)
-> Seq Scan on pg_attribute (cost=0.00..66.64 rows=2364 width=203)
-> Materialize (cost=0.00..12.42 rows=295 width=200)
-> Seq Scan on pg_class (cost=0.00..10.95 rows=295 width=200)
(6 rows)
postgres=# select pg_cmdstatus(pid,2) from pg_stat_activity where pid <> pg_backend_pid();
pg_cmdstatus
-----------------------------------------------------
select * from pg_class, pg_attribute limit 4000000;
(1 row)
postgres=# select pg_cmdstatus(pid,3) from pg_stat_activity where pid <> pg_backend_pid();
pg_cmdstatus
---------------
SELECT 144427
(1 row)
postgres=# select pg_cmdstatus(pid,3) from pg_stat_activity where pid <> pg_backend_pid();
pg_cmdstatus
---------------
SELECT 209742
(1 row)
postgres=# select pg_cmdstatus(pid,3) from pg_stat_activity where pid <> pg_backend_pid();
pg_cmdstatus
---------------
SELECT 288472
(1 row)
In future a function can be replaced by statement EXPLAIN pid WITH autocomplete - It can show a subset of EXPLAIN ANALYZE -- but it needs a some parametrization of executor environment.