Tweak behavior of pg_stat_activity.leader_pid
authorMichael Paquier
Sun, 26 Jul 2020 07:32:20 +0000 (16:32 +0900)
committerMichael Paquier
Sun, 26 Jul 2020 07:32:20 +0000 (16:32 +0900)
The initial implementation of leader_pid in pg_stat_activity added by
b025f32 took the approach to strictly print what a PGPROC entry
includes.  In short, if a backend has been involved in parallel query at
least once, leader_pid would remain set as long as the backend is alive.
For a parallel group leader, this means that the field would always be
set after it participated at least once in parallel query, and after
more discussions this could be confusing if using for example a
connection pooler.

This commit changes the data printed so as leader_pid becomes always
NULL for a parallel group leader, showing up a non-NULL value only for
the parallel workers, and actually as long as a parallel query is
running as workers are shut down once the query has completed.

This does not change the definition of any catalog, so no catalog bump
is needed.  Per discussion with Justin Pryzby, Álvaro Herrera, Julien
Rouhaud and me.

Discussion: https://postgr.es/m/20200721035145[email protected]
Backpatch-through: 13

doc/src/sgml/monitoring.sgml
src/backend/utils/adt/pgstatfuncs.c

index 1dc9ffafc0c0402f75692729e3c2bada41ffd755..408ecbcae05c3f8fdf539a9b130262fe15df7454 100644 (file)
@@ -687,12 +687,9 @@ postgres   27093  0.0  0.0  30096  2752 ?        Ss   11:34   0:00 postgres: ser
        leader_pid integer
       
       
-       Process ID of the parallel group leader if this process is or
-       has been involved in parallel query, or null. This field is set
-       when a process wants to cooperate with parallel workers, and
-       remains set as long as the process exists. For a parallel group leader,
-       this field is set to its own process ID. For a parallel worker,
-       this field is set to the process ID of the parallel group leader.
+       Process ID of the parallel group leader, if this process is a
+       parallel query worker.  NULL if this process is a
+       parallel group leader or does not participate in parallel query.
       
      
 
index 2aff739466ff1294e6a1df94f46c8b7b67c58b2d..95738a4e34eecb2992442d299031b8d243d86d3e 100644 (file)
@@ -735,7 +735,13 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
                wait_event = pgstat_get_wait_event(raw_wait_event);
 
                leader = proc->lockGroupLeader;
-               if (leader)
+
+               /*
+                * Show the leader only for active parallel workers.  This
+                * leaves the field as NULL for the leader of a parallel
+                * group.
+                */
+               if (leader && leader->pid != beentry->st_procpid)
                {
                    values[29] = Int32GetDatum(leader->pid);
                    nulls[29] = false;