Debian Codegraph @

src/test/modules/worker_spi/t/001_worker_spi.pl

raw
1 # Copyright (c) 2023-2025, PostgreSQL Global Development Group
2
3 # Test worker_spi module.
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 my $node = PostgreSQL::Test::Cluster->new('mynode');
12 $node->init;
13 $node->start;
14
15 note "testing dynamic bgworkers";
16
17 $node->safe_psql('postgres', 'CREATE EXTENSION worker_spi;');
18
19 # Launch one dynamic worker, then wait for its initialization to complete.
20 # This consists in making sure that a table name "counted" is created
21 # on a new schema whose name includes the index defined in input argument
22 # of worker_spi_launch().
23 # By default, dynamic bgworkers connect to the "postgres" database with
24 # an undefined role, falling back to the GUC defaults (or InvalidOid for
25 # worker_spi_launch).
26 my $result =
27 $node->safe_psql('postgres', 'SELECT worker_spi_launch(4) IS NOT NULL;');
28 is($result, 't', "dynamic bgworker launched");
29 $node->poll_query_until(
30 'postgres',
31 qq[SELECT count(*) > 0 FROM information_schema.tables
32 WHERE table_schema = 'schema4' AND table_name = 'counted';]);
33 $node->safe_psql('postgres',
34 "INSERT INTO schema4.counted VALUES ('total', 0), ('delta', 1);");
35 # Issue a SIGHUP on the node to force the worker to loop once, accelerating
36 # this test.
37 $node->reload;
38 # Wait until the worker has processed the tuple that has just been inserted.
39 $node->poll_query_until('postgres',
40 qq[SELECT count(*) FROM schema4.counted WHERE type = 'delta';], '0');
41 $result = $node->safe_psql('postgres', 'SELECT * FROM schema4.counted;');
42 is($result, qq(total|1), 'dynamic bgworker correctly consumed tuple data');
43
44 # Check the wait event used by the dynamic bgworker.
45 $result = $node->poll_query_until(
46 'postgres',
47 qq[SELECT wait_event FROM pg_stat_activity WHERE backend_type ~ 'worker_spi';],
48 qq[WorkerSpiMain]);
49 is($result, 1, 'dynamic bgworker has reported "WorkerSpiMain" as wait event');
50
51 # Check the wait event used by the dynamic bgworker appears in pg_wait_events
52 $result = $node->safe_psql('postgres',
53 q[SELECT count(*) > 0 from pg_wait_events where type = 'Extension' and name = 'WorkerSpiMain';]
54 );
55 is($result, 't', '"WorkerSpiMain" is reported in pg_wait_events');
56
57 note "testing bgworkers loaded with shared_preload_libraries";
58
59 # Create the database first so as the workers can connect to it when
60 # the library is loaded.
61 $node->safe_psql('postgres', q(CREATE DATABASE mydb;));
62 $node->safe_psql('postgres', q(CREATE ROLE myrole SUPERUSER LOGIN;));
63 $node->safe_psql('mydb', 'CREATE EXTENSION worker_spi;');
64
65 # Now load the module as a shared library.
66 # Update max_worker_processes to make room for enough bgworkers, including
67 # parallel workers these may spawn.
68 $node->append_conf(
69 'postgresql.conf', q{
70 shared_preload_libraries = 'worker_spi'
71 worker_spi.database = 'mydb'
72 worker_spi.total_workers = 3
73 max_worker_processes = 32
74 });
75 $node->restart;
76
77 # Check that bgworkers have been registered and launched.
78 ok( $node->poll_query_until(
79 'mydb',
80 qq[SELECT datname, count(datname), wait_event FROM pg_stat_activity
81 WHERE backend_type = 'worker_spi' GROUP BY datname, wait_event;],
82 'mydb|3|WorkerSpiMain'),
83 'bgworkers all launched'
84 ) or die "Timed out while waiting for bgworkers to be launched";
85
86 # Ask worker_spi to launch dynamic bgworkers with the library loaded, then
87 # check their existence. Use IDs that do not overlap with the schemas created
88 # by the previous workers. These ones use a new role, on different databases.
89 my $myrole_id = $node->safe_psql('mydb',
90 "SELECT oid FROM pg_roles where rolname = 'myrole';");
91 my $mydb_id = $node->safe_psql('mydb',
92 "SELECT oid FROM pg_database where datname = 'mydb';");
93 my $postgresdb_id = $node->safe_psql('mydb',
94 "SELECT oid FROM pg_database where datname = 'postgres';");
95 my $worker1_pid = $node->safe_psql('mydb',
96 "SELECT worker_spi_launch(10, $mydb_id, $myrole_id);");
97 my $worker2_pid = $node->safe_psql('mydb',
98 "SELECT worker_spi_launch(11, $postgresdb_id, $myrole_id);");
99
100 ok( $node->poll_query_until(
101 'mydb',
102 qq[SELECT datname, usename, wait_event FROM pg_stat_activity
103 WHERE backend_type = 'worker_spi dynamic' AND
104 pid IN ($worker1_pid, $worker2_pid) ORDER BY datname;],
105 qq[mydb|myrole|WorkerSpiMain
106 postgres|myrole|WorkerSpiMain]),
107 'dynamic bgworkers all launched'
108 ) or die "Timed out while waiting for dynamic bgworkers to be launched";
109
110 # Check BGWORKER_BYPASS_ALLOWCONN.
111 $node->safe_psql('postgres',
112 q(CREATE DATABASE noconndb ALLOW_CONNECTIONS false;));
113 my $noconndb_id = $node->safe_psql('mydb',
114 "SELECT oid FROM pg_database where datname = 'noconndb';");
115 my $log_offset = -s $node->logfile;
116
117 # worker_spi_launch() may be able to detect that the worker has been
118 # stopped, so do not rely on safe_psql().
119 $node->psql('postgres',
120 qq[SELECT worker_spi_launch(12, $noconndb_id, $myrole_id);]);
121 $node->wait_for_log(
122 qr/database "noconndb" is not currently accepting connections/,
123 $log_offset);
124
125 # bgworker bypasses the connection check, and can be launched.
126 my $worker4_pid = $node->safe_psql('postgres',
127 qq[SELECT worker_spi_launch(12, $noconndb_id, $myrole_id, '{"ALLOWCONN"}');]
128 );
129 ok( $node->poll_query_until(
130 'postgres',
131 qq[SELECT datname, usename, wait_event FROM pg_stat_activity
132 WHERE backend_type = 'worker_spi dynamic' AND
133 pid IN ($worker4_pid) ORDER BY datname;],
134 qq[noconndb|myrole|WorkerSpiMain]),
135 'dynamic bgworker with BYPASS_ALLOWCONN started');
136
137 # Check BGWORKER_BYPASS_ROLELOGINCHECK.
138 # First create a role without login access.
139 $node->safe_psql(
140 'postgres', qq[
141 CREATE ROLE nologrole WITH NOLOGIN;
142 GRANT CREATE ON DATABASE mydb TO nologrole;
143 ]);
144 my $nologrole_id = $node->safe_psql('mydb',
145 "SELECT oid FROM pg_roles where rolname = 'nologrole';");
146 $log_offset = -s $node->logfile;
147
148 # bgworker cannot be launched with login restriction.
149 $node->psql('postgres',
150 qq[SELECT worker_spi_launch(13, $mydb_id, $nologrole_id);]);
151 $node->wait_for_log(qr/role "nologrole" is not permitted to log in/,
152 $log_offset);
153
154 # bgworker bypasses the login restriction, and can be launched.
155 $log_offset = -s $node->logfile;
156 my $worker5_pid = $node->safe_psql('mydb',
157 qq[SELECT worker_spi_launch(13, $mydb_id, $nologrole_id, '{"ROLELOGINCHECK"}');]
158 );
159 ok( $node->poll_query_until(
160 'mydb',
161 qq[SELECT datname, usename, wait_event FROM pg_stat_activity
162 WHERE backend_type = 'worker_spi dynamic' AND
163 pid = $worker5_pid;],
164 'mydb|nologrole|WorkerSpiMain'),
165 'dynamic bgworker with BYPASS_ROLELOGINCHECK launched');
166
167 done_testing();