Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qZcVO-0069kG-9I for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Aug 2023 19:30:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qZcVL-005QHH-0q for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Aug 2023 19:29:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qZcVK-005QF2-MQ for pgsql-hackers@lists.postgresql.org; Fri, 25 Aug 2023 19:29:58 +0000 Received: from mail-oi1-x234.google.com ([2607:f8b0:4864:20::234]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qZcVF-000wQ8-SA for pgsql-hackers@postgresql.org; Fri, 25 Aug 2023 19:29:57 +0000 Received: by mail-oi1-x234.google.com with SMTP id 5614622812f47-3a9b40fc500so451673b6e.1 for ; Fri, 25 Aug 2023 12:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692991793; x=1693596593; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Y1wh73IcJ8u0x1//5PmRQpDoYtAaqf/CT/xuZEOzGxU=; b=J8Kvf/yBJ83Y6QtawjNSGfUM6S+cGu+EyRPmZag+AH2sfTe82LGUhWMR3x/Got8/uc bNFJTickLArb7HrtLiYYHckNmHdorQzsf3DK68BNPY3Qx2hEcauO64pKjYiKekPiSdzu /2g/hWuPHKUWC2CX/tHecupCxNWO5Z4lxECQIkUzy3ieTSda7rRvWFuxFQWPkzCNcRrt 7oYo4enL2OIV+WfbqwXdYySWOmxJ+JSCKrtcjE0onp911BcT9l7s1cg+A3v4O10xCcJJ Vmuu9Huz6k4Q+G9yTic6BjA7OzMWjYRzD0fU0eSWxEs28xWRWHdTkDXlCHT8evfrRlmr 3p0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692991793; x=1693596593; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Y1wh73IcJ8u0x1//5PmRQpDoYtAaqf/CT/xuZEOzGxU=; b=CJ6IlLaEpRyqF+bk5VhHKAaCEzKvbxf65WGskkOKa2q306yS2PChrBFkYs14zZ2RSY tGZzM/8YdQbwrb6dlZJhaJIHsXbmQXjPT0L8BDv2Kzvb8KTl4vdf2h80jLrJ/jJfF8LH 5WrQvihyNld8LCNtLkH6zBXJ1gpoxZ/UuTjoLnkdhxuWYfmNHZizmvB/46gvomcFnXCO 3py0MeRT33xW7dUX1hzjbBZpe2kWus4/pt/pb1sWZOhSrFcdfKQoDTYcWFcF91IigX5M f7gvqNZ6/g0rZAKfHeJg3b5Rd2MTa3NLbdXsQxlPLCVlgmftthPuCinfV22HwkbmWROZ ZP9w== X-Gm-Message-State: AOJu0Yy0kRggBdlTnwSeVCReHcb7GQ0YcfBebvxHTZVBmHCJALJN7kbT oSbSa0uSkEw+Twq2JVun5/Q= X-Google-Smtp-Source: AGHT+IGHErMELYgvTwK0IFdb967LGiD2J8P+e+XVjbY84IpozQI8nAbuOaHA//3JWuON242In1ZH+w== X-Received: by 2002:a05:6808:b22:b0:39c:93ba:cb92 with SMTP id t2-20020a0568080b2200b0039c93bacb92mr3415497oij.8.1692991793165; Fri, 25 Aug 2023 12:29:53 -0700 (PDT) Received: from nathanxps13 ([2600:1700:48f8:340f:d0b8:171:a4d2:a240]) by smtp.gmail.com with ESMTPSA id c6-20020aca1c06000000b003a5b027ccb2sm1075881oic.38.2023.08.25.12.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 12:29:51 -0700 (PDT) Date: Fri, 25 Aug 2023 12:29:49 -0700 From: Nathan Bossart To: Ian Lawrence Barwick Cc: pgsql-hackers , robertmhaas@gmail.com Subject: Re: pg_stat_get_backend_subxact() and backend IDs? Message-ID: <20230825192949.GA1917227@nathanxps13> References: <20230824023206.GA1128857@nathanxps13.lan> <20230824025140.GB1128857@nathanxps13.lan> <20230824161913.GA1394441@nathanxps13.lan> <20230825153251.GA1593219@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="HcAYCG3uE/tztfnV" Content-Disposition: inline In-Reply-To: <20230825153251.GA1593219@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --HcAYCG3uE/tztfnV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --HcAYCG3uE/tztfnV Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-fix-pg_stat_get_backend_subxact-to-use-real-backe.patch" From e1ce8bf67f1b713294ef8d38dbee26bfc7ef16f4 Mon Sep 17 00:00:00 2001 From: Nathan Bossart 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 --HcAYCG3uE/tztfnV--