| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test streaming of simple large transaction |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
# Check that the parallel apply worker has finished applying the streaming |
| 12 |
# transaction. |
| 13 |
sub check_parallel_log |
| 14 |
{ |
| 15 |
my ($node_subscriber, $offset, $is_parallel, $type) = @_; |
| 16 |
|
| 17 |
if ($is_parallel) |
| 18 |
{ |
| 19 |
$node_subscriber->wait_for_log( |
| 20 |
qr/DEBUG: ( [A-Z0-9]+:)? finished processing the STREAM $type command/, |
| 21 |
$offset); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
# Common test steps for both the streaming=on and streaming=parallel cases. |
| 26 |
sub test_streaming |
| 27 |
{ |
| 28 |
my ($node_publisher, $node_subscriber, $appname, $is_parallel) = @_; |
| 29 |
|
| 30 |
# Interleave a pair of transactions, each exceeding the 64kB limit. |
| 31 |
my $offset = 0; |
| 32 |
|
| 33 |
my $h = $node_publisher->background_psql('postgres', on_error_stop => 0); |
| 34 |
|
| 35 |
# Check the subscriber log from now on. |
| 36 |
$offset = -s $node_subscriber->logfile; |
| 37 |
|
| 38 |
$h->query_safe( |
| 39 |
q{ |
| 40 |
BEGIN; |
| 41 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(3, 5000) s(i); |
| 42 |
UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0; |
| 43 |
DELETE FROM test_tab WHERE mod(a,3) = 0; |
| 44 |
}); |
| 45 |
|
| 46 |
$node_publisher->safe_psql( |
| 47 |
'postgres', q{ |
| 48 |
BEGIN; |
| 49 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(5001, 9999) s(i); |
| 50 |
DELETE FROM test_tab WHERE a > 5000; |
| 51 |
COMMIT; |
| 52 |
}); |
| 53 |
|
| 54 |
$h->query_safe('COMMIT'); |
| 55 |
# errors make the next test fail, so ignore them here |
| 56 |
$h->quit; |
| 57 |
|
| 58 |
$node_publisher->wait_for_catchup($appname); |
| 59 |
|
| 60 |
check_parallel_log($node_subscriber, $offset, $is_parallel, 'COMMIT'); |
| 61 |
|
| 62 |
my $result = |
| 63 |
$node_subscriber->safe_psql('postgres', |
| 64 |
"SELECT count(*), count(c), count(d = 999) FROM test_tab"); |
| 65 |
is($result, qq(3334|3334|3334), |
| 66 |
'check extra columns contain local defaults'); |
| 67 |
|
| 68 |
# Test the streaming in binary mode |
| 69 |
$node_subscriber->safe_psql('postgres', |
| 70 |
"ALTER SUBSCRIPTION tap_sub SET (binary = on)"); |
| 71 |
|
| 72 |
# Check the subscriber log from now on. |
| 73 |
$offset = -s $node_subscriber->logfile; |
| 74 |
|
| 75 |
# Insert, update and delete enough rows to exceed the 64kB limit. |
| 76 |
$node_publisher->safe_psql( |
| 77 |
'postgres', q{ |
| 78 |
BEGIN; |
| 79 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(5001, 10000) s(i); |
| 80 |
UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0; |
| 81 |
DELETE FROM test_tab WHERE mod(a,3) = 0; |
| 82 |
COMMIT; |
| 83 |
}); |
| 84 |
|
| 85 |
$node_publisher->wait_for_catchup($appname); |
| 86 |
|
| 87 |
check_parallel_log($node_subscriber, $offset, $is_parallel, 'COMMIT'); |
| 88 |
|
| 89 |
$result = |
| 90 |
$node_subscriber->safe_psql('postgres', |
| 91 |
"SELECT count(*), count(c), count(d = 999) FROM test_tab"); |
| 92 |
is($result, qq(6667|6667|6667), |
| 93 |
'check extra columns contain local defaults'); |
| 94 |
|
| 95 |
# Change the local values of the extra columns on the subscriber, |
| 96 |
# update publisher, and check that subscriber retains the expected |
| 97 |
# values. This is to ensure that non-streaming transactions behave |
| 98 |
# properly after a streaming transaction. |
| 99 |
$node_subscriber->safe_psql('postgres', |
| 100 |
"UPDATE test_tab SET c = 'epoch'::timestamptz + 987654321 * interval '1s'" |
| 101 |
); |
| 102 |
|
| 103 |
# Check the subscriber log from now on. |
| 104 |
$offset = -s $node_subscriber->logfile; |
| 105 |
|
| 106 |
$node_publisher->safe_psql('postgres', |
| 107 |
"UPDATE test_tab SET b = sha256(a::text::bytea)"); |
| 108 |
|
| 109 |
$node_publisher->wait_for_catchup($appname); |
| 110 |
|
| 111 |
check_parallel_log($node_subscriber, $offset, $is_parallel, 'COMMIT'); |
| 112 |
|
| 113 |
$result = $node_subscriber->safe_psql('postgres', |
| 114 |
"SELECT count(*), count(extract(epoch from c) = 987654321), count(d = 999) FROM test_tab" |
| 115 |
); |
| 116 |
is($result, qq(6667|6667|6667), |
| 117 |
'check extra columns contain locally changed data'); |
| 118 |
|
| 119 |
# Cleanup the test data |
| 120 |
$node_publisher->safe_psql('postgres', |
| 121 |
"DELETE FROM test_tab WHERE (a > 2)"); |
| 122 |
$node_publisher->wait_for_catchup($appname); |
| 123 |
} |
| 124 |
|
| 125 |
# Create publisher node |
| 126 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 127 |
$node_publisher->init(allows_streaming => 'logical'); |
| 128 |
$node_publisher->append_conf('postgresql.conf', |
| 129 |
'logical_decoding_work_mem = 64kB'); |
| 130 |
$node_publisher->start; |
| 131 |
|
| 132 |
# Create subscriber node |
| 133 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 134 |
$node_subscriber->init; |
| 135 |
$node_subscriber->start; |
| 136 |
|
| 137 |
# Create some preexisting content on publisher |
| 138 |
$node_publisher->safe_psql('postgres', |
| 139 |
"CREATE TABLE test_tab (a int primary key, b bytea)"); |
| 140 |
$node_publisher->safe_psql('postgres', |
| 141 |
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')"); |
| 142 |
|
| 143 |
$node_publisher->safe_psql('postgres', "CREATE TABLE test_tab_2 (a int)"); |
| 144 |
|
| 145 |
# Setup structure on subscriber |
| 146 |
$node_subscriber->safe_psql('postgres', |
| 147 |
"CREATE TABLE test_tab (a int primary key, b bytea, c timestamptz DEFAULT now(), d bigint DEFAULT 999)" |
| 148 |
); |
| 149 |
|
| 150 |
$node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab_2 (a int)"); |
| 151 |
$node_subscriber->safe_psql('postgres', |
| 152 |
"CREATE UNIQUE INDEX idx_tab on test_tab_2(a)"); |
| 153 |
|
| 154 |
# Setup logical replication |
| 155 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 156 |
$node_publisher->safe_psql('postgres', |
| 157 |
"CREATE PUBLICATION tap_pub FOR TABLE test_tab, test_tab_2"); |
| 158 |
|
| 159 |
my $appname = 'tap_sub'; |
| 160 |
|
| 161 |
################################ |
| 162 |
# Test using streaming mode 'on' |
| 163 |
################################ |
| 164 |
$node_subscriber->safe_psql('postgres', |
| 165 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)" |
| 166 |
); |
| 167 |
|
| 168 |
# Wait for initial table sync to finish |
| 169 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 170 |
|
| 171 |
my $result = |
| 172 |
$node_subscriber->safe_psql('postgres', |
| 173 |
"SELECT count(*), count(c), count(d = 999) FROM test_tab"); |
| 174 |
is($result, qq(2|2|2), 'check initial data was copied to subscriber'); |
| 175 |
|
| 176 |
test_streaming($node_publisher, $node_subscriber, $appname, 0); |
| 177 |
|
| 178 |
###################################### |
| 179 |
# Test using streaming mode 'parallel' |
| 180 |
###################################### |
| 181 |
my $oldpid = $node_publisher->safe_psql('postgres', |
| 182 |
"SELECT pid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';" |
| 183 |
); |
| 184 |
|
| 185 |
$node_subscriber->safe_psql('postgres', |
| 186 |
"ALTER SUBSCRIPTION tap_sub SET(streaming = parallel, binary = off)"); |
| 187 |
|
| 188 |
$node_publisher->poll_query_until('postgres', |
| 189 |
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';" |
| 190 |
) |
| 191 |
or die |
| 192 |
"Timed out while waiting for apply to restart after changing SUBSCRIPTION"; |
| 193 |
|
| 194 |
# We need to check DEBUG logs to ensure that the parallel apply worker has |
| 195 |
# applied the transaction. So, bump up the log verbosity. |
| 196 |
$node_subscriber->append_conf('postgresql.conf', "log_min_messages = debug1"); |
| 197 |
$node_subscriber->reload; |
| 198 |
|
| 199 |
# Run a query to make sure that the reload has taken effect. |
| 200 |
$node_subscriber->safe_psql('postgres', q{SELECT 1}); |
| 201 |
|
| 202 |
test_streaming($node_publisher, $node_subscriber, $appname, 1); |
| 203 |
|
| 204 |
# Test that the deadlock is detected among the leader and parallel apply |
| 205 |
# workers. |
| 206 |
|
| 207 |
$node_subscriber->append_conf('postgresql.conf', "deadlock_timeout = 10ms"); |
| 208 |
$node_subscriber->reload; |
| 209 |
|
| 210 |
# Run a query to make sure that the reload has taken effect. |
| 211 |
$node_subscriber->safe_psql('postgres', q{SELECT 1}); |
| 212 |
|
| 213 |
# Interleave a pair of transactions, each exceeding the 64kB limit. |
| 214 |
my $h = $node_publisher->background_psql('postgres', on_error_stop => 0); |
| 215 |
|
| 216 |
# Confirm if a deadlock between the leader apply worker and the parallel apply |
| 217 |
# worker can be detected. |
| 218 |
|
| 219 |
my $offset = -s $node_subscriber->logfile; |
| 220 |
|
| 221 |
$h->query_safe( |
| 222 |
q{ |
| 223 |
BEGIN; |
| 224 |
INSERT INTO test_tab_2 SELECT i FROM generate_series(1, 5000) s(i); |
| 225 |
}); |
| 226 |
|
| 227 |
# Ensure that the parallel apply worker executes the insert command before the |
| 228 |
# leader worker. |
| 229 |
$node_subscriber->wait_for_log( |
| 230 |
qr/DEBUG: ( [A-Z0-9]+:)? applied [0-9]+ changes in the streaming chunk/, |
| 231 |
$offset); |
| 232 |
|
| 233 |
$node_publisher->safe_psql('postgres', "INSERT INTO test_tab_2 values(1)"); |
| 234 |
|
| 235 |
$h->query_safe('COMMIT'); |
| 236 |
$h->quit; |
| 237 |
|
| 238 |
$node_subscriber->wait_for_log(qr/ERROR: ( [A-Z0-9]+:)? deadlock detected/, |
| 239 |
$offset); |
| 240 |
|
| 241 |
# In order for the two transactions to be completed normally without causing |
| 242 |
# conflicts due to the unique index, we temporarily drop it. |
| 243 |
$node_subscriber->safe_psql('postgres', "DROP INDEX idx_tab"); |
| 244 |
|
| 245 |
# Wait for this streaming transaction to be applied in the apply worker. |
| 246 |
$node_publisher->wait_for_catchup($appname); |
| 247 |
|
| 248 |
$result = |
| 249 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2"); |
| 250 |
is($result, qq(5001), 'data replicated to subscriber after dropping index'); |
| 251 |
|
| 252 |
# Clean up test data from the environment. |
| 253 |
$node_publisher->safe_psql('postgres', "TRUNCATE TABLE test_tab_2"); |
| 254 |
$node_publisher->wait_for_catchup($appname); |
| 255 |
$node_subscriber->safe_psql('postgres', |
| 256 |
"CREATE UNIQUE INDEX idx_tab on test_tab_2(a)"); |
| 257 |
|
| 258 |
# Confirm if a deadlock between two parallel apply workers can be detected. |
| 259 |
|
| 260 |
# Check the subscriber log from now on. |
| 261 |
$offset = -s $node_subscriber->logfile; |
| 262 |
|
| 263 |
$h->query_safe( |
| 264 |
q{ |
| 265 |
BEGIN; |
| 266 |
INSERT INTO test_tab_2 SELECT i FROM generate_series(1, 5000) s(i); |
| 267 |
}); |
| 268 |
|
| 269 |
# Ensure that the first parallel apply worker executes the insert command |
| 270 |
# before the second one. |
| 271 |
$node_subscriber->wait_for_log( |
| 272 |
qr/DEBUG: ( [A-Z0-9]+:)? applied [0-9]+ changes in the streaming chunk/, |
| 273 |
$offset); |
| 274 |
|
| 275 |
$node_publisher->safe_psql('postgres', |
| 276 |
"INSERT INTO test_tab_2 SELECT i FROM generate_series(1, 5000) s(i)"); |
| 277 |
|
| 278 |
$h->query_safe('COMMIT'); |
| 279 |
$h->quit; |
| 280 |
|
| 281 |
$node_subscriber->wait_for_log(qr/ERROR: ( [A-Z0-9]+:)? deadlock detected/, |
| 282 |
$offset); |
| 283 |
|
| 284 |
# In order for the two transactions to be completed normally without causing |
| 285 |
# conflicts due to the unique index, we temporarily drop it. |
| 286 |
$node_subscriber->safe_psql('postgres', "DROP INDEX idx_tab"); |
| 287 |
|
| 288 |
# Wait for this streaming transaction to be applied in the apply worker. |
| 289 |
$node_publisher->wait_for_catchup($appname); |
| 290 |
|
| 291 |
$result = |
| 292 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2"); |
| 293 |
is($result, qq(10000), 'data replicated to subscriber after dropping index'); |
| 294 |
|
| 295 |
# Test serializing changes to files and notify the parallel apply worker to |
| 296 |
# apply them at the end of the transaction. |
| 297 |
$node_subscriber->append_conf('postgresql.conf', |
| 298 |
'debug_logical_replication_streaming = immediate'); |
| 299 |
# Reset the log_min_messages to default. |
| 300 |
$node_subscriber->append_conf('postgresql.conf', |
| 301 |
"log_min_messages = warning"); |
| 302 |
$node_subscriber->reload; |
| 303 |
|
| 304 |
# Run a query to make sure that the reload has taken effect. |
| 305 |
$node_subscriber->safe_psql('postgres', q{SELECT 1}); |
| 306 |
|
| 307 |
$offset = -s $node_subscriber->logfile; |
| 308 |
|
| 309 |
$node_publisher->safe_psql('postgres', |
| 310 |
"INSERT INTO test_tab_2 SELECT i FROM generate_series(1, 5000) s(i)"); |
| 311 |
|
| 312 |
# Ensure that the changes are serialized. |
| 313 |
$node_subscriber->wait_for_log( |
| 314 |
qr/LOG: ( [A-Z0-9]+:)? logical replication apply worker will serialize the remaining changes of remote transaction \d+ to a file/, |
| 315 |
$offset); |
| 316 |
|
| 317 |
$node_publisher->wait_for_catchup($appname); |
| 318 |
|
| 319 |
# Check that transaction is committed on subscriber |
| 320 |
$result = |
| 321 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2"); |
| 322 |
is($result, qq(15000), |
| 323 |
'parallel apply worker replayed all changes from file'); |
| 324 |
|
| 325 |
$node_subscriber->stop; |
| 326 |
$node_publisher->stop; |
| 327 |
|
| 328 |
done_testing(); |