From de61b1f1a1d44efb54b2ca3dcce0375aa05307c1 Mon Sep 17 00:00:00 2001 From: Adesh Thakare Date: Thu, 3 Dec 2026 00:19:43 +0000 Subject: [PATCH v1 1/2] Let pg_signal_autovacuum_worker signal parallel autovacuum workers. An autovacuum worker advertises no role in its PGPROC, but its parallel workers advertise the bootstrap superuser. So roles with privileges of pg_signal_autovacuum_worker, which may cancel or terminate the autovacuum worker itself, got "permission denied" from pg_cancel_backend() and pg_terminate_backend() for its parallel workers, and could only stop them by signaling the leader. Fix by checking a parallel worker as its leader when the leader advertises no role, as a39e53a700c did for DROP DATABASE ... FORCE. Parallel workers of a bgworker connected without a user name are still superuser-only, since their leader is checked as a process advertising no role. Parallel workers of other leaders are unchanged. Backpatch to v19, where parallel autovacuum was introduced. --- doc/src/sgml/func/func-admin.sgml | 8 +++--- src/backend/storage/ipc/signalfuncs.c | 37 ++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/doc/src/sgml/func/func-admin.sgml b/doc/src/sgml/func/func-admin.sgml index ed872264f..abd4fda7a 100644 --- a/doc/src/sgml/func/func-admin.sgml +++ b/doc/src/sgml/func/func-admin.sgml @@ -158,8 +158,8 @@ however only superusers can cancel superuser backends. As an exception, roles with privileges of pg_signal_autovacuum_worker are permitted to - cancel autovacuum worker processes, which are otherwise considered - superuser backends. + cancel autovacuum worker processes and the parallel workers they + launch, which are otherwise considered superuser backends. @@ -236,8 +236,8 @@ however only superusers can terminate superuser backends. As an exception, roles with privileges of pg_signal_autovacuum_worker are permitted to - terminate autovacuum worker processes, which are otherwise considered - superuser backends. + terminate autovacuum worker processes and the parallel workers they + launch, which are otherwise considered superuser backends. If timeout is not specified or zero, this diff --git a/src/backend/storage/ipc/signalfuncs.c b/src/backend/storage/ipc/signalfuncs.c index 61ca4d2cb..f9521bdf5 100644 --- a/src/backend/storage/ipc/signalfuncs.c +++ b/src/backend/storage/ipc/signalfuncs.c @@ -52,6 +52,9 @@ static int pg_signal_backend(int pid, int sig) { PGPROC *proc = BackendPidGetProc(pid); + PGPROC *leader; + Oid roleId; + BackendType backendType; /* * BackendPidGetProc returns NULL if the pid isn't valid; but by the time @@ -77,18 +80,44 @@ pg_signal_backend(int pid, int sig) return SIGNAL_BACKEND_ERROR; } + /* + * An autovacuum worker and a bgworker with no user name advertise no + * role, but the parallel workers they launch advertise the bootstrap + * superuser. Check such a parallel worker as its leader, so that whoever + * may signal the leader may signal its parallel workers as well. That + * doesn't let anyone stop a process they couldn't already stop, since a + * leader that errors out or exits terminates its parallel workers. + * TerminateOtherDBBackends() does the same. + * + * lockGroupLeader can be reset concurrently when the target exits, so + * read it only once. A parallel worker joins its lock group before it + * advertises a role, and the leader's PGPROC isn't reused until the last + * member of the group has exited. + */ + leader = proc->lockGroupLeader; + if (leader != NULL && leader != proc && !OidIsValid(leader->roleId)) + { + roleId = leader->roleId; + backendType = leader->backendType; + } + else + { + roleId = proc->roleId; + backendType = proc->backendType; + } + /* * Only allow superusers to signal superuser-owned backends. Any process * not advertising a role might have the importance of a superuser-owned * backend, so treat it that way. As an exception, we allow roles with * privileges of pg_signal_autovacuum_worker to signal autovacuum workers - * (which do not advertise a role). + * (which do not advertise a role) and their parallel workers. * * Otherwise, users can signal backends for roles they have privileges of. */ - if (!OidIsValid(proc->roleId) || superuser_arg(proc->roleId)) + if (!OidIsValid(roleId) || superuser_arg(roleId)) { - if (proc->backendType == B_AUTOVAC_WORKER) + if (backendType == B_AUTOVAC_WORKER) { if (!has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_AUTOVACUUM_WORKER)) return SIGNAL_BACKEND_NOAUTOVAC; @@ -96,7 +125,7 @@ pg_signal_backend(int pid, int sig) else if (!superuser()) return SIGNAL_BACKEND_NOSUPERUSER; } - else if (!has_privs_of_role(GetUserId(), proc->roleId) && + else if (!has_privs_of_role(GetUserId(), roleId) && !has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_BACKEND)) return SIGNAL_BACKEND_NOPERMISSION; -- 2.47.3