From 8d555a5c65e04ee21dfcfdcf58e9278d0f839559 Mon Sep 17 00:00:00 2001 From: Adesh Thakare Date: Thu, 3 Dec 2026 00:19:43 +0000 Subject: [PATCH v1 2/2] Add a test for signaling parallel autovacuum workers. Hold each parallel worker at the parallel-vacuum-worker-start injection point and check who may cancel or terminate it: members of pg_signal_autovacuum_worker may, other roles get the autovacuum worker error, and the parallel workers of a superuser's own VACUUM (PARALLEL n) are still superuser-only. --- src/test/modules/test_autovacuum/meson.build | 1 + .../t/003_signal_parallel_autovacuum.pl | 208 ++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 src/test/modules/test_autovacuum/t/003_signal_parallel_autovacuum.pl diff --git a/src/test/modules/test_autovacuum/meson.build b/src/test/modules/test_autovacuum/meson.build index 970b9aaae..ff2b7fdc1 100644 --- a/src/test/modules/test_autovacuum/meson.build +++ b/src/test/modules/test_autovacuum/meson.build @@ -11,6 +11,7 @@ tests += { 'tests': [ 't/001_parallel_autovacuum.pl', 't/002_toast_relopts.pl', + 't/003_signal_parallel_autovacuum.pl', ], }, } diff --git a/src/test/modules/test_autovacuum/t/003_signal_parallel_autovacuum.pl b/src/test/modules/test_autovacuum/t/003_signal_parallel_autovacuum.pl new file mode 100644 index 000000000..52c564a01 --- /dev/null +++ b/src/test/modules/test_autovacuum/t/003_signal_parallel_autovacuum.pl @@ -0,0 +1,208 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Test who may cancel or terminate the parallel workers of an autovacuum +# worker. +# +# An autovacuum worker advertises no role, so members of +# pg_signal_autovacuum_worker may signal it. Its parallel workers advertise +# the bootstrap superuser, so pg_signal_backend() checks them as their leader. +# This test checks that: +# - pg_signal_autovacuum_worker members can signal them, as they can signal +# the autovacuum worker itself; +# - other roles get the same error as for the autovacuum worker itself; +# - terminating the autovacuum worker itself still works; +# - parallel workers of a superuser's own VACUUM (PARALLEL n) can still be +# signaled only by superusers. +# +# Each parallel worker is held at the 'parallel-vacuum-worker-start' injection +# point, so it is still running when we signal it. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init; + +# Only the test table is autovacuumed, by one worker with one parallel worker. +$node->append_conf( + 'postgresql.conf', qq{ +autovacuum_max_workers = 1 +autovacuum_max_parallel_workers = 1 +max_worker_processes = 10 +max_parallel_workers = 10 +autovacuum_naptime = '1s' +min_parallel_index_scan_size = 0 +autovacuum_vacuum_threshold = 100000 +autovacuum_analyze_threshold = 100000 +autovacuum_vacuum_insert_threshold = -1 +}); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql( + 'postgres', q{ +CREATE EXTENSION injection_points; +CREATE ROLE regress_av_signal LOGIN IN ROLE pg_signal_autovacuum_worker; +CREATE ROLE regress_sig_backend LOGIN IN ROLE pg_signal_backend; +CREATE ROLE regress_nopriv LOGIN; +CREATE TABLE tbl (a int, b int) + WITH (autovacuum_enabled = false, autovacuum_parallel_workers = 1, + autovacuum_vacuum_threshold = 1); +CREATE INDEX tbl_a ON tbl (a); +CREATE INDEX tbl_b ON tbl (b); +}); + +sub make_dead_tuples +{ + $node->safe_psql('postgres', + 'INSERT INTO tbl SELECT g, g FROM generate_series(1, 100) g; DELETE FROM tbl;' + ); +} + +# Wait for a parallel worker held at the injection point whose leader has the +# given backend type, and return its PID. +sub held_worker +{ + my ($leader_type) = @_; + my $query = qq{ +SELECT w.pid FROM pg_stat_activity w JOIN pg_stat_activity l ON l.pid = w.leader_pid +WHERE w.backend_type = 'parallel worker' + AND w.wait_event = 'parallel-vacuum-worker-start' + AND l.backend_type = '$leader_type'}; + $node->poll_query_until('postgres', "SELECT count(*) = 1 FROM ($query) s") + or die "timed out waiting for a parallel worker of a $leader_type"; + return $node->safe_psql('postgres', $query); +} + +sub wait_gone +{ + my ($pid) = @_; + $node->poll_query_until('postgres', + "SELECT count(*) = 0 FROM pg_stat_activity WHERE pid = $pid") + or die "timed out waiting for PID $pid to exit"; +} + +# Run "SELECT $func($pid)" as $user; return (stdout, stderr). +sub signal_as +{ + my ($user, $func, $pid) = @_; + my ($out, $err) = ('', ''); + $node->psql( + 'postgres', "SELECT $func($pid)", + connstr => $node->connstr('postgres') . " user=$user", + stdout => \$out, + stderr => \$err); + return ($out, $err); +} + +my ($out, $err, $pid, $log_offset); + +# Case 1: parallel worker of an autovacuum worker. +$node->safe_psql('postgres', + "SELECT injection_points_attach('parallel-vacuum-worker-start', 'wait')"); +make_dead_tuples(); +$node->safe_psql('postgres', + 'ALTER TABLE tbl SET (autovacuum_enabled = true)'); +$pid = held_worker('autovacuum worker'); + +# Roles that can't signal the autovacuum worker get the same error for its +# parallel worker. +($out, $err) = signal_as('regress_nopriv', 'pg_cancel_backend', $pid); +like( + $err, + qr/Only roles with privileges of the "pg_signal_autovacuum_worker" role may cancel autovacuum workers/, + 'parallel autovacuum worker: unprivileged role gets autovacuum worker error'); + +($out, $err) = signal_as('regress_sig_backend', 'pg_cancel_backend', $pid); +like( + $err, + qr/Only roles with privileges of the "pg_signal_autovacuum_worker" role may cancel autovacuum workers/, + 'parallel autovacuum worker: pg_signal_backend member gets autovacuum worker error' +); + +($out, $err) = signal_as('regress_av_signal', 'pg_cancel_backend', $pid); +is($err, '', + 'parallel autovacuum worker: pg_signal_autovacuum_worker cancel: no error'); +is($out, 't', + 'parallel autovacuum worker: pg_signal_autovacuum_worker can cancel'); +wait_gone($pid); + +# Autovacuum retries the table; its new parallel worker is held again. +$pid = held_worker('autovacuum worker'); + +# Terminating the autovacuum worker itself still works and stops its +# parallel worker. +my $leader = $node->safe_psql('postgres', + "SELECT leader_pid FROM pg_stat_activity WHERE pid = $pid"); +($out, $err) = signal_as('regress_av_signal', 'pg_terminate_backend', $leader); +is($err, '', 'autovacuum worker: pg_signal_autovacuum_worker terminate: no error'); +is($out, 't', 'autovacuum worker: pg_signal_autovacuum_worker can terminate'); +wait_gone($pid); +wait_gone($leader); + +$pid = held_worker('autovacuum worker'); + +# Stop holding new workers before the last signal, so autovacuum can finish. +$node->safe_psql('postgres', + "SELECT injection_points_detach('parallel-vacuum-worker-start')"); +$log_offset = -s $node->logfile; +($out, $err) = signal_as('regress_av_signal', 'pg_terminate_backend', $pid); +is($err, '', + 'parallel autovacuum worker: pg_signal_autovacuum_worker terminate: no error'); +is($out, 't', + 'parallel autovacuum worker: pg_signal_autovacuum_worker can terminate'); +wait_gone($pid); +ok( $node->log_contains( + qr/FATAL: .*terminating background worker "parallel worker" due to administrator command/, + $log_offset), + 'parallel autovacuum worker was terminated'); + +$node->safe_psql('postgres', + 'ALTER TABLE tbl SET (autovacuum_enabled = false)'); + +# Case 2: parallel worker of a superuser's own VACUUM stays superuser-only. +make_dead_tuples(); +$node->safe_psql('postgres', + "SELECT injection_points_attach('parallel-vacuum-worker-start', 'wait')"); +my $bg = $node->background_psql('postgres'); +$bg->query_until(qr/vacuum_started/, q{ +\echo vacuum_started +VACUUM (PARALLEL 1) tbl; +}); +$pid = held_worker('client backend'); + +($out, $err) = signal_as('regress_av_signal', 'pg_cancel_backend', $pid); +like( + $err, + qr/Only roles with the SUPERUSER attribute may cancel queries of roles with the SUPERUSER attribute/, + 'superuser VACUUM parallel worker: pg_signal_autovacuum_worker cannot cancel' +); + +($out, $err) = signal_as('regress_sig_backend', 'pg_terminate_backend', $pid); +like( + $err, + qr/Only roles with the SUPERUSER attribute may terminate processes of roles with the SUPERUSER attribute/, + 'superuser VACUUM parallel worker: pg_signal_backend member cannot terminate' +); + +$node->safe_psql( + 'postgres', q{ +SELECT injection_points_wakeup('parallel-vacuum-worker-start'); +SELECT injection_points_detach('parallel-vacuum-worker-start'); +}); +$bg->quit; + +$node->stop; +done_testing(); -- 2.47.3