pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
To: Ian Lawrence Barwick <barwick@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Cc: robertmhaas@gmail.com
Subject: Re: pg_stat_get_backend_subxact() and backend IDs?
Date: Fri, 25 Aug 2023 12:29:49 -0700
Message-ID: <20230825192949.GA1917227@nathanxps13> (raw)
In-Reply-To: <20230825153251.GA1593219@nathanxps13>
References: <CAB8KJ=j-ACb3H4L9a_b3ZG3iCYDW5aEu3WsPAzkm2S7JzS1Few@mail.gmail.com>
	<20230824023206.GA1128857@nathanxps13.lan>
	<20230824025140.GB1128857@nathanxps13.lan>
	<20230824161913.GA1394441@nathanxps13.lan>
	<CAB8KJ=iWYvB1odjFRZybsPeUV8HUAiVVjY9RMHSdvWy1t77cLg@mail.gmail.com>
	<20230825153251.GA1593219@nathanxps13>

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

Attachments:

v3-0001-fix-pg_stat_get_backend_subxact-to-use-real-backe.patch (text/x-diff, 3.3K) download From e1ce8bf67f1b713294ef8d38dbee26bfc7ef16f4 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <nathan@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

view thread (22+ messages)  latest in thread

Message-ID: <20230825192949.GA1917227@nathanxps13>
Permalink:  ../20230825192949.GA1917227@nathanxps13/
Also on:    postgresql.org/message-id/20230825192949.GA1917227@nathanxps13

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com, barwick@gmail.com, robertmhaas@gmail.com
  Subject: Re: pg_stat_get_backend_subxact() and backend IDs?
  In-Reply-To: <20230825192949.GA1917227@nathanxps13>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox