| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test streaming of transaction with subtransactions, DDLs, DMLs, and |
| 5 |
# rollbacks |
| 6 |
# |
| 7 |
# This file is mainly to test the DDL/DML interaction of the publisher side, |
| 8 |
# so we didn't add a parallel apply version for the tests in this file. |
| 9 |
use strict; |
| 10 |
use warnings FATAL => 'all'; |
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
use PostgreSQL::Test::Utils; |
| 13 |
use Test::More; |
| 14 |
|
| 15 |
# Create publisher node |
| 16 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 17 |
$node_publisher->init(allows_streaming => 'logical'); |
| 18 |
$node_publisher->append_conf('postgresql.conf', |
| 19 |
'debug_logical_replication_streaming = immediate'); |
| 20 |
$node_publisher->start; |
| 21 |
|
| 22 |
# Create subscriber node |
| 23 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 24 |
$node_subscriber->init; |
| 25 |
$node_subscriber->start; |
| 26 |
|
| 27 |
# Create some preexisting content on publisher |
| 28 |
$node_publisher->safe_psql('postgres', |
| 29 |
"CREATE TABLE test_tab (a int primary key, b bytea)"); |
| 30 |
$node_publisher->safe_psql('postgres', |
| 31 |
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')"); |
| 32 |
|
| 33 |
# Setup structure on subscriber |
| 34 |
$node_subscriber->safe_psql('postgres', |
| 35 |
"CREATE TABLE test_tab (a int primary key, b bytea, c INT, d INT, e INT)" |
| 36 |
); |
| 37 |
|
| 38 |
# Setup logical replication |
| 39 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 40 |
$node_publisher->safe_psql('postgres', |
| 41 |
"CREATE PUBLICATION tap_pub FOR TABLE test_tab"); |
| 42 |
|
| 43 |
my $appname = 'tap_sub'; |
| 44 |
$node_subscriber->safe_psql('postgres', |
| 45 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)" |
| 46 |
); |
| 47 |
|
| 48 |
# Wait for initial table sync to finish |
| 49 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 50 |
|
| 51 |
my $result = |
| 52 |
$node_subscriber->safe_psql('postgres', |
| 53 |
"SELECT count(*), count(c) FROM test_tab"); |
| 54 |
is($result, qq(2|0), 'check initial data was copied to subscriber'); |
| 55 |
|
| 56 |
# streamed transaction with DDL, DML and ROLLBACKs |
| 57 |
$node_publisher->safe_psql( |
| 58 |
'postgres', q{ |
| 59 |
BEGIN; |
| 60 |
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea)); |
| 61 |
ALTER TABLE test_tab ADD COLUMN c INT; |
| 62 |
SAVEPOINT s1; |
| 63 |
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), -4); |
| 64 |
ALTER TABLE test_tab ADD COLUMN d INT; |
| 65 |
SAVEPOINT s2; |
| 66 |
INSERT INTO test_tab VALUES (5, sha256(5::text::bytea), -5, 5*2); |
| 67 |
ALTER TABLE test_tab ADD COLUMN e INT; |
| 68 |
SAVEPOINT s3; |
| 69 |
INSERT INTO test_tab VALUES (6, sha256(6::text::bytea), -6, 6*2, -6*3); |
| 70 |
ALTER TABLE test_tab DROP COLUMN c; |
| 71 |
ROLLBACK TO s1; |
| 72 |
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), 4); |
| 73 |
COMMIT; |
| 74 |
}); |
| 75 |
|
| 76 |
$node_publisher->wait_for_catchup($appname); |
| 77 |
|
| 78 |
$result = |
| 79 |
$node_subscriber->safe_psql('postgres', |
| 80 |
"SELECT count(*), count(c) FROM test_tab"); |
| 81 |
is($result, qq(4|1), |
| 82 |
'check rollback to savepoint was reflected on subscriber and extra columns contain local defaults' |
| 83 |
); |
| 84 |
|
| 85 |
$node_subscriber->stop; |
| 86 |
$node_publisher->stop; |
| 87 |
|
| 88 |
done_testing(); |