Look up backend type in pg_signal_backend() more cheaply.
authorNathan Bossart <nathan@postgresql.org>
Wed, 27 Nov 2024 16:32:25 +0000 (10:32 -0600)
committerNathan Bossart <nathan@postgresql.org>
Wed, 27 Nov 2024 16:32:25 +0000 (10:32 -0600)
commit61171a632d10ad7abe1d7ad891176266cc5ef0e1
treeaa1edd0a2a42043be5ebbb3c99486a558926e4d1
parent6a5bcf7f7dd1e3c1c669912d82591397dbe24a10
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
src/backend/storage/ipc/signalfuncs.c
src/backend/utils/activity/backend_status.c
src/include/utils/backend_status.h