Debian Codegraph @

src/test/subscription/t/031_column_list.pl

raw
1 # Copyright (c) 2022-2025, PostgreSQL Global Development Group
2
3 # Test partial-column publication of tables
4 use strict;
5 use warnings FATAL => 'all';
6 use PostgreSQL::Test::Cluster;
7 use PostgreSQL::Test::Utils;
8 use Test::More;
9
10 # create publisher node
11 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
12 $node_publisher->init(allows_streaming => 'logical');
13 $node_publisher->start;
14
15 # create subscriber node
16 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
17 $node_subscriber->init;
18 $node_subscriber->append_conf('postgresql.conf',
19 qq(max_logical_replication_workers = 6));
20 $node_subscriber->start;
21
22 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
23 my $offset = 0;
24
25 # setup tables on both nodes
26
27 # tab1: simple 1:1 replication
28 $node_publisher->safe_psql(
29 'postgres', qq(
30 CREATE TABLE tab1 (a int PRIMARY KEY, "B" int, c int)
31 ));
32
33 $node_subscriber->safe_psql(
34 'postgres', qq(
35 CREATE TABLE tab1 (a int PRIMARY KEY, "B" int, c int)
36 ));
37
38 # tab2: replication from regular to table with fewer columns
39 $node_publisher->safe_psql(
40 'postgres', qq(
41 CREATE TABLE tab2 (a int PRIMARY KEY, b varchar, c int);
42 ));
43
44 $node_subscriber->safe_psql(
45 'postgres', qq(
46 CREATE TABLE tab2 (a int PRIMARY KEY, b varchar)
47 ));
48
49 # tab3: simple 1:1 replication with weird column names
50 $node_publisher->safe_psql(
51 'postgres', qq(
52 CREATE TABLE tab3 ("a'" int PRIMARY KEY, "B" varchar, "c'" int)
53 ));
54
55 $node_subscriber->safe_psql(
56 'postgres', qq(
57 CREATE TABLE tab3 ("a'" int PRIMARY KEY, "c'" int)
58 ));
59
60 # test_part: partitioned tables, with partitioning (including multi-level
61 # partitioning, and fewer columns on the subscriber)
62 $node_publisher->safe_psql(
63 'postgres', qq(
64 CREATE TABLE test_part (a int PRIMARY KEY, b text, c timestamptz) PARTITION BY LIST (a);
65 CREATE TABLE test_part_1_1 PARTITION OF test_part FOR VALUES IN (1,2,3,4,5,6);
66 CREATE TABLE test_part_2_1 PARTITION OF test_part FOR VALUES IN (7,8,9,10,11,12) PARTITION BY LIST (a);
67 CREATE TABLE test_part_2_2 PARTITION OF test_part_2_1 FOR VALUES IN (7,8,9,10);
68 ));
69
70 $node_subscriber->safe_psql(
71 'postgres', qq(
72 CREATE TABLE test_part (a int PRIMARY KEY, b text) PARTITION BY LIST (a);
73 CREATE TABLE test_part_1_1 PARTITION OF test_part FOR VALUES IN (1,2,3,4,5,6);
74 CREATE TABLE test_part_2_1 PARTITION OF test_part FOR VALUES IN (7,8,9,10,11,12) PARTITION BY LIST (a);
75 CREATE TABLE test_part_2_2 PARTITION OF test_part_2_1 FOR VALUES IN (7,8,9,10);
76 ));
77
78 # tab4: table with user-defined enum types
79 $node_publisher->safe_psql(
80 'postgres', qq(
81 CREATE TYPE test_typ AS ENUM ('blue', 'red');
82 CREATE TABLE tab4 (a INT PRIMARY KEY, b test_typ, c int, d text);
83 ));
84
85 $node_subscriber->safe_psql(
86 'postgres', qq(
87 CREATE TYPE test_typ AS ENUM ('blue', 'red');
88 CREATE TABLE tab4 (a INT PRIMARY KEY, b test_typ, d text);
89 ));
90
91
92 # TEST: create publication and subscription for some of the tables with
93 # column lists
94 $node_publisher->safe_psql(
95 'postgres', qq(
96 CREATE PUBLICATION pub1
97 FOR TABLE tab1 (a, "B"), tab3 ("a'", "c'"), test_part (a, b), tab4 (a, b, d)
98 WITH (publish_via_partition_root = 'true');
99 ));
100
101 # check that we got the right prattrs values for the publication in the
102 # pg_publication_rel catalog (order by relname, to get stable ordering)
103 my $result = $node_publisher->safe_psql(
104 'postgres', qq(
105 SELECT relname, prattrs
106 FROM pg_publication_rel pb JOIN pg_class pc ON(pb.prrelid = pc.oid)
107 ORDER BY relname
108 ));
109
110 is( $result, qq(tab1|1 2
111 tab3|1 3
112 tab4|1 2 4
113 test_part|1 2), 'publication relation updated');
114
115 # TEST: insert data into the tables, create subscription and see if sync
116 # replicates the right columns
117 $node_publisher->safe_psql(
118 'postgres', qq(
119 INSERT INTO tab1 VALUES (1, 2, 3);
120 INSERT INTO tab1 VALUES (4, 5, 6);
121 ));
122
123 $node_publisher->safe_psql(
124 'postgres', qq(
125 INSERT INTO tab3 VALUES (1, 2, 3);
126 INSERT INTO tab3 VALUES (4, 5, 6);
127 ));
128
129 $node_publisher->safe_psql(
130 'postgres', qq(
131 INSERT INTO tab4 VALUES (1, 'red', 3, 'oh my');
132 INSERT INTO tab4 VALUES (2, 'blue', 4, 'hello');
133 ));
134
135 # replication of partitioned table
136 $node_publisher->safe_psql(
137 'postgres', qq(
138 INSERT INTO test_part VALUES (1, 'abc', '2021-07-04 12:00:00');
139 INSERT INTO test_part VALUES (2, 'bcd', '2021-07-03 11:12:13');
140 INSERT INTO test_part VALUES (7, 'abc', '2021-07-04 12:00:00');
141 INSERT INTO test_part VALUES (8, 'bcd', '2021-07-03 11:12:13');
142 ));
143
144 # create subscription for the publication, wait for sync to complete,
145 # then check the sync results
146 $node_subscriber->safe_psql(
147 'postgres', qq(
148 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1
149 ));
150
151 $node_subscriber->wait_for_subscription_sync;
152
153 # tab1: only (a,b) is replicated
154 $result =
155 $node_subscriber->safe_psql('postgres', "SELECT * FROM tab1 ORDER BY a");
156 is( $result, qq(1|2|
157 4|5|), 'insert on column tab1.c is not replicated');
158
159 # tab3: only (a,c) is replicated
160 $result = $node_subscriber->safe_psql('postgres',
161 qq(SELECT * FROM tab3 ORDER BY "a'"));
162 is( $result, qq(1|3
163 4|6), 'insert on column tab3.b is not replicated');
164
165 # tab4: only (a,b,d) is replicated
166 $result =
167 $node_subscriber->safe_psql('postgres', "SELECT * FROM tab4 ORDER BY a");
168 is( $result, qq(1|red|oh my
169 2|blue|hello), 'insert on column tab4.c is not replicated');
170
171 # test_part: (a,b) is replicated
172 $result = $node_subscriber->safe_psql('postgres',
173 "SELECT * FROM test_part ORDER BY a");
174 is( $result, qq(1|abc
175 2|bcd
176 7|abc
177 8|bcd), 'insert on column test_part.c columns is not replicated');
178
179
180 # TEST: now insert more data into the tables, and wait until we replicate
181 # them (not by tablesync, but regular decoding and replication)
182
183 $node_publisher->safe_psql(
184 'postgres', qq(
185 INSERT INTO tab1 VALUES (2, 3, 4);
186 INSERT INTO tab1 VALUES (5, 6, 7);
187 ));
188
189 $node_publisher->safe_psql(
190 'postgres', qq(
191 INSERT INTO tab3 VALUES (2, 3, 4);
192 INSERT INTO tab3 VALUES (5, 6, 7);
193 ));
194
195 $node_publisher->safe_psql(
196 'postgres', qq(
197 INSERT INTO tab4 VALUES (3, 'red', 5, 'foo');
198 INSERT INTO tab4 VALUES (4, 'blue', 6, 'bar');
199 ));
200
201 # replication of partitioned table
202 $node_publisher->safe_psql(
203 'postgres', qq(
204 INSERT INTO test_part VALUES (3, 'xxx', '2022-02-01 10:00:00');
205 INSERT INTO test_part VALUES (4, 'yyy', '2022-03-02 15:12:13');
206 INSERT INTO test_part VALUES (9, 'zzz', '2022-04-03 21:00:00');
207 INSERT INTO test_part VALUES (10, 'qqq', '2022-05-04 22:12:13');
208 ));
209
210 # wait for catchup before checking the subscriber
211 $node_publisher->wait_for_catchup('sub1');
212
213 # tab1: only (a,b) is replicated
214 $result =
215 $node_subscriber->safe_psql('postgres', "SELECT * FROM tab1 ORDER BY a");
216 is( $result, qq(1|2|
217 2|3|
218 4|5|
219 5|6|), 'insert on column tab1.c is not replicated');
220
221 # tab3: only (a,c) is replicated
222 $result = $node_subscriber->safe_psql('postgres',
223 qq(SELECT * FROM tab3 ORDER BY "a'"));
224 is( $result, qq(1|3
225 2|4
226 4|6
227 5|7), 'insert on column tab3.b is not replicated');
228
229 # tab4: only (a,b,d) is replicated
230 $result =
231 $node_subscriber->safe_psql('postgres', "SELECT * FROM tab4 ORDER BY a");
232 is( $result, qq(1|red|oh my
233 2|blue|hello
234 3|red|foo
235 4|blue|bar), 'insert on column tab4.c is not replicated');
236
237 # test_part: (a,b) is replicated
238 $result = $node_subscriber->safe_psql('postgres',
239 "SELECT * FROM test_part ORDER BY a");
240 is( $result, qq(1|abc
241 2|bcd
242 3|xxx
243 4|yyy
244 7|abc
245 8|bcd
246 9|zzz
247 10|qqq), 'insert on column test_part.c columns is not replicated');
248
249
250 # TEST: do some updates on some of the tables, both on columns included
251 # in the column list and other
252
253 # tab1: update of replicated column
254 $node_publisher->safe_psql('postgres',
255 qq(UPDATE tab1 SET "B" = 2 * "B" where a = 1));
256
257 # tab1: update of non-replicated column
258 $node_publisher->safe_psql('postgres',
259 qq(UPDATE tab1 SET c = 2*c where a = 4));
260
261 # tab3: update of non-replicated
262 $node_publisher->safe_psql('postgres',
263 qq(UPDATE tab3 SET "B" = "B" || ' updated' where "a'" = 4));
264
265 # tab3: update of replicated column
266 $node_publisher->safe_psql('postgres',
267 qq(UPDATE tab3 SET "c'" = 2 * "c'" where "a'" = 1));
268
269 # tab4
270 $node_publisher->safe_psql('postgres',
271 qq(UPDATE tab4 SET b = 'blue', c = c * 2, d = d || ' updated' where a = 1)
272 );
273
274 # tab4
275 $node_publisher->safe_psql('postgres',
276 qq(UPDATE tab4 SET b = 'red', c = c * 2, d = d || ' updated' where a = 2)
277 );
278
279 # wait for the replication to catch up, and check the UPDATE results got
280 # replicated correctly, with the right column list
281 $node_publisher->wait_for_catchup('sub1');
282
283 $result =
284 $node_subscriber->safe_psql('postgres', qq(SELECT * FROM tab1 ORDER BY a));
285 is( $result,
286 qq(1|4|
287 2|3|
288 4|5|
289 5|6|), 'only update on column tab1.b is replicated');
290
291 $result = $node_subscriber->safe_psql('postgres',
292 qq(SELECT * FROM tab3 ORDER BY "a'"));
293 is( $result,
294 qq(1|6
295 2|4
296 4|6
297 5|7), 'only update on column tab3.c is replicated');
298
299 $result =
300 $node_subscriber->safe_psql('postgres', qq(SELECT * FROM tab4 ORDER BY a));
301
302 is( $result, qq(1|blue|oh my updated
303 2|red|hello updated
304 3|red|foo
305 4|blue|bar), 'update on column tab4.c is not replicated');
306
307
308 # TEST: add table with a column list, insert data, replicate
309
310 # insert some data before adding it to the publication
311 $node_publisher->safe_psql(
312 'postgres', qq(
313 INSERT INTO tab2 VALUES (1, 'abc', 3);
314 ));
315
316 $node_publisher->safe_psql('postgres',
317 "ALTER PUBLICATION pub1 ADD TABLE tab2 (a, b)");
318
319 $node_subscriber->safe_psql('postgres',
320 "ALTER SUBSCRIPTION sub1 REFRESH PUBLICATION");
321
322 # wait for the tablesync to complete, add a bit more data and then check
323 # the results of the replication
324 $node_subscriber->wait_for_subscription_sync;
325
326 $node_publisher->safe_psql(
327 'postgres', qq(
328 INSERT INTO tab2 VALUES (2, 'def', 6);
329 ));
330
331 $node_publisher->wait_for_catchup('sub1');
332
333 $result =
334 $node_subscriber->safe_psql('postgres', "SELECT * FROM tab2 ORDER BY a");
335 is( $result, qq(1|abc
336 2|def), 'insert on column tab2.c is not replicated');
337
338 # do a couple updates, check the correct stuff gets replicated
339 $node_publisher->safe_psql(
340 'postgres', qq(
341 UPDATE tab2 SET c = 5 where a = 1;
342 UPDATE tab2 SET b = 'xyz' where a = 2;
343 ));
344
345 $node_publisher->wait_for_catchup('sub1');
346
347 $result =
348 $node_subscriber->safe_psql('postgres', "SELECT * FROM tab2 ORDER BY a");
349 is( $result, qq(1|abc
350 2|xyz), 'update on column tab2.c is not replicated');
351
352
353 # TEST: add a table to two publications with same column lists, and
354 # create a single subscription replicating both publications
355 $node_publisher->safe_psql(
356 'postgres', qq(
357 CREATE TABLE tab5 (a int PRIMARY KEY, b int, c int, d int);
358 CREATE PUBLICATION pub2 FOR TABLE tab5 (a, b);
359 CREATE PUBLICATION pub3 FOR TABLE tab5 (a, b);
360
361 -- insert a couple initial records
362 INSERT INTO tab5 VALUES (1, 11, 111, 1111);
363 INSERT INTO tab5 VALUES (2, 22, 222, 2222);
364 ));
365
366 $node_subscriber->safe_psql(
367 'postgres', qq(
368 CREATE TABLE tab5 (a int PRIMARY KEY, b int, d int);
369 ));
370
371 $node_subscriber->safe_psql(
372 'postgres', qq(
373 DROP SUBSCRIPTION sub1;
374 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub2, pub3
375 ));
376
377 $node_subscriber->wait_for_subscription_sync($node_publisher, 'sub1');
378
379 # insert data and make sure the columns in column list get fully replicated
380 $node_publisher->safe_psql(
381 'postgres', qq(
382 INSERT INTO tab5 VALUES (3, 33, 333, 3333);
383 INSERT INTO tab5 VALUES (4, 44, 444, 4444);
384 ));
385
386 $node_publisher->wait_for_catchup('sub1');
387
388 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM tab5 ORDER BY a"),
389 qq(1|11|
390 2|22|
391 3|33|
392 4|44|),
393 'overlapping publications with overlapping column lists');
394
395
396 # TEST: create a table with a column list, then change the replica
397 # identity by replacing a primary key (but use a different column in
398 # the column list)
399 $node_publisher->safe_psql(
400 'postgres', qq(
401 CREATE TABLE tab6 (a int PRIMARY KEY, b int, c int, d int);
402 CREATE PUBLICATION pub4 FOR TABLE tab6 (a, b);
403
404 -- initial data
405 INSERT INTO tab6 VALUES (1, 22, 333, 4444);
406 ));
407
408 $node_subscriber->safe_psql(
409 'postgres', qq(
410 CREATE TABLE tab6 (a int PRIMARY KEY, b int, c int, d int);
411 ));
412
413 $node_subscriber->safe_psql(
414 'postgres', qq(
415 DROP SUBSCRIPTION sub1;
416 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub4
417 ));
418
419 $node_subscriber->wait_for_subscription_sync;
420
421 $node_publisher->safe_psql(
422 'postgres', qq(
423 INSERT INTO tab6 VALUES (2, 33, 444, 5555);
424 UPDATE tab6 SET b = b * 2, c = c * 3, d = d * 4;
425 ));
426
427 $node_publisher->wait_for_catchup('sub1');
428
429 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM tab6 ORDER BY a"),
430 qq(1|44||
431 2|66||), 'replication with the original primary key');
432
433 # now redefine the constraint - move the primary key to a different column
434 # (which is still covered by the column list, though)
435
436 $node_publisher->safe_psql(
437 'postgres', qq(
438 ALTER TABLE tab6 DROP CONSTRAINT tab6_pkey;
439 ALTER TABLE tab6 ADD PRIMARY KEY (b);
440 ));
441
442 # we need to do the same thing on the subscriber
443 # XXX What would happen if this happens before the publisher ALTER? Or
444 # interleaved, somehow? But that seems unrelated to column lists.
445 $node_subscriber->safe_psql(
446 'postgres', qq(
447 ALTER TABLE tab6 DROP CONSTRAINT tab6_pkey;
448 ALTER TABLE tab6 ADD PRIMARY KEY (b);
449 ));
450
451 $node_subscriber->safe_psql(
452 'postgres', qq(
453 ALTER SUBSCRIPTION sub1 REFRESH PUBLICATION
454 ));
455
456 $node_subscriber->wait_for_subscription_sync;
457
458 $node_publisher->safe_psql(
459 'postgres', qq(
460 INSERT INTO tab6 VALUES (3, 55, 666, 8888);
461 UPDATE tab6 SET b = b * 2, c = c * 3, d = d * 4;
462 ));
463
464 $node_publisher->wait_for_catchup('sub1');
465
466 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM tab6 ORDER BY a"),
467 qq(1|88||
468 2|132||
469 3|110||),
470 'replication with the modified primary key');
471
472
473 # TEST: create a table with a column list, then change the replica
474 # identity by replacing a primary key with a key on multiple columns
475 # (all of them covered by the column list)
476 $node_publisher->safe_psql(
477 'postgres', qq(
478 CREATE TABLE tab7 (a int PRIMARY KEY, b int, c int, d int);
479 CREATE PUBLICATION pub5 FOR TABLE tab7 (a, b);
480
481 -- some initial data
482 INSERT INTO tab7 VALUES (1, 22, 333, 4444);
483 ));
484
485 $node_subscriber->safe_psql(
486 'postgres', qq(
487 CREATE TABLE tab7 (a int PRIMARY KEY, b int, c int, d int);
488 ));
489
490 $node_subscriber->safe_psql(
491 'postgres', qq(
492 DROP SUBSCRIPTION sub1;
493 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub5
494 ));
495
496 $node_subscriber->wait_for_subscription_sync;
497
498 $node_publisher->safe_psql(
499 'postgres', qq(
500 INSERT INTO tab7 VALUES (2, 33, 444, 5555);
501 UPDATE tab7 SET b = b * 2, c = c * 3, d = d * 4;
502 ));
503
504 $node_publisher->wait_for_catchup('sub1');
505
506 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM tab7 ORDER BY a"),
507 qq(1|44||
508 2|66||), 'replication with the original primary key');
509
510 # now redefine the constraint - move the primary key to a different column
511 # (which is not covered by the column list)
512 $node_publisher->safe_psql(
513 'postgres', qq(
514 ALTER TABLE tab7 DROP CONSTRAINT tab7_pkey;
515 ALTER TABLE tab7 ADD PRIMARY KEY (a, b);
516 ));
517
518 $node_publisher->safe_psql(
519 'postgres', qq(
520 INSERT INTO tab7 VALUES (3, 55, 666, 7777);
521 UPDATE tab7 SET b = b * 2, c = c * 3, d = d * 4;
522 ));
523
524 $node_publisher->wait_for_catchup('sub1');
525
526 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM tab7 ORDER BY a"),
527 qq(1|88||
528 2|132||
529 3|110||),
530 'replication with the modified primary key');
531
532 # now switch the primary key again to another columns not covered by the
533 # column list, but also generate writes between the drop and creation
534 # of the new constraint
535
536 $node_publisher->safe_psql(
537 'postgres', qq(
538 ALTER TABLE tab7 DROP CONSTRAINT tab7_pkey;
539 INSERT INTO tab7 VALUES (4, 77, 888, 9999);
540 -- update/delete is not allowed for tables without RI
541 ALTER TABLE tab7 ADD PRIMARY KEY (b, a);
542 UPDATE tab7 SET b = b * 2, c = c * 3, d = d * 4;
543 DELETE FROM tab7 WHERE a = 1;
544 ));
545
546 $node_publisher->safe_psql(
547 'postgres', qq(
548 ));
549
550 $node_publisher->wait_for_catchup('sub1');
551
552 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM tab7 ORDER BY a"),
553 qq(2|264||
554 3|220||
555 4|154||),
556 'replication with the modified primary key');
557
558
559 # TEST: partitioned tables (with publish_via_partition_root = false)
560 # and replica identity. The (leaf) partitions may have different RI, so
561 # we need to check the partition RI (with respect to the column list)
562 # while attaching the partition.
563
564 # First, let's create a partitioned table with two partitions, each with
565 # a different RI, but a column list not covering all those RI.
566
567 $node_publisher->safe_psql(
568 'postgres', qq(
569 CREATE TABLE test_part_a (a int, b int, c int) PARTITION BY LIST (a);
570
571 CREATE TABLE test_part_a_1 PARTITION OF test_part_a FOR VALUES IN (1,2,3,4,5);
572 ALTER TABLE test_part_a_1 ADD PRIMARY KEY (a);
573 ALTER TABLE test_part_a_1 REPLICA IDENTITY USING INDEX test_part_a_1_pkey;
574
575 CREATE TABLE test_part_a_2 PARTITION OF test_part_a FOR VALUES IN (6,7,8,9,10);
576 ALTER TABLE test_part_a_2 ADD PRIMARY KEY (b);
577 ALTER TABLE test_part_a_2 REPLICA IDENTITY USING INDEX test_part_a_2_pkey;
578
579 -- initial data, one row in each partition
580 INSERT INTO test_part_a VALUES (1, 3);
581 INSERT INTO test_part_a VALUES (6, 4);
582 ));
583
584 # do the same thing on the subscriber (with the opposite column order)
585 $node_subscriber->safe_psql(
586 'postgres', qq(
587 CREATE TABLE test_part_a (b int, a int) PARTITION BY LIST (a);
588
589 CREATE TABLE test_part_a_1 PARTITION OF test_part_a FOR VALUES IN (1,2,3,4,5);
590 ALTER TABLE test_part_a_1 ADD PRIMARY KEY (a);
591 ALTER TABLE test_part_a_1 REPLICA IDENTITY USING INDEX test_part_a_1_pkey;
592
593 CREATE TABLE test_part_a_2 PARTITION OF test_part_a FOR VALUES IN (6,7,8,9,10);
594 ALTER TABLE test_part_a_2 ADD PRIMARY KEY (b);
595 ALTER TABLE test_part_a_2 REPLICA IDENTITY USING INDEX test_part_a_2_pkey;
596 ));
597
598 # create a publication replicating just the column "a", which is not enough
599 # for the second partition
600 $node_publisher->safe_psql(
601 'postgres', qq(
602 CREATE PUBLICATION pub6 FOR TABLE test_part_a (b, a) WITH (publish_via_partition_root = true);
603 ALTER PUBLICATION pub6 ADD TABLE test_part_a_1 (a);
604 ALTER PUBLICATION pub6 ADD TABLE test_part_a_2 (b);
605 ));
606
607 # create the subscription for the above publication, wait for sync to
608 # complete
609 $node_subscriber->safe_psql(
610 'postgres', qq(
611 DROP SUBSCRIPTION sub1;
612 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub6
613 ));
614
615 $node_subscriber->wait_for_subscription_sync;
616
617 $node_publisher->safe_psql(
618 'postgres', qq(
619 INSERT INTO test_part_a VALUES (2, 5);
620 INSERT INTO test_part_a VALUES (7, 6);
621 ));
622
623 $node_publisher->wait_for_catchup('sub1');
624
625 is( $node_subscriber->safe_psql(
626 'postgres', "SELECT a, b FROM test_part_a ORDER BY a, b"),
627 qq(1|3
628 2|5
629 6|4
630 7|6),
631 'partitions with different replica identities not replicated correctly');
632
633 # This time start with a column list covering RI for all partitions, but
634 # then update the column list to not cover column "b" (needed by the
635 # second partition)
636
637 $node_publisher->safe_psql(
638 'postgres', qq(
639 CREATE TABLE test_part_b (a int, b int) PARTITION BY LIST (a);
640
641 CREATE TABLE test_part_b_1 PARTITION OF test_part_b FOR VALUES IN (1,2,3,4,5);
642 ALTER TABLE test_part_b_1 ADD PRIMARY KEY (a);
643 ALTER TABLE test_part_b_1 REPLICA IDENTITY USING INDEX test_part_b_1_pkey;
644
645 CREATE TABLE test_part_b_2 PARTITION OF test_part_b FOR VALUES IN (6,7,8,9,10);
646 ALTER TABLE test_part_b_2 ADD PRIMARY KEY (b);
647 ALTER TABLE test_part_b_2 REPLICA IDENTITY USING INDEX test_part_b_2_pkey;
648
649 -- initial data, one row in each partitions
650 INSERT INTO test_part_b VALUES (1, 1);
651 INSERT INTO test_part_b VALUES (6, 2);
652 ));
653
654 # do the same thing on the subscriber
655 $node_subscriber->safe_psql(
656 'postgres', qq(
657 CREATE TABLE test_part_b (a int, b int) PARTITION BY LIST (a);
658
659 CREATE TABLE test_part_b_1 PARTITION OF test_part_b FOR VALUES IN (1,2,3,4,5);
660 ALTER TABLE test_part_b_1 ADD PRIMARY KEY (a);
661 ALTER TABLE test_part_b_1 REPLICA IDENTITY USING INDEX test_part_b_1_pkey;
662
663 CREATE TABLE test_part_b_2 PARTITION OF test_part_b FOR VALUES IN (6,7,8,9,10);
664 ALTER TABLE test_part_b_2 ADD PRIMARY KEY (b);
665 ALTER TABLE test_part_b_2 REPLICA IDENTITY USING INDEX test_part_b_2_pkey;
666 ));
667
668 # create a publication replicating both columns, which is sufficient for
669 # both partitions
670 $node_publisher->safe_psql(
671 'postgres', qq(
672 CREATE PUBLICATION pub7 FOR TABLE test_part_b (a, b) WITH (publish_via_partition_root = true);
673 ));
674
675 # create the subscription for the above publication, wait for sync to
676 # complete
677 $node_subscriber->safe_psql(
678 'postgres', qq(
679 DROP SUBSCRIPTION sub1;
680 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub7
681 ));
682
683 $node_subscriber->wait_for_subscription_sync;
684
685 $node_publisher->safe_psql(
686 'postgres', qq(
687 INSERT INTO test_part_b VALUES (2, 3);
688 INSERT INTO test_part_b VALUES (7, 4);
689 ));
690
691 $node_publisher->wait_for_catchup('sub1');
692
693 is( $node_subscriber->safe_psql(
694 'postgres', "SELECT * FROM test_part_b ORDER BY a, b"),
695 qq(1|1
696 2|3
697 6|2
698 7|4),
699 'partitions with different replica identities not replicated correctly');
700
701
702 # TEST: This time start with a column list covering RI for all partitions,
703 # but then update RI for one of the partitions to not be covered by the
704 # column list anymore.
705
706 $node_publisher->safe_psql(
707 'postgres', qq(
708 CREATE TABLE test_part_c (a int, b int, c int) PARTITION BY LIST (a);
709
710 CREATE TABLE test_part_c_1 PARTITION OF test_part_c FOR VALUES IN (1,3);
711 ALTER TABLE test_part_c_1 ADD PRIMARY KEY (a);
712 ALTER TABLE test_part_c_1 REPLICA IDENTITY USING INDEX test_part_c_1_pkey;
713
714 CREATE TABLE test_part_c_2 PARTITION OF test_part_c FOR VALUES IN (2,4);
715 ALTER TABLE test_part_c_2 ADD PRIMARY KEY (b);
716 ALTER TABLE test_part_c_2 REPLICA IDENTITY USING INDEX test_part_c_2_pkey;
717
718 -- initial data, one row for each partition
719 INSERT INTO test_part_c VALUES (1, 3, 5);
720 INSERT INTO test_part_c VALUES (2, 4, 6);
721 ));
722
723 # do the same thing on the subscriber
724 $node_subscriber->safe_psql(
725 'postgres', qq(
726 CREATE TABLE test_part_c (a int, b int, c int) PARTITION BY LIST (a);
727
728 CREATE TABLE test_part_c_1 PARTITION OF test_part_c FOR VALUES IN (1,3);
729 ALTER TABLE test_part_c_1 ADD PRIMARY KEY (a);
730 ALTER TABLE test_part_c_1 REPLICA IDENTITY USING INDEX test_part_c_1_pkey;
731
732 CREATE TABLE test_part_c_2 PARTITION OF test_part_c FOR VALUES IN (2,4);
733 ALTER TABLE test_part_c_2 ADD PRIMARY KEY (b);
734 ALTER TABLE test_part_c_2 REPLICA IDENTITY USING INDEX test_part_c_2_pkey;
735 ));
736
737 # create a publication replicating data through partition root, with a column
738 # list on the root, and then add the partitions one by one with separate
739 # column lists (but those are not applied)
740 $node_publisher->safe_psql(
741 'postgres', qq(
742 CREATE PUBLICATION pub8 FOR TABLE test_part_c WITH (publish_via_partition_root = false);
743 ALTER PUBLICATION pub8 ADD TABLE test_part_c_1 (a,c);
744 ALTER PUBLICATION pub8 ADD TABLE test_part_c_2 (a,b);
745 ));
746
747 # create the subscription for the above publication, wait for sync to
748 # complete
749 $node_subscriber->safe_psql(
750 'postgres', qq(
751 DROP SUBSCRIPTION sub1;
752 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub8;
753 ));
754
755 $node_subscriber->wait_for_subscription_sync;
756
757 $node_publisher->safe_psql(
758 'postgres', qq(
759 INSERT INTO test_part_c VALUES (3, 7, 8);
760 INSERT INTO test_part_c VALUES (4, 9, 10);
761 ));
762
763 $node_publisher->wait_for_catchup('sub1');
764
765 is( $node_subscriber->safe_psql(
766 'postgres', "SELECT * FROM test_part_c ORDER BY a, b"),
767 qq(1||5
768 2|4|
769 3||8
770 4|9|),
771 'partitions with different replica identities not replicated correctly');
772
773
774 # create a publication not replicating data through partition root, without
775 # a column list on the root, and then add the partitions one by one with
776 # separate column lists
777 $node_publisher->safe_psql(
778 'postgres', qq(
779 DROP PUBLICATION pub8;
780 CREATE PUBLICATION pub8 FOR TABLE test_part_c WITH (publish_via_partition_root = false);
781 ALTER PUBLICATION pub8 ADD TABLE test_part_c_1 (a);
782 ALTER PUBLICATION pub8 ADD TABLE test_part_c_2 (a,b);
783 ));
784
785 # add the publication to our subscription, wait for sync to complete
786 $node_subscriber->safe_psql(
787 'postgres', qq(
788 ALTER SUBSCRIPTION sub1 REFRESH PUBLICATION;
789 TRUNCATE test_part_c;
790 ));
791
792 $node_subscriber->wait_for_subscription_sync;
793
794 $node_publisher->safe_psql(
795 'postgres', qq(
796 TRUNCATE test_part_c;
797 INSERT INTO test_part_c VALUES (1, 3, 5);
798 INSERT INTO test_part_c VALUES (2, 4, 6);
799 ));
800
801 $node_publisher->wait_for_catchup('sub1');
802
803 is( $node_subscriber->safe_psql(
804 'postgres', "SELECT * FROM test_part_c ORDER BY a, b"),
805 qq(1||
806 2|4|),
807 'partitions with different replica identities not replicated correctly');
808
809
810 # TEST: Start with a single partition, with RI compatible with the column
811 # list, and then attach a partition with incompatible RI.
812
813 $node_publisher->safe_psql(
814 'postgres', qq(
815 CREATE TABLE test_part_d (a int, b int) PARTITION BY LIST (a);
816
817 CREATE TABLE test_part_d_1 PARTITION OF test_part_d FOR VALUES IN (1,3);
818 ALTER TABLE test_part_d_1 ADD PRIMARY KEY (a);
819 ALTER TABLE test_part_d_1 REPLICA IDENTITY USING INDEX test_part_d_1_pkey;
820
821 INSERT INTO test_part_d VALUES (1, 2);
822 ));
823
824 # do the same thing on the subscriber (in fact, create both partitions right
825 # away, no need to delay that)
826 $node_subscriber->safe_psql(
827 'postgres', qq(
828 CREATE TABLE test_part_d (a int, b int) PARTITION BY LIST (a);
829
830 CREATE TABLE test_part_d_1 PARTITION OF test_part_d FOR VALUES IN (1,3);
831 ALTER TABLE test_part_d_1 ADD PRIMARY KEY (a);
832 ALTER TABLE test_part_d_1 REPLICA IDENTITY USING INDEX test_part_d_1_pkey;
833
834 CREATE TABLE test_part_d_2 PARTITION OF test_part_d FOR VALUES IN (2,4);
835 ALTER TABLE test_part_d_2 ADD PRIMARY KEY (a);
836 ALTER TABLE test_part_d_2 REPLICA IDENTITY USING INDEX test_part_d_2_pkey;
837 ));
838
839 # create a publication replicating both columns, which is sufficient for
840 # both partitions
841 $node_publisher->safe_psql(
842 'postgres', qq(
843 CREATE PUBLICATION pub9 FOR TABLE test_part_d (a) WITH (publish_via_partition_root = true);
844 ));
845
846 # create the subscription for the above publication, wait for sync to
847 # complete
848 $node_subscriber->safe_psql(
849 'postgres', qq(
850 DROP SUBSCRIPTION sub1;
851 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub9
852 ));
853
854 $node_subscriber->wait_for_subscription_sync;
855
856 $node_publisher->safe_psql(
857 'postgres', qq(
858 INSERT INTO test_part_d VALUES (3, 4);
859 ));
860
861 $node_publisher->wait_for_catchup('sub1');
862
863 is( $node_subscriber->safe_psql(
864 'postgres', "SELECT * FROM test_part_d ORDER BY a, b"),
865 qq(1|
866 3|),
867 'partitions with different replica identities not replicated correctly');
868
869
870 # TEST: With a table included in the publications which is FOR ALL TABLES, it
871 # means replicate all columns.
872
873 # drop unnecessary tables, so as not to interfere with the FOR ALL TABLES
874 $node_publisher->safe_psql(
875 'postgres', qq(
876 DROP TABLE tab1, tab2, tab3, tab4, tab5, tab6, tab7,
877 test_part, test_part_a, test_part_b, test_part_c, test_part_d;
878 ));
879
880 $node_publisher->safe_psql(
881 'postgres', qq(
882 CREATE TABLE test_mix_2 (a int PRIMARY KEY, b int, c int);
883 CREATE PUBLICATION pub_mix_3 FOR TABLE test_mix_2 (a, b, c);
884 CREATE PUBLICATION pub_mix_4 FOR ALL TABLES;
885
886 -- initial data
887 INSERT INTO test_mix_2 VALUES (1, 2, 3);
888 ));
889
890 $node_subscriber->safe_psql(
891 'postgres', qq(
892 CREATE TABLE test_mix_2 (a int PRIMARY KEY, b int, c int);
893 DROP SUBSCRIPTION sub1;
894 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub_mix_3, pub_mix_4;
895 ));
896
897 $node_subscriber->wait_for_subscription_sync;
898
899 $node_publisher->safe_psql(
900 'postgres', qq(
901 INSERT INTO test_mix_2 VALUES (4, 5, 6);
902 ));
903
904 $node_publisher->wait_for_catchup('sub1');
905
906 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM test_mix_2"),
907 qq(1|2|3
908 4|5|6),
909 'all columns should be replicated');
910
911
912 # TEST: With a table included in the publication which is FOR TABLES IN
913 # SCHEMA, it means replicate all columns.
914
915 $node_subscriber->safe_psql(
916 'postgres', qq(
917 DROP SUBSCRIPTION sub1;
918 CREATE TABLE test_mix_3 (a int PRIMARY KEY, b int, c int);
919 ));
920
921 $node_publisher->safe_psql(
922 'postgres', qq(
923 DROP TABLE test_mix_2;
924 CREATE TABLE test_mix_3 (a int PRIMARY KEY, b int, c int);
925 CREATE PUBLICATION pub_mix_5 FOR TABLE test_mix_3 (a, b, c);
926 CREATE PUBLICATION pub_mix_6 FOR TABLES IN SCHEMA public;
927
928 -- initial data
929 INSERT INTO test_mix_3 VALUES (1, 2, 3);
930 ));
931
932 $node_subscriber->safe_psql(
933 'postgres', qq(
934 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub_mix_5, pub_mix_6;
935 ));
936
937 $node_subscriber->wait_for_subscription_sync;
938
939 $node_publisher->safe_psql(
940 'postgres', qq(
941 INSERT INTO test_mix_3 VALUES (4, 5, 6);
942 ));
943
944 $node_publisher->wait_for_catchup('sub1');
945
946 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM test_mix_3"),
947 qq(1|2|3
948 4|5|6),
949 'all columns should be replicated');
950
951
952 # TEST: Check handling of publish_via_partition_root - if a partition is
953 # published through partition root, we should only apply the column list
954 # defined for the whole table (not the partitions) - both during the initial
955 # sync and when replicating changes. This is what we do for row filters.
956
957 $node_subscriber->safe_psql(
958 'postgres', qq(
959 DROP SUBSCRIPTION sub1;
960
961 CREATE TABLE test_root (a int PRIMARY KEY, b int, c int) PARTITION BY RANGE (a);
962 CREATE TABLE test_root_1 PARTITION OF test_root FOR VALUES FROM (1) TO (10);
963 CREATE TABLE test_root_2 PARTITION OF test_root FOR VALUES FROM (10) TO (20);
964 ));
965
966 $node_publisher->safe_psql(
967 'postgres', qq(
968 CREATE TABLE test_root (a int PRIMARY KEY, b int, c int) PARTITION BY RANGE (a);
969 CREATE TABLE test_root_1 PARTITION OF test_root FOR VALUES FROM (1) TO (10);
970 CREATE TABLE test_root_2 PARTITION OF test_root FOR VALUES FROM (10) TO (20);
971
972 CREATE PUBLICATION pub_test_root FOR TABLE test_root (a) WITH (publish_via_partition_root = true);
973 CREATE PUBLICATION pub_test_root_1 FOR TABLE test_root_1 (a, b);
974
975 -- initial data
976 INSERT INTO test_root VALUES (1, 2, 3);
977 INSERT INTO test_root VALUES (10, 20, 30);
978 ));
979
980 # Subscribe to pub_test_root and pub_test_root_1 at the same time, which means
981 # that the initial data will be synced once, and only the column list of the
982 # parent table (test_root) in the publication pub_test_root will be used for
983 # both table sync and data replication.
984 $node_subscriber->safe_psql(
985 'postgres', qq(
986 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub_test_root, pub_test_root_1;
987 ));
988
989 $node_subscriber->wait_for_subscription_sync;
990
991 $node_publisher->safe_psql(
992 'postgres', qq(
993 INSERT INTO test_root VALUES (2, 3, 4);
994 INSERT INTO test_root VALUES (11, 21, 31);
995 ));
996
997 $node_publisher->wait_for_catchup('sub1');
998
999 is( $node_subscriber->safe_psql(
1000 'postgres', "SELECT * FROM test_root ORDER BY a, b, c"),
1001 qq(1||
1002 2||
1003 10||
1004 11||),
1005 'publication via partition root applies column list');
1006
1007
1008 # TEST: Multiple publications which publish schema of parent table and
1009 # partition. The partition is published through two publications, once
1010 # through a schema (so no column list) containing the parent, and then
1011 # also directly (with all columns). The expected outcome is there is
1012 # no column list.
1013
1014 $node_publisher->safe_psql(
1015 'postgres', qq(
1016 DROP PUBLICATION pub1, pub2, pub3, pub4, pub5, pub6, pub7, pub8;
1017
1018 CREATE SCHEMA s1;
1019 CREATE TABLE s1.t (a int, b int, c int) PARTITION BY RANGE (a);
1020 CREATE TABLE t_1 PARTITION OF s1.t FOR VALUES FROM (1) TO (10);
1021
1022 CREATE PUBLICATION pub1 FOR TABLES IN SCHEMA s1;
1023 CREATE PUBLICATION pub2 FOR TABLE t_1(a, b, c);
1024
1025 -- initial data
1026 INSERT INTO s1.t VALUES (1, 2, 3);
1027 ));
1028
1029 $node_subscriber->safe_psql(
1030 'postgres', qq(
1031 CREATE SCHEMA s1;
1032 CREATE TABLE s1.t (a int, b int, c int) PARTITION BY RANGE (a);
1033 CREATE TABLE t_1 PARTITION OF s1.t FOR VALUES FROM (1) TO (10);
1034
1035 DROP SUBSCRIPTION sub1;
1036 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1, pub2;
1037 ));
1038
1039 $node_subscriber->wait_for_subscription_sync;
1040
1041 $node_publisher->safe_psql(
1042 'postgres', qq(
1043 INSERT INTO s1.t VALUES (4, 5, 6);
1044 ));
1045
1046 $node_publisher->wait_for_catchup('sub1');
1047
1048 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM s1.t ORDER BY a"),
1049 qq(1|2|3
1050 4|5|6),
1051 'two publications, publishing the same relation');
1052
1053 # Now resync the subscription, but with publications in the opposite order.
1054 # The result should be the same.
1055
1056 $node_subscriber->safe_psql(
1057 'postgres', qq(
1058 TRUNCATE s1.t;
1059
1060 ALTER SUBSCRIPTION sub1 SET PUBLICATION pub2, pub1;
1061 ));
1062
1063 $node_subscriber->wait_for_subscription_sync;
1064
1065 $node_publisher->safe_psql(
1066 'postgres', qq(
1067 INSERT INTO s1.t VALUES (7, 8, 9);
1068 ));
1069
1070 $node_publisher->wait_for_catchup('sub1');
1071
1072 is( $node_subscriber->safe_psql('postgres', "SELECT * FROM s1.t ORDER BY a"),
1073 qq(7|8|9),
1074 'two publications, publishing the same relation');
1075
1076
1077 # TEST: One publication, containing both the parent and child relations.
1078 # The expected outcome is list "a", because that's the column list defined
1079 # for the top-most ancestor added to the publication.
1080
1081 $node_publisher->safe_psql(
1082 'postgres', qq(
1083 DROP SCHEMA s1 CASCADE;
1084 CREATE TABLE t (a int, b int, c int) PARTITION BY RANGE (a);
1085 CREATE TABLE t_1 PARTITION OF t FOR VALUES FROM (1) TO (10)
1086 PARTITION BY RANGE (a);
1087 CREATE TABLE t_2 PARTITION OF t_1 FOR VALUES FROM (1) TO (10);
1088
1089 CREATE PUBLICATION pub3 FOR TABLE t_1 (a), t_2
1090 WITH (PUBLISH_VIA_PARTITION_ROOT);
1091
1092 -- initial data
1093 INSERT INTO t VALUES (1, 2, 3);
1094 ));
1095
1096 $node_subscriber->safe_psql(
1097 'postgres', qq(
1098 DROP SCHEMA s1 CASCADE;
1099 CREATE TABLE t (a int, b int, c int) PARTITION BY RANGE (a);
1100 CREATE TABLE t_1 PARTITION OF t FOR VALUES FROM (1) TO (10)
1101 PARTITION BY RANGE (a);
1102 CREATE TABLE t_2 PARTITION OF t_1 FOR VALUES FROM (1) TO (10);
1103
1104 DROP SUBSCRIPTION sub1;
1105 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub3;
1106 ));
1107
1108 $node_subscriber->wait_for_subscription_sync;
1109
1110 $node_publisher->safe_psql(
1111 'postgres', qq(
1112 INSERT INTO t VALUES (4, 5, 6);
1113 ));
1114
1115 $node_publisher->wait_for_catchup('sub1');
1116
1117 is( $node_subscriber->safe_psql(
1118 'postgres', "SELECT * FROM t ORDER BY a, b, c"),
1119 qq(1||
1120 4||),
1121 'publication containing both parent and child relation');
1122
1123
1124 # TEST: One publication, containing both the parent and child relations.
1125 # The expected outcome is list "a", because that's the column list defined
1126 # for the top-most ancestor added to the publication.
1127 # Note: The difference from the preceding test is that in this case both
1128 # relations have a column list defined.
1129
1130 $node_publisher->safe_psql(
1131 'postgres', qq(
1132 DROP TABLE t;
1133 CREATE TABLE t (a int, b int, c int) PARTITION BY RANGE (a);
1134 CREATE TABLE t_1 PARTITION OF t FOR VALUES FROM (1) TO (10)
1135 PARTITION BY RANGE (a);
1136 CREATE TABLE t_2 PARTITION OF t_1 FOR VALUES FROM (1) TO (10);
1137
1138 CREATE PUBLICATION pub4 FOR TABLE t_1 (a), t_2 (b)
1139 WITH (PUBLISH_VIA_PARTITION_ROOT);
1140
1141 -- initial data
1142 INSERT INTO t VALUES (1, 2, 3);
1143 ));
1144
1145 $node_subscriber->safe_psql(
1146 'postgres', qq(
1147 DROP TABLE t;
1148 CREATE TABLE t (a int, b int, c int) PARTITION BY RANGE (a);
1149 CREATE TABLE t_1 PARTITION OF t FOR VALUES FROM (1) TO (10)
1150 PARTITION BY RANGE (a);
1151 CREATE TABLE t_2 PARTITION OF t_1 FOR VALUES FROM (1) TO (10);
1152
1153 DROP SUBSCRIPTION sub1;
1154 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub4;
1155 ));
1156
1157 $node_subscriber->wait_for_subscription_sync;
1158
1159 $node_publisher->safe_psql(
1160 'postgres', qq(
1161 INSERT INTO t VALUES (4, 5, 6);
1162 ));
1163
1164 $node_publisher->wait_for_catchup('sub1');
1165
1166 is( $node_subscriber->safe_psql(
1167 'postgres', "SELECT * FROM t ORDER BY a, b, c"),
1168 qq(1||
1169 4||),
1170 'publication containing both parent and child relation');
1171
1172 # TEST: Only columns in the column list should exist in the old tuple of UPDATE
1173 # and DELETE.
1174
1175 $node_publisher->safe_psql(
1176 'postgres', qq(
1177 CREATE TABLE test_oldtuple_col (a int PRIMARY KEY, b int, c int);
1178 CREATE PUBLICATION pub_check_oldtuple FOR TABLE test_oldtuple_col (a, b);
1179 INSERT INTO test_oldtuple_col VALUES(1, 2, 3);
1180 SELECT * FROM pg_create_logical_replication_slot('test_slot', 'pgoutput');
1181 UPDATE test_oldtuple_col SET a = 2;
1182 DELETE FROM test_oldtuple_col;
1183 ));
1184
1185
1186 # Check at 7th byte of binary data for the number of columns in the old tuple.
1187 #
1188 # 7 = 1 (count from 1) + 1 byte (message type) + 4 byte (relid) + 1 byte (flag
1189 # for old key).
1190 #
1191 # The message type of UPDATE is 85('U').
1192 # The message type of DELETE is 68('D').
1193 $result = $node_publisher->safe_psql(
1194 'postgres', qq(
1195 SELECT substr(data, 7, 2) = int2send(2::smallint)
1196 FROM pg_logical_slot_peek_binary_changes('test_slot', NULL, NULL,
1197 'proto_version', '1',
1198 'publication_names', 'pub_check_oldtuple')
1199 WHERE get_byte(data, 0) = 85 OR get_byte(data, 0) = 68
1200 ));
1201
1202 is( $result, qq(t
1203 t), 'check the number of columns in the old tuple');
1204
1205 # TEST: Dropped columns are not considered for the column list, and generated
1206 # columns are not replicated if they are not explicitly included in the column
1207 # list. So, the publication having a column list except for those columns and a
1208 # publication without any column list (aka all columns as part of the columns
1209 # list) are considered to have the same column list.
1210 $node_publisher->safe_psql(
1211 'postgres', qq(
1212 CREATE TABLE test_mix_4 (a int PRIMARY KEY, b int, c int, d int GENERATED ALWAYS AS (a + 1) STORED, e int GENERATED ALWAYS AS (a + 2) VIRTUAL);
1213 ALTER TABLE test_mix_4 DROP COLUMN c;
1214
1215 CREATE PUBLICATION pub_mix_7 FOR TABLE test_mix_4 (a, b);
1216 CREATE PUBLICATION pub_mix_8 FOR TABLE test_mix_4;
1217
1218 -- initial data
1219 INSERT INTO test_mix_4 VALUES (1, 2);
1220 ));
1221
1222 $node_subscriber->safe_psql(
1223 'postgres', qq(
1224 DROP SUBSCRIPTION sub1;
1225 CREATE TABLE test_mix_4 (a int PRIMARY KEY, b int, c int, d int);
1226 ));
1227
1228 $node_subscriber->safe_psql(
1229 'postgres', qq(
1230 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub_mix_7, pub_mix_8;
1231 ));
1232
1233 $node_subscriber->wait_for_subscription_sync;
1234
1235 is( $node_subscriber->safe_psql(
1236 'postgres', "SELECT * FROM test_mix_4 ORDER BY a"),
1237 qq(1|2||),
1238 'initial synchronization with multiple publications with the same column list'
1239 );
1240
1241 $node_publisher->safe_psql(
1242 'postgres', qq(
1243 INSERT INTO test_mix_4 VALUES (3, 4);
1244 ));
1245
1246 $node_publisher->wait_for_catchup('sub1');
1247
1248 is( $node_subscriber->safe_psql(
1249 'postgres', "SELECT * FROM test_mix_4 ORDER BY a"),
1250 qq(1|2||
1251 3|4||),
1252 'replication with multiple publications with the same column list');
1253
1254 # TEST: With a table included in multiple publications with different column
1255 # lists, we should catch the error when creating the subscription.
1256
1257 $node_publisher->safe_psql(
1258 'postgres', qq(
1259 CREATE TABLE test_mix_1 (a int PRIMARY KEY, b int, c int);
1260 CREATE PUBLICATION pub_mix_1 FOR TABLE test_mix_1 (a, b);
1261 CREATE PUBLICATION pub_mix_2 FOR TABLE test_mix_1 (a, c);
1262 ));
1263
1264 $node_subscriber->safe_psql(
1265 'postgres', qq(
1266 DROP SUBSCRIPTION sub1;
1267 CREATE TABLE test_mix_1 (a int PRIMARY KEY, b int, c int);
1268 ));
1269
1270 my ($cmdret, $stdout, $stderr) = $node_subscriber->psql(
1271 'postgres', qq(
1272 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub_mix_1, pub_mix_2;
1273 ));
1274
1275 ok( $stderr =~
1276 qr/cannot use different column lists for table "public.test_mix_1" in different publications/,
1277 'different column lists detected');
1278
1279 # TEST: If the column list is changed after creating the subscription, we
1280 # should catch the error reported by walsender.
1281
1282 $node_publisher->safe_psql(
1283 'postgres', qq(
1284 ALTER PUBLICATION pub_mix_1 SET TABLE test_mix_1 (a, c);
1285 ));
1286
1287 $node_subscriber->safe_psql(
1288 'postgres', qq(
1289 CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub_mix_1, pub_mix_2;
1290 ));
1291
1292 $node_publisher->wait_for_catchup('sub1');
1293
1294 $node_publisher->safe_psql(
1295 'postgres', qq(
1296 ALTER PUBLICATION pub_mix_1 SET TABLE test_mix_1 (a, b);
1297 INSERT INTO test_mix_1 VALUES(1, 1, 1);
1298 ));
1299
1300 $offset = $node_publisher->wait_for_log(
1301 qr/cannot use different column lists for table "public.test_mix_1" in different publications/,
1302 $offset);
1303
1304 $node_subscriber->stop('fast');
1305 $node_publisher->stop('fast');
1306
1307 done_testing();