Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright (c) 2026, PostgreSQL Global Development Group
 
# Test background workers can be terminated by db commands
 
use strict;
use warnings FATAL => 'all';
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
{
    my ($node, $database, $testcase, $interruptible, $restart_time) = @_;
    $restart_time //= -1;
 
    # Launch a background worker on the given database.
    my $pid = $node->safe_psql(
        'postgres', qq(
        SELECT worker_spi_launch($testcase, '$database'::regdatabase, 0, '{}', $interruptible, $restart_time);
    ));
 
    BAIL_OUT("worker_spi_launch returned no valid PID for testcase $testcase")
      unless defined($pid) && $pid =~ /\A[1-9][0-9]*\z/;
 
    # Check that the bgworker is initialized and napping.
    my $result =
      $node->poll_query_until('postgres',
        qq[SELECT wait_event FROM pg_stat_activity WHERE pid = $pid;],
        qq[WorkerSpiMain]);
    is($result, 1, "dynamic bgworker $testcase launched");
 
    return $pid;
}
 
# Run query and verify that the bgworker with the specified PID has been
# terminated.
sub run_bgworker_interruptible_test
{
    my ($node, $command, $testname, $pid) = @_;
    my $offset = -s $node->logfile;
 
    $node->safe_psql('postgres', $command);
 
    $node->wait_for_log(
        qr/terminating background worker \"worker_spi dynamic\" due to administrator command/,
        $offset);
 
    # Postmaster entry reporting the worker as exiting.
    $node->wait_for_log(
        qr/LOG: .*background worker \"worker_spi dynamic\" \(PID $pid\) exited with exit code/,
        $offset);
 
    my $result = $node->safe_psql('postgres',
        "SELECT count(*) = 0 FROM pg_stat_activity WHERE pid = $pid;");
    is($result, 't', "dynamic bgworker stopped for $testname");
}
 
my $node = PostgreSQL::Test::Cluster->new('mynode');
$node->init;
# The naptime is large enough to give some room on slow machines, so as
# the spawned workers have the time to process the interrupt requests sent
# by the database commands.
$node->append_conf(
    "postgresql.conf", qq(
autovacuum = off
# Allow slots for RuDB service workers as well as test workers.
max_worker_processes = 24
debug_parallel_query = off
log_min_messages = debug1
worker_spi.naptime = 600
));
$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';
}
 
$node->safe_psql('postgres', 'CREATE EXTENSION worker_spi;');
$node->safe_psql('postgres', 'CREATE DATABASE worker_template TEMPLATE template0;');
$node->safe_psql('worker_template', 'CREATE EXTENSION worker_spi;');
 
# Launch a background worker without BGWORKER_INTERRUPTIBLE.
my $pid = launch_bgworker($node, 'worker_template', 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(
    'postgres',
    "CREATE DATABASE testdb WITH TEMPLATE worker_template",
    stderr => \$stderr);
ok( $stderr =~
      "source database \"worker_template\" is being accessed by other users",
    "background worker blocked the database creation");
 
# Confirm that the non-interruptible bgworker is still running.
my $result = $node->safe_psql(
    "postgres", qq(
        SELECT count(1) FROM pg_stat_activity
        WHERE backend_type = 'worker_spi dynamic';));
 
is($result, '1',
    "background worker is still running after CREATE DATABASE WITH TEMPLATE");
 
# Terminate the non-interruptible worker for the next tests.
$node->safe_psql(
    "postgres", qq(
        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.
 
# Test case 1: CREATE DATABASE WITH TEMPLATE
$pid = launch_bgworker($node, 'worker_template', 1, 'true');
run_bgworker_interruptible_test(
    $node,
    "CREATE DATABASE testdb WITH TEMPLATE worker_template",
    "CREATE DATABASE WITH TEMPLATE", $pid);
 
# Test case 2: ALTER DATABASE RENAME
$pid = launch_bgworker($node, 'testdb', 2, 'true');
run_bgworker_interruptible_test(
    $node,
    "ALTER DATABASE testdb RENAME TO renameddb",
    "ALTER DATABASE RENAME", $pid);
 
# Preparation for the next test, create a tablespace.
my $tablespace = PostgreSQL::Test::Utils::tempdir;
$node->safe_psql('postgres',
    "CREATE TABLESPACE test_tablespace LOCATION '$tablespace'");
 
# Test case 3: ALTER DATABASE SET TABLESPACE
$pid = launch_bgworker($node, 'renameddb', 3, 'true');
run_bgworker_interruptible_test(
    $node,
    "ALTER DATABASE renameddb SET TABLESPACE test_tablespace",
    "ALTER DATABASE SET TABLESPACE", $pid);
 
# Test case 4: DROP DATABASE
$pid = launch_bgworker($node, 'renameddb', 4, 'true');
run_bgworker_interruptible_test(
    $node,
    "DROP DATABASE renameddb",
    "DROP DATABASE", $pid);
 
# Test case 5: restart after termination.  Restart time at 0 implies zero
# delay in the restart.
$pid = launch_bgworker($node, 'worker_template', 5, 'true', 0);
run_bgworker_interruptible_test(
    $node,
    "CREATE DATABASE testdb2 WITH TEMPLATE worker_template",
    "CREATE DATABASE WITH TEMPLATE, restartable worker", $pid);
$result = $node->poll_query_until(
    'postgres',
    qq[SELECT count(*) FROM pg_stat_activity
        WHERE backend_type = 'worker_spi dynamic'
        AND wait_event = 'WorkerSpiMain';],
    '1');
is($result, 1, "bgworker restarted after termination");
 
done_testing();