Skip to content

Commit

Permalink
Improve pg_get_backend_status performance for PG16 and PG17
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Jul 2, 2024
1 parent 5261c07 commit 985f68b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1258,18 +1258,21 @@ pgsm_hash_string(const char *str, int len)
static PgBackendStatus *
pg_get_backend_status(void)
{

#if PG_VERSION_NUM >= 170000
return &(pgstat_get_beentry_by_proc_number(MyProcPid)->backendStatus);
#elif PG_VERSION_NUL >= 160000
return &(pgstat_get_local_beentry_by_backend_id(MyBackendId)->backendStatus);
#else
LocalPgBackendStatus *local_beentry;
int num_backends = pgstat_fetch_stat_numbackends();
int i;

for (i = 1; i <= num_backends; i++)
{
PgBackendStatus *beentry;
#if PG_VERSION_NUM < 160000

local_beentry = pgstat_fetch_stat_local_beentry(i);
#else
local_beentry = pgstat_get_local_beentry_by_index(i);
#endif
if (!local_beentry)
continue;

Expand All @@ -1279,6 +1282,8 @@ pg_get_backend_status(void)
return beentry;
}
return NULL;

#endif
}

/*
Expand Down

0 comments on commit 985f68b

Please sign in to comment.