|
| 1 | +# Copyright (c) 2025, PostgreSQL Global Development Group |
| 2 | + |
| 3 | +# Test background workers can be terminated by db commands |
| 4 | + |
| 5 | +use strict; |
| 6 | +use warnings FATAL => 'all'; |
| 7 | +use PostgreSQL::Test::Cluster; |
| 8 | +use PostgreSQL::Test::Utils; |
| 9 | +use Test::More; |
| 10 | + |
| 11 | +# This test depends on injection points to detect whether background workers |
| 12 | +# remain. |
| 13 | +if ($ENV{enable_injection_points} ne 'yes') |
| 14 | +{ |
| 15 | + plan skip_all => 'Injection points not supported by this build'; |
| 16 | +} |
| 17 | + |
| 18 | +# Ensure the worker_spi dynamic worker is launched on the specified database |
| 19 | +sub launch_bgworker |
| 20 | +{ |
| 21 | + my ($node, $database, $testcase, $request_terminate) = @_; |
| 22 | + my $offset = -s $node->logfile; |
| 23 | + |
| 24 | + # Launch a background worker on the given database |
| 25 | + my $result = $node->safe_psql( |
| 26 | + $database, qq( |
| 27 | + SELECT worker_spi_launch($testcase, oid, 0, '{}', $request_terminate) IS NOT NULL |
| 28 | + FROM pg_database WHERE datname = '$database'; |
| 29 | + )); |
| 30 | + is($result, 't', "dynamic bgworker $testcase launched"); |
| 31 | + |
| 32 | + # Check the worker is surely initialized |
| 33 | + $node->wait_for_log( |
| 34 | + qr/LOG: worker_spi dynamic worker $testcase initialized with .*\..*/, |
| 35 | + $offset); |
| 36 | +} |
| 37 | + |
| 38 | +# Run the given query and verify the background worker can be terminated |
| 39 | +sub run_db_command |
| 40 | +{ |
| 41 | + my ($node, $command, $testname) = @_; |
| 42 | + my $offset = -s $node->logfile; |
| 43 | + |
| 44 | + $node->safe_psql('postgres', $command); |
| 45 | + |
| 46 | + $node->wait_for_log( |
| 47 | + qr/terminating background worker \"worker_spi dynamic\" due to administrator command/, |
| 48 | + $offset); |
| 49 | + |
| 50 | + note("background worker can be terminated at $testname"); |
| 51 | +} |
| 52 | + |
| 53 | +my $node = PostgreSQL::Test::Cluster->new('mynode'); |
| 54 | +$node->init; |
| 55 | +$node->start; |
| 56 | + |
| 57 | +# Check if the extension injection_points is available, as it may be |
| 58 | +# possible that this script is run with installcheck, where the module |
| 59 | +# would not be installed by default. |
| 60 | +if (!$node->check_extension('injection_points')) |
| 61 | +{ |
| 62 | + plan skip_all => 'Extension injection_points not installed'; |
| 63 | +} |
| 64 | + |
| 65 | +$node->safe_psql('postgres', 'CREATE EXTENSION worker_spi;'); |
| 66 | + |
| 67 | +# Launch a background worker without BGWORKER_EXIT_AT_DATABASE_CHANGE |
| 68 | +launch_bgworker($node, 'postgres', 0, "false"); |
| 69 | + |
| 70 | +# Ensure CREATE DATABASE WITH TEMPLATE fails because background worker retains |
| 71 | + |
| 72 | +# The injection point 'reduce-ncounts' reduces the number of backend |
| 73 | +# retries, allowing for shorter test runs. See CountOtherDBBackends(). |
| 74 | +$node->safe_psql('postgres', "CREATE EXTENSION injection_points;"); |
| 75 | +$node->safe_psql('postgres', |
| 76 | + "SELECT injection_points_attach('reduce-ncounts', 'error');"); |
| 77 | + |
| 78 | +my $stderr; |
| 79 | + |
| 80 | +$node->psql( |
| 81 | + 'postgres', |
| 82 | + "CREATE DATABASE testdb WITH TEMPLATE postgres", |
| 83 | + stderr => \$stderr); |
| 84 | +ok( $stderr =~ |
| 85 | + "source database \"postgres\" is being accessed by other users", |
| 86 | + "background worker blocked the database creation"); |
| 87 | + |
| 88 | +# Confirm a background worker is still running |
| 89 | +my $result = $node->safe_psql( |
| 90 | + "postgres", qq( |
| 91 | + SELECT count(1) FROM pg_stat_activity |
| 92 | + WHERE backend_type = 'worker_spi dynamic';)); |
| 93 | + |
| 94 | +is($result, '1', |
| 95 | + "background worker is still running after CREATE DATABASE WITH TEMPLATE"); |
| 96 | + |
| 97 | +# Terminate the worker for upcoming tests |
| 98 | +$node->safe_psql( |
| 99 | + "postgres", qq( |
| 100 | + SELECT pg_terminate_backend(pid) |
| 101 | + FROM pg_stat_activity WHERE backend_type = 'worker_spi dynamic';)); |
| 102 | + |
| 103 | +# The injection point won't be used anymore, release it. |
| 104 | +$node->safe_psql('postgres', |
| 105 | + "SELECT injection_points_detach('reduce-ncounts');"); |
| 106 | + |
| 107 | +# Ensure BGWORKER_EXIT_AT_DATABASE_CHANGE allows background workers to be |
| 108 | +# terminated at some database manipulations. |
| 109 | +# |
| 110 | +# Testcase 1: CREATE DATABASE WITH TEMPLATE |
| 111 | +launch_bgworker($node, 'postgres', 1, "true"); |
| 112 | +run_db_command( |
| 113 | + $node, |
| 114 | + "CREATE DATABASE testdb WITH TEMPLATE postgres", |
| 115 | + "CREATE DATABASE WITH TEMPLATE"); |
| 116 | + |
| 117 | +# Testcase 2: ALTER DATABASE RENAME |
| 118 | +launch_bgworker($node, 'testdb', 2, "true"); |
| 119 | +run_db_command( |
| 120 | + $node, |
| 121 | + "ALTER DATABASE testdb RENAME TO renameddb", |
| 122 | + "ALTER DATABASE RENAME"); |
| 123 | + |
| 124 | +# Preparation for the next test; create another tablespace |
| 125 | +my $tablespace = PostgreSQL::Test::Utils::tempdir; |
| 126 | +$node->safe_psql('postgres', |
| 127 | + "CREATE TABLESPACE test_tablespace LOCATION '$tablespace'"); |
| 128 | + |
| 129 | +# Testcase 3: ALTER DATABASE SET TABLESPACE |
| 130 | +launch_bgworker($node, 'renameddb', 3, "true"); |
| 131 | +run_db_command( |
| 132 | + $node, |
| 133 | + "ALTER DATABASE renameddb SET TABLESPACE test_tablespace", |
| 134 | + "ALTER DATABASE SET TABLESPACE"); |
| 135 | + |
| 136 | +# Testcase 4: DROP DATABASE |
| 137 | +launch_bgworker($node, 'renameddb', 4, "true"); |
| 138 | +run_db_command($node, "DROP DATABASE renameddb", "DROP DATABASE"); |
| 139 | + |
| 140 | +done_testing(); |
0 commit comments