Skip to content

Commit ddc8893

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 24992c6 commit ddc8893

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
@@ -688,27 +688,17 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
688688
MemSet(values, 0, sizeof(values));
689689
MemSet(nulls, 0, sizeof(nulls));
690690

691-
if (pid != -1)
692-
{
693-
/* Skip any which are not the one we're looking for. */
694-
PgBackendStatus *be = pgstat_fetch_stat_beentry(curr_backend);
695-
696-
if (!be || be->st_procpid != pid)
697-
continue;
698-
699-
}
700-
701691
/* Get the next one in the list */
702692
local_beentry = pgstat_fetch_stat_local_beentry(curr_backend);
703693
if (!local_beentry)
704-
continue;
705-
706-
beentry = &local_beentry->backendStatus;
707-
if (!beentry)
708694
{
709695
int i;
710696

711-
for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
697+
/* Ignore missing entries if looking for specific PID */
698+
if (pid != -1)
699+
continue;
700+
701+
for (i = 0; i < lengthof(nulls); i++)
712702
nulls[i] = true;
713703

714704
nulls[5] = false;
@@ -718,6 +708,12 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
718708
continue;
719709
}
720710

711+
beentry = &local_beentry->backendStatus;
712+
713+
/* If looking for specific PID, ignore all the others */
714+
if (pid != -1 && beentry->st_procpid != pid)
715+
continue;
716+
721717
/* Values available to all callers */
722718
values[0] = ObjectIdGetDatum(beentry->st_databaseid);
723719
values[1] = Int32GetDatum(beentry->st_procpid);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy