On Fri, Aug 25, 2023 at 08:32:51AM -0700, Nathan Bossart wrote: > On second thought, renaming these exported functions so close to release is > probably not a great idea. I should probably skip back-patching that one. > Or I could have the existing functions call the new ones in v16 for > backward compatibility...
Here is a new version of the patch that avoids changing the names of the existing functions. I'm not thrilled about the name (pgstat_fetch_stat_local_beentry_by_backend_id), so I am open to suggestions. In any case, I'd like to rename all three of the pgstat_fetch_stat_* functions in v17. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com
>From e1ce8bf67f1b713294ef8d38dbee26bfc7ef16f4 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <nat...@postgresql.org> Date: Thu, 24 Aug 2023 07:58:01 -0700 Subject: [PATCH v3 1/1] fix pg_stat_get_backend_subxact to use real backend id --- src/backend/utils/activity/backend_status.c | 39 ++++++++++++++++----- src/backend/utils/adt/pgstatfuncs.c | 2 +- src/include/utils/backend_status.h | 1 + 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/backend/utils/activity/backend_status.c b/src/backend/utils/activity/backend_status.c index 38f91a495b..231cc5dd9a 100644 --- a/src/backend/utils/activity/backend_status.c +++ b/src/backend/utils/activity/backend_status.c @@ -1089,9 +1089,34 @@ cmp_lbestatus(const void *a, const void *b) PgBackendStatus * pgstat_fetch_stat_beentry(BackendId beid) { - LocalPgBackendStatus key; LocalPgBackendStatus *ret; + ret = pgstat_fetch_stat_local_beentry_by_backend_id(beid); + if (ret) + return &ret->backendStatus; + + return NULL; +} + + +/* ---------- + * pgstat_fetch_stat_local_beentry_by_backend_id() - + * + * Like pgstat_fetch_stat_beentry() but with locally computed additions + * (like xid and xmin values of the backend) + * + * The beid argument is the BackendId of the desired session + * (note that this is unlike pgstat_fetch_stat_local_beentry()). + * + * NB: caller is responsible for checking if the user is permitted to see this + * info (especially the querystring). + * ---------- + */ +LocalPgBackendStatus * +pgstat_fetch_stat_local_beentry_by_backend_id(BackendId beid) +{ + LocalPgBackendStatus key; + pgstat_read_current_status(); /* @@ -1099,14 +1124,10 @@ pgstat_fetch_stat_beentry(BackendId beid) * bsearch() to search it efficiently. */ key.backend_id = beid; - ret = (LocalPgBackendStatus *) bsearch(&key, localBackendStatusTable, - localNumBackends, - sizeof(LocalPgBackendStatus), - cmp_lbestatus); - if (ret) - return &ret->backendStatus; - - return NULL; + return (LocalPgBackendStatus *) bsearch(&key, localBackendStatusTable, + localNumBackends, + sizeof(LocalPgBackendStatus), + cmp_lbestatus); } diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 2b9742ad21..68ae044399 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -727,7 +727,7 @@ pg_stat_get_backend_subxact(PG_FUNCTION_ARGS) BlessTupleDesc(tupdesc); - if ((local_beentry = pgstat_fetch_stat_local_beentry(beid)) != NULL) + if ((local_beentry = pgstat_fetch_stat_local_beentry_by_backend_id(beid)) != NULL) { /* Fill values and NULLs */ values[0] = Int32GetDatum(local_beentry->backend_subxact_count); diff --git a/src/include/utils/backend_status.h b/src/include/utils/backend_status.h index 77939a0aed..060431a39b 100644 --- a/src/include/utils/backend_status.h +++ b/src/include/utils/backend_status.h @@ -335,6 +335,7 @@ extern uint64 pgstat_get_my_query_id(void); extern int pgstat_fetch_stat_numbackends(void); extern PgBackendStatus *pgstat_fetch_stat_beentry(BackendId beid); extern LocalPgBackendStatus *pgstat_fetch_stat_local_beentry(int beid); +extern LocalPgBackendStatus *pgstat_fetch_stat_local_beentry_by_backend_id(BackendId beid); extern char *pgstat_clip_activity(const char *raw_activity); -- 2.25.1