From 1703fa5247615d5d8e6d223f94606e951548b72d Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Thu, 2 Apr 2026 12:10:01 +0900 Subject: [PATCH] Introduce PG_BACKEND_WAIT_MAX_RETRIES --- src/backend/storage/ipc/procarray.c | 27 +++++++++------ .../worker_spi/t/002_worker_terminate.pl | 34 +++++-------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index 033ae68b57e..3e6df3780fd 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -3777,11 +3777,12 @@ CountUserBackends(Oid roleid) /* * CountOtherDBBackends -- check for other backends running in the given DB * - * If there are other backends in the DB, we will wait a maximum of 5 seconds - * for them to exit (or 0.3s for testing purposes). Autovacuum backends are - * encouraged to exit early by sending them SIGTERM, but normal user backends - * are just waited for. If background workers connected to this database are - * marked as interruptible, they are terminated. + * If there are other backends in the DB, we wait up to 50 times with 100ms + * between attempts (5s total by default). For test purposes, the retry + * count can be overridden with PG_BACKEND_WAIT_MAX_RETRIES. Autovacuum + * backends are encouraged to exit early by sending them SIGTERM, but normal + * user backends are just waited for. If background workers connected to this + * database are marked as interruptible, they are terminated. * * The current backend is always ignored; it is caller's responsibility to * check whether the current backend uses the given DB, if it's important. @@ -3808,15 +3809,19 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared) int autovac_pids[MAXAUTOVACPIDS]; /* - * Retry up to 50 times with 100ms between attempts (max 5s total). Can be - * reduced to 3 attempts (max 0.3s total) to speed up tests. + * Retry up to 50 times with 100ms between attempts (max 5s total) by + * default. Use PG_BACKEND_WAIT_MAX_RETRIES if it's set. */ int ntries = 50; + char *env_wait; -#ifdef USE_INJECTION_POINTS - if (IS_INJECTION_POINT_ATTACHED("procarray-reduce-count")) - ntries = 3; -#endif + env_wait = getenv("PG_BACKEND_WAIT_MAX_RETRIES"); + if (env_wait != NULL) + { + int val = atoi(env_wait); + if (val > 0) + ntries = val; + } for (int tries = 0; tries < ntries; tries++) { diff --git a/src/test/modules/worker_spi/t/002_worker_terminate.pl b/src/test/modules/worker_spi/t/002_worker_terminate.pl index b0e6a5376d4..bf9d663ca1f 100644 --- a/src/test/modules/worker_spi/t/002_worker_terminate.pl +++ b/src/test/modules/worker_spi/t/002_worker_terminate.pl @@ -8,13 +8,6 @@ use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; -# This test depends on injection points to detect whether background workers -# remain. -if ($ENV{enable_injection_points} ne 'yes') -{ - plan skip_all => 'Injection points not supported by this build'; -} - # Ensure the worker_spi dynamic worker is launched on the specified database. # Returns the PID of the worker launched. sub launch_bgworker @@ -70,15 +63,12 @@ autovacuum = off debug_parallel_query = off log_min_messages = debug1 )); -$node->start; -# Check if the extension injection_points is available, as it may be -# possible that this script is run with installcheck, where the module -# would not be installed by default. -if (!$node->check_extension('injection_points')) -{ - plan skip_all => 'Extension injection_points not installed'; -} +# Reduce the number of backend retries, allowing for shorter test runs. See +# CountOtherDBBackends(). +$ENV{PG_BACKEND_WAIT_MAX_RETRIES} = 3; + +$node->start; $node->safe_psql('postgres', 'CREATE EXTENSION worker_spi;'); @@ -88,12 +78,6 @@ my $pid = launch_bgworker($node, 'postgres', 0, 'false'); # Ensure CREATE DATABASE WITH TEMPLATE fails because a non-interruptible # bgworker exists. -# The injection point 'procarray-reduce-count' reduces the number of backend -# retries, allowing for shorter test runs. See CountOtherDBBackends(). -$node->safe_psql('postgres', "CREATE EXTENSION injection_points;"); -$node->safe_psql('postgres', - "SELECT injection_points_attach('procarray-reduce-count', 'error');"); - my $stderr; $node->psql( @@ -119,13 +103,13 @@ $node->safe_psql( SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE backend_type = 'worker_spi dynamic';)); -# The injection point is not used anymore, release it. -$node->safe_psql('postgres', - "SELECT injection_points_detach('procarray-reduce-count');"); - # Check that BGWORKER_INTERRUPTIBLE allows background workers to be # terminated with database-related commands. +# Make the wait time longer than default to stabilize some builfarm clients +$ENV{PG_BACKEND_WAIT_MAX_RETRIES} = 100; +$node->restart; + # Test case 1: CREATE DATABASE WITH TEMPLATE $pid = launch_bgworker($node, 'postgres', 1, 'true'); run_bgworker_interruptible_test( -- 2.47.3