Debian Codegraph @

src/test/subscription/t/029_on_error.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Tests for disable_on_error and SKIP transaction features.
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 $offset = 0;
12
13 # Test skipping the transaction. This function must be called after the caller
14 # has inserted data that conflicts with the subscriber. The finish LSN of the
15 # error transaction that is used to specify to ALTER SUBSCRIPTION ... SKIP is
16 # fetched from the server logs. After executing ALTER SUBSCRIPTION ... SKIP, we
17 # check if logical replication can continue working by inserting $nonconflict_data
18 # on the publisher.
19 sub test_skip_lsn
20 {
21 my ($node_publisher, $node_subscriber, $nonconflict_data, $expected, $msg)
22 = @_;
23
24 # Wait until a conflict occurs on the subscriber.
25 $node_subscriber->poll_query_until('postgres',
26 "SELECT subenabled = FALSE FROM pg_subscription WHERE subname = 'sub'"
27 );
28
29 # Get the finish LSN of the error transaction, mapping the expected
30 # ERROR with its CONTEXT when retrieving this information.
31 my $contents = slurp_file($node_subscriber->logfile, $offset);
32 $contents =~
33 qr/conflict detected on relation "public.tbl".*\n.*DETAIL:.* Key already exists in unique index "tbl_pkey", modified by .*origin.* transaction \d+ at .*\n.*Key \(i\)=\(\d+\); existing local row .*; remote row .*\n.*CONTEXT:.* for replication target relation "public.tbl" in transaction \d+, finished at ([[:xdigit:]]+\/[[:xdigit:]]+)/m
34 or die "could not get error-LSN";
35 my $lsn = $1;
36
37 # Set skip lsn.
38 $node_subscriber->safe_psql('postgres',
39 "ALTER SUBSCRIPTION sub SKIP (lsn = '$lsn')");
40
41 # Re-enable the subscription.
42 $node_subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION sub ENABLE");
43
44 # Wait for the failed transaction to be skipped
45 $node_subscriber->poll_query_until('postgres',
46 "SELECT subskiplsn = '0/0' FROM pg_subscription WHERE subname = 'sub'"
47 );
48
49 # Check the log to ensure that the transaction is skipped, and advance the
50 # offset of the log file for the next test.
51 $offset = $node_subscriber->wait_for_log(
52 qr/LOG: ( [A-Z0-9]+:)? logical replication completed skipping transaction at LSN $lsn/,
53 $offset);
54
55 # Insert non-conflict data
56 $node_publisher->safe_psql('postgres',
57 "INSERT INTO tbl VALUES $nonconflict_data");
58
59 $node_publisher->wait_for_catchup('sub');
60
61 # Check replicated data
62 my $res =
63 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tbl");
64 is($res, $expected, $msg);
65 }
66
67 # Create publisher node. Set a low value of logical_decoding_work_mem to test
68 # streaming cases.
69 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
70 $node_publisher->init(allows_streaming => 'logical');
71 $node_publisher->append_conf(
72 'postgresql.conf',
73 qq[
74 logical_decoding_work_mem = 64kB
75 max_prepared_transactions = 10
76 ]);
77 $node_publisher->start;
78
79 # Create subscriber node
80 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
81 $node_subscriber->init;
82 $node_subscriber->append_conf(
83 'postgresql.conf',
84 qq[
85 max_prepared_transactions = 10
86 track_commit_timestamp = on
87 ]);
88 $node_subscriber->start;
89
90 # Initial table setup on both publisher and subscriber. On the subscriber, we
91 # create the same tables but with a primary key. Also, insert some data that
92 # will conflict with the data replicated from publisher later.
93 $node_publisher->safe_psql(
94 'postgres',
95 qq[
96 CREATE TABLE tbl (i INT, t BYTEA);
97 ALTER TABLE tbl REPLICA IDENTITY FULL;
98 INSERT INTO tbl VALUES (1, NULL);
99 ]);
100 $node_subscriber->safe_psql(
101 'postgres',
102 qq[
103 CREATE TABLE tbl (i INT PRIMARY KEY, t BYTEA);
104 INSERT INTO tbl VALUES (1, NULL);
105 ]);
106
107 # Create a pub/sub to set up logical replication. This tests that the
108 # uniqueness violation will cause the subscription to fail during initial
109 # synchronization and make it disabled.
110 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
111 $node_publisher->safe_psql('postgres',
112 "CREATE PUBLICATION pub FOR TABLE tbl");
113 $node_subscriber->safe_psql('postgres',
114 "CREATE SUBSCRIPTION sub CONNECTION '$publisher_connstr' PUBLICATION pub WITH (disable_on_error = true, streaming = on, two_phase = on)"
115 );
116
117 # Initial synchronization failure causes the subscription to be disabled.
118 $node_subscriber->poll_query_until('postgres',
119 "SELECT subenabled = false FROM pg_catalog.pg_subscription WHERE subname = 'sub'"
120 ) or die "Timed out while waiting for subscriber to be disabled";
121
122 # Truncate the table on the subscriber which caused the subscription to be
123 # disabled.
124 $node_subscriber->safe_psql('postgres', "TRUNCATE tbl");
125
126 # Re-enable the subscription "sub".
127 $node_subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION sub ENABLE");
128
129 # Wait for the data to replicate.
130 $node_subscriber->wait_for_subscription_sync($node_publisher, 'sub');
131
132 # Confirm that we have finished the table sync.
133 my $result =
134 $node_subscriber->safe_psql('postgres', "SELECT COUNT(*) FROM tbl");
135 is($result, qq(1), "subscription sub replicated data");
136
137 # Insert data to tbl, raising an error on the subscriber due to violation
138 # of the unique constraint on tbl. Then skip the transaction.
139 $node_publisher->safe_psql(
140 'postgres',
141 qq[
142 BEGIN;
143 INSERT INTO tbl VALUES (1, NULL);
144 COMMIT;
145 ]);
146 test_skip_lsn($node_publisher, $node_subscriber,
147 "(2, NULL)", "2", "test skipping transaction");
148
149 # Test for PREPARE and COMMIT PREPARED. Update the data and PREPARE the
150 # transaction, raising an error on the subscriber due to violation of the
151 # unique constraint on tbl. Then skip the transaction.
152 $node_publisher->safe_psql(
153 'postgres',
154 qq[
155 BEGIN;
156 UPDATE tbl SET i = 2;
157 PREPARE TRANSACTION 'gtx';
158 COMMIT PREPARED 'gtx';
159 ]);
160 test_skip_lsn($node_publisher, $node_subscriber,
161 "(3, NULL)", "3", "test skipping prepare and commit prepared ");
162
163 # Test for STREAM COMMIT. Insert enough rows to tbl to exceed the 64kB
164 # limit, also raising an error on the subscriber during applying spooled
165 # changes for the same reason. Then skip the transaction.
166 $node_publisher->safe_psql(
167 'postgres',
168 qq[
169 BEGIN;
170 INSERT INTO tbl SELECT i, sha256(i::text::bytea) FROM generate_series(1, 10000) s(i);
171 COMMIT;
172 ]);
173 test_skip_lsn($node_publisher, $node_subscriber,
174 "(4, sha256(4::text::bytea))",
175 "4", "test skipping stream-commit");
176
177 $result = $node_subscriber->safe_psql('postgres',
178 "SELECT COUNT(*) FROM pg_prepared_xacts");
179 is($result, "0",
180 "check all prepared transactions are resolved on the subscriber");
181
182 $node_subscriber->stop;
183 $node_publisher->stop;
184
185 done_testing();