Skip to content

Commit a4309e8

Browse files
Restrict copying of invalidated replication slots.
Previously, invalidated logical and physical replication slots could be copied using the pg_copy_logical_replication_slot and pg_copy_physical_replication_slot functions. Replication slots that were invalidated for reasons other than WAL removal retained their restart_lsn. This meant that a new slot copied from an invalidated slot could have a restart_lsn pointing to a WAL segment that might have already been removed. This commit restricts the copying of invalidated replication slots. Backpatch to v16, where slots could retain their restart_lsn when invalidated for reasons other than WAL removal. For v15 and earlier, this check is not required since slots can only be invalidated due to WAL removal, and existing checks already handle this issue. Author: Shlok Kyal <shlok.kyal.oss@gmail.com> Reviewed-by: vignesh C <vignesh21@gmail.com> Reviewed-by: Zhijie Hou <houzj.fnst@fujitsu.com> Reviewed-by: Peter Smith <smithpb2250@gmail.com> Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Discussion: https://postgr.es/m/CANhcyEU65aH0VYnLiu%3DOhNNxhnhNhwcXBeT-jvRe1OiJTo_Ayg%40mail.gmail.com Backpatch-through: 16
1 parent e019112 commit a4309e8

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

doc/src/sgml/func.sgml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29095,7 +29095,8 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
2909529095
The copied physical slot starts to reserve WAL from the same <acronym>LSN</acronym> as the
2909629096
source slot.
2909729097
<parameter>temporary</parameter> is optional. If <parameter>temporary</parameter>
29098-
is omitted, the same value as the source slot is used.
29098+
is omitted, the same value as the source slot is used. Copy of an
29099+
invalidated slot is not allowed.
2909929100
</para></entry>
2910029101
</row>
2910129102

@@ -29120,7 +29121,8 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
2912029121
The <literal>failover</literal> option of the source logical slot
2912129122
is not copied and is set to <literal>false</literal> by default. This
2912229123
is to avoid the risk of being unable to continue logical replication
29123-
after failover to standby where the slot is being synchronized.
29124+
after failover to standby where the slot is being synchronized. Copy of
29125+
an invalidated slot is not allowed.
2912429126
</para></entry>
2912529127
</row>
2912629128

src/backend/replication/slotfuncs.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,13 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
681681
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
682682
errmsg("cannot copy a replication slot that doesn't reserve WAL")));
683683

684+
/* Cannot copy an invalidated replication slot */
685+
if (first_slot_contents.data.invalidated != RS_INVAL_NONE)
686+
ereport(ERROR,
687+
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
688+
errmsg("cannot copy invalidated replication slot \"%s\"",
689+
NameStr(*src_name)));
690+
684691
/* Overwrite params from optional arguments */
685692
if (PG_NARGS() >= 3)
686693
temporary = PG_GETARG_BOOL(2);
@@ -782,6 +789,20 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
782789
NameStr(*src_name)),
783790
errhint("Retry when the source replication slot's confirmed_flush_lsn is valid.")));
784791

792+
/*
793+
* Copying an invalid slot doesn't make sense. Note that the source
794+
* slot can become invalid after we create the new slot and copy the
795+
* data of source slot. This is possible because the operations in
796+
* InvalidateObsoleteReplicationSlots() are not serialized with this
797+
* function. Even though we can't detect such a case here, the copied
798+
* slot will become invalid in the next checkpoint cycle.
799+
*/
800+
if (second_slot_contents.data.invalidated != RS_INVAL_NONE)
801+
ereport(ERROR,
802+
errmsg("cannot copy replication slot \"%s\"",
803+
NameStr(*src_name)),
804+
errdetail("The source replication slot was invalidated during the copy operation."));
805+
785806
/* Install copied values again */
786807
SpinLockAcquire(&MyReplicationSlot->mutex);
787808
MyReplicationSlot->effective_xmin = copy_effective_xmin;

src/test/recovery/t/035_standby_logical_decoding.pl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,15 @@ sub wait_until_vacuum_can_remove
583583
"can no longer get changes from replication slot \"vacuum_full_activeslot\""
584584
);
585585

586+
# Attempt to copy an invalidated logical replication slot
587+
($result, $stdout, $stderr) = $node_standby->psql(
588+
'postgres',
589+
qq[select pg_copy_logical_replication_slot('vacuum_full_inactiveslot', 'vacuum_full_inactiveslot_copy');],
590+
replication => 'database');
591+
ok( $stderr =~
592+
/ERROR: cannot copy invalidated replication slot "vacuum_full_inactiveslot"/,
593+
"invalidated slot cannot be copied");
594+
586595
# Turn hot_standby_feedback back on
587596
change_hot_standby_feedback_and_wait_for_xmins(1, 1);
588597

0 commit comments

Comments
 (0)