| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test streaming of large transaction with DDL and subtransactions |
| 5 |
# |
| 6 |
# This file is mainly to test the DDL/DML interaction of the publisher side, |
| 7 |
# so we didn't add a parallel apply version for the tests in this file. |
| 8 |
use strict; |
| 9 |
use warnings FATAL => 'all'; |
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
# Create publisher node |
| 15 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 16 |
$node_publisher->init(allows_streaming => 'logical'); |
| 17 |
$node_publisher->append_conf('postgresql.conf', |
| 18 |
'logical_decoding_work_mem = 64kB'); |
| 19 |
$node_publisher->start; |
| 20 |
|
| 21 |
# Create subscriber node |
| 22 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 23 |
$node_subscriber->init; |
| 24 |
$node_subscriber->start; |
| 25 |
|
| 26 |
# Create some preexisting content on publisher |
| 27 |
$node_publisher->safe_psql('postgres', |
| 28 |
"CREATE TABLE test_tab (a int primary key, b varchar)"); |
| 29 |
$node_publisher->safe_psql('postgres', |
| 30 |
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')"); |
| 31 |
|
| 32 |
# Setup structure on subscriber |
| 33 |
$node_subscriber->safe_psql('postgres', |
| 34 |
"CREATE TABLE test_tab (a int primary key, b bytea, c INT, d INT, e INT, f INT)" |
| 35 |
); |
| 36 |
|
| 37 |
# Setup logical replication |
| 38 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 39 |
$node_publisher->safe_psql('postgres', |
| 40 |
"CREATE PUBLICATION tap_pub FOR TABLE test_tab"); |
| 41 |
|
| 42 |
my $appname = 'tap_sub'; |
| 43 |
$node_subscriber->safe_psql('postgres', |
| 44 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)" |
| 45 |
); |
| 46 |
|
| 47 |
# Wait for initial table sync to finish |
| 48 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 49 |
|
| 50 |
my $result = |
| 51 |
$node_subscriber->safe_psql('postgres', |
| 52 |
"SELECT count(*), count(c), count(d = 999) FROM test_tab"); |
| 53 |
is($result, qq(2|0|0), 'check initial data was copied to subscriber'); |
| 54 |
|
| 55 |
# a small (non-streamed) transaction with DDL and DML |
| 56 |
$node_publisher->safe_psql( |
| 57 |
'postgres', q{ |
| 58 |
BEGIN; |
| 59 |
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea)); |
| 60 |
ALTER TABLE test_tab ADD COLUMN c INT; |
| 61 |
SAVEPOINT s1; |
| 62 |
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), -4); |
| 63 |
COMMIT; |
| 64 |
}); |
| 65 |
|
| 66 |
# large (streamed) transaction with DDL and DML |
| 67 |
$node_publisher->safe_psql( |
| 68 |
'postgres', q{ |
| 69 |
BEGIN; |
| 70 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i FROM generate_series(5, 1000) s(i); |
| 71 |
ALTER TABLE test_tab ADD COLUMN d INT; |
| 72 |
SAVEPOINT s1; |
| 73 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i FROM generate_series(1001, 2000) s(i); |
| 74 |
COMMIT; |
| 75 |
}); |
| 76 |
|
| 77 |
# a small (non-streamed) transaction with DDL and DML |
| 78 |
$node_publisher->safe_psql( |
| 79 |
'postgres', q{ |
| 80 |
BEGIN; |
| 81 |
INSERT INTO test_tab VALUES (2001, sha256(2001::text::bytea), -2001, 2*2001); |
| 82 |
ALTER TABLE test_tab ADD COLUMN e INT; |
| 83 |
SAVEPOINT s1; |
| 84 |
INSERT INTO test_tab VALUES (2002, sha256(2002::text::bytea), -2002, 2*2002, -3*2002); |
| 85 |
COMMIT; |
| 86 |
}); |
| 87 |
|
| 88 |
$node_publisher->wait_for_catchup($appname); |
| 89 |
|
| 90 |
$result = |
| 91 |
$node_subscriber->safe_psql('postgres', |
| 92 |
"SELECT count(*), count(c), count(d), count(e) FROM test_tab"); |
| 93 |
is($result, qq(2002|1999|1002|1), |
| 94 |
'check data was copied to subscriber in streaming mode and extra columns contain local defaults' |
| 95 |
); |
| 96 |
|
| 97 |
# A large (streamed) transaction with DDL and DML. One of the DDL is performed |
| 98 |
# after DML to ensure that we invalidate the schema sent for test_tab so that |
| 99 |
# the next transaction has to send the schema again. |
| 100 |
$node_publisher->safe_psql( |
| 101 |
'postgres', q{ |
| 102 |
BEGIN; |
| 103 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i, -3*i FROM generate_series(2003,5000) s(i); |
| 104 |
ALTER TABLE test_tab ADD COLUMN f INT; |
| 105 |
COMMIT; |
| 106 |
}); |
| 107 |
|
| 108 |
# A small transaction that won't get streamed. This is just to ensure that we |
| 109 |
# send the schema again to reflect the last column added in the previous test. |
| 110 |
$node_publisher->safe_psql( |
| 111 |
'postgres', q{ |
| 112 |
BEGIN; |
| 113 |
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i, -3*i, 4*i FROM generate_series(5001,5005) s(i); |
| 114 |
COMMIT; |
| 115 |
}); |
| 116 |
|
| 117 |
$node_publisher->wait_for_catchup($appname); |
| 118 |
|
| 119 |
$result = |
| 120 |
$node_subscriber->safe_psql('postgres', |
| 121 |
"SELECT count(*), count(c), count(d), count(e), count(f) FROM test_tab"); |
| 122 |
is($result, qq(5005|5002|4005|3004|5), |
| 123 |
'check data was copied to subscriber for both streaming and non-streaming transactions' |
| 124 |
); |
| 125 |
|
| 126 |
$node_subscriber->stop; |
| 127 |
$node_publisher->stop; |
| 128 |
|
| 129 |
done_testing(); |