Debian Codegraph @

src/test/subscription/t/001_rep_changes.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Basic logical replication test
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 # Initialize publisher node
12 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
13 $node_publisher->init(allows_streaming => 'logical');
14 $node_publisher->start;
15
16 # Create subscriber node
17 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
18 $node_subscriber->init;
19 $node_subscriber->start;
20
21 # Create some preexisting content on publisher
22 $node_publisher->safe_psql(
23 'postgres',
24 "CREATE FUNCTION public.pg_get_replica_identity_index(int)
25 RETURNS regclass LANGUAGE sql AS 'SELECT 1/0'"); # shall not call
26 $node_publisher->safe_psql('postgres',
27 "CREATE TABLE tab_notrep AS SELECT generate_series(1,10) AS a");
28 $node_publisher->safe_psql('postgres',
29 "CREATE TABLE tab_ins AS SELECT generate_series(1,1002) AS a");
30 $node_publisher->safe_psql('postgres',
31 "CREATE TABLE tab_full AS SELECT generate_series(1,10) AS a");
32 $node_publisher->safe_psql('postgres', "CREATE TABLE tab_full2 (x text)");
33 $node_publisher->safe_psql('postgres',
34 "INSERT INTO tab_full2 VALUES ('a'), ('b'), ('b')");
35 $node_publisher->safe_psql('postgres',
36 "CREATE TABLE tab_rep (a int primary key)");
37 $node_publisher->safe_psql('postgres',
38 "CREATE TABLE tab_mixed (a int primary key, b text, c numeric)");
39 $node_publisher->safe_psql('postgres',
40 "INSERT INTO tab_mixed (a, b, c) VALUES (1, 'foo', 1.1)");
41 $node_publisher->safe_psql('postgres',
42 "CREATE TABLE tab_include (a int, b text, CONSTRAINT covering PRIMARY KEY(a) INCLUDE(b))"
43 );
44 $node_publisher->safe_psql('postgres',
45 "CREATE TABLE tab_full_pk (a int primary key, b text)");
46 $node_publisher->safe_psql('postgres',
47 "ALTER TABLE tab_full_pk REPLICA IDENTITY FULL");
48 # Let this table with REPLICA IDENTITY NOTHING, allowing only INSERT changes.
49 $node_publisher->safe_psql('postgres', "CREATE TABLE tab_nothing (a int)");
50 $node_publisher->safe_psql('postgres',
51 "ALTER TABLE tab_nothing REPLICA IDENTITY NOTHING");
52
53 # Replicate the changes without replica identity index
54 $node_publisher->safe_psql('postgres',
55 "CREATE TABLE tab_no_replidentity_index(c1 int)");
56 $node_publisher->safe_psql('postgres',
57 "CREATE INDEX idx_no_replidentity_index ON tab_no_replidentity_index(c1)"
58 );
59
60 # Replicate the changes without columns
61 $node_publisher->safe_psql('postgres', "CREATE TABLE tab_no_col()");
62 $node_publisher->safe_psql('postgres',
63 "INSERT INTO tab_no_col default VALUES");
64
65 # Setup structure on subscriber
66 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_notrep (a int)");
67 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_ins (a int)");
68 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_full (a int)");
69 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_full2 (x text)");
70 $node_subscriber->safe_psql('postgres',
71 "CREATE TABLE tab_rep (a int primary key)");
72 $node_subscriber->safe_psql('postgres',
73 "CREATE TABLE tab_full_pk (a int primary key, b text)");
74 $node_subscriber->safe_psql('postgres',
75 "ALTER TABLE tab_full_pk REPLICA IDENTITY FULL");
76 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_nothing (a int)");
77
78 # different column count and order than on publisher
79 $node_subscriber->safe_psql('postgres',
80 "CREATE TABLE tab_mixed (d text default 'local', c numeric, b text, a int primary key)"
81 );
82
83 # replication of the table with included index
84 $node_subscriber->safe_psql('postgres',
85 "CREATE TABLE tab_include (a int, b text, CONSTRAINT covering PRIMARY KEY(a) INCLUDE(b))"
86 );
87
88 # replication of the table without replica identity index
89 $node_subscriber->safe_psql('postgres',
90 "CREATE TABLE tab_no_replidentity_index(c1 int)");
91 $node_subscriber->safe_psql('postgres',
92 "CREATE INDEX idx_no_replidentity_index ON tab_no_replidentity_index(c1)"
93 );
94
95 # replication of the table without columns
96 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_no_col()");
97
98 # Setup logical replication
99 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
100 $node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub");
101 $node_publisher->safe_psql('postgres',
102 "CREATE PUBLICATION tap_pub_ins_only WITH (publish = insert)");
103 $node_publisher->safe_psql('postgres',
104 "ALTER PUBLICATION tap_pub ADD TABLE tab_rep, tab_full, tab_full2, tab_mixed, tab_include, tab_nothing, tab_full_pk, tab_no_replidentity_index, tab_no_col"
105 );
106 $node_publisher->safe_psql('postgres',
107 "ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_ins");
108
109 $node_subscriber->safe_psql('postgres',
110 "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub, tap_pub_ins_only"
111 );
112
113 # Wait for initial table sync to finish
114 $node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
115
116 # Reset IO statistics, for the WAL sender check with pg_stat_io.
117 $node_publisher->safe_psql('postgres', "SELECT pg_stat_reset_shared('io')");
118
119 my $result =
120 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_notrep");
121 is($result, qq(0), 'check non-replicated table is empty on subscriber');
122
123 $result =
124 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_ins");
125 is($result, qq(1002), 'check initial data was copied to subscriber');
126
127 $node_publisher->safe_psql('postgres',
128 "INSERT INTO tab_ins SELECT generate_series(1,50)");
129 $node_publisher->safe_psql('postgres', "DELETE FROM tab_ins WHERE a > 20");
130 $node_publisher->safe_psql('postgres', "UPDATE tab_ins SET a = -a");
131
132 $node_publisher->safe_psql('postgres',
133 "INSERT INTO tab_rep SELECT generate_series(1,50)");
134 $node_publisher->safe_psql('postgres', "DELETE FROM tab_rep WHERE a > 20");
135 $node_publisher->safe_psql('postgres', "UPDATE tab_rep SET a = -a");
136
137 $node_publisher->safe_psql('postgres',
138 "INSERT INTO tab_mixed VALUES (2, 'bar', 2.2)");
139
140 $node_publisher->safe_psql('postgres',
141 "INSERT INTO tab_full_pk VALUES (1, 'foo'), (2, 'baz')");
142
143 $node_publisher->safe_psql('postgres',
144 "INSERT INTO tab_nothing VALUES (generate_series(1,20))");
145
146 $node_publisher->safe_psql('postgres',
147 "INSERT INTO tab_include SELECT generate_series(1,50)");
148 $node_publisher->safe_psql('postgres',
149 "DELETE FROM tab_include WHERE a > 20");
150 $node_publisher->safe_psql('postgres', "UPDATE tab_include SET a = -a");
151
152 $node_publisher->safe_psql('postgres',
153 "INSERT INTO tab_no_replidentity_index VALUES(1)");
154
155 $node_publisher->safe_psql('postgres',
156 "INSERT INTO tab_no_col default VALUES");
157
158 $node_publisher->wait_for_catchup('tap_sub');
159
160 $result = $node_subscriber->safe_psql('postgres',
161 "SELECT count(*), min(a), max(a) FROM tab_ins");
162 is($result, qq(1052|1|1002), 'check replicated inserts on subscriber');
163
164 $result = $node_subscriber->safe_psql('postgres',
165 "SELECT count(*), min(a), max(a) FROM tab_rep");
166 is($result, qq(20|-20|-1), 'check replicated changes on subscriber');
167
168 $result = $node_subscriber->safe_psql('postgres', "SELECT * FROM tab_mixed");
169 is( $result, qq(local|1.1|foo|1
170 local|2.2|bar|2), 'check replicated changes with different column order');
171
172 $result =
173 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_nothing");
174 is($result, qq(20), 'check replicated changes with REPLICA IDENTITY NOTHING');
175
176 $result = $node_subscriber->safe_psql('postgres',
177 "SELECT count(*), min(a), max(a) FROM tab_include");
178 is($result, qq(20|-20|-1),
179 'check replicated changes with primary key index with included columns');
180
181 is( $node_subscriber->safe_psql(
182 'postgres', q(SELECT c1 FROM tab_no_replidentity_index)),
183 1,
184 "value replicated to subscriber without replica identity index");
185
186 $result =
187 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_no_col");
188 is($result, qq(2), 'check replicated changes for table having no columns');
189
190 # Wait for the logical WAL sender to update its IO statistics. This is
191 # done before the next restart, which would force a flush of its stats, and
192 # far enough from the reset done above to not impact the run time.
193 $node_publisher->poll_query_until(
194 'postgres',
195 qq[SELECT sum(reads) > 0
196 FROM pg_catalog.pg_stat_io
197 WHERE backend_type = 'walsender'
198 AND object = 'wal']
199 )
200 or die
201 "Timed out while waiting for the walsender to update its IO statistics";
202
203 # insert some duplicate rows
204 $node_publisher->safe_psql('postgres',
205 "INSERT INTO tab_full SELECT generate_series(1,10)");
206
207 # Test behaviour of ALTER PUBLICATION ... DROP TABLE
208 #
209 # When a publisher drops a table from publication, it should also stop sending
210 # its changes to subscribers. We look at the subscriber whether it receives
211 # the row that is inserted to the table on the publisher after it is dropped
212 # from the publication.
213 $result = $node_subscriber->safe_psql('postgres',
214 "SELECT count(*), min(a), max(a) FROM tab_ins");
215 is($result, qq(1052|1|1002),
216 'check rows on subscriber before table drop from publication');
217
218 # Drop the table from publication
219 $node_publisher->safe_psql('postgres',
220 "ALTER PUBLICATION tap_pub_ins_only DROP TABLE tab_ins");
221
222 # Insert a row in publisher, but publisher will not send this row to subscriber
223 $node_publisher->safe_psql('postgres', "INSERT INTO tab_ins VALUES(8888)");
224
225 $node_publisher->wait_for_catchup('tap_sub');
226
227 # Subscriber will not receive the inserted row, after table is dropped from
228 # publication, so row count should remain the same.
229 $result = $node_subscriber->safe_psql('postgres',
230 "SELECT count(*), min(a), max(a) FROM tab_ins");
231 is($result, qq(1052|1|1002),
232 'check rows on subscriber after table drop from publication');
233
234 # Delete the inserted row in publisher
235 $node_publisher->safe_psql('postgres', "DELETE FROM tab_ins WHERE a = 8888");
236
237 # Add the table to publication again
238 $node_publisher->safe_psql('postgres',
239 "ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_ins");
240
241 # Refresh publication after table is added to publication
242 $node_subscriber->safe_psql('postgres',
243 "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
244
245 # Test replication with multiple publications for a subscription such that the
246 # operations are performed on the table from the first publication in the list.
247
248 # Create tables on publisher
249 $node_publisher->safe_psql('postgres', "CREATE TABLE temp1 (a int)");
250 $node_publisher->safe_psql('postgres', "CREATE TABLE temp2 (a int)");
251
252 # Create tables on subscriber
253 $node_subscriber->safe_psql('postgres', "CREATE TABLE temp1 (a int)");
254 $node_subscriber->safe_psql('postgres', "CREATE TABLE temp2 (a int)");
255
256 # Setup logical replication that will only be used for this test
257 $node_publisher->safe_psql('postgres',
258 "CREATE PUBLICATION tap_pub_temp1 FOR TABLE temp1 WITH (publish = insert)"
259 );
260 $node_publisher->safe_psql('postgres',
261 "CREATE PUBLICATION tap_pub_temp2 FOR TABLE temp2");
262 $node_subscriber->safe_psql('postgres',
263 "CREATE SUBSCRIPTION tap_sub_temp1 CONNECTION '$publisher_connstr' PUBLICATION tap_pub_temp1, tap_pub_temp2"
264 );
265
266 # Wait for initial table sync to finish
267 $node_subscriber->wait_for_subscription_sync($node_publisher,
268 'tap_sub_temp1');
269
270 # Subscriber table will have no rows initially
271 $result =
272 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM temp1");
273 is($result, qq(0),
274 'check initial rows on subscriber with multiple publications');
275
276 # Insert a row into the table that's part of first publication in subscriber
277 # list of publications.
278 $node_publisher->safe_psql('postgres', "INSERT INTO temp1 VALUES (1)");
279
280 $node_publisher->wait_for_catchup('tap_sub_temp1');
281
282 # Subscriber should receive the inserted row
283 $result =
284 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM temp1");
285 is($result, qq(1), 'check rows on subscriber with multiple publications');
286
287 # Drop subscription as we don't need it anymore
288 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_temp1");
289
290 # Drop publications as we don't need them anymore
291 $node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_temp1");
292 $node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_temp2");
293
294 # Clean up the tables on both publisher and subscriber as we don't need them
295 $node_publisher->safe_psql('postgres', "DROP TABLE temp1");
296 $node_publisher->safe_psql('postgres', "DROP TABLE temp2");
297 $node_subscriber->safe_psql('postgres', "DROP TABLE temp1");
298 $node_subscriber->safe_psql('postgres', "DROP TABLE temp2");
299
300 # add REPLICA IDENTITY FULL so we can update
301 $node_publisher->safe_psql('postgres',
302 "ALTER TABLE tab_full REPLICA IDENTITY FULL");
303 $node_subscriber->safe_psql('postgres',
304 "ALTER TABLE tab_full REPLICA IDENTITY FULL");
305 $node_publisher->safe_psql('postgres',
306 "ALTER TABLE tab_full2 REPLICA IDENTITY FULL");
307 $node_subscriber->safe_psql('postgres',
308 "ALTER TABLE tab_full2 REPLICA IDENTITY FULL");
309 $node_publisher->safe_psql('postgres',
310 "ALTER TABLE tab_ins REPLICA IDENTITY FULL");
311 $node_subscriber->safe_psql('postgres',
312 "ALTER TABLE tab_ins REPLICA IDENTITY FULL");
313 # tab_mixed can use DEFAULT, since it has a primary key
314
315 # and do the updates
316 $node_publisher->safe_psql('postgres', "UPDATE tab_full SET a = a * a");
317 $node_publisher->safe_psql('postgres',
318 "UPDATE tab_full2 SET x = 'bb' WHERE x = 'b'");
319 $node_publisher->safe_psql('postgres',
320 "UPDATE tab_mixed SET b = 'baz' WHERE a = 1");
321 $node_publisher->safe_psql('postgres',
322 "UPDATE tab_full_pk SET b = 'bar' WHERE a = 1");
323
324 $node_publisher->wait_for_catchup('tap_sub');
325
326 $result = $node_subscriber->safe_psql('postgres',
327 "SELECT count(*), min(a), max(a) FROM tab_full");
328 is($result, qq(20|1|100),
329 'update works with REPLICA IDENTITY FULL and duplicate tuples');
330
331 $result = $node_subscriber->safe_psql('postgres',
332 "SELECT x FROM tab_full2 ORDER BY 1");
333 is( $result, qq(a
334 bb
335 bb),
336 'update works with REPLICA IDENTITY FULL and text datums');
337
338 $result = $node_subscriber->safe_psql('postgres',
339 "SELECT * FROM tab_mixed ORDER BY a");
340 is( $result, qq(local|1.1|baz|1
341 local|2.2|bar|2),
342 'update works with different column order and subscriber local values');
343
344 $result = $node_subscriber->safe_psql('postgres',
345 "SELECT * FROM tab_full_pk ORDER BY a");
346 is( $result, qq(1|bar
347 2|baz),
348 'update works with REPLICA IDENTITY FULL and a primary key');
349
350 $node_subscriber->safe_psql('postgres', "DELETE FROM tab_full_pk");
351 $node_subscriber->safe_psql('postgres', "DELETE FROM tab_full WHERE a = 25");
352
353 # Note that the current location of the log file is not grabbed immediately
354 # after reloading the configuration, but after sending one SQL command to
355 # the node so as we are sure that the reloading has taken effect.
356 my $log_location_pub = -s $node_publisher->logfile;
357 my $log_location_sub = -s $node_subscriber->logfile;
358
359 $node_publisher->safe_psql('postgres',
360 "UPDATE tab_full_pk SET b = 'quux' WHERE a = 1");
361 $node_publisher->safe_psql('postgres',
362 "UPDATE tab_full SET a = a + 1 WHERE a = 25");
363 $node_publisher->safe_psql('postgres', "DELETE FROM tab_full_pk WHERE a = 2");
364
365 $node_publisher->wait_for_catchup('tap_sub');
366
367 my $logfile = slurp_file($node_subscriber->logfile, $log_location_sub);
368 ok( $logfile =~
369 qr/conflict detected on relation "public.tab_full_pk": conflict=update_missing.*\n.*DETAIL:.* Could not find the row to be updated.*\n.*Remote row \(1, quux\); replica identity \(a\)=\(1\)/m,
370 'update target row is missing');
371 ok( $logfile =~
372 qr/conflict detected on relation "public.tab_full": conflict=update_missing.*\n.*DETAIL:.* Could not find the row to be updated.*\n.*Remote row \(26\); replica identity full \(25\)/m,
373 'update target row is missing');
374 ok( $logfile =~
375 qr/conflict detected on relation "public.tab_full_pk": conflict=delete_missing.*\n.*DETAIL:.* Could not find the row to be deleted.*\n.*Replica identity \(a\)=\(2\)/m,
376 'delete target row is missing');
377
378 $node_subscriber->append_conf('postgresql.conf',
379 "log_min_messages = warning");
380 $node_subscriber->reload;
381
382 # check behavior with toasted values
383
384 $node_publisher->safe_psql('postgres',
385 "UPDATE tab_mixed SET b = repeat('xyzzy', 100000) WHERE a = 2");
386
387 $node_publisher->wait_for_catchup('tap_sub');
388
389 $result = $node_subscriber->safe_psql('postgres',
390 "SELECT a, length(b), c, d FROM tab_mixed ORDER BY a");
391 is( $result, qq(1|3|1.1|local
392 2|500000|2.2|local),
393 'update transmits large column value');
394
395 $node_publisher->safe_psql('postgres',
396 "UPDATE tab_mixed SET c = 3.3 WHERE a = 2");
397
398 $node_publisher->wait_for_catchup('tap_sub');
399
400 $result = $node_subscriber->safe_psql('postgres',
401 "SELECT a, length(b), c, d FROM tab_mixed ORDER BY a");
402 is( $result, qq(1|3|1.1|local
403 2|500000|3.3|local),
404 'update with non-transmitted large column value');
405
406 # check behavior with dropped columns
407
408 # this update should get transmitted before the column goes away
409 $node_publisher->safe_psql('postgres',
410 "UPDATE tab_mixed SET b = 'bar', c = 2.2 WHERE a = 2");
411
412 $node_publisher->safe_psql('postgres', "ALTER TABLE tab_mixed DROP COLUMN b");
413
414 $node_publisher->safe_psql('postgres',
415 "UPDATE tab_mixed SET c = 11.11 WHERE a = 1");
416
417 $node_publisher->wait_for_catchup('tap_sub');
418
419 $result = $node_subscriber->safe_psql('postgres',
420 "SELECT * FROM tab_mixed ORDER BY a");
421 is( $result, qq(local|11.11|baz|1
422 local|2.2|bar|2),
423 'update works with dropped publisher column');
424
425 $node_subscriber->safe_psql('postgres',
426 "ALTER TABLE tab_mixed DROP COLUMN d");
427
428 $node_publisher->safe_psql('postgres',
429 "UPDATE tab_mixed SET c = 22.22 WHERE a = 2");
430
431 $node_publisher->wait_for_catchup('tap_sub');
432
433 $result = $node_subscriber->safe_psql('postgres',
434 "SELECT * FROM tab_mixed ORDER BY a");
435 is( $result, qq(11.11|baz|1
436 22.22|bar|2),
437 'update works with dropped subscriber column');
438
439 # Verify that GUC settings supplied in the CONNECTION string take effect on
440 # the publisher's walsender. We do this by enabling log_statement_stats in
441 # the CONNECTION string later and checking that the publisher's log contains a
442 # QUERY STATISTICS message.
443 #
444 # First, confirm that no such QUERY STATISTICS message appears before enabling
445 # log_statement_stats.
446 $logfile = slurp_file($node_publisher->logfile, $log_location_pub);
447 unlike(
448 $logfile,
449 qr/QUERY STATISTICS/,
450 'log_statement_stats has not been enabled yet');
451 $log_location_pub = -s $node_publisher->logfile;
452
453 # check that change of connection string and/or publication list causes
454 # restart of subscription workers. We check the state along with
455 # application_name to ensure that the walsender is (re)started.
456 #
457 # Not all of these are registered as tests as we need to poll for a change
458 # but the test suite will fail nonetheless when something goes wrong.
459 #
460 # Enable log_statement_stats as the change of connection string,
461 # which is also for the above mentioned test of GUC settings passed through
462 # CONNECTION.
463 my $oldpid = $node_publisher->safe_psql('postgres',
464 "SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
465 );
466 $node_subscriber->safe_psql('postgres',
467 "ALTER SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr options=''-c log_statement_stats=on'''"
468 );
469 $node_publisher->poll_query_until('postgres',
470 "SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
471 )
472 or die
473 "Timed out while waiting for apply to restart after changing CONNECTION";
474
475 # Check that the expected QUERY STATISTICS message appears,
476 # which shows that log_statement_stats=on from the CONNECTION string
477 # was correctly passed through to and honored by the walsender.
478 $logfile = slurp_file($node_publisher->logfile, $log_location_pub);
479 like(
480 $logfile,
481 qr/QUERY STATISTICS/,
482 'log_statement_stats in CONNECTION string had effect on publisher\'s walsender'
483 );
484
485 $oldpid = $node_publisher->safe_psql('postgres',
486 "SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
487 );
488 $node_subscriber->safe_psql('postgres',
489 "ALTER SUBSCRIPTION tap_sub SET PUBLICATION tap_pub_ins_only WITH (copy_data = false)"
490 );
491 $node_publisher->poll_query_until('postgres',
492 "SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
493 )
494 or die
495 "Timed out while waiting for apply to restart after changing PUBLICATION";
496
497 $node_publisher->safe_psql('postgres',
498 "INSERT INTO tab_ins SELECT generate_series(1001,1100)");
499 $node_publisher->safe_psql('postgres', "DELETE FROM tab_rep");
500
501 # Restart the publisher and check the state of the subscriber which
502 # should be in a streaming state after catching up.
503 $node_publisher->stop('fast');
504 $node_publisher->start;
505
506 $node_publisher->wait_for_catchup('tap_sub');
507
508 $result = $node_subscriber->safe_psql('postgres',
509 "SELECT count(*), min(a), max(a) FROM tab_ins");
510 is($result, qq(1152|1|1100),
511 'check replicated inserts after subscription publication change');
512
513 $result = $node_subscriber->safe_psql('postgres',
514 "SELECT count(*), min(a), max(a) FROM tab_rep");
515 is($result, qq(20|-20|-1),
516 'check changes skipped after subscription publication change');
517
518 # check alter publication (relcache invalidation etc)
519 $node_publisher->safe_psql('postgres',
520 "ALTER PUBLICATION tap_pub_ins_only SET (publish = 'insert, delete')");
521 $node_publisher->safe_psql('postgres',
522 "ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_full");
523 $node_publisher->safe_psql('postgres', "DELETE FROM tab_ins WHERE a > 0");
524 $node_subscriber->safe_psql('postgres',
525 "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION WITH (copy_data = false)"
526 );
527 $node_publisher->safe_psql('postgres', "INSERT INTO tab_full VALUES(0)");
528
529 $node_publisher->wait_for_catchup('tap_sub');
530
531 # Check that we don't send BEGIN and COMMIT because of empty transaction
532 # optimization. We have to look for the DEBUG1 log messages about that, so
533 # temporarily bump up the log verbosity.
534 $node_publisher->append_conf('postgresql.conf', "log_min_messages = debug1");
535 $node_publisher->reload;
536
537 # Note that the current location of the log file is not grabbed immediately
538 # after reloading the configuration, but after sending one SQL command to
539 # the node so that we are sure that the reloading has taken effect.
540 $log_location_pub = -s $node_publisher->logfile;
541
542 $node_publisher->safe_psql('postgres', "INSERT INTO tab_notrep VALUES (11)");
543
544 $node_publisher->wait_for_catchup('tap_sub');
545
546 $logfile = slurp_file($node_publisher->logfile, $log_location_pub);
547 ok($logfile =~ qr/skipped replication of an empty transaction with XID/,
548 'empty transaction is skipped');
549
550 $result =
551 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_notrep");
552 is($result, qq(0), 'check non-replicated table is empty on subscriber');
553
554 $node_publisher->append_conf('postgresql.conf', "log_min_messages = warning");
555 $node_publisher->reload;
556
557 # note that data are different on provider and subscriber
558 $result = $node_subscriber->safe_psql('postgres',
559 "SELECT count(*), min(a), max(a) FROM tab_ins");
560 is($result, qq(1052|1|1002),
561 'check replicated deletes after alter publication');
562
563 $result = $node_subscriber->safe_psql('postgres',
564 "SELECT count(*), min(a), max(a) FROM tab_full");
565 is($result, qq(19|0|100), 'check replicated insert after alter publication');
566
567 # check restart on rename
568 $oldpid = $node_publisher->safe_psql('postgres',
569 "SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
570 );
571 $node_subscriber->safe_psql('postgres',
572 "ALTER SUBSCRIPTION tap_sub RENAME TO tap_sub_renamed");
573 $node_publisher->poll_query_until('postgres',
574 "SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub_renamed' AND state = 'streaming';"
575 )
576 or die
577 "Timed out while waiting for apply to restart after renaming SUBSCRIPTION";
578
579 # check all the cleanup
580 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_renamed");
581
582 $result = $node_subscriber->safe_psql('postgres',
583 "SELECT count(*) FROM pg_subscription");
584 is($result, qq(0), 'check subscription was dropped on subscriber');
585
586 $result = $node_publisher->safe_psql('postgres',
587 "SELECT count(*) FROM pg_replication_slots");
588 is($result, qq(0), 'check replication slot was dropped on publisher');
589
590 $result = $node_subscriber->safe_psql('postgres',
591 "SELECT count(*) FROM pg_subscription_rel");
592 is($result, qq(0),
593 'check subscription relation status was dropped on subscriber');
594
595 $result = $node_publisher->safe_psql('postgres',
596 "SELECT count(*) FROM pg_replication_slots");
597 is($result, qq(0), 'check replication slot was dropped on publisher');
598
599 $result = $node_subscriber->safe_psql('postgres',
600 "SELECT count(*) FROM pg_replication_origin");
601 is($result, qq(0), 'check replication origin was dropped on subscriber');
602
603 $node_subscriber->stop('fast');
604 $node_publisher->stop('fast');
605
606 # CREATE PUBLICATION while wal_level=minimal should succeed, with a WARNING
607 $node_publisher->append_conf(
608 'postgresql.conf', qq(
609 wal_level=minimal
610 max_wal_senders=0
611 ));
612 $node_publisher->start;
613 ($result, my $retout, my $reterr) = $node_publisher->psql(
614 'postgres', qq{
615 BEGIN;
616 CREATE TABLE skip_wal();
617 CREATE PUBLICATION tap_pub2 FOR TABLE skip_wal;
618 ROLLBACK;
619 });
620 ok( $reterr =~
621 m/WARNING: "wal_level" is insufficient to publish logical changes/,
622 'CREATE PUBLICATION while "wal_level=minimal"');
623
624 done_testing();