Skip to content

Commit bca6eeb

Browse files
committed
Fix miserable coding in pg_stat_get_activity().
Commit dd1a3bc replaced a test on whether a subroutine returned a null pointer with a test on whether &pointer->backendStatus was null. This accidentally failed to fail, at least on common compilers, because backendStatus is the first field in the struct; but it was surely trouble waiting to happen. Commit f91feba then messed things up further, changing the logic to local_beentry = pgstat_fetch_stat_local_beentry(curr_backend); if (!local_beentry) continue; beentry = &local_beentry->backendStatus; if (!beentry) { where the second "if" is now dead code, so that the intended behavior of printing a row with "<backend information not available>" cannot occur. I suspect this is all moot because pgstat_fetch_stat_local_beentry will never actually return null in this function's usage, but it's still very poor coding. Repair back to 9.4 where the original problem was introduced.
1 parent f337658 commit bca6eeb

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -577,27 +577,17 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
577577
MemSet(values, 0, sizeof(values));
578578
MemSet(nulls, 0, sizeof(nulls));
579579

580-
if (pid != -1)
581-
{
582-
/* Skip any which are not the one we're looking for. */
583-
PgBackendStatus *be = pgstat_fetch_stat_beentry(curr_backend);
584-
585-
if (!be || be->st_procpid != pid)
586-
continue;
587-
588-
}
589-
590580
/* Get the next one in the list */
591581
local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
592582
if (!local_beentry)
593-
continue;
594-
595-
beentry = &local_beentry->backendStatus;
596-
if (!beentry)
597583
{
598584
int i;
599585

600-
for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
586+
/* Ignore missing entries if looking for specific PID */
587+
if (pid != -1)
588+
continue;
589+
590+
for (i = 0; i < lengthof(nulls); i++)
601591
nulls[i] = true;
602592

603593
nulls[5] = false;
@@ -607,6 +597,12 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
607597
continue;
608598
}
609599

600+
beentry = &local_beentry->backendStatus;
601+
602+
/* If looking for specific PID, ignore all the others */
603+
if (pid != -1 && beentry->st_procpid != pid)
604+
continue;
605+
610606
/* Values available to all callers */
611607
values[0] = ObjectIdGetDatum(beentry->st_databaseid);
612608
values[1] = Int32GetDatum(beentry->st_procpid);

0 commit comments

Comments
 (0)