Debian Codegraph @

src/test/subscription/t/013_partition.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test logical replication with partitioned tables
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 # setup
12
13 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
14 $node_publisher->init(allows_streaming => 'logical');
15 $node_publisher->start;
16
17 my $node_subscriber1 = PostgreSQL::Test::Cluster->new('subscriber1');
18 $node_subscriber1->init;
19 $node_subscriber1->start;
20
21 my $node_subscriber2 = PostgreSQL::Test::Cluster->new('subscriber2');
22 $node_subscriber2->init;
23 $node_subscriber2->start;
24
25 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
26
27 # publisher
28 $node_publisher->safe_psql('postgres', "CREATE PUBLICATION pub1");
29 $node_publisher->safe_psql('postgres',
30 "CREATE PUBLICATION pub_all FOR ALL TABLES");
31 $node_publisher->safe_psql('postgres',
32 "CREATE TABLE tab1 (a int PRIMARY KEY, b text) PARTITION BY LIST (a)");
33 $node_publisher->safe_psql('postgres',
34 "CREATE TABLE tab1_1 (b text, a int NOT NULL)");
35 $node_publisher->safe_psql('postgres',
36 "ALTER TABLE tab1 ATTACH PARTITION tab1_1 FOR VALUES IN (1, 2, 3)");
37 $node_publisher->safe_psql('postgres',
38 "CREATE TABLE tab1_2 PARTITION OF tab1 FOR VALUES IN (4, 5, 6)");
39 $node_publisher->safe_psql('postgres',
40 "CREATE TABLE tab1_def PARTITION OF tab1 DEFAULT");
41 $node_publisher->safe_psql('postgres',
42 "ALTER PUBLICATION pub1 ADD TABLE tab1, tab1_1");
43
44 # subscriber1
45 #
46 # This is partitioned differently from the publisher. tab1_2 is
47 # subpartitioned. This tests the tuple routing code on the
48 # subscriber.
49 $node_subscriber1->safe_psql('postgres',
50 "CREATE TABLE tab1 (c text, a int PRIMARY KEY, b text) PARTITION BY LIST (a)"
51 );
52 # make a BRIN index to test aminsertcleanup logic in subscriber
53 $node_subscriber1->safe_psql('postgres',
54 "CREATE INDEX tab1_c_brin_idx ON tab1 USING brin (c)");
55 $node_subscriber1->safe_psql('postgres',
56 "CREATE TABLE tab1_1 (b text, c text DEFAULT 'sub1_tab1', a int NOT NULL)"
57 );
58 $node_subscriber1->safe_psql('postgres',
59 "ALTER TABLE tab1 ATTACH PARTITION tab1_1 FOR VALUES IN (1, 2, 3)");
60 $node_subscriber1->safe_psql('postgres',
61 "CREATE TABLE tab1_2 PARTITION OF tab1 (c DEFAULT 'sub1_tab1') FOR VALUES IN (4, 5, 6) PARTITION BY LIST (a)"
62 );
63 $node_subscriber1->safe_psql('postgres',
64 "CREATE TABLE tab1_2_1 (c text, b text, a int NOT NULL)");
65 $node_subscriber1->safe_psql('postgres',
66 "ALTER TABLE tab1_2 ATTACH PARTITION tab1_2_1 FOR VALUES IN (5)");
67 $node_subscriber1->safe_psql('postgres',
68 "CREATE TABLE tab1_2_2 PARTITION OF tab1_2 FOR VALUES IN (4, 6)");
69 $node_subscriber1->safe_psql('postgres',
70 "CREATE TABLE tab1_def PARTITION OF tab1 (c DEFAULT 'sub1_tab1') DEFAULT"
71 );
72 $node_subscriber1->safe_psql('postgres',
73 "CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1"
74 );
75
76 # Add set of AFTER replica triggers for testing that they are fired
77 # correctly. This uses a table that records details of all trigger
78 # activities. Triggers are marked as enabled for a subset of the
79 # partition tree.
80 $node_subscriber1->safe_psql(
81 'postgres', qq{
82 CREATE TABLE sub1_trigger_activity (tgtab text, tgop text,
83 tgwhen text, tglevel text, olda int, newa int);
84 CREATE FUNCTION sub1_trigger_activity_func() RETURNS TRIGGER AS \$\$
85 BEGIN
86 IF (TG_OP = 'INSERT') THEN
87 INSERT INTO public.sub1_trigger_activity
88 SELECT TG_RELNAME, TG_OP, TG_WHEN, TG_LEVEL, NULL, NEW.a;
89 ELSIF (TG_OP = 'UPDATE') THEN
90 INSERT INTO public.sub1_trigger_activity
91 SELECT TG_RELNAME, TG_OP, TG_WHEN, TG_LEVEL, OLD.a, NEW.a;
92 END IF;
93 RETURN NULL;
94 END;
95 \$\$ LANGUAGE plpgsql;
96 CREATE TRIGGER sub1_tab1_log_op_trigger
97 AFTER INSERT OR UPDATE ON tab1
98 FOR EACH ROW EXECUTE PROCEDURE sub1_trigger_activity_func();
99 ALTER TABLE ONLY tab1 ENABLE REPLICA TRIGGER sub1_tab1_log_op_trigger;
100 CREATE TRIGGER sub1_tab1_2_log_op_trigger
101 AFTER INSERT OR UPDATE ON tab1_2
102 FOR EACH ROW EXECUTE PROCEDURE sub1_trigger_activity_func();
103 ALTER TABLE ONLY tab1_2 ENABLE REPLICA TRIGGER sub1_tab1_2_log_op_trigger;
104 CREATE TRIGGER sub1_tab1_2_2_log_op_trigger
105 AFTER INSERT OR UPDATE ON tab1_2_2
106 FOR EACH ROW EXECUTE PROCEDURE sub1_trigger_activity_func();
107 ALTER TABLE ONLY tab1_2_2 ENABLE REPLICA TRIGGER sub1_tab1_2_2_log_op_trigger;
108 });
109
110 # subscriber 2
111 #
112 # This does not use partitioning. The tables match the leaf tables on
113 # the publisher.
114 $node_subscriber2->safe_psql('postgres',
115 "CREATE TABLE tab1 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab1', b text)"
116 );
117 $node_subscriber2->safe_psql('postgres',
118 "CREATE TABLE tab1_1 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab1_1', b text)"
119 );
120 $node_subscriber2->safe_psql('postgres',
121 "CREATE TABLE tab1_2 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab1_2', b text)"
122 );
123 $node_subscriber2->safe_psql('postgres',
124 "CREATE TABLE tab1_def (a int PRIMARY KEY, b text, c text DEFAULT 'sub2_tab1_def')"
125 );
126 $node_subscriber2->safe_psql('postgres',
127 "CREATE SUBSCRIPTION sub2 CONNECTION '$publisher_connstr' PUBLICATION pub_all"
128 );
129
130 # Add set of AFTER replica triggers for testing that they are fired
131 # correctly, using the same method as the first subscriber.
132 $node_subscriber2->safe_psql(
133 'postgres', qq{
134 CREATE TABLE sub2_trigger_activity (tgtab text,
135 tgop text, tgwhen text, tglevel text, olda int, newa int);
136 CREATE FUNCTION sub2_trigger_activity_func() RETURNS TRIGGER AS \$\$
137 BEGIN
138 IF (TG_OP = 'INSERT') THEN
139 INSERT INTO public.sub2_trigger_activity
140 SELECT TG_RELNAME, TG_OP, TG_WHEN, TG_LEVEL, NULL, NEW.a;
141 ELSIF (TG_OP = 'UPDATE') THEN
142 INSERT INTO public.sub2_trigger_activity
143 SELECT TG_RELNAME, TG_OP, TG_WHEN, TG_LEVEL, OLD.a, NEW.a;
144 END IF;
145 RETURN NULL;
146 END;
147 \$\$ LANGUAGE plpgsql;
148 CREATE TRIGGER sub2_tab1_log_op_trigger
149 AFTER INSERT OR UPDATE ON tab1
150 FOR EACH ROW EXECUTE PROCEDURE sub2_trigger_activity_func();
151 ALTER TABLE ONLY tab1 ENABLE REPLICA TRIGGER sub2_tab1_log_op_trigger;
152 CREATE TRIGGER sub2_tab1_2_log_op_trigger
153 AFTER INSERT OR UPDATE ON tab1_2
154 FOR EACH ROW EXECUTE PROCEDURE sub2_trigger_activity_func();
155 ALTER TABLE ONLY tab1_2 ENABLE REPLICA TRIGGER sub2_tab1_2_log_op_trigger;
156 });
157
158 # Wait for initial sync of all subscriptions
159 $node_subscriber1->wait_for_subscription_sync;
160 $node_subscriber2->wait_for_subscription_sync;
161
162 # Tests for replication using leaf partition identity and schema
163
164 # insert
165 $node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (1)");
166 $node_publisher->safe_psql('postgres', "INSERT INTO tab1_1 (a) VALUES (3)");
167 $node_publisher->safe_psql('postgres', "INSERT INTO tab1_2 VALUES (5)");
168 $node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (0)");
169
170 $node_publisher->wait_for_catchup('sub1');
171 $node_publisher->wait_for_catchup('sub2');
172
173 my $result = $node_subscriber1->safe_psql('postgres',
174 "SELECT c, a FROM tab1 ORDER BY 1, 2");
175 is( $result, qq(sub1_tab1|0
176 sub1_tab1|1
177 sub1_tab1|3
178 sub1_tab1|5), 'inserts into tab1 and its partitions replicated');
179
180 $result = $node_subscriber1->safe_psql('postgres',
181 "SELECT a FROM tab1_2_1 ORDER BY 1");
182 is($result, qq(5), 'inserts into tab1_2 replicated into tab1_2_1 correctly');
183
184 $result = $node_subscriber1->safe_psql('postgres',
185 "SELECT a FROM tab1_2_2 ORDER BY 1");
186 is($result, qq(), 'inserts into tab1_2 replicated into tab1_2_2 correctly');
187
188 $result = $node_subscriber2->safe_psql('postgres',
189 "SELECT c, a FROM tab1_1 ORDER BY 1, 2");
190 is( $result, qq(sub2_tab1_1|1
191 sub2_tab1_1|3), 'inserts into tab1_1 replicated');
192
193 $result = $node_subscriber2->safe_psql('postgres',
194 "SELECT c, a FROM tab1_2 ORDER BY 1, 2");
195 is($result, qq(sub2_tab1_2|5), 'inserts into tab1_2 replicated');
196
197 # The AFTER trigger of tab1_2 should have recorded one INSERT.
198 $result = $node_subscriber2->safe_psql('postgres',
199 "SELECT * FROM sub2_trigger_activity ORDER BY tgtab, tgop, tgwhen, olda, newa;"
200 );
201 is( $result,
202 qq(tab1_2|INSERT|AFTER|ROW||5),
203 'check replica insert after trigger applied on subscriber');
204
205 $result = $node_subscriber2->safe_psql('postgres',
206 "SELECT c, a FROM tab1_def ORDER BY 1, 2");
207 is($result, qq(sub2_tab1_def|0), 'inserts into tab1_def replicated');
208
209 # update (replicated as update)
210 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 2 WHERE a = 1");
211 # All of the following cause an update to be applied to a partitioned
212 # table on subscriber1: tab1_2 is leaf partition on publisher, whereas
213 # it's sub-partitioned on subscriber1.
214 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 6 WHERE a = 5");
215 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 4 WHERE a = 6");
216 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 6 WHERE a = 4");
217
218 $node_publisher->wait_for_catchup('sub1');
219 $node_publisher->wait_for_catchup('sub2');
220
221 $result = $node_subscriber1->safe_psql('postgres',
222 "SELECT c, a FROM tab1 ORDER BY 1, 2");
223 is( $result, qq(sub1_tab1|0
224 sub1_tab1|2
225 sub1_tab1|3
226 sub1_tab1|6), 'update of tab1_1, tab1_2 replicated');
227
228 $result = $node_subscriber1->safe_psql('postgres',
229 "SELECT a FROM tab1_2_1 ORDER BY 1");
230 is($result, qq(), 'updates of tab1_2 replicated into tab1_2_1 correctly');
231
232 $result = $node_subscriber1->safe_psql('postgres',
233 "SELECT a FROM tab1_2_2 ORDER BY 1");
234 is($result, qq(6), 'updates of tab1_2 replicated into tab1_2_2 correctly');
235
236 # The AFTER trigger should have recorded the UPDATEs of tab1_2_2.
237 $result = $node_subscriber1->safe_psql('postgres',
238 "SELECT * FROM sub1_trigger_activity ORDER BY tgtab, tgop, tgwhen, olda, newa;"
239 );
240 is( $result, qq(tab1_2_2|INSERT|AFTER|ROW||6
241 tab1_2_2|UPDATE|AFTER|ROW|4|6
242 tab1_2_2|UPDATE|AFTER|ROW|6|4),
243 'check replica update after trigger applied on subscriber');
244
245 $result = $node_subscriber2->safe_psql('postgres',
246 "SELECT c, a FROM tab1_1 ORDER BY 1, 2");
247 is( $result, qq(sub2_tab1_1|2
248 sub2_tab1_1|3), 'update of tab1_1 replicated');
249
250 $result = $node_subscriber2->safe_psql('postgres',
251 "SELECT c, a FROM tab1_2 ORDER BY 1, 2");
252 is($result, qq(sub2_tab1_2|6), 'tab1_2 updated');
253
254 # The AFTER trigger should have recorded the updates of tab1_2.
255 $result = $node_subscriber2->safe_psql('postgres',
256 "SELECT * FROM sub2_trigger_activity ORDER BY tgtab, tgop, tgwhen, olda, newa;"
257 );
258 is( $result, qq(tab1_2|INSERT|AFTER|ROW||5
259 tab1_2|UPDATE|AFTER|ROW|4|6
260 tab1_2|UPDATE|AFTER|ROW|5|6
261 tab1_2|UPDATE|AFTER|ROW|6|4),
262 'check replica update after trigger applied on subscriber');
263
264 $result = $node_subscriber2->safe_psql('postgres',
265 "SELECT c, a FROM tab1_def ORDER BY 1");
266 is($result, qq(sub2_tab1_def|0), 'tab1_def unchanged');
267
268 # update (replicated as delete+insert)
269 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 1 WHERE a = 0");
270 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 4 WHERE a = 1");
271
272 $node_publisher->wait_for_catchup('sub1');
273 $node_publisher->wait_for_catchup('sub2');
274
275 $result = $node_subscriber1->safe_psql('postgres',
276 "SELECT c, a FROM tab1 ORDER BY 1, 2");
277 is( $result, qq(sub1_tab1|2
278 sub1_tab1|3
279 sub1_tab1|4
280 sub1_tab1|6),
281 'update of tab1 (delete from tab1_def + insert into tab1_1) replicated');
282
283 $result = $node_subscriber1->safe_psql('postgres',
284 "SELECT a FROM tab1_2_2 ORDER BY 1");
285 is( $result, qq(4
286 6), 'updates of tab1 (delete + insert) replicated into tab1_2_2 correctly');
287
288 $result = $node_subscriber2->safe_psql('postgres',
289 "SELECT c, a FROM tab1_1 ORDER BY 1, 2");
290 is( $result, qq(sub2_tab1_1|2
291 sub2_tab1_1|3), 'tab1_1 unchanged');
292
293 $result = $node_subscriber2->safe_psql('postgres',
294 "SELECT c, a FROM tab1_2 ORDER BY 1, 2");
295 is( $result, qq(sub2_tab1_2|4
296 sub2_tab1_2|6), 'insert into tab1_2 replicated');
297
298 $result = $node_subscriber2->safe_psql('postgres',
299 "SELECT a FROM tab1_def ORDER BY 1");
300 is($result, qq(), 'delete from tab1_def replicated');
301
302 # delete
303 $node_publisher->safe_psql('postgres',
304 "DELETE FROM tab1 WHERE a IN (2, 3, 5)");
305 $node_publisher->safe_psql('postgres', "DELETE FROM tab1_2");
306
307 $node_publisher->wait_for_catchup('sub1');
308 $node_publisher->wait_for_catchup('sub2');
309
310 $result = $node_subscriber1->safe_psql('postgres', "SELECT a FROM tab1");
311 is($result, qq(), 'delete from tab1_1, tab1_2 replicated');
312
313 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab1_1");
314 is($result, qq(), 'delete from tab1_1 replicated');
315
316 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab1_2");
317 is($result, qq(), 'delete from tab1_2 replicated');
318
319 # truncate
320 $node_subscriber1->safe_psql('postgres',
321 "INSERT INTO tab1 (a) VALUES (1), (2), (5)");
322 $node_subscriber2->safe_psql('postgres', "INSERT INTO tab1_2 (a) VALUES (2)");
323 $node_publisher->safe_psql('postgres', "TRUNCATE tab1_2");
324
325 $node_publisher->wait_for_catchup('sub1');
326 $node_publisher->wait_for_catchup('sub2');
327
328 $result =
329 $node_subscriber1->safe_psql('postgres', "SELECT a FROM tab1 ORDER BY 1");
330 is( $result, qq(1
331 2), 'truncate of tab1_2 replicated');
332
333 $result =
334 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab1_2 ORDER BY 1");
335 is($result, qq(), 'truncate of tab1_2 replicated');
336
337 $node_publisher->safe_psql('postgres', "TRUNCATE tab1");
338
339 $node_publisher->wait_for_catchup('sub1');
340 $node_publisher->wait_for_catchup('sub2');
341
342 $result =
343 $node_subscriber1->safe_psql('postgres', "SELECT a FROM tab1 ORDER BY 1");
344 is($result, qq(), 'truncate of tab1_1 replicated');
345 $result =
346 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab1 ORDER BY 1");
347 is($result, qq(), 'truncate of tab1 replicated');
348
349 $node_publisher->safe_psql('postgres',
350 "INSERT INTO tab1 VALUES (1, 'foo'), (4, 'bar'), (10, 'baz')");
351
352 $node_publisher->wait_for_catchup('sub1');
353 $node_publisher->wait_for_catchup('sub2');
354
355 $node_subscriber1->safe_psql('postgres', "DELETE FROM tab1");
356
357 # Note that the current location of the log file is not grabbed immediately
358 # after reloading the configuration, but after sending one SQL command to
359 # the node so as we are sure that the reloading has taken effect.
360 my $log_location = -s $node_subscriber1->logfile;
361
362 $node_publisher->safe_psql('postgres',
363 "UPDATE tab1 SET b = 'quux' WHERE a = 4");
364 $node_publisher->safe_psql('postgres', "DELETE FROM tab1");
365
366 $node_publisher->wait_for_catchup('sub1');
367 $node_publisher->wait_for_catchup('sub2');
368
369 my $logfile = slurp_file($node_subscriber1->logfile(), $log_location);
370 ok( $logfile =~
371 qr/conflict detected on relation "public.tab1_2_2": conflict=update_missing.*\n.*DETAIL:.* Could not find the row to be updated.*\n.*Remote row \(null, 4, quux\); replica identity \(a\)=\(4\)/,
372 'update target row is missing in tab1_2_2');
373 ok( $logfile =~
374 qr/conflict detected on relation "public.tab1_1": conflict=delete_missing.*\n.*DETAIL:.* Could not find the row to be deleted.*\n.*Replica identity \(a\)=\(1\)/,
375 'delete target row is missing in tab1_1');
376 ok( $logfile =~
377 qr/conflict detected on relation "public.tab1_2_2": conflict=delete_missing.*\n.*DETAIL:.* Could not find the row to be deleted.*\n.*Replica identity \(a\)=\(4\)/,
378 'delete target row is missing in tab1_2_2');
379 ok( $logfile =~
380 qr/conflict detected on relation "public.tab1_def": conflict=delete_missing.*\n.*DETAIL:.* Could not find the row to be deleted.*\n.*Replica identity \(a\)=\(10\)/,
381 'delete target row is missing in tab1_def');
382
383 # Tests for replication using root table identity and schema
384
385 # publisher
386 $node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1");
387 $node_publisher->safe_psql('postgres',
388 "CREATE TABLE tab2 (a int PRIMARY KEY, b text) PARTITION BY LIST (a)");
389 $node_publisher->safe_psql('postgres',
390 "CREATE TABLE tab2_1 (b text, a int NOT NULL)");
391 $node_publisher->safe_psql('postgres',
392 "ALTER TABLE tab2 ATTACH PARTITION tab2_1 FOR VALUES IN (0, 1, 2, 3)");
393 $node_publisher->safe_psql('postgres',
394 "CREATE TABLE tab2_2 PARTITION OF tab2 FOR VALUES IN (5, 6)");
395
396 $node_publisher->safe_psql('postgres',
397 "CREATE TABLE tab3 (a int PRIMARY KEY, b text) PARTITION BY LIST (a)");
398 $node_publisher->safe_psql('postgres',
399 "CREATE TABLE tab3_1 PARTITION OF tab3 FOR VALUES IN (0, 1, 2, 3, 5, 6)");
400
401 $node_publisher->safe_psql('postgres',
402 "CREATE TABLE tab4 (a int PRIMARY KEY) PARTITION BY LIST (a)");
403 $node_publisher->safe_psql('postgres',
404 "CREATE TABLE tab4_1 PARTITION OF tab4 FOR VALUES IN (-1, 0, 1) PARTITION BY LIST (a)"
405 );
406 $node_publisher->safe_psql('postgres',
407 "CREATE TABLE tab4_1_1 PARTITION OF tab4_1 FOR VALUES IN (-1, 0, 1)");
408
409 $node_publisher->safe_psql('postgres',
410 "ALTER PUBLICATION pub_all SET (publish_via_partition_root = true)");
411 # Note: tab3_1's parent is not in the publication, in which case its
412 # changes are published using own identity. For tab2, even though both parent
413 # and child tables are present but changes will be replicated via the parent's
414 # identity and only once.
415 $node_publisher->safe_psql('postgres',
416 "CREATE PUBLICATION pub_viaroot FOR TABLE tab2, tab2_1, tab3_1 WITH (publish_via_partition_root = true)"
417 );
418
419 $node_publisher->safe_psql('postgres',
420 "CREATE PUBLICATION pub_lower_level FOR TABLE tab4_1 WITH (publish_via_partition_root = true)"
421 );
422
423 # prepare data for the initial sync
424 $node_publisher->safe_psql('postgres', "INSERT INTO tab2 VALUES (1)");
425 $node_publisher->safe_psql('postgres', "INSERT INTO tab4 VALUES (-1)");
426
427 # subscriber 1
428 $node_subscriber1->safe_psql('postgres', "DROP SUBSCRIPTION sub1");
429 $node_subscriber1->safe_psql('postgres',
430 "CREATE TABLE tab2 (a int PRIMARY KEY, c text DEFAULT 'sub1_tab2', b text) PARTITION BY RANGE (a)"
431 );
432 $node_subscriber1->safe_psql('postgres',
433 "CREATE TABLE tab2_1 (c text DEFAULT 'sub1_tab2', b text, a int NOT NULL)"
434 );
435 $node_subscriber1->safe_psql('postgres',
436 "ALTER TABLE tab2 ATTACH PARTITION tab2_1 FOR VALUES FROM (0) TO (10)");
437 $node_subscriber1->safe_psql('postgres',
438 "CREATE TABLE tab3_1 (c text DEFAULT 'sub1_tab3_1', b text, a int NOT NULL PRIMARY KEY)"
439 );
440 $node_subscriber1->safe_psql('postgres',
441 "CREATE SUBSCRIPTION sub_viaroot CONNECTION '$publisher_connstr' PUBLICATION pub_viaroot"
442 );
443
444 # subscriber 2
445 $node_subscriber2->safe_psql('postgres', "DROP TABLE tab1");
446 $node_subscriber2->safe_psql('postgres',
447 "CREATE TABLE tab1 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab1', b text) PARTITION BY HASH (a)"
448 );
449 # Note: tab1's partitions are named tab1_1 and tab1_2 on the publisher.
450 $node_subscriber2->safe_psql('postgres',
451 "CREATE TABLE tab1_part1 (b text, c text, a int NOT NULL)");
452 $node_subscriber2->safe_psql('postgres',
453 "ALTER TABLE tab1 ATTACH PARTITION tab1_part1 FOR VALUES WITH (MODULUS 2, REMAINDER 0)"
454 );
455 $node_subscriber2->safe_psql('postgres',
456 "CREATE TABLE tab1_part2 PARTITION OF tab1 FOR VALUES WITH (MODULUS 2, REMAINDER 1)"
457 );
458 $node_subscriber2->safe_psql('postgres',
459 "CREATE TABLE tab2 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab2', b text)"
460 );
461 $node_subscriber2->safe_psql('postgres',
462 "CREATE TABLE tab3 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab3', b text)"
463 );
464 $node_subscriber2->safe_psql('postgres',
465 "CREATE TABLE tab3_1 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab3_1', b text)"
466 );
467
468 # Note: We create two separate tables, not a partitioned one, so that we can
469 # easily identity through which relation were the changes replicated.
470 $node_subscriber2->safe_psql('postgres',
471 "CREATE TABLE tab4 (a int PRIMARY KEY)");
472 $node_subscriber2->safe_psql('postgres',
473 "CREATE TABLE tab4_1 (a int PRIMARY KEY)");
474 # Since we specified publish_via_partition_root in pub_all and
475 # pub_lower_level, all partition tables use their root tables' identity and
476 # schema. We set the list of publications so that the FOR ALL TABLES
477 # publication is second (the list order matters).
478 $node_subscriber2->safe_psql('postgres',
479 "ALTER SUBSCRIPTION sub2 SET PUBLICATION pub_lower_level, pub_all");
480
481 # Wait for initial sync of all subscriptions
482 $node_subscriber1->wait_for_subscription_sync;
483 $node_subscriber2->wait_for_subscription_sync;
484
485 # check that data is synced correctly
486 $result = $node_subscriber1->safe_psql('postgres', "SELECT c, a FROM tab2");
487 is($result, qq(sub1_tab2|1), 'initial data synced for pub_viaroot');
488 $result =
489 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab4 ORDER BY 1");
490 is($result, qq(-1), 'initial data synced for pub_lower_level and pub_all');
491 $result =
492 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab4_1 ORDER BY 1");
493 is($result, qq(), 'initial data synced for pub_lower_level and pub_all');
494
495 # insert
496 $node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (1), (0)");
497 $node_publisher->safe_psql('postgres', "INSERT INTO tab1_1 (a) VALUES (3)");
498 $node_publisher->safe_psql('postgres', "INSERT INTO tab1_2 VALUES (5)");
499 $node_publisher->safe_psql('postgres',
500 "INSERT INTO tab2 VALUES (0), (3), (5)");
501 $node_publisher->safe_psql('postgres',
502 "INSERT INTO tab3 VALUES (1), (0), (3), (5)");
503
504 # Insert a row into the leaf partition, should be replicated through the
505 # partition root (thanks to the FOR ALL TABLES partition).
506 $node_publisher->safe_psql('postgres', "INSERT INTO tab4 VALUES (0)");
507
508 $node_publisher->wait_for_catchup('sub_viaroot');
509 $node_publisher->wait_for_catchup('sub2');
510
511 $result = $node_subscriber1->safe_psql('postgres',
512 "SELECT c, a FROM tab2 ORDER BY 1, 2");
513 is( $result, qq(sub1_tab2|0
514 sub1_tab2|1
515 sub1_tab2|3
516 sub1_tab2|5), 'inserts into tab2 replicated');
517
518 $result = $node_subscriber1->safe_psql('postgres',
519 "SELECT c, a FROM tab3_1 ORDER BY 1, 2");
520 is( $result, qq(sub1_tab3_1|0
521 sub1_tab3_1|1
522 sub1_tab3_1|3
523 sub1_tab3_1|5), 'inserts into tab3_1 replicated');
524
525 $result = $node_subscriber2->safe_psql('postgres',
526 "SELECT c, a FROM tab1 ORDER BY 1, 2");
527 is( $result, qq(sub2_tab1|0
528 sub2_tab1|1
529 sub2_tab1|3
530 sub2_tab1|5), 'inserts into tab1 replicated');
531
532 $result = $node_subscriber2->safe_psql('postgres',
533 "SELECT c, a FROM tab2 ORDER BY 1, 2");
534 is( $result, qq(sub2_tab2|0
535 sub2_tab2|1
536 sub2_tab2|3
537 sub2_tab2|5), 'inserts into tab2 replicated');
538
539 $result = $node_subscriber2->safe_psql('postgres',
540 "SELECT c, a FROM tab3 ORDER BY 1, 2");
541 is( $result, qq(sub2_tab3|0
542 sub2_tab3|1
543 sub2_tab3|3
544 sub2_tab3|5), 'inserts into tab3 replicated');
545
546 # tab4 change should be replicated through the root partition, which
547 # maps to the tab4 relation on subscriber.
548 $result =
549 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab4 ORDER BY 1");
550 is( $result, qq(-1
551 0), 'inserts into tab4 replicated');
552
553 $result =
554 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab4_1 ORDER BY 1");
555 is($result, qq(), 'inserts into tab4_1 replicated');
556
557
558 # now switch the order of publications in the list, try again, the result
559 # should be the same (no dependence on order of publications)
560 $node_subscriber2->safe_psql('postgres',
561 "ALTER SUBSCRIPTION sub2 SET PUBLICATION pub_all, pub_lower_level");
562
563 # make sure the subscription on the second subscriber is synced, before
564 # continuing
565 $node_subscriber2->wait_for_subscription_sync;
566
567 # Insert a change into the leaf partition, should be replicated through
568 # the partition root (thanks to the FOR ALL TABLES partition).
569 $node_publisher->safe_psql('postgres', "INSERT INTO tab4 VALUES (1)");
570
571 $node_publisher->wait_for_catchup('sub2');
572
573 # tab4 change should be replicated through the root partition, which
574 # maps to the tab4 relation on subscriber.
575 $result =
576 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab4 ORDER BY 1");
577 is( $result, qq(-1
578 0
579 1), 'inserts into tab4 replicated');
580
581 $result =
582 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab4_1 ORDER BY 1");
583 is($result, qq(), 'inserts into tab4_1 replicated');
584
585
586 # update (replicated as update)
587 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 6 WHERE a = 5");
588 $node_publisher->safe_psql('postgres', "UPDATE tab2 SET a = 6 WHERE a = 5");
589 $node_publisher->safe_psql('postgres', "UPDATE tab3 SET a = 6 WHERE a = 5");
590
591 $node_publisher->wait_for_catchup('sub_viaroot');
592 $node_publisher->wait_for_catchup('sub2');
593
594 $result = $node_subscriber1->safe_psql('postgres',
595 "SELECT c, a FROM tab2 ORDER BY 1, 2");
596 is( $result, qq(sub1_tab2|0
597 sub1_tab2|1
598 sub1_tab2|3
599 sub1_tab2|6), 'update of tab2 replicated');
600
601 $result = $node_subscriber1->safe_psql('postgres',
602 "SELECT c, a FROM tab3_1 ORDER BY 1, 2");
603 is( $result, qq(sub1_tab3_1|0
604 sub1_tab3_1|1
605 sub1_tab3_1|3
606 sub1_tab3_1|6), 'update of tab3_1 replicated');
607
608 $result = $node_subscriber2->safe_psql('postgres',
609 "SELECT c, a FROM tab1 ORDER BY 1, 2");
610 is( $result, qq(sub2_tab1|0
611 sub2_tab1|1
612 sub2_tab1|3
613 sub2_tab1|6), 'inserts into tab1 replicated');
614
615 $result = $node_subscriber2->safe_psql('postgres',
616 "SELECT c, a FROM tab2 ORDER BY 1, 2");
617 is( $result, qq(sub2_tab2|0
618 sub2_tab2|1
619 sub2_tab2|3
620 sub2_tab2|6), 'inserts into tab2 replicated');
621
622 $result = $node_subscriber2->safe_psql('postgres',
623 "SELECT c, a FROM tab3 ORDER BY 1, 2");
624 is( $result, qq(sub2_tab3|0
625 sub2_tab3|1
626 sub2_tab3|3
627 sub2_tab3|6), 'inserts into tab3 replicated');
628
629 # update (replicated as delete+insert)
630 $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 2 WHERE a = 6");
631 $node_publisher->safe_psql('postgres', "UPDATE tab2 SET a = 2 WHERE a = 6");
632 $node_publisher->safe_psql('postgres', "UPDATE tab3 SET a = 2 WHERE a = 6");
633
634 $node_publisher->wait_for_catchup('sub_viaroot');
635 $node_publisher->wait_for_catchup('sub2');
636
637 $result = $node_subscriber1->safe_psql('postgres',
638 "SELECT c, a FROM tab2 ORDER BY 1, 2");
639 is( $result, qq(sub1_tab2|0
640 sub1_tab2|1
641 sub1_tab2|2
642 sub1_tab2|3), 'update of tab2 replicated');
643
644 $result = $node_subscriber1->safe_psql('postgres',
645 "SELECT c, a FROM tab3_1 ORDER BY 1, 2");
646 is( $result, qq(sub1_tab3_1|0
647 sub1_tab3_1|1
648 sub1_tab3_1|2
649 sub1_tab3_1|3), 'update of tab3_1 replicated');
650
651 $result = $node_subscriber2->safe_psql('postgres',
652 "SELECT c, a FROM tab1 ORDER BY 1, 2");
653 is( $result, qq(sub2_tab1|0
654 sub2_tab1|1
655 sub2_tab1|2
656 sub2_tab1|3), 'update of tab1 replicated');
657
658 $result = $node_subscriber2->safe_psql('postgres',
659 "SELECT c, a FROM tab2 ORDER BY 1, 2");
660 is( $result, qq(sub2_tab2|0
661 sub2_tab2|1
662 sub2_tab2|2
663 sub2_tab2|3), 'update of tab2 replicated');
664
665 $result = $node_subscriber2->safe_psql('postgres',
666 "SELECT c, a FROM tab3 ORDER BY 1, 2");
667 is( $result, qq(sub2_tab3|0
668 sub2_tab3|1
669 sub2_tab3|2
670 sub2_tab3|3), 'update of tab3 replicated');
671
672 # delete
673 $node_publisher->safe_psql('postgres', "DELETE FROM tab1");
674 $node_publisher->safe_psql('postgres', "DELETE FROM tab2");
675 $node_publisher->safe_psql('postgres', "DELETE FROM tab3");
676
677 $node_publisher->wait_for_catchup('sub_viaroot');
678 $node_publisher->wait_for_catchup('sub2');
679
680 $result = $node_subscriber1->safe_psql('postgres', "SELECT a FROM tab2");
681 is($result, qq(), 'delete tab2 replicated');
682
683 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab1");
684 is($result, qq(), 'delete from tab1 replicated');
685
686 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab2");
687 is($result, qq(), 'delete from tab2 replicated');
688
689 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab3");
690 is($result, qq(), 'delete from tab3 replicated');
691
692 # truncate
693 $node_publisher->safe_psql('postgres',
694 "INSERT INTO tab1 VALUES (1), (2), (5)");
695 $node_publisher->safe_psql('postgres',
696 "INSERT INTO tab2 VALUES (1), (2), (5)");
697 # these will NOT be replicated
698 $node_publisher->safe_psql('postgres', "TRUNCATE tab1_2, tab2_1, tab3_1");
699
700 $node_publisher->wait_for_catchup('sub_viaroot');
701 $node_publisher->wait_for_catchup('sub2');
702
703 $result =
704 $node_subscriber1->safe_psql('postgres', "SELECT a FROM tab2 ORDER BY 1");
705 is( $result, qq(1
706 2
707 5), 'truncate of tab2_1 NOT replicated');
708
709 $result =
710 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab1 ORDER BY 1");
711 is( $result, qq(1
712 2
713 5), 'truncate of tab1_2 NOT replicated');
714
715 $result =
716 $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab2 ORDER BY 1");
717 is( $result, qq(1
718 2
719 5), 'truncate of tab2_1 NOT replicated');
720
721 $node_publisher->safe_psql('postgres', "TRUNCATE tab1, tab2, tab3");
722
723 $node_publisher->wait_for_catchup('sub_viaroot');
724 $node_publisher->wait_for_catchup('sub2');
725
726 $result = $node_subscriber1->safe_psql('postgres', "SELECT a FROM tab2");
727 is($result, qq(), 'truncate of tab2 replicated');
728
729 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab1");
730 is($result, qq(), 'truncate of tab1 replicated');
731
732 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab2");
733 is($result, qq(), 'truncate of tab2 replicated');
734
735 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab3");
736 is($result, qq(), 'truncate of tab3 replicated');
737
738 $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab3_1");
739 is($result, qq(), 'truncate of tab3_1 replicated');
740
741 # check that the map to convert tuples from leaf partition to the root
742 # table is correctly rebuilt when a new column is added
743 $node_publisher->safe_psql('postgres',
744 "ALTER TABLE tab2 DROP b, ADD COLUMN c text DEFAULT 'pub_tab2', ADD b text"
745 );
746 $node_publisher->safe_psql('postgres',
747 "INSERT INTO tab2 (a, b) VALUES (1, 'xxx'), (3, 'yyy'), (5, 'zzz')");
748 $node_publisher->safe_psql('postgres',
749 "INSERT INTO tab2 (a, b, c) VALUES (6, 'aaa', 'xxx_c')");
750
751 $node_publisher->wait_for_catchup('sub_viaroot');
752 $node_publisher->wait_for_catchup('sub2');
753
754 $result = $node_subscriber1->safe_psql('postgres',
755 "SELECT c, a, b FROM tab2 ORDER BY 1, 2");
756 is( $result, qq(pub_tab2|1|xxx
757 pub_tab2|3|yyy
758 pub_tab2|5|zzz
759 xxx_c|6|aaa), 'inserts into tab2 replicated');
760
761 $result = $node_subscriber2->safe_psql('postgres',
762 "SELECT c, a, b FROM tab2 ORDER BY 1, 2");
763 is( $result, qq(pub_tab2|1|xxx
764 pub_tab2|3|yyy
765 pub_tab2|5|zzz
766 xxx_c|6|aaa), 'inserts into tab2 replicated');
767
768 $node_subscriber1->safe_psql('postgres', "DELETE FROM tab2");
769
770 # Note that the current location of the log file is not grabbed immediately
771 # after reloading the configuration, but after sending one SQL command to
772 # the node so as we are sure that the reloading has taken effect.
773 $log_location = -s $node_subscriber1->logfile;
774
775 $node_publisher->safe_psql('postgres',
776 "UPDATE tab2 SET b = 'quux' WHERE a = 5");
777 $node_publisher->safe_psql('postgres', "DELETE FROM tab2 WHERE a = 1");
778
779 $node_publisher->wait_for_catchup('sub_viaroot');
780 $node_publisher->wait_for_catchup('sub2');
781
782 $logfile = slurp_file($node_subscriber1->logfile(), $log_location);
783 ok( $logfile =~
784 qr/conflict detected on relation "public.tab2_1": conflict=update_missing.*\n.*DETAIL:.* Could not find the row to be updated.*\n.*Remote row \(pub_tab2, quux, 5\); replica identity \(a\)=\(5\)/,
785 'update target row is missing in tab2_1');
786 ok( $logfile =~
787 qr/conflict detected on relation "public.tab2_1": conflict=delete_missing.*\n.*DETAIL:.* Could not find the row to be deleted.*\n.*Replica identity \(a\)=\(1\)/,
788 'delete target row is missing in tab2_1');
789
790 # Enable the track_commit_timestamp to detect the conflict when attempting
791 # to update a row that was previously modified by a different origin.
792 $node_subscriber1->append_conf('postgresql.conf',
793 'track_commit_timestamp = on');
794 $node_subscriber1->restart;
795
796 $node_subscriber1->safe_psql('postgres',
797 "INSERT INTO tab2 VALUES (3, 'yyy')");
798 $node_publisher->safe_psql('postgres',
799 "UPDATE tab2 SET b = 'quux' WHERE a = 3");
800
801 $node_publisher->wait_for_catchup('sub_viaroot');
802
803 $logfile = slurp_file($node_subscriber1->logfile(), $log_location);
804 ok( $logfile =~
805 qr/conflict detected on relation "public.tab2_1": conflict=update_origin_differs.*\n.*DETAIL:.* Updating the row that was modified locally in transaction [0-9]+ at .*\n.*Existing local row \(yyy, null, 3\); remote row \(pub_tab2, quux, 3\); replica identity \(a\)=\(3\)/,
806 'updating a row that was modified by a different origin');
807
808 # The remaining tests no longer test conflict detection.
809 $node_subscriber1->append_conf('postgresql.conf',
810 'track_commit_timestamp = off');
811 $node_subscriber1->restart;
812
813 # Test that replication continues to work correctly after altering the
814 # partition of a partitioned target table.
815
816 $node_publisher->safe_psql(
817 'postgres', q{
818 CREATE TABLE tab5 (a int NOT NULL, b int);
819 CREATE UNIQUE INDEX tab5_a_idx ON tab5 (a);
820 ALTER TABLE tab5 REPLICA IDENTITY USING INDEX tab5_a_idx;});
821
822 $node_subscriber2->safe_psql(
823 'postgres', q{
824 CREATE TABLE tab5 (a int NOT NULL, b int, c int) PARTITION BY LIST (a);
825 CREATE TABLE tab5_1 PARTITION OF tab5 DEFAULT;
826 CREATE UNIQUE INDEX tab5_a_idx ON tab5 (a);
827 ALTER TABLE tab5 REPLICA IDENTITY USING INDEX tab5_a_idx;
828 ALTER TABLE tab5_1 REPLICA IDENTITY USING INDEX tab5_1_a_idx;});
829
830 $node_subscriber2->safe_psql('postgres',
831 "ALTER SUBSCRIPTION sub2 REFRESH PUBLICATION");
832
833 $node_subscriber2->wait_for_subscription_sync;
834
835 # Make partition map cache
836 $node_publisher->safe_psql('postgres', "INSERT INTO tab5 VALUES (1, 1)");
837 $node_publisher->safe_psql('postgres', "UPDATE tab5 SET a = 2 WHERE a = 1");
838
839 $node_publisher->wait_for_catchup('sub2');
840
841 $result = $node_subscriber2->safe_psql('postgres',
842 "SELECT a, b FROM tab5 ORDER BY 1");
843 is($result, qq(2|1), 'updates of tab5 replicated correctly');
844
845 # Change the column order of partition on subscriber
846 $node_subscriber2->safe_psql(
847 'postgres', q{
848 ALTER TABLE tab5 DETACH PARTITION tab5_1;
849 ALTER TABLE tab5_1 DROP COLUMN b;
850 ALTER TABLE tab5_1 ADD COLUMN b int;
851 ALTER TABLE tab5 ATTACH PARTITION tab5_1 DEFAULT});
852
853 $node_publisher->safe_psql('postgres', "UPDATE tab5 SET a = 3 WHERE a = 2");
854
855 $node_publisher->wait_for_catchup('sub2');
856
857 $result = $node_subscriber2->safe_psql('postgres',
858 "SELECT a, b, c FROM tab5 ORDER BY 1");
859 is($result, qq(3|1|),
860 'updates of tab5 replicated correctly after altering table on subscriber'
861 );
862
863 # Test that replication into the partitioned target table continues to
864 # work correctly when the published table is altered.
865 $node_publisher->safe_psql(
866 'postgres', q{
867 ALTER TABLE tab5 DROP COLUMN b, ADD COLUMN c INT;
868 ALTER TABLE tab5 ADD COLUMN b INT;});
869
870 $node_publisher->safe_psql('postgres', "UPDATE tab5 SET c = 1 WHERE a = 3");
871
872 $node_publisher->wait_for_catchup('sub2');
873
874 $result = $node_subscriber2->safe_psql('postgres',
875 "SELECT a, b, c FROM tab5 ORDER BY 1");
876 is($result, qq(3||1),
877 'updates of tab5 replicated correctly after altering table on publisher');
878
879 # Test that replication works correctly as long as the leaf partition
880 # has the necessary REPLICA IDENTITY, even though the actual target
881 # partitioned table does not.
882 $node_subscriber2->safe_psql('postgres',
883 "ALTER TABLE tab5 REPLICA IDENTITY NOTHING");
884
885 $node_publisher->safe_psql('postgres', "UPDATE tab5 SET a = 4 WHERE a = 3");
886
887 $node_publisher->wait_for_catchup('sub2');
888
889 $result = $node_subscriber2->safe_psql('postgres',
890 "SELECT a, b, c FROM tab5_1 ORDER BY 1");
891 is($result, qq(4||1), 'updates of tab5 replicated correctly');
892
893 done_testing();