| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test streaming of transaction containing multiple subtransactions and rollbacks |
| 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 |
my $offset = 0; |
| 31 |
|
| 32 |
# Check the subscriber log from now on. |
| 33 |
$offset = -s $node_subscriber->logfile; |
| 34 |
|
| 35 |
# streamed transaction with DDL, DML and ROLLBACKs |
| 36 |
$node_publisher->safe_psql( |
| 37 |
'postgres', q{ |
| 38 |
BEGIN; |
| 39 |
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea)); |
| 40 |
SAVEPOINT s1; |
| 41 |
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea)); |
| 42 |
SAVEPOINT s2; |
| 43 |
INSERT INTO test_tab VALUES (5, sha256(5::text::bytea)); |
| 44 |
SAVEPOINT s3; |
| 45 |
INSERT INTO test_tab VALUES (6, sha256(6::text::bytea)); |
| 46 |
ROLLBACK TO s2; |
| 47 |
INSERT INTO test_tab VALUES (7, sha256(7::text::bytea)); |
| 48 |
ROLLBACK TO s1; |
| 49 |
INSERT INTO test_tab VALUES (8, sha256(8::text::bytea)); |
| 50 |
SAVEPOINT s4; |
| 51 |
INSERT INTO test_tab VALUES (9, sha256(9::text::bytea)); |
| 52 |
SAVEPOINT s5; |
| 53 |
INSERT INTO test_tab VALUES (10, sha256(10::text::bytea)); |
| 54 |
COMMIT; |
| 55 |
}); |
| 56 |
|
| 57 |
$node_publisher->wait_for_catchup($appname); |
| 58 |
|
| 59 |
check_parallel_log($node_subscriber, $offset, $is_parallel, 'COMMIT'); |
| 60 |
|
| 61 |
my $result = |
| 62 |
$node_subscriber->safe_psql('postgres', |
| 63 |
"SELECT count(*), count(c) FROM test_tab"); |
| 64 |
is($result, qq(6|0), |
| 65 |
'check rollback to savepoint was reflected on subscriber and extra columns contain local defaults' |
| 66 |
); |
| 67 |
|
| 68 |
# Check the subscriber log from now on. |
| 69 |
$offset = -s $node_subscriber->logfile; |
| 70 |
|
| 71 |
# streamed transaction with subscriber receiving out of order |
| 72 |
# subtransaction ROLLBACKs |
| 73 |
$node_publisher->safe_psql( |
| 74 |
'postgres', q{ |
| 75 |
BEGIN; |
| 76 |
INSERT INTO test_tab VALUES (11, sha256(11::text::bytea)); |
| 77 |
SAVEPOINT s1; |
| 78 |
INSERT INTO test_tab VALUES (12, sha256(12::text::bytea)); |
| 79 |
SAVEPOINT s2; |
| 80 |
INSERT INTO test_tab VALUES (13, sha256(13::text::bytea)); |
| 81 |
SAVEPOINT s3; |
| 82 |
INSERT INTO test_tab VALUES (14, sha256(14::text::bytea)); |
| 83 |
RELEASE s2; |
| 84 |
INSERT INTO test_tab VALUES (15, sha256(15::text::bytea)); |
| 85 |
ROLLBACK TO s1; |
| 86 |
COMMIT; |
| 87 |
}); |
| 88 |
|
| 89 |
$node_publisher->wait_for_catchup($appname); |
| 90 |
|
| 91 |
check_parallel_log($node_subscriber, $offset, $is_parallel, 'COMMIT'); |
| 92 |
|
| 93 |
$result = |
| 94 |
$node_subscriber->safe_psql('postgres', |
| 95 |
"SELECT count(*), count(c) FROM test_tab"); |
| 96 |
is($result, qq(7|0), |
| 97 |
'check rollback to savepoint was reflected on subscriber'); |
| 98 |
|
| 99 |
# Check the subscriber log from now on. |
| 100 |
$offset = -s $node_subscriber->logfile; |
| 101 |
|
| 102 |
# streamed transaction with subscriber receiving rollback |
| 103 |
$node_publisher->safe_psql( |
| 104 |
'postgres', q{ |
| 105 |
BEGIN; |
| 106 |
INSERT INTO test_tab VALUES (16, sha256(16::text::bytea)); |
| 107 |
SAVEPOINT s1; |
| 108 |
INSERT INTO test_tab VALUES (17, sha256(17::text::bytea)); |
| 109 |
SAVEPOINT s2; |
| 110 |
INSERT INTO test_tab VALUES (18, sha256(18::text::bytea)); |
| 111 |
ROLLBACK; |
| 112 |
}); |
| 113 |
|
| 114 |
$node_publisher->wait_for_catchup($appname); |
| 115 |
|
| 116 |
check_parallel_log($node_subscriber, $offset, $is_parallel, 'ABORT'); |
| 117 |
|
| 118 |
$result = |
| 119 |
$node_subscriber->safe_psql('postgres', |
| 120 |
"SELECT count(*), count(c) FROM test_tab"); |
| 121 |
is($result, qq(7|0), 'check rollback was reflected on subscriber'); |
| 122 |
|
| 123 |
# Cleanup the test data |
| 124 |
$node_publisher->safe_psql('postgres', |
| 125 |
"DELETE FROM test_tab WHERE (a > 2)"); |
| 126 |
$node_publisher->wait_for_catchup($appname); |
| 127 |
} |
| 128 |
|
| 129 |
# Create publisher node |
| 130 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 131 |
$node_publisher->init(allows_streaming => 'logical'); |
| 132 |
$node_publisher->append_conf('postgresql.conf', |
| 133 |
'debug_logical_replication_streaming = immediate'); |
| 134 |
$node_publisher->start; |
| 135 |
|
| 136 |
# Create subscriber node |
| 137 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 138 |
$node_subscriber->init; |
| 139 |
$node_subscriber->start; |
| 140 |
|
| 141 |
# Create some preexisting content on publisher |
| 142 |
$node_publisher->safe_psql('postgres', |
| 143 |
"CREATE TABLE test_tab (a int primary key, b bytea)"); |
| 144 |
$node_publisher->safe_psql('postgres', |
| 145 |
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')"); |
| 146 |
$node_publisher->safe_psql('postgres', "CREATE TABLE test_tab_2 (a int)"); |
| 147 |
|
| 148 |
# Setup structure on subscriber |
| 149 |
$node_subscriber->safe_psql('postgres', |
| 150 |
"CREATE TABLE test_tab (a int primary key, b text, c INT, d INT, e INT)"); |
| 151 |
$node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab_2 (a int)"); |
| 152 |
|
| 153 |
# Setup logical replication |
| 154 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 155 |
$node_publisher->safe_psql('postgres', |
| 156 |
"CREATE PUBLICATION tap_pub FOR TABLE test_tab, test_tab_2"); |
| 157 |
|
| 158 |
my $appname = 'tap_sub'; |
| 159 |
|
| 160 |
################################ |
| 161 |
# Test using streaming mode 'on' |
| 162 |
################################ |
| 163 |
$node_subscriber->safe_psql('postgres', |
| 164 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)" |
| 165 |
); |
| 166 |
|
| 167 |
# Wait for initial table sync to finish |
| 168 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 169 |
|
| 170 |
my $result = |
| 171 |
$node_subscriber->safe_psql('postgres', |
| 172 |
"SELECT count(*), count(c) FROM test_tab"); |
| 173 |
is($result, qq(2|0), 'check initial data was copied to subscriber'); |
| 174 |
|
| 175 |
test_streaming($node_publisher, $node_subscriber, $appname, 0); |
| 176 |
|
| 177 |
###################################### |
| 178 |
# Test using streaming mode 'parallel' |
| 179 |
###################################### |
| 180 |
my $oldpid = $node_publisher->safe_psql('postgres', |
| 181 |
"SELECT pid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';" |
| 182 |
); |
| 183 |
|
| 184 |
$node_subscriber->safe_psql('postgres', |
| 185 |
"ALTER SUBSCRIPTION tap_sub SET(streaming = parallel)"); |
| 186 |
|
| 187 |
$node_publisher->poll_query_until('postgres', |
| 188 |
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';" |
| 189 |
) |
| 190 |
or die |
| 191 |
"Timed out while waiting for apply to restart after changing SUBSCRIPTION"; |
| 192 |
|
| 193 |
# We need to check DEBUG logs to ensure that the parallel apply worker has |
| 194 |
# applied the transaction. So, bump up the log verbosity. |
| 195 |
$node_subscriber->append_conf('postgresql.conf', "log_min_messages = debug1"); |
| 196 |
$node_subscriber->reload; |
| 197 |
|
| 198 |
# Run a query to make sure that the reload has taken effect. |
| 199 |
$node_subscriber->safe_psql('postgres', q{SELECT 1}); |
| 200 |
|
| 201 |
test_streaming($node_publisher, $node_subscriber, $appname, 1); |
| 202 |
|
| 203 |
# Test serializing changes to files and notify the parallel apply worker to |
| 204 |
# apply them at the end of the transaction. |
| 205 |
$node_subscriber->append_conf('postgresql.conf', |
| 206 |
'debug_logical_replication_streaming = immediate'); |
| 207 |
# Reset the log_min_messages to default. |
| 208 |
$node_subscriber->append_conf('postgresql.conf', |
| 209 |
"log_min_messages = warning"); |
| 210 |
$node_subscriber->reload; |
| 211 |
|
| 212 |
# Run a query to make sure that the reload has taken effect. |
| 213 |
$node_subscriber->safe_psql('postgres', q{SELECT 1}); |
| 214 |
|
| 215 |
my $offset = -s $node_subscriber->logfile; |
| 216 |
|
| 217 |
$node_publisher->safe_psql( |
| 218 |
'postgres', q{ |
| 219 |
BEGIN; |
| 220 |
INSERT INTO test_tab_2 values(1); |
| 221 |
ROLLBACK; |
| 222 |
}); |
| 223 |
|
| 224 |
# Ensure that the changes are serialized. |
| 225 |
$node_subscriber->wait_for_log( |
| 226 |
qr/LOG: ( [A-Z0-9]+:)? logical replication apply worker will serialize the remaining changes of remote transaction \d+ to a file/, |
| 227 |
$offset); |
| 228 |
|
| 229 |
$node_publisher->wait_for_catchup($appname); |
| 230 |
|
| 231 |
# Check that transaction is aborted on subscriber |
| 232 |
$result = |
| 233 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2"); |
| 234 |
is($result, qq(0), 'check rollback was reflected on subscriber'); |
| 235 |
|
| 236 |
# Serialize the ABORT sub-transaction. |
| 237 |
$offset = -s $node_subscriber->logfile; |
| 238 |
|
| 239 |
$node_publisher->safe_psql( |
| 240 |
'postgres', q{ |
| 241 |
BEGIN; |
| 242 |
INSERT INTO test_tab_2 values(1); |
| 243 |
SAVEPOINT sp; |
| 244 |
INSERT INTO test_tab_2 values(1); |
| 245 |
ROLLBACK TO sp; |
| 246 |
COMMIT; |
| 247 |
}); |
| 248 |
|
| 249 |
# Ensure that the changes are serialized. |
| 250 |
$node_subscriber->wait_for_log( |
| 251 |
qr/LOG: ( [A-Z0-9]+:)? logical replication apply worker will serialize the remaining changes of remote transaction \d+ to a file/, |
| 252 |
$offset); |
| 253 |
|
| 254 |
$node_publisher->wait_for_catchup($appname); |
| 255 |
|
| 256 |
# Check that only sub-transaction is aborted on subscriber. |
| 257 |
$result = |
| 258 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2"); |
| 259 |
is($result, qq(1), 'check rollback to savepoint was reflected on subscriber'); |
| 260 |
|
| 261 |
$node_subscriber->stop; |
| 262 |
$node_publisher->stop; |
| 263 |
|
| 264 |
done_testing(); |