Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
 
# Copyright (c) 2021-2026, PostgreSQL Global Development Group
 
# Tests for various bugs found over time
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
# Bug #15114
 
# The bug was that determining which columns are part of the replica
# identity index using RelationGetIndexAttrBitmap() would run
# eval_const_expressions() on index expressions and predicates across
# all indexes of the table, which in turn might require a snapshot,
# but there wasn't one set, so it crashes.  There were actually two
# separate bugs, one on the publisher and one on the subscriber.  The
# fix was to avoid the constant expressions simplification in
# RelationGetIndexAttrBitmap(), so it's safe to call in more contexts.
 
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
$node_publisher->init(allows_streaming => 'logical');
$node_publisher->start;
 
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
$node_subscriber->init;
$node_subscriber->start;
 
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
 
$node_publisher->safe_psql('postgres',
    "CREATE TABLE tab1 (a int PRIMARY KEY, b int)");
 
$node_publisher->safe_psql('postgres',
    "CREATE FUNCTION double(x int) RETURNS int IMMUTABLE LANGUAGE SQL AS 'select x * 2'"
);
 
# an index with a predicate that lends itself to constant expressions
# evaluation
$node_publisher->safe_psql('postgres',
    "CREATE INDEX ON tab1 (b) WHERE a > double(1)");
 
# and the same setup on the subscriber
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE tab1 (a int PRIMARY KEY, b int)");
 
$node_subscriber->safe_psql('postgres',
    "CREATE FUNCTION double(x int) RETURNS int IMMUTABLE LANGUAGE SQL AS 'select x * 2'"
);
 
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX ON tab1 (b) WHERE a > double(1)");
 
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION pub1 FOR ALL TABLES");
 
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1"
);
 
$node_publisher->wait_for_catchup('sub1');
 
# This would crash, first on the publisher, and then (if the publisher
# is fixed) on the subscriber.
$node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (1, 2)");
 
$node_publisher->wait_for_catchup('sub1');
 
pass('index predicates do not cause crash');
 
# We'll re-use these nodes below, so drop their replication state.
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub1");
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1");
# Drop the tables too.
$node_publisher->safe_psql('postgres', "DROP TABLE tab1");
 
$node_publisher->stop('fast');
$node_subscriber->stop('fast');
 
 
# Handling of temporary and unlogged tables with FOR ALL TABLES publications
 
# If a FOR ALL TABLES publication exists, temporary and unlogged
# tables are ignored for publishing changes.  The bug was that we
# would still check in that case that such a table has a replica
# identity set before accepting updates.  If it did not it would cause
# an error when an update was attempted.
 
$node_publisher->rotate_logfile();
$node_publisher->start();
 
# Although we don't use node_subscriber in this test, keep its logfile
# name in step with node_publisher for later tests.
$node_subscriber->rotate_logfile();
 
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION pub FOR ALL TABLES");
 
is( $node_publisher->psql(
        'postgres',
        "CREATE TEMPORARY TABLE tt1 AS SELECT 1 AS a; UPDATE tt1 SET a = 2;"),
    0,
    'update to temporary table without replica identity with FOR ALL TABLES publication'
);
 
is( $node_publisher->psql(
        'postgres',
        "CREATE UNLOGGED TABLE tu1 AS SELECT 1 AS a; UPDATE tu1 SET a = 2;"),
    0,
    'update to unlogged table without replica identity with FOR ALL TABLES publication'
);
 
# Again, drop replication state but not tables.
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub");
 
$node_publisher->stop('fast');
 
 
# Bug #16643 - https://postgr.es/m/16643-eaadeb2a1a58d28c@postgresql.org
#
# Initial sync doesn't complete; the protocol was not being followed per
# expectations after commit 07082b08cc5d.
my $node_twoways = PostgreSQL::Test::Cluster->new('twoways');
$node_twoways->init(allows_streaming => 'logical');
$node_twoways->start;
for my $db (qw(d1 d2))
{
    $node_twoways->safe_psql('postgres', "CREATE DATABASE $db");
    $node_twoways->safe_psql($db, "CREATE TABLE t (f int)");
    $node_twoways->safe_psql($db, "CREATE TABLE t2 (f int)");
}
 
my $rows = 3000;
$node_twoways->safe_psql(
    'd1', qq{
    INSERT INTO t SELECT * FROM generate_series(1, $rows);
    INSERT INTO t2 SELECT * FROM generate_series(1, $rows);
    CREATE PUBLICATION testpub FOR TABLE t;
    SELECT pg_create_logical_replication_slot('testslot', 'pgoutput');
    });
 
$node_twoways->safe_psql('d2',
        "CREATE SUBSCRIPTION testsub CONNECTION \$\$"
      . $node_twoways->connstr('d1')
      . "\$\$ PUBLICATION testpub WITH (create_slot=false, "
      . "slot_name='testslot')");
$node_twoways->safe_psql(
    'd1', qq{
    INSERT INTO t SELECT * FROM generate_series(1, $rows);
    INSERT INTO t2 SELECT * FROM generate_series(1, $rows);
    });
$node_twoways->safe_psql('d1', 'ALTER PUBLICATION testpub ADD TABLE t2');
$node_twoways->safe_psql('d2',
    'ALTER SUBSCRIPTION testsub REFRESH PUBLICATION');
 
# We cannot rely solely on wait_for_catchup() here; it isn't sufficient
# when tablesync workers might still be running. So in addition to that,
# verify that tables are synced.
$node_twoways->wait_for_subscription_sync($node_twoways, 'testsub', 'd2');
 
is($node_twoways->safe_psql('d2', "SELECT count(f) FROM t"),
    $rows * 2, "2x$rows rows in t");
is($node_twoways->safe_psql('d2', "SELECT count(f) FROM t2"),
    $rows * 2, "2x$rows rows in t2");
 
# Verify table data is synced with cascaded replication setup. This is mainly
# to test whether the data written by tablesync worker gets replicated.
my $node_pub = PostgreSQL::Test::Cluster->new('testpublisher1');
$node_pub->init(allows_streaming => 'logical');
$node_pub->start;
 
my $node_pub_sub = PostgreSQL::Test::Cluster->new('testpublisher_subscriber');
$node_pub_sub->init(allows_streaming => 'logical');
$node_pub_sub->start;
 
my $node_sub = PostgreSQL::Test::Cluster->new('testsubscriber1');
$node_sub->init;
$node_sub->start;
 
# Create the tables in all nodes.
$node_pub->safe_psql('postgres', "CREATE TABLE tab1 (a int)");
$node_pub_sub->safe_psql('postgres', "CREATE TABLE tab1 (a int)");
$node_sub->safe_psql('postgres', "CREATE TABLE tab1 (a int)");
 
# Create a cascaded replication setup like:
# N1 - Create publication testpub1.
# N2 - Create publication testpub2 and also include subscriber which subscribes
#      to testpub1.
# N3 - Create subscription testsub2 subscribes to testpub2.
#
# Note that subscription on N3 needs to be created before subscription on N2 to
# test whether the data written by tablesync worker of N2 gets replicated.
$node_pub->safe_psql('postgres',
    "CREATE PUBLICATION testpub1 FOR TABLE tab1");
 
$node_pub_sub->safe_psql('postgres',
    "CREATE PUBLICATION testpub2 FOR TABLE tab1");
 
my $publisher1_connstr = $node_pub->connstr . ' dbname=postgres';
my $publisher2_connstr = $node_pub_sub->connstr . ' dbname=postgres';
 
$node_sub->safe_psql('postgres',
    "CREATE SUBSCRIPTION testsub2 CONNECTION '$publisher2_connstr' PUBLICATION testpub2"
);
 
$node_pub_sub->safe_psql('postgres',
    "CREATE SUBSCRIPTION testsub1 CONNECTION '$publisher1_connstr' PUBLICATION testpub1"
);
 
$node_pub->safe_psql('postgres',
    "INSERT INTO tab1 values(generate_series(1,10))");
 
# Verify that the data is cascaded from testpub1 to testsub1 and further from
# testpub2 (which had testsub1) to testsub2.
$node_pub->wait_for_catchup('testsub1');
$node_pub_sub->wait_for_catchup('testsub2');
 
# Drop subscriptions as we don't need them anymore
$node_pub_sub->safe_psql('postgres', "DROP SUBSCRIPTION testsub1");
$node_sub->safe_psql('postgres', "DROP SUBSCRIPTION testsub2");
 
# Drop publications as we don't need them anymore
$node_pub->safe_psql('postgres', "DROP PUBLICATION testpub1");
$node_pub_sub->safe_psql('postgres', "DROP PUBLICATION testpub2");
 
# Clean up the tables on both publisher and subscriber as we don't need them
$node_pub->safe_psql('postgres', "DROP TABLE tab1");
$node_pub_sub->safe_psql('postgres', "DROP TABLE tab1");
$node_sub->safe_psql('postgres', "DROP TABLE tab1");
 
$node_pub->stop('fast');
$node_pub_sub->stop('fast');
$node_sub->stop('fast');
 
# https://postgr.es/m/OS0PR01MB61133CA11630DAE45BC6AD95FB939%40OS0PR01MB6113.jpnprd01.prod.outlook.com
 
# The bug was that when changing the REPLICA IDENTITY INDEX to another one, the
# target table's relcache was not being invalidated. This leads to skipping
# UPDATE/DELETE operations during apply on the subscriber side as the columns
# required to search corresponding rows won't get logged.
 
$node_publisher->rotate_logfile();
$node_publisher->start();
 
$node_subscriber->rotate_logfile();
$node_subscriber->start();
 
$node_publisher->safe_psql('postgres',
    "CREATE TABLE tab_replidentity_index(a int not null, b int not null)");
$node_publisher->safe_psql('postgres',
    "CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)"
);
$node_publisher->safe_psql('postgres',
    "CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)"
);
 
# use index idx_replidentity_index_a as REPLICA IDENTITY on publisher.
$node_publisher->safe_psql('postgres',
    "ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_a"
);
 
$node_publisher->safe_psql('postgres',
    "INSERT INTO tab_replidentity_index VALUES(1, 1),(2, 2)");
 
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE tab_replidentity_index(a int not null, b int not null)");
$node_subscriber->safe_psql('postgres',
    "CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)"
);
$node_subscriber->safe_psql('postgres',
    "CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)"
);
# use index idx_replidentity_index_b as REPLICA IDENTITY on subscriber because
# it reflects the future scenario we are testing: changing REPLICA IDENTITY
# INDEX.
$node_subscriber->safe_psql('postgres',
    "ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b"
);
 
$publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub FOR TABLE tab_replidentity_index");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
);
 
# Wait for initial table sync to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
 
is( $node_subscriber->safe_psql(
        'postgres', "SELECT * FROM tab_replidentity_index"),
    qq(1|1
2|2),
    "check initial data on subscriber");
 
# Set REPLICA IDENTITY to idx_replidentity_index_b on publisher, then run UPDATE and DELETE.
$node_publisher->safe_psql(
    'postgres', qq[
    ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b;
    UPDATE tab_replidentity_index SET a = -a WHERE a = 1;
    DELETE FROM tab_replidentity_index WHERE a = 2;
]);
 
$node_publisher->wait_for_catchup('tap_sub');
is( $node_subscriber->safe_psql(
        'postgres', "SELECT * FROM tab_replidentity_index"),
    qq(-1|1),
    "update works with REPLICA IDENTITY");
 
# Clean up
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub");
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub");
$node_publisher->safe_psql('postgres', "DROP TABLE tab_replidentity_index");
$node_subscriber->safe_psql('postgres', "DROP TABLE tab_replidentity_index");
 
# Test schema invalidation by renaming the schema
 
# Create tables on publisher
$node_publisher->safe_psql('postgres', "CREATE SCHEMA sch1");
$node_publisher->safe_psql('postgres', "CREATE TABLE sch1.t1 (c1 int)");
 
# Create tables on subscriber
$node_subscriber->safe_psql('postgres', "CREATE SCHEMA sch1");
$node_subscriber->safe_psql('postgres', "CREATE TABLE sch1.t1 (c1 int)");
$node_subscriber->safe_psql('postgres', "CREATE SCHEMA sch2");
$node_subscriber->safe_psql('postgres', "CREATE TABLE sch2.t1 (c1 int)");
 
# Setup logical replication that will cover t1 under both schema names
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_sch FOR ALL TABLES");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_sch CONNECTION '$publisher_connstr' PUBLICATION tap_pub_sch"
);
 
# Wait for initial table sync to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub_sch');
 
# Check what happens to data inserted before and after schema rename
$node_publisher->safe_psql(
    'postgres',
    "begin;
insert into sch1.t1 values(1);
alter schema sch1 rename to sch2;
create schema sch1;
create table sch1.t1(c1 int);
insert into sch1.t1 values(2);
insert into sch2.t1 values(3);
commit;");
 
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub_sch');
 
# Subscriber's sch1.t1 should receive the row inserted into the new sch1.t1,
# but not the row inserted into the old sch1.t1 post-rename.
my $result = $node_subscriber->safe_psql('postgres', "SELECT * FROM sch1.t1");
is( $result, qq(1
2), 'check data in subscriber sch1.t1 after schema rename');
 
# Subscriber's sch2.t1 won't have gotten anything yet ...
$result = $node_subscriber->safe_psql('postgres', "SELECT * FROM sch2.t1");
is($result, '', 'no data yet in subscriber sch2.t1 after schema rename');
 
# ... but it should show up after REFRESH.
$node_subscriber->safe_psql('postgres',
    'ALTER SUBSCRIPTION tap_sub_sch REFRESH PUBLICATION');
 
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub_sch');
 
$result = $node_subscriber->safe_psql('postgres', "SELECT * FROM sch2.t1");
is( $result, qq(1
3), 'check data in subscriber sch2.t1 after schema rename');
 
# Again, drop replication state but not tables.
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_sch");
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_sch");
 
$node_publisher->stop('fast');
$node_subscriber->stop('fast');
 
# The bug was that when the REPLICA IDENTITY FULL is used with dropped
# we fail to apply updates and deletes
$node_publisher->rotate_logfile();
$node_publisher->start();
 
$node_subscriber->rotate_logfile();
$node_subscriber->start();
 
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE TABLE dropped_cols (a int, b_drop int, c int);
    ALTER TABLE dropped_cols REPLICA IDENTITY FULL;
    CREATE PUBLICATION pub_dropped_cols FOR TABLE dropped_cols;
    -- some initial data
    INSERT INTO dropped_cols VALUES (1, 1, 1);
));
 
$node_subscriber->safe_psql(
    'postgres', qq(
     CREATE TABLE dropped_cols (a int, b_drop int, c int);
));
 
$publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION sub_dropped_cols CONNECTION '$publisher_connstr' PUBLICATION pub_dropped_cols"
);
$node_subscriber->wait_for_subscription_sync;
 
$node_publisher->safe_psql(
    'postgres', qq(
        ALTER TABLE dropped_cols DROP COLUMN b_drop;
));
$node_subscriber->safe_psql(
    'postgres', qq(
        ALTER TABLE dropped_cols DROP COLUMN b_drop;
));
 
$node_publisher->safe_psql(
    'postgres', qq(
        UPDATE dropped_cols SET a = 100;
));
$node_publisher->wait_for_catchup('sub_dropped_cols');
 
is( $node_subscriber->safe_psql(
        'postgres', "SELECT count(*) FROM dropped_cols WHERE a = 100"),
    qq(1),
    'replication with RI FULL and dropped columns');
 
$node_publisher->stop('fast');
$node_subscriber->stop('fast');
 
# The bug was that pgoutput was incorrectly replacing missing attributes in
# tuples with NULL. This could result in incorrect replication with
# `REPLICA IDENTITY FULL`.
 
$node_publisher->rotate_logfile();
$node_publisher->start();
 
$node_subscriber->rotate_logfile();
$node_subscriber->start();
 
# Set up a table with schema `(a int, b bool)` where the `b` attribute is
# missing for one row due to the `ALTER TABLE ... ADD COLUMN ... DEFAULT`
# fast path.
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE TABLE tab_default (a int);
    ALTER TABLE tab_default REPLICA IDENTITY FULL;
    INSERT INTO tab_default VALUES (1);
    ALTER TABLE tab_default ADD COLUMN b bool DEFAULT false NOT NULL;
    INSERT INTO tab_default VALUES (2, true);
    CREATE PUBLICATION pub1 FOR TABLE tab_default;
));
 
# Replicate to the subscriber.
$node_subscriber->safe_psql(
    'postgres', qq(
    CREATE TABLE tab_default (a int, b bool);
    CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1;
));
 
$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub1');
$result =
  $node_subscriber->safe_psql('postgres', "SELECT a, b FROM tab_default");
is( $result, qq(1|f
2|t), 'check snapshot on subscriber');
 
# Update all rows in the table and ensure the rows with the missing `b`
# attribute replicate correctly.
$node_publisher->safe_psql('postgres', "UPDATE tab_default SET a = a + 1");
$node_publisher->wait_for_catchup('sub1');
 
# When the bug is present, the `1|f` row will not be updated to `2|f` because
# the publisher incorrectly fills in `NULL` for `b` and publishes an update
# for `1|NULL`, which doesn't exist in the subscriber.
$result =
  $node_subscriber->safe_psql('postgres', "SELECT a, b FROM tab_default");
is( $result, qq(2|f
3|t), 'check replicated update on subscriber');
 
my $publisher_host = $node_publisher->host;
my $publisher_port = $node_publisher->port;
my $connstr_db =
  "host=$publisher_host port=$publisher_port replication=database dbname=postgres";
 
# Test create and immediate drop of replication slot via replication commands
# (this exposed a memory-management bug in v18)
is( $node_publisher->psql(
        'postgres',
        qq[
        CREATE_REPLICATION_SLOT test_slot LOGICAL pgoutput (SNAPSHOT export);
        DROP_REPLICATION_SLOT test_slot;
    ],
        timeout => $PostgreSQL::Test::Utils::timeout_default,
        extra_params => [ '-d', $connstr_db ]),
    0,
    'create and immediate drop of replication slot');
 
# REPLICATION users should not be able to bypass LOAD restrictions.
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE USER repluser REPLICATION;
));
 
my ($ret, $stdout, $stderr) = $node_publisher->psql(
    'postgres',
    qq[
        SET ROLE repluser;
        CREATE_REPLICATION_SLOT fail_slot LOGICAL regress;',
    ],
    timeout => $PostgreSQL::Test::Utils::timeout_default,
    extra_params => [ '-d', $connstr_db ]);
 
is($ret, 3, 'loading unblessed output plugin fails');
like(
    $stderr,
    qr/ERROR:  library "regress" may not be used as an output plugin/,
    'loading unblessed output plugin fails: stderr');
 
$node_publisher->stop('fast');
$node_subscriber->stop('fast');
 
# The bug was that when an ERROR was caught and handled by a (PL/pgSQL)
# function, the apply worker reset the replication origin but continued
# processing subsequent changes. So, we fail to update the replication origin
# during further apply operations. This can lead to the apply worker requesting
# the changes that have been applied again after restarting.
 
$node_publisher->rotate_logfile();
$node_publisher->start();
 
$node_subscriber->rotate_logfile();
$node_subscriber->start();
 
# Set up a publication with a table
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE TABLE t1 (a int);
    CREATE PUBLICATION regress_pub FOR TABLE t1;
));
 
# Set up a subscription which subscribes the publication
$node_subscriber->safe_psql(
    'postgres', qq(
    CREATE TABLE t1 (a int);
    CREATE SUBSCRIPTION regress_sub CONNECTION '$publisher_connstr' PUBLICATION regress_pub;
));
 
$node_subscriber->wait_for_subscription_sync($node_publisher, 'regress_sub');
 
# Create an AFTER INSERT trigger on the table that raises and subsequently
# handles an exception. Subsequent insertions will trigger this exception,
# causing the apply worker to invoke its error callback with an ERROR. However,
# since the error is caught within the trigger, the apply worker will continue
# processing changes.
$node_subscriber->safe_psql(
    'postgres', q{
CREATE FUNCTION handle_exception_trigger()
RETURNS TRIGGER AS $$
BEGIN
    BEGIN
        -- Raise an exception
        RAISE EXCEPTION 'This is a test exception';
    EXCEPTION
        WHEN OTHERS THEN
            RETURN NEW;
    END;
 
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;
 
CREATE TRIGGER silent_exception_trigger
AFTER INSERT OR UPDATE ON t1
FOR EACH ROW
EXECUTE FUNCTION handle_exception_trigger();
 
ALTER TABLE t1 ENABLE ALWAYS TRIGGER silent_exception_trigger;
});
 
# Obtain current remote_lsn value to check its advancement later
my $remote_lsn = $node_subscriber->safe_psql('postgres',
    "SELECT remote_lsn FROM pg_replication_origin_status os, pg_subscription s WHERE os.external_id = 'pg_' || s.oid AND s.subname = 'regress_sub'"
);
 
# Insert a tuple to replicate changes
$node_publisher->safe_psql('postgres', "INSERT INTO t1 VALUES (1);");
$node_publisher->wait_for_catchup('regress_sub');
 
# Confirms the origin can be advanced
$result = $node_subscriber->safe_psql('postgres',
    "SELECT remote_lsn > '$remote_lsn' FROM pg_replication_origin_status os, pg_subscription s WHERE os.external_id = 'pg_' || s.oid AND s.subname = 'regress_sub'"
);
is($result, 't',
    'remote_lsn has advanced for apply worker raising an exception');
 
$node_publisher->stop('fast');
$node_subscriber->stop('fast');
 
# BUG #18988
# The bug happened due to a self-deadlock between the DROP SUBSCRIPTION
# command and the walsender process for accessing pg_subscription. This
# occurred when DROP SUBSCRIPTION attempted to remove a replication slot by
# connecting to a newly created database whose caches are not yet
# initialized.
#
# The bug is fixed by reducing the lock-level during DROP SUBSCRIPTION.
$node_publisher->start();
 
$publisher_connstr = $node_publisher->connstr . ' dbname=regress_db';
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE DATABASE regress_db;
    CREATE SUBSCRIPTION regress_sub1 CONNECTION '$publisher_connstr' PUBLICATION regress_pub WITH (connect=false);
));
 
($ret, $stdout, $stderr) =
  $node_publisher->psql('postgres', q{DROP SUBSCRIPTION regress_sub1});
 
isnt($ret, 0, "replication slot does not exist: exit code not 0");
like(
    $stderr,
    qr/ERROR:  could not drop replication slot "regress_sub1" on publisher/,
    "could not drop replication slot: error message");
 
$node_publisher->safe_psql('postgres', "DROP DATABASE regress_db");
 
$node_publisher->stop('fast');
 
# https://postgr.es/m/19c7623e882.4080fd5426212.311756747309556767%40zohocorp.com
 
# The bug was that when an ERROR was raised while processing an INSERT ... ON
# CONFLICT statement, the decoded change misses to be free'd. This can cause an
# assertion failure if enabled.
 
$node_publisher->rotate_logfile();
$node_publisher->start();
 
# Create a publication with the zero-division row filter. It always throws an
# ERROR before publishing changes, when the filter is evaluated.
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE TABLE tab_upsert (a INT PRIMARY KEY, b INT);
    CREATE PUBLICATION pub_rowfilter_error FOR TABLE tab_upsert WHERE ((a / 0) > 0);
    SELECT * FROM pg_create_logical_replication_slot('upsert_slot', 'pgoutput');
    INSERT INTO tab_upsert (a, b) VALUES (1, 1)
        ON CONFLICT(a) DO UPDATE SET b = excluded.b;
));
 
# Decode the changes with a publication whose row filter causes a
# division by zero error, and verify that the logical decoder doesn't crash.
($ret, $stdout, $stderr) = $node_publisher->psql(
    'postgres', qq(
    SELECT *
    FROM pg_logical_slot_peek_binary_changes(
        'upsert_slot',
        NULL,
        NULL,
        'proto_version', '1',
        'publication_names', 'pub_rowfilter_error'
    );
));
 
ok( $stderr =~ qr/division by zero/,
    'peek logical changes with row filter causing division by zero throws error'
);
 
# Clean up
$node_publisher->safe_psql('postgres',
    "SELECT pg_drop_replication_slot('upsert_slot')");
$node_publisher->safe_psql('postgres',
    "DROP PUBLICATION pub_rowfilter_error");
$node_publisher->safe_psql('postgres', "DROP TABLE tab_upsert");
 
$node_publisher->stop('fast');
 
# Start clusters for tests with injection points. Rotate log files for making
# debugging easier.
$node_publisher->rotate_logfile();
$node_publisher->start;
$node_subscriber->rotate_logfile();
$node_subscriber->start;
 
SKIP:
{
    skip "injection points not supported by this build", 4
      if $node_subscriber->check_extension('injection_points') == 0;
 
    # Test that relations which must never be a logical replication target are
    # rejected.  A PostgreSQL publisher cannot name them in the first place, so
    # simulate one that does by rewriting the relation of the RELATION message
    # as the apply worker receives it.
 
    # Use a separate relation per case below.  The publisher sends a RELATION
    # message for a given relation only once per session, so a second case on
    # the same relation would never reach the injection point.
    $node_publisher->safe_psql(
        'postgres', qq{
CREATE TABLE tab_target_rel1 (a int);
CREATE TABLE tab_target_rel2 (a int);
CREATE TABLE tab_target_rel3 (a int);
CREATE PUBLICATION pub_target_rel
  FOR TABLE tab_target_rel1, tab_target_rel2, tab_target_rel3;
    });
 
    $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
    $node_subscriber->safe_psql(
        'postgres', qq{
CREATE EXTENSION IF NOT EXISTS injection_points;
CREATE TABLE tab_target_rel1 (a int);
CREATE TABLE tab_target_rel2 (a int);
CREATE TABLE tab_target_rel3 (a int);
CREATE TABLE tab_toasted (a text);
CREATE MATERIALIZED VIEW matview_target AS SELECT 1 AS a;
CREATE SUBSCRIPTION sub_target_rel
  CONNECTION '$publisher_connstr'
  PUBLICATION pub_target_rel
  WITH (copy_data = false);
    });
    $node_subscriber->wait_for_subscription_sync($node_publisher,
        'sub_target_rel');
 
    # The TOAST table of an ordinary table will do as a target.
    my $toast_table = $node_subscriber->safe_psql(
        'postgres',
        "SELECT reltoastrelid::regclass::text FROM pg_class
           WHERE oid = 'tab_toasted'::regclass");
    foreach my $case (
        [ 'tab_target_rel1', 'pg_catalog.pg_class', 'system tables' ],
        [ 'tab_target_rel2', $toast_table, 'TOAST tables' ],
        [ 'tab_target_rel3', 'public.matview_target', 'materialized views' ])
    {
        my ($table, $target, $detail) = @$case;
 
        $node_subscriber->safe_psql(
            'postgres', qq{
SELECT injection_points_attach('logical-replication-relation-received',
  'injection_points', 'logicalrep_relation_redirect', '$target');
        });
 
        my $log_offset = -s $node_subscriber->logfile;
        $node_publisher->safe_psql('postgres',
            "INSERT INTO $table VALUES (1)");
 
        $node_subscriber->wait_for_log(
            qr/cannot use relation "\Q$target\E" as logical replication target/,
            $log_offset);
        $node_subscriber->wait_for_log(
            qr/This operation is not supported for \Q$detail\E/, $log_offset);
        pass("$detail are rejected as logical replication targets");
 
        # Once the relation is no longer rewritten, the apply worker retries
        # and gets past the change it failed on.
        $node_subscriber->safe_psql('postgres',
            "SELECT injection_points_detach('logical-replication-relation-received')"
        );
        $node_publisher->wait_for_catchup('sub_target_rel');
    }
 
    # Cleanup
    $node_subscriber->safe_psql(
        'postgres', qq{
DROP SUBSCRIPTION sub_target_rel;
DROP MATERIALIZED VIEW matview_target;
DROP TABLE tab_target_rel1, tab_target_rel2, tab_target_rel3, tab_toasted;
    });
    $node_publisher->safe_psql(
        'postgres', qq{
DROP PUBLICATION pub_target_rel;
DROP TABLE tab_target_rel1, tab_target_rel2, tab_target_rel3;
    });
 
    $node_publisher->stop('fast');
    $node_subscriber->stop('fast');
 
    # Test that ALTER SUBSCRIPTION ... REFRESH PUBLICATION skips a subscribed
    # relation that is dropped concurrently during the refresh.
 
    $node_publisher->rotate_logfile();
    $node_publisher->start;
    $node_subscriber->rotate_logfile();
    $node_subscriber->start;
 
    # Subscribe to a table and a sequence.
    $node_publisher->safe_psql(
        'postgres', qq{
CREATE TABLE tab_drop_refresh (a int);
CREATE SEQUENCE seq_drop_refresh;
CREATE PUBLICATION pub_drop_refresh FOR TABLE tab_drop_refresh;
CREATE PUBLICATION pub_seq_drop_refresh FOR ALL SEQUENCES;
    });
 
    $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
    $node_subscriber->safe_psql(
        'postgres', qq{
CREATE EXTENSION IF NOT EXISTS injection_points;
CREATE TABLE tab_drop_refresh (a int);
CREATE SEQUENCE seq_drop_refresh;
CREATE SUBSCRIPTION sub_drop_refresh
  CONNECTION '$publisher_connstr'
  PUBLICATION pub_drop_refresh, pub_seq_drop_refresh
  WITH (copy_data = false, origin = none);
    });
    $node_subscriber->wait_for_subscription_sync($node_publisher,
        'sub_drop_refresh');
 
    $node_publisher->safe_psql(
        'postgres',
        qq{
ALTER PUBLICATION pub_drop_refresh DROP TABLE tab_drop_refresh;
DROP SEQUENCE seq_drop_refresh;
    });
 
    # Pause the refresh after it collects the relation list, drop the table
    # and the sequence, then wake it.
    $node_subscriber->safe_psql('postgres',
        qq{SELECT injection_points_attach('subscription-refresh-before-origin-check', 'wait');}
    );
 
    my $psql = $node_subscriber->background_psql('postgres');
    $psql->query_until(
        qr/starting_refresh/, q{
        \echo starting_refresh
        ALTER SUBSCRIPTION sub_drop_refresh REFRESH PUBLICATION;
    });
 
    $node_subscriber->wait_for_event('client backend',
        'subscription-refresh-before-origin-check');
 
    $node_subscriber->safe_psql(
        'postgres', qq{
DROP TABLE tab_drop_refresh;
DROP SEQUENCE seq_drop_refresh;
    });
 
    $node_subscriber->safe_psql('postgres',
        qq{SELECT injection_points_wakeup('subscription-refresh-before-origin-check');}
    );
 
    # quit() returns false unless psql exited cleanly, which it does not if the
    # refresh errored out or the backend crashed.
    ok($psql->quit, 'refresh completed without crashing the server');
 
    $node_subscriber->safe_psql(
        'postgres', qq{
SELECT injection_points_detach('subscription-refresh-before-origin-check');
DROP SUBSCRIPTION sub_drop_refresh;
    });
    $node_publisher->safe_psql('postgres',
        qq{DROP PUBLICATION pub_drop_refresh, pub_seq_drop_refresh;});
}
 
$node_publisher->stop('fast');
$node_subscriber->stop('fast');
 
done_testing();