Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
# Copyright (c) 2025-2026, PostgreSQL Global Development Group
#
# Test that effective_wal_level changes upon logical replication slot creation
# and deletion.
 
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
# Check both wal_level and effective_wal_level values on the given node
# are expected.
sub test_wal_level
{
    my ($node, $expected, $msg) = @_;
 
    is( $node->safe_psql(
            'postgres',
            qq[select current_setting('wal_level'), current_setting('effective_wal_level');]
        ),
        "$expected",
        "$msg");
}
 
# Wait for the checkpointer to decrease effective_wal_level to 'replica'.
sub wait_for_logical_decoding_disabled
{
    my ($node) = @_;
 
    $node->poll_query_until('postgres',
        qq[select current_setting('effective_wal_level') = 'replica';]);
}
 
# Close a background psql session and wait for its server backend to finish
# exit processing so that its local injection point names can be safely reused.
sub quit_and_wait
{
    my ($node, $background_psql, $backend_pid) = @_;
 
    die "invalid backend PID: $backend_pid" unless $backend_pid =~ /^\d+$/;
    $background_psql->quit;
    $node->poll_query_until('postgres',
        qq[select count(*) = 0 from pg_stat_activity where pid = $backend_pid]
    ) or die "timed out waiting for backend $backend_pid to exit";
}
 
# Initialize the primary server with wal_level = 'replica'.
my $primary = PostgreSQL::Test::Cluster->new('primary');
$primary->init(allows_streaming => 1);
$primary->append_conf('postgresql.conf', "log_min_messages = debug1");
$primary->start();
 
# Check both initial wal_level and effective_wal_level values.
test_wal_level($primary, "replica|replica",
    "wal_level and effective_wal_level start with the same value 'replica'");
 
# Create a physical slot and verify that it doesn't affect effective_wal_level.
$primary->safe_psql('postgres',
    qq[select pg_create_physical_replication_slot('test_phy_slot', false, false)]
);
test_wal_level($primary, "replica|replica",
    "effective_wal_level doesn't change with a new physical slot");
$primary->safe_psql('postgres',
    qq[select pg_drop_replication_slot('test_phy_slot')]);
 
# Create a temporary logical slot but exit without releasing it explicitly.
# This enables logical decoding but skips disabling it and delegates to the
# checkpointer.
$primary->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('test_tmp_slot', 'test_decoding', true)]
);
ok( $primary->log_contains(
        "logical decoding is enabled upon creating a new logical replication slot"
    ),
    "logical decoding has been enabled upon creating a temp slot");
 
# Wait for the checkpointer to disable logical decoding.
wait_for_logical_decoding_disabled($primary);
 
# Test that logical decoding is disabled after repack
$primary->safe_psql('postgres', qq[create table foo(a int primary key)]);
$primary->safe_psql('postgres', qq[repack (concurrently) foo;]);
ok( $primary->log_contains(
        "logical decoding is enabled upon creating a new logical replication slot"
    ),
    "logical decoding enabled by repack");
 
# Wait for the checkpointer to disable logical decoding.
wait_for_logical_decoding_disabled($primary);
test_wal_level($primary, "replica|replica",
    "logical decoding disabled after repack");
 
# Test that a backend applies an XLogLogicalInfo update that it received
# while its transaction block was in the failed state, once the block ends.
# The failed transaction keeps its XID until ROLLBACK, so the update is
# deferred; it must not be carried over into the next transaction.
my $psql_aborted = $primary->background_psql('postgres', on_error_stop => 0);
$psql_aborted->query_safe(q[begin; select pg_current_xact_id();]);
my ($aborted_out, $aborted_ret) = $psql_aborted->query(q[select 1/0;]);
is($aborted_ret, 1, "transaction block failed");
$psql_aborted->{stderr} = '';
 
# Enable logical decoding while the backend is idle in the failed transaction
# block.  This waits for all backends to absorb the barrier.
$primary->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('test_aborted_slot', 'test_decoding')]
);
 
# Check the value in the transaction right after ROLLBACK.  Note that both
# commands need to be sent together, as query_safe() appends an empty query
# that would run in its own transaction and apply the deferred update.
is( $psql_aborted->query_safe(
        q[rollback; select current_setting('effective_wal_level');]),
    'logical',
    "effective_wal_level is updated after rolling back a failed transaction block"
);
$psql_aborted->quit;
 
$primary->safe_psql('postgres',
    qq[select pg_drop_replication_slot('test_aborted_slot')]);
wait_for_logical_decoding_disabled($primary);
 
# Create a new logical slot and check that effective_wal_level must be increased
# to 'logical'.
$primary->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('test_slot', 'pgoutput')]);
test_wal_level($primary, "replica|logical",
    "effective_wal_level increased to 'logical' upon a logical slot creation"
);
 
# Restart the server and check again.
$primary->restart();
test_wal_level($primary, "replica|logical",
    "effective_wal_level remains 'logical' even after a server restart");
 
# Create and drop another logical slot, then verify that effective_wal_level remains
# 'logical'.
$primary->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('test_slot2', 'pgoutput')]);
$primary->safe_psql('postgres',
    qq[select pg_drop_replication_slot('test_slot2')]);
test_wal_level($primary, "replica|logical",
    "effective_wal_level stays 'logical' as one slot remains");
 
# Verify that the server cannot start with wal_level='minimal' when there is
# at least one replication slot.
$primary->adjust_conf('postgresql.conf', 'wal_level', 'minimal');
$primary->adjust_conf('postgresql.conf', 'max_wal_senders', '0');
$primary->stop;
 
command_fails(
    [
        'pg_ctl',
        '--pgdata' => $primary->data_dir,
        '--log' => $primary->logfile,
        'start',
    ],
    "cannot start server with wal_level='minimal' as there is in-use logical slot"
);
 
my $logfile = slurp_file($primary->logfile());
like(
    $logfile,
    qr/logical replication slot "test_slot" exists, but "wal_level" < "replica"/,
    'logical slots requires logical decoding enabled at server startup');
 
# Revert the modified settings.
$primary->adjust_conf('postgresql.conf', 'wal_level', 'replica');
$primary->adjust_conf('postgresql.conf', 'max_wal_senders', '10');
 
# Add other settings to test if we disable logical decoding when invalidating the last
# logical slot.
$primary->append_conf(
    'postgresql.conf',
    qq[
min_wal_size = 32MB
max_wal_size = 32MB
max_slot_wal_keep_size = 16MB
]);
$primary->start;
 
# Advance WAL and verify that the slot gets invalidated.
$primary->advance_wal(2);
$primary->safe_psql('postgres', qq[CHECKPOINT]);
is( $primary->safe_psql(
        'postgres',
        qq[
select invalidation_reason = 'wal_removed' from pg_replication_slots where slot_name = 'test_slot';
                 ]),
    't',
    'test_slot gets invalidated due to wal_removed');
 
# Verify that logical decoding is disabled after invalidating the last logical slot.
wait_for_logical_decoding_disabled($primary);
test_wal_level($primary, "replica|replica",
    "effective_wal_level got decreased to 'replica' after invalidating the last logical slot"
);
 
# Revert the modified settings, and restart the server.
$primary->adjust_conf('postgresql.conf', 'max_slot_wal_keep_size', undef);
$primary->adjust_conf('postgresql.conf', 'min_wal_size', undef);
$primary->adjust_conf('postgresql.conf', 'max_wal_size', undef);
$primary->restart;
 
# Recreate the logical slot to enable logical decoding again.
$primary->safe_psql('postgres',
    qq[select pg_drop_replication_slot('test_slot')]);
$primary->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('test_slot', 'pgoutput')]);
 
# Take backup during the effective_wal_level being 'logical'. But note that
# replication slots are not included in the backup.
$primary->backup('my_backup');
 
# Initialize standby1 node.
my $standby1 = PostgreSQL::Test::Cluster->new('standby1');
$standby1->init_from_backup($primary, 'my_backup', has_streaming => 1);
$standby1->start;
 
# Creating a logical slot on standby should succeed as the primary enables
# it.
$primary->wait_for_replay_catchup($standby1);
$standby1->create_logical_slot_on_standby($primary, 'standby1_slot',
    'postgres');
 
# Promote the standby1 node that has one logical slot. So effective_wal_level
# remains 'logical' even after the promotion.
$standby1->promote;
test_wal_level($standby1, "replica|logical",
    "effective_wal_level remains 'logical' even after the promotion");
 
# Confirm if we can create a logical slot after the promotion.
$standby1->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('standby1_slot2', 'pgoutput')]
);
$standby1->stop;
 
# Initialize standby2 node and start it with wal_level = 'logical'.
my $standby2 = PostgreSQL::Test::Cluster->new('standby2');
$standby2->init_from_backup($primary, 'my_backup', has_streaming => 1);
$standby2->append_conf('postgresql.conf', qq[wal_level = 'logical']);
$standby2->start();
$standby2->backup('my_backup3');
 
# Initialize cascade standby and start with wal_level = 'replica'.
my $cascade = PostgreSQL::Test::Cluster->new('cascade');
$cascade->init_from_backup($standby2, 'my_backup3', has_streaming => 1);
$cascade->adjust_conf('postgresql.conf', 'wal_level', 'replica');
$cascade->start();
 
# Regardless of their wal_level values, effective_wal_level values on the
# standby and the cascaded standby depend on the primary's value, 'logical'.
test_wal_level($standby2, "logical|logical",
    "check wal_level and effective_wal_level on standby");
test_wal_level($cascade, "replica|logical",
    "check wal_level and effective_wal_level on cascaded standby");
 
# Drop the primary's last logical slot, decreasing effective_wal_level to
# 'replica' on all nodes.
$primary->safe_psql('postgres',
    qq[select pg_drop_replication_slot('test_slot')]);
wait_for_logical_decoding_disabled($primary);
 
$primary->wait_for_replay_catchup($standby2);
$standby2->wait_for_replay_catchup($cascade, $primary);
 
test_wal_level($primary, "replica|replica",
    "effective_wal_level got decreased to 'replica' on primary");
test_wal_level($standby2, "logical|replica",
    "effective_wal_level got decreased to 'replica' on standby");
test_wal_level($cascade, "replica|replica",
    "effective_wal_level got decreased to 'replica' on cascaded standby");
 
# Promote standby2, increasing effective_wal_level to 'logical' as its wal_level
# is set to 'logical'.
$standby2->promote;
 
# Verify that effective_wal_level is increased to 'logical' on the cascaded standby.
$standby2->wait_for_replay_catchup($cascade);
test_wal_level($cascade, "replica|logical",
    "effective_wal_level got increased to 'logical' on standby as the new primary has wal_level='logical'"
);
 
$standby2->stop;
$cascade->stop;
 
# Initialize standby3 node and start it.
my $standby3 = PostgreSQL::Test::Cluster->new('standby3');
$standby3->init_from_backup($primary, 'my_backup', has_streaming => 1);
$standby3->start;
 
# Create logical slots on both nodes.
$primary->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('test_slot', 'pgoutput')]);
$primary->wait_for_replay_catchup($standby3);
$standby3->create_logical_slot_on_standby($primary, 'standby3_slot',
    'postgres');
 
# Drop the logical slot from the primary, decreasing effective_wal_level to
# 'replica' on the primary, which leads to invalidating the logical slot on the
# standby due to 'wal_level_insufficient'.
$primary->safe_psql('postgres',
    qq[select pg_drop_replication_slot('test_slot')]);
wait_for_logical_decoding_disabled($primary);
test_wal_level($primary, "replica|replica",
    "effective_wal_level got decreased to 'replica' on the primary to invalidate standby's slots"
);
$standby3->poll_query_until(
    'postgres', qq[
select invalidation_reason = 'wal_level_insufficient' from pg_replication_slots where slot_name = 'standby3_slot'
                ]);
 
# Restart the server to verify that the slot is successfully restored during
# startup.
$standby3->restart;
 
# Check that the logical decoding is not enabled on the standby3. Note that it still has
# the invalidated logical slot.
test_wal_level($standby3, "replica|replica",
    "effective_wal_level got decreased to 'replica' on standby");
 
my ($result, $stdout, $stderr) = $standby3->psql('postgres',
    qq[select pg_logical_slot_get_changes('standby3_slot', null, null)]);
like(
    $stderr,
    qr/ERROR:  logical decoding on standby requires "effective_wal_level" >= "logical" on the primary/,
    "cannot use logical decoding on standby as it is disabled on primary");
 
# Restart the primary with setting wal_level = 'logical' and create a new logical
# slot.
$primary->append_conf('postgresql.conf', qq[wal_level = 'logical']);
$primary->restart;
$primary->safe_psql('postgres',
    qq[select pg_create_logical_replication_slot('test_slot', 'pgoutput')]);
 
# effective_wal_level should be 'logical' on both nodes.
$primary->wait_for_replay_catchup($standby3);
test_wal_level($primary, "logical|logical",
    "check WAL levels on the primary node");
test_wal_level($standby3, "replica|logical",
    "effective_wal_level got increased to 'logical' again on standby");
 
# Set wal_level to 'replica' and restart the primary. Since one logical slot
# is still present on the primary, effective_wal_level remains 'logical' even
# if wal_level got decreased to 'replica'.
$primary->adjust_conf('postgresql.conf', 'wal_level', 'replica');
$primary->restart;
$primary->wait_for_replay_catchup($standby3);
 
# Verify that the effective_wal_level remains 'logical' on both nodes
test_wal_level($primary, "replica|logical",
    "effective_wal_level remains 'logical' on primary even after setting wal_level to 'replica'"
);
test_wal_level($standby3, "replica|logical",
    "effective_wal_level remains 'logical' on standby even after setting wal_level to 'replica' on primary"
);
 
# Promote the standby3 and verify that effective_wal_level got decreased to
# 'replica' after the promotion since there is no valid logical slot.
$standby3->promote;
test_wal_level($standby3, "replica|replica",
    "effective_wal_level got decreased to 'replica' as there is no valid logical slot"
);
 
# Cleanup the invalidated slot.
$standby3->safe_psql('postgres',
    qq[select pg_drop_replication_slot('standby3_slot')]);
 
$standby3->stop;
 
if (   $ENV{enable_injection_points} eq 'yes'
    && $primary->check_extension('injection_points'))
{
    # Test the race condition at end of the recovery between the startup and logical
    # decoding status change. This test requires injection points enabled.
 
    # Initialize standby4 and start it.
    my $standby4 = PostgreSQL::Test::Cluster->new('standby4');
    $standby4->init_from_backup($primary, 'my_backup', has_streaming => 1);
    $standby4->start;
 
    # Both servers have one logical slot.
    $primary->wait_for_replay_catchup($standby4);
    $standby4->create_logical_slot_on_standby($primary, 'standby4_slot',
        'postgres');
 
    # Enable and attach the injection point on the standby4.
    $primary->safe_psql('postgres', 'create extension injection_points');
    $primary->wait_for_replay_catchup($standby4);
    $standby4->safe_psql('postgres',
        qq[select injection_points_attach('startup-logical-decoding-status-change-end-of-recovery', 'wait');]
    );
 
    # Trigger promotion with no wait, and wait for the startup process to reach
    # the injection point.
    $standby4->safe_psql('postgres', qq[select pg_promote(false)]);
    note('promote the standby and waiting for injection_point');
    $standby4->wait_for_event('startup',
        'startup-logical-decoding-status-change-end-of-recovery');
    note(
        "injection_point 'startup-logical-decoding-status-change-end-of-recovery' is reached"
    );
 
    # Drop the logical slot, requesting to disable logical decoding to the checkpointer.
    $standby4->safe_psql('postgres',
        qq[select pg_drop_replication_slot('standby4_slot');]);
 
    # Resume the startup process to complete the recovery.
    $standby4->safe_psql('postgres',
        qq[select injection_points_wakeup('startup-logical-decoding-status-change-end-of-recovery')]
    );
 
    # Verify that logical decoding got disabled after the recovery.
    wait_for_logical_decoding_disabled($standby4);
    test_wal_level($standby4, "replica|replica",
        "effective_wal_level properly got decreased to 'replica'");
    $standby4->stop;
 
    # Test the abort process of logical decoding activation. We drop the primary's
    # slot to decrease its effective_wal_level to 'replica'.
    $primary->safe_psql('postgres',
        qq[select pg_drop_replication_slot('test_slot')]);
    wait_for_logical_decoding_disabled($primary);
    test_wal_level($primary, "replica|replica",
        "effective_wal_level got decreased to 'replica' on primary");
 
    # Test that concurrent activations don't write redundant status-change records.
 
    # Start a psql session and stop it in the middle of the activation process.
    my $psql_create_slot = $primary->background_psql('postgres');
    my $psql_create_slot_pid =
      $psql_create_slot->query_safe('select pg_backend_pid()');
    $psql_create_slot->query_until(
        qr/create_slot_1/,
        q(\echo create_slot_1
select injection_points_set_local();
select injection_points_attach('logical-decoding-activation', 'wait');
select pg_create_logical_replication_slot('slot_1', 'test_decoding');
));
    $primary->wait_for_event('client backend', 'logical-decoding-activation');
    note("injection_point 'logical-decoding-activation' is reached");
 
    # A second backend concurrently enables logical decoding and finishes creating
    # its slot, writing the status-change record. The slot reserves its decoding
    # start point after its own status-change record.
    $primary->safe_psql('postgres',
        qq[select pg_create_logical_replication_slot('slot_2', 'test_decoding')]
    );
    test_wal_level($primary, "replica|logical",
        "logical decoding enabled by the first of two concurrent activations"
    );
 
    # Resume the first backend to complete the slot creation. It must not write
    # a second redundant status-change record as logical decoding is already
    # enabled.
    $primary->safe_psql('postgres',
        qq[select injection_points_wakeup('logical-decoding-activation')]);
 
    # Let the released backend finish creating its slot.
    quit_and_wait($primary, $psql_create_slot, $psql_create_slot_pid);
 
    # Decode from slot_2, whose start point precedes where a redundant
    # status-change record would have been written; this fails in xlog_decode()
    # if one exists.
    is( $primary->safe_psql(
            'postgres',
            qq[SELECT count(*) FROM pg_logical_slot_get_changes('slot_2', NULL, NULL, 'skip-empty-xacts', '1')]
        ),
        0,
        'decoding a concurrently-created slot succeeds');
 
    # Restore the disabled state for the tests that follow.
    $primary->safe_psql(
        'postgres',
        qq[
select pg_drop_replication_slot('slot_1');
select pg_drop_replication_slot('slot_2');
]);
    wait_for_logical_decoding_disabled($primary);
 
    # Test a race when logical decoding activation is concurrently interrupted.
 
    # Start a psql session to test the case where the activation process is
    # interrupted.
    $psql_create_slot =
      $primary->background_psql('postgres', on_error_stop => 0);
    $psql_create_slot_pid =
      $psql_create_slot->query_safe('select pg_backend_pid()');
 
    # Start the logical decoding activation process upon creating the logical
    # slot, but it will wait due to the injection point.
    $psql_create_slot->query_until(
        qr/create_slot_canceled/,
        q(\echo create_slot_canceled
select injection_points_set_local();
select injection_points_attach('logical-decoding-activation', 'wait');
select pg_create_logical_replication_slot('slot_canceled', 'pgoutput');
));
 
    $primary->wait_for_event('client backend', 'logical-decoding-activation');
    note("injection_point 'logical-decoding-activation' is reached");
 
    # Cancel the backend initiated by $psql_create_slot, aborting its activation
    # process.
    $primary->safe_psql(
        'postgres',
        qq[
select pg_cancel_backend(pid) from pg_stat_activity where query ~ 'slot_canceled' and pid <> pg_backend_pid()
]);
 
    # Verify that the backend aborted the activation process.
    $primary->wait_for_log("aborting logical decoding activation process");
    wait_for_logical_decoding_disabled($primary);
    pass("the activation process aborted");
 
    quit_and_wait($primary, $psql_create_slot, $psql_create_slot_pid);
 
    # Test concurrent activation processes run and one is interrupted.
    $psql_create_slot =
      $primary->background_psql('postgres', on_error_stop => 0);
    $psql_create_slot_pid =
      $psql_create_slot->query_safe('select pg_backend_pid()');
 
    # Start a psql session and stops in the middle of the activation
    # process.
    $psql_create_slot->query_until(
        qr/create_slot_canceled/,
        q(\echo create_slot_canceled
select injection_points_set_local();
select injection_points_attach('logical-decoding-activation', 'wait');
select pg_create_logical_replication_slot('slot_canceled2', 'pgoutput');
));
    $primary->wait_for_event('client backend', 'logical-decoding-activation');
    note("injection_point 'logical-decoding-activation' is reached");
 
    # Another backend concurrently enables logical decoding.
    $primary->safe_psql('postgres',
        qq[select pg_create_logical_replication_slot('test_slot2', 'pgoutput')]
    );
 
    # Concurrent slot creation should not be blocked. So wait until
    # test_slot2 is created and logical decoding is enabled.
    test_wal_level($primary, "replica|logical",
        "the concurrent activation has done properly");
 
    # Cancel the backend initiated by $psql_create_slot, aborting its activation
    # process.
    my $log_offset = -s $primary->logfile;
    $primary->safe_psql(
        'postgres',
        qq[
select pg_cancel_backend(pid) from pg_stat_activity where query ~ 'slot_canceled2' and pid <> pg_backend_pid()
]);
    # Canceling the backend should not affect the concurrent slot creation.
    $primary->wait_for_log("canceling statement due to user request",
        $log_offset);
    test_wal_level($primary, "replica|logical",
        "effective_wal_level remains 'logical' even after the concurrent activation is interrupted"
    );
 
    quit_and_wait($primary, $psql_create_slot, $psql_create_slot_pid);
 
    # Test races between the deactivation of logical decoding and slot
    # creation during recovery. Logical decoding can be deactivated while a
    # logical slot is being created on a standby, either by replaying an
    # XLOG_LOGICAL_DECODING_STATUS_CHANGE record or by the end-of-recovery
    # transition upon promotion. Slot creation paths must detect the
    # deactivation after creating their slot and fail cleanly, instead
    # of leaving a slot that cannot be decoded.
 
    # Restore the disabled state, and disable autovacuum so that the primary
    # assigns no XIDs, keeping the slot synchronization test below
    # deterministic.
    $primary->safe_psql('postgres',
        qq[select pg_drop_replication_slot('test_slot2')]);
    wait_for_logical_decoding_disabled($primary);
    $primary->safe_psql(
        'postgres', qq[
alter system set autovacuum = off;
select pg_reload_conf();
]);
 
    # Initialize standby5 node, setting up the requirements for slot
    # synchronization.
    $primary->safe_psql('postgres',
        qq[select pg_create_physical_replication_slot('phys_slot')]);
    my $standby5 = PostgreSQL::Test::Cluster->new('standby5');
    $standby5->init_from_backup($primary, 'my_backup', has_streaming => 1);
    my $connstr = $primary->connstr;
    $standby5->append_conf(
        'postgresql.conf', qq[
primary_slot_name = 'phys_slot'
primary_conninfo = '$connstr dbname=postgres'
hot_standby_feedback = on
]);
    $standby5->start;
 
    # Test that slot synchronization doesn't persist a slot whose remote
    # slot information was fetched before a deactivation was replayed.
    # Otherwise, the slot would survive with a restart_lsn preceding the
    # deactivation, as the slot invalidation performed by the replay cannot
    # find the slot that is not created yet.
 
    # Enable logical decoding by creating a failover-enabled slot, and wait
    # for the standby to replay the activation.
    $primary->safe_psql('postgres',
        qq[select pg_create_logical_replication_slot('sync_slot', 'test_decoding', false, false, true)]
    );
    $primary->wait_for_replay_catchup($standby5);
    test_wal_level($standby5, "replica|logical",
        "logical decoding got activated on standby5");
 
    # Start slot synchronization, stopping it after fetching the remote slot
    # information but before creating the local slot.
    my $psql_sync_slot = $standby5->background_psql('postgres');
    my $psql_sync_slot_pid =
      $psql_sync_slot->query_safe('select pg_backend_pid()');
    $psql_sync_slot->query_until(
        qr/sync_slots/,
        q(\echo sync_slots
select injection_points_set_local();
select injection_points_attach('replication-slot-create-begin', 'wait');
select pg_sync_replication_slots();
));
    $standby5->wait_for_event('client backend',
        'replication-slot-create-begin');
    note("injection_point 'replication-slot-create-begin' is reached");
 
    # Drop the last logical slot on the primary and wait for the standby to
    # replay the deactivation.
    $primary->safe_psql('postgres',
        qq[select pg_drop_replication_slot('sync_slot')]);
    wait_for_logical_decoding_disabled($primary);
    $primary->wait_for_replay_catchup($standby5);
    test_wal_level($standby5, "replica|replica",
        "logical decoding got deactivated on standby5");
 
    # Resume the slot synchronization; it must skip persisting the slot.
    $log_offset = -s $standby5->logfile;
    $standby5->safe_psql('postgres',
        qq[select injection_points_wakeup('replication-slot-create-begin')]);
    $standby5->wait_for_log(
        qr/could not synchronize replication slot "sync_slot"/, $log_offset);
    quit_and_wait($standby5, $psql_sync_slot, $psql_sync_slot_pid);
    is( $standby5->safe_psql(
            'postgres', qq[select count(*) from pg_replication_slots]),
        '0',
        "no synced slot is left behind on standby5");
 
    # Test the same race, but where the slot is re-created on the primary
    # before the slot synchronization resumes. Logical decoding is enabled
    # again at the time the local slot is created, so checking the logical
    # decoding status alone cannot tell that the remote slot information
    # predates the deactivation.
 
    $primary->safe_psql('postgres',
        qq[select pg_create_logical_replication_slot('sync_slot', 'test_decoding', false, false, true)]
    );
    $primary->wait_for_replay_catchup($standby5);
    test_wal_level($standby5, "replica|logical",
        "logical decoding got activated on standby5 for the re-creation test"
    );
 
    $psql_sync_slot = $standby5->background_psql('postgres');
    $psql_sync_slot_pid =
      $psql_sync_slot->query_safe('select pg_backend_pid()');
    $psql_sync_slot->query_until(
        qr/sync_slots/,
        q(\echo sync_slots
select injection_points_set_local();
select injection_points_attach('replication-slot-create-begin', 'wait');
select pg_sync_replication_slots();
));
    $standby5->wait_for_event('client backend',
        'replication-slot-create-begin');
    note("injection_point 'replication-slot-create-begin' is reached");
 
    # Drop and re-create the slot, and wait for the standby to replay both
    # the deactivation and the activation.
    $primary->safe_psql('postgres',
        qq[select pg_drop_replication_slot('sync_slot')]);
    wait_for_logical_decoding_disabled($primary);
    $primary->safe_psql('postgres',
        qq[select pg_create_logical_replication_slot('sync_slot', 'test_decoding', false, false, true)]
    );
    my $restart_lsn = $primary->safe_psql('postgres',
        qq[select restart_lsn from pg_replication_slots where slot_name = 'sync_slot']
    );
    $primary->wait_for_replay_catchup($standby5);
    test_wal_level($standby5, "replica|logical",
        "logical decoding got deactivated and activated again on standby5");
 
    # Resume the slot synchronization. It must drop the slot created from
    # the stale information, and re-create it from the re-created remote slot
    # on retry.
    $log_offset = -s $standby5->logfile;
    $standby5->safe_psql(
        'postgres', qq[
select injection_points_detach('replication-slot-create-begin');
select injection_points_wakeup('replication-slot-create-begin');
]);
    $standby5->wait_for_log(
        qr/could not synchronize replication slot "sync_slot".*\n.*DETAIL:  Logical decoding was disabled after the remote slot's restart LSN/,
        $log_offset);
    $standby5->poll_query_until('postgres',
        qq[select restart_lsn >= '$restart_lsn' from pg_replication_slots where slot_name = 'sync_slot']
      )
      or die
      "timed out waiting for the slot to be re-created from the re-created remote slot";
 
    # Drop the remote slot so that the slot synchronization has nothing left
    # to retry, keeping logical decoding enabled with another slot as the
    # slot synchronization requires it.
    $primary->safe_psql(
        'postgres', qq[
select pg_create_logical_replication_slot('test_slot4', 'test_decoding');
select pg_drop_replication_slot('sync_slot');
]);
    $primary->wait_for_replay_catchup($standby5);
    quit_and_wait($standby5, $psql_sync_slot, $psql_sync_slot_pid);
 
    # The slot created on retry may or may not have been persisted by the
    # time the remote slot was dropped. One more synchronization cycle
    # drops the local slot either way.
    $standby5->safe_psql('postgres', qq[select pg_sync_replication_slots()]);
 
    $primary->safe_psql('postgres',
        qq[select pg_drop_replication_slot('test_slot4')]);
    wait_for_logical_decoding_disabled($primary);
    $primary->wait_for_replay_catchup($standby5);
    is( $standby5->safe_psql(
            'postgres', qq[select count(*) from pg_replication_slots]),
        '0',
        "no synced slot is left behind on standby5 after the re-creation test"
    );
 
    # Test that logical slot creation on a standby fails cleanly if logical
    # decoding is concurrently deactivated by the end-of-recovery transition
    # upon promotion, which cannot find the slot that is not created yet.
 
    # Enable logical decoding again.
    $primary->safe_psql('postgres',
        qq[select pg_create_logical_replication_slot('test_slot3', 'test_decoding')]
    );
    $primary->wait_for_replay_catchup($standby5);
    test_wal_level($standby5, "replica|logical",
        "logical decoding got activated again on standby5");
 
    # Hold the startup process right after the end-of-recovery status
    # change, before recovery actually ends.
    $standby5->safe_psql('postgres',
        qq[select injection_points_attach('startup-logical-decoding-status-change-end-of-recovery', 'wait')]
    );
 
    # Start creating a logical slot, stopping it before creating the slot.
    $psql_create_slot =
      $standby5->background_psql('postgres', on_error_stop => 0);
    $psql_create_slot_pid =
      $psql_create_slot->query_safe('select pg_backend_pid()');
    $psql_create_slot->query_until(
        qr/create_standby5_slot/,
        q(\echo create_standby5_slot
select injection_points_set_local();
select injection_points_attach('replication-slot-create-begin', 'wait');
select pg_create_logical_replication_slot('standby5_slot', 'test_decoding');
));
    $standby5->wait_for_event('client backend',
        'replication-slot-create-begin');
    note("injection_point 'replication-slot-create-begin' is reached");
 
    # Promote the standby without waiting; the startup process deactivates
    # logical decoding as no logical slot, and stops at the injection point,
    # still in recovery.
    $standby5->safe_psql('postgres', qq[select pg_promote(false)]);
    $standby5->wait_for_event('startup',
        'startup-logical-decoding-status-change-end-of-recovery');
 
    # Resume the slot creation; it must fail cleanly.
    $log_offset = -s $standby5->logfile;
    $standby5->safe_psql('postgres',
        qq[select injection_points_wakeup('replication-slot-create-begin')]);
    $standby5->wait_for_log(
        qr/ERROR: .* logical decoding on standby requires "effective_wal_level" >= "logical" on the primary/,
        $log_offset);
    quit_and_wait($standby5, $psql_create_slot, $psql_create_slot_pid);
    is( $standby5->safe_psql(
            'postgres', qq[select count(*) from pg_replication_slots]),
        '0',
        "no slot is left behind on standby5");
 
    # Let the promotion complete.
    $standby5->safe_psql('postgres',
        qq[select injection_points_wakeup('startup-logical-decoding-status-change-end-of-recovery')]
    );
    $standby5->safe_psql('postgres',
        qq[select injection_points_detach('startup-logical-decoding-status-change-end-of-recovery')]
    );
    $standby5->poll_query_until('postgres',
        qq[select not pg_is_in_recovery()])
      or die "timed out waiting for promotion";
 
    # Retrying the slot creation on the promoted standby5 must succeed and
    # activate logical decoding.
    $standby5->safe_psql('postgres',
        qq[select pg_create_logical_replication_slot('standby5_slot', 'test_decoding')]
    );
    test_wal_level($standby5, "replica|logical",
        "logical decoding got activated on the promoted standby5 after retry"
    );
}
 
$primary->stop;
 
done_testing();