Skip to content

Commit 61171a6

Browse files
Look up backend type in pg_signal_backend() more cheaply.
Commit ccd3802, which introduced the pg_signal_autovacuum_worker role, added a call to pgstat_get_beentry_by_proc_number() for the purpose of determining whether the process is an autovacuum worker. This function calls pgstat_read_current_status(), which can be fairly expensive and may return cached, out-of-date information. Since we just need to look up the target backend's BackendType, and we already know its ProcNumber, we can instead inspect the BackendStatusArray directly, which is much less expensive and possibly more up-to-date. There are some caveats with this approach (which are documented in the code), but it's still substantially better than before. Reported-by: Andres Freund Reviewed-by: Andres Freund Discussion: https://postgr.es/m/ujenaa2uabzfkwxwmfifawzdozh3ljr7geozlhftsuosgm7n7q%40g3utqqyyosb6
1 parent 6a5bcf7 commit 61171a6

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/backend/storage/ipc/signalfuncs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ pg_signal_backend(int pid, int sig)
8888
if (!OidIsValid(proc->roleId) || superuser_arg(proc->roleId))
8989
{
9090
ProcNumber procNumber = GetNumberFromPGProc(proc);
91-
PgBackendStatus *procStatus = pgstat_get_beentry_by_proc_number(procNumber);
91+
BackendType backendType = pgstat_get_backend_type_by_proc_number(procNumber);
9292

93-
if (procStatus && procStatus->st_backendType == B_AUTOVAC_WORKER)
93+
if (backendType == B_AUTOVAC_WORKER)
9494
{
9595
if (!has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_AUTOVACUUM_WORKER))
9696
return SIGNAL_BACKEND_NOAUTOVAC;

src/backend/utils/activity/backend_status.c

+25
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,31 @@ pgstat_get_my_query_id(void)
10361036
return MyBEEntry->st_query_id;
10371037
}
10381038

1039+
/* ----------
1040+
* pgstat_get_backend_type_by_proc_number() -
1041+
*
1042+
* Return the type of the backend with the specified ProcNumber. This looks
1043+
* directly at the BackendStatusArray, so the return value may be out of date.
1044+
* The only current use of this function is in pg_signal_backend(), which is
1045+
* inherently racy, so we don't worry too much about this.
1046+
*
1047+
* It is the caller's responsibility to use this wisely; at minimum, callers
1048+
* should ensure that procNumber is valid and perform the required permissions
1049+
* checks.
1050+
* ----------
1051+
*/
1052+
BackendType
1053+
pgstat_get_backend_type_by_proc_number(ProcNumber procNumber)
1054+
{
1055+
volatile PgBackendStatus *status = &BackendStatusArray[procNumber];
1056+
1057+
/*
1058+
* We bypass the changecount mechanism since fetching and storing an int
1059+
* is almost certainly atomic.
1060+
*/
1061+
return status->st_backendType;
1062+
}
1063+
10391064
/* ----------
10401065
* cmp_lbestatus
10411066
*

src/include/utils/backend_status.h

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser);
323323
extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
324324
int buflen);
325325
extern uint64 pgstat_get_my_query_id(void);
326+
extern BackendType pgstat_get_backend_type_by_proc_number(ProcNumber procNumber);
326327

327328

328329
/* ----------

0 commit comments

Comments
 (0)