Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d0fe304

Browse files
Use actual backend IDs in pg_stat_get_backend_subxact().
Unlike the other pg_stat_get_backend* functions, pg_stat_get_backend_subxact() looks up the backend entry by using its integer argument as a 1-based index in an internal array. The other functions look for the entry with the matching session backend ID. These numbers often match, but that isn't reliably true. This commit resolves this discrepancy by introducing pgstat_get_local_beentry_by_backend_id() and using it in pg_stat_get_backend_subxact(). We cannot use pgstat_get_beentry_by_backend_id() because it returns a PgBackendStatus, which lacks the locally computed additions available in LocalPgBackendStatus that are required by pg_stat_get_backend_subxact(). Author: Ian Barwick Reviewed-by: Sami Imseih, Michael Paquier, Robert Haas Discussion: https://postgr.es/m/CAB8KJ%3Dj-ACb3H4L9a_b3ZG3iCYDW5aEu3WsPAzkm2S7JzS1Few%40mail.gmail.com Backpatch-through: 16
1 parent 3d51cb5 commit d0fe304

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/backend/utils/activity/backend_status.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,9 +1088,33 @@ cmp_lbestatus(const void *a, const void *b)
10881088
*/
10891089
PgBackendStatus *
10901090
pgstat_get_beentry_by_backend_id(BackendId beid)
1091+
{
1092+
LocalPgBackendStatus *ret = pgstat_get_local_beentry_by_backend_id(beid);
1093+
1094+
if (ret)
1095+
return &ret->backendStatus;
1096+
1097+
return NULL;
1098+
}
1099+
1100+
1101+
/* ----------
1102+
* pgstat_get_local_beentry_by_backend_id() -
1103+
*
1104+
* Like pgstat_get_beentry_by_backend_id() but with locally computed additions
1105+
* (like xid and xmin values of the backend)
1106+
*
1107+
* The beid argument is the BackendId of the desired session
1108+
* (note that this is unlike pgstat_get_local_beentry_by_index()).
1109+
*
1110+
* NB: caller is responsible for checking if the user is permitted to see this
1111+
* info (especially the querystring).
1112+
* ----------
1113+
*/
1114+
LocalPgBackendStatus *
1115+
pgstat_get_local_beentry_by_backend_id(BackendId beid)
10911116
{
10921117
LocalPgBackendStatus key;
1093-
LocalPgBackendStatus *ret;
10941118

10951119
pgstat_read_current_status();
10961120

@@ -1099,14 +1123,8 @@ pgstat_get_beentry_by_backend_id(BackendId beid)
10991123
* bsearch() to search it efficiently.
11001124
*/
11011125
key.backend_id = beid;
1102-
ret = (LocalPgBackendStatus *) bsearch(&key, localBackendStatusTable,
1103-
localNumBackends,
1104-
sizeof(LocalPgBackendStatus),
1105-
cmp_lbestatus);
1106-
if (ret)
1107-
return &ret->backendStatus;
1108-
1109-
return NULL;
1126+
return bsearch(&key, localBackendStatusTable, localNumBackends,
1127+
sizeof(LocalPgBackendStatus), cmp_lbestatus);
11101128
}
11111129

11121130

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ pg_stat_get_backend_subxact(PG_FUNCTION_ARGS)
727727

728728
BlessTupleDesc(tupdesc);
729729

730-
if ((local_beentry = pgstat_get_local_beentry_by_index(beid)) != NULL)
730+
if ((local_beentry = pgstat_get_local_beentry_by_backend_id(beid)) != NULL)
731731
{
732732
/* Fill values and NULLs */
733733
values[0] = Int32GetDatum(local_beentry->backend_subxact_count);

src/include/utils/backend_status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ extern uint64 pgstat_get_my_query_id(void);
334334
*/
335335
extern int pgstat_fetch_stat_numbackends(void);
336336
extern PgBackendStatus *pgstat_get_beentry_by_backend_id(BackendId beid);
337+
extern LocalPgBackendStatus *pgstat_get_local_beentry_by_backend_id(BackendId beid);
337338
extern LocalPgBackendStatus *pgstat_get_local_beentry_by_index(int idx);
338339
extern char *pgstat_clip_activity(const char *raw_activity);
339340

0 commit comments

Comments
 (0)