Debian Codegraph @

src/test/subscription/t/006_rewrite.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test logical replication behavior with heap rewrites
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
12 $node_publisher->init(allows_streaming => 'logical');
13 $node_publisher->start;
14
15 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
16 $node_subscriber->init;
17 $node_subscriber->start;
18
19 my $ddl = "CREATE TABLE test1 (a int, b text);";
20 $node_publisher->safe_psql('postgres', $ddl);
21 $node_subscriber->safe_psql('postgres', $ddl);
22
23 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
24
25 $node_publisher->safe_psql('postgres',
26 "CREATE PUBLICATION mypub FOR ALL TABLES;");
27 $node_subscriber->safe_psql('postgres',
28 "CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;"
29 );
30
31 # Wait for initial sync to finish
32 $node_subscriber->wait_for_subscription_sync($node_publisher, 'mysub');
33
34 $node_publisher->safe_psql('postgres',
35 q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');});
36
37 $node_publisher->wait_for_catchup('mysub');
38
39 is( $node_subscriber->safe_psql('postgres', q{SELECT a, b FROM test1}),
40 qq(1|one
41 2|two),
42 'initial data replicated to subscriber');
43
44 # DDL that causes a heap rewrite
45 my $ddl2 = "ALTER TABLE test1 ADD c int NOT NULL DEFAULT 0;";
46 $node_subscriber->safe_psql('postgres', $ddl2);
47 $node_publisher->safe_psql('postgres', $ddl2);
48
49 $node_publisher->wait_for_catchup('mysub');
50
51 $node_publisher->safe_psql('postgres',
52 q{INSERT INTO test1 (a, b, c) VALUES (3, 'three', 33);});
53
54 $node_publisher->wait_for_catchup('mysub');
55
56 is( $node_subscriber->safe_psql('postgres', q{SELECT a, b, c FROM test1}),
57 qq(1|one|0
58 2|two|0
59 3|three|33),
60 'data replicated to subscriber');
61
62 $node_subscriber->stop;
63 $node_publisher->stop;
64
65 done_testing();