Skip to content

Commit 87e8599

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 82a8f0f commit 87e8599

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

doc/src/sgml/func.sgml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27258,7 +27258,8 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
2725827258
The copied physical slot starts to reserve WAL from the same <acronym>LSN</acronym> as the
2725927259
source slot.
2726027260
<parameter>temporary</parameter> is optional. If <parameter>temporary</parameter>
27261-
is omitted, the same value as the source slot is used.
27261+
is omitted, the same value as the source slot is used. Copy of an
27262+
invalidated slot is not allowed.
2726227263
</para></entry>
2726327264
</row>
2726427265

@@ -27280,6 +27281,7 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
2728027281
from the same <acronym>LSN</acronym> as the source logical slot. Both
2728127282
<parameter>temporary</parameter> and <parameter>plugin</parameter> are
2728227283
optional; if they are omitted, the values of the source slot are used.
27284+
Copy of an invalidated slot is not allowed.
2728327285
</para></entry>
2728427286
</row>
2728527287

src/backend/replication/slotfuncs.c

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

759+
/* Cannot copy an invalidated replication slot */
760+
if (first_slot_contents.data.invalidated != RS_INVAL_NONE)
761+
ereport(ERROR,
762+
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
763+
errmsg("cannot copy invalidated replication slot \"%s\"",
764+
NameStr(*src_name)));
765+
759766
/* Overwrite params from optional arguments */
760767
if (PG_NARGS() >= 3)
761768
temporary = PG_GETARG_BOOL(2);
@@ -843,6 +850,20 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
843850
NameStr(*src_name)),
844851
errhint("Retry when the source replication slot's confirmed_flush_lsn is valid.")));
845852

853+
/*
854+
* Copying an invalid slot doesn't make sense. Note that the source
855+
* slot can become invalid after we creat the new slot and copy the
856+
* data of source slot. This is possible because the operations in
857+
* InvalidateObsoleteReplicationSlots() are not serialized with this
858+
* function. Even though we can't detect such a case here, the copied
859+
* slot will become invalid in the next checkpoint cycle.
860+
*/
861+
if (second_slot_contents.data.invalidated != RS_INVAL_NONE)
862+
ereport(ERROR,
863+
errmsg("cannot copy replication slot \"%s\"",
864+
NameStr(*src_name)),
865+
errdetail("The source replication slot was invalidated during the copy operation."));
866+
846867
/* Install copied values again */
847868
SpinLockAcquire(&MyReplicationSlot->mutex);
848869
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
@@ -563,6 +563,15 @@ sub wait_until_vacuum_can_remove
563563
"can no longer get changes from replication slot \"vacuum_full_activeslot\""
564564
);
565565

566+
# Attempt to copy an invalidated logical replication slot
567+
($result, $stdout, $stderr) = $node_standby->psql(
568+
'postgres',
569+
qq[select pg_copy_logical_replication_slot('vacuum_full_inactiveslot', 'vacuum_full_inactiveslot_copy');],
570+
replication => 'database');
571+
ok( $stderr =~
572+
/ERROR: cannot copy invalidated replication slot "vacuum_full_inactiveslot"/,
573+
"invalidated slot cannot be copied");
574+
566575
# Turn hot_standby_feedback back on
567576
change_hot_standby_feedback_and_wait_for_xmins(1, 1);
568577

0 commit comments

Comments
 (0)