1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-19 13:42:17 +03:00

Look up backend type in pg_signal_backend() more cheaply.

Commit ccd38024bc, 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
This commit is contained in:
Nathan Bossart
2024-11-27 10:32:25 -06:00
parent 6a5bcf7f7d
commit 61171a632d
3 changed files with 28 additions and 2 deletions

View File

@@ -88,9 +88,9 @@ pg_signal_backend(int pid, int sig)
if (!OidIsValid(proc->roleId) || superuser_arg(proc->roleId))
{
ProcNumber procNumber = GetNumberFromPGProc(proc);
PgBackendStatus *procStatus = pgstat_get_beentry_by_proc_number(procNumber);
BackendType backendType = pgstat_get_backend_type_by_proc_number(procNumber);
if (procStatus && procStatus->st_backendType == B_AUTOVAC_WORKER)
if (backendType == B_AUTOVAC_WORKER)
{
if (!has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_AUTOVACUUM_WORKER))
return SIGNAL_BACKEND_NOAUTOVAC;