| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test streaming of transaction containing subtransactions |
| 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 |
# Insert, update and delete some rows. |
| 36 |
$node_publisher->safe_psql( |
| 37 |
'postgres', q{ |
| 38 |
BEGIN; |
| 39 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(3, 5) s(i); |
| 40 |
UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0; |
| 41 |
DELETE FROM test_tab WHERE mod(a,3) = 0; |
| 42 |
SAVEPOINT s1; |
| 43 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(6, 8) s(i); |
| 44 |
UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0; |
| 45 |
DELETE FROM test_tab WHERE mod(a,3) = 0; |
| 46 |
SAVEPOINT s2; |
| 47 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(9, 11) s(i); |
| 48 |
UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0; |
| 49 |
DELETE FROM test_tab WHERE mod(a,3) = 0; |
| 50 |
SAVEPOINT s3; |
| 51 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(12, 14) s(i); |
| 52 |
UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0; |
| 53 |
DELETE FROM test_tab WHERE mod(a,3) = 0; |
| 54 |
SAVEPOINT s4; |
| 55 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(15, 17) s(i); |
| 56 |
UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0; |
| 57 |
DELETE FROM test_tab WHERE mod(a,3) = 0; |
| 58 |
COMMIT; |
| 59 |
}); |
| 60 |
|
| 61 |
$node_publisher->wait_for_catchup($appname); |
| 62 |
|
| 63 |
check_parallel_log($node_subscriber, $offset, $is_parallel, 'COMMIT'); |
| 64 |
|
| 65 |
my $result = |
| 66 |
$node_subscriber->safe_psql('postgres', |
| 67 |
"SELECT count(*), count(c), count(d = 999) FROM test_tab"); |
| 68 |
is($result, qq(12|12|12), |
| 69 |
'check data was copied to subscriber in streaming mode and extra columns contain local defaults' |
| 70 |
); |
| 71 |
|
| 72 |
# Cleanup the test data |
| 73 |
$node_publisher->safe_psql('postgres', |
| 74 |
"DELETE FROM test_tab WHERE (a > 2)"); |
| 75 |
$node_publisher->wait_for_catchup($appname); |
| 76 |
} |
| 77 |
|
| 78 |
# Create publisher node |
| 79 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 80 |
$node_publisher->init(allows_streaming => 'logical'); |
| 81 |
$node_publisher->append_conf('postgresql.conf', |
| 82 |
'debug_logical_replication_streaming = immediate'); |
| 83 |
$node_publisher->start; |
| 84 |
|
| 85 |
# Create subscriber node |
| 86 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 87 |
$node_subscriber->init; |
| 88 |
$node_subscriber->start; |
| 89 |
|
| 90 |
# Create some preexisting content on publisher |
| 91 |
$node_publisher->safe_psql('postgres', |
| 92 |
"CREATE TABLE test_tab (a int primary key, b bytea)"); |
| 93 |
$node_publisher->safe_psql('postgres', |
| 94 |
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')"); |
| 95 |
|
| 96 |
# Setup structure on subscriber |
| 97 |
$node_subscriber->safe_psql('postgres', |
| 98 |
"CREATE TABLE test_tab (a int primary key, b bytea, c timestamptz DEFAULT now(), d bigint DEFAULT 999)" |
| 99 |
); |
| 100 |
|
| 101 |
# Setup logical replication |
| 102 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 103 |
$node_publisher->safe_psql('postgres', |
| 104 |
"CREATE PUBLICATION tap_pub FOR TABLE test_tab"); |
| 105 |
|
| 106 |
my $appname = 'tap_sub'; |
| 107 |
|
| 108 |
################################ |
| 109 |
# Test using streaming mode 'on' |
| 110 |
################################ |
| 111 |
$node_subscriber->safe_psql('postgres', |
| 112 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)" |
| 113 |
); |
| 114 |
|
| 115 |
# Wait for initial table sync to finish |
| 116 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 117 |
|
| 118 |
my $result = |
| 119 |
$node_subscriber->safe_psql('postgres', |
| 120 |
"SELECT count(*), count(c), count(d = 999) FROM test_tab"); |
| 121 |
is($result, qq(2|2|2), 'check initial data was copied to subscriber'); |
| 122 |
|
| 123 |
test_streaming($node_publisher, $node_subscriber, $appname, 0); |
| 124 |
|
| 125 |
###################################### |
| 126 |
# Test using streaming mode 'parallel' |
| 127 |
###################################### |
| 128 |
my $oldpid = $node_publisher->safe_psql('postgres', |
| 129 |
"SELECT pid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';" |
| 130 |
); |
| 131 |
|
| 132 |
$node_subscriber->safe_psql('postgres', |
| 133 |
"ALTER SUBSCRIPTION tap_sub SET(streaming = parallel)"); |
| 134 |
|
| 135 |
$node_publisher->poll_query_until('postgres', |
| 136 |
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';" |
| 137 |
) |
| 138 |
or die |
| 139 |
"Timed out while waiting for apply to restart after changing SUBSCRIPTION"; |
| 140 |
|
| 141 |
# We need to check DEBUG logs to ensure that the parallel apply worker has |
| 142 |
# applied the transaction. So, bump up the log verbosity. |
| 143 |
$node_subscriber->append_conf('postgresql.conf', "log_min_messages = debug1"); |
| 144 |
$node_subscriber->reload; |
| 145 |
|
| 146 |
# Run a query to make sure that the reload has taken effect. |
| 147 |
$node_subscriber->safe_psql('postgres', q{SELECT 1}); |
| 148 |
|
| 149 |
test_streaming($node_publisher, $node_subscriber, $appname, 1); |
| 150 |
|
| 151 |
$node_subscriber->stop; |
| 152 |
$node_publisher->stop; |
| 153 |
|
| 154 |
done_testing(); |