Restrict copying of invalidated replication slots.
authorMasahiko Sawada <[email protected]>
Thu, 3 Apr 2025 17:30:02 +0000 (10:30 -0700)
committerMasahiko Sawada <[email protected]>
Thu, 3 Apr 2025 17:30:02 +0000 (10:30 -0700)
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 <[email protected]>
Reviewed-by: vignesh C <[email protected]>
Reviewed-by: Zhijie Hou <[email protected]>
Reviewed-by: Peter Smith <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Reviewed-by: Amit Kapila <[email protected]>
Discussion: https://postgr.es/m/CANhcyEU65aH0VYnLiu%3DOhNNxhnhNhwcXBeT-jvRe1OiJTo_Ayg%40mail.gmail.com
Backpatch-through: 16

doc/src/sgml/func.sgml
src/backend/replication/slotfuncs.c
src/test/recovery/t/035_standby_logical_decoding.pl

index ebd42096795c4981ac37793c2a08c7044364b81c..f441ec43314d7e20b25ab6fd44c42914ce0af170 100644 (file)
@@ -29095,7 +29095,8 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
         The copied physical slot starts to reserve WAL from the same <acronym>LSN</acronym> as the
         source slot.
         <parameter>temporary</parameter> is optional. If <parameter>temporary</parameter>
-        is omitted, the same value as the source slot is used.
+        is omitted, the same value as the source slot is used. Copy of an
+        invalidated slot is not allowed.
        </para></entry>
       </row>
 
@@ -29120,7 +29121,8 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
         The <literal>failover</literal> option of the source logical slot
         is not copied and is set to <literal>false</literal> by default. This
         is to avoid the risk of being unable to continue logical replication
-        after failover to standby where the slot is being synchronized.
+        after failover to standby where the slot is being synchronized. Copy of
+        an invalidated slot is not allowed.
        </para></entry>
       </row>
 
index 43c3cc7336b62c4219201942f3981b2abe9f2810..01e98bc1ceff709933b51b9a023dd4794a885983 100644 (file)
@@ -681,6 +681,13 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                 errmsg("cannot copy a replication slot that doesn't reserve WAL")));
 
+   /* Cannot copy an invalidated replication slot */
+   if (first_slot_contents.data.invalidated != RS_INVAL_NONE)
+       ereport(ERROR,
+               errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+               errmsg("cannot copy invalidated replication slot \"%s\"",
+                      NameStr(*src_name)));
+
    /* Overwrite params from optional arguments */
    if (PG_NARGS() >= 3)
        temporary = PG_GETARG_BOOL(2);
@@ -782,6 +789,20 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
                            NameStr(*src_name)),
                     errhint("Retry when the source replication slot's confirmed_flush_lsn is valid.")));
 
+       /*
+        * Copying an invalid slot doesn't make sense. Note that the source
+        * slot can become invalid after we create the new slot and copy the
+        * data of source slot. This is possible because the operations in
+        * InvalidateObsoleteReplicationSlots() are not serialized with this
+        * function. Even though we can't detect such a case here, the copied
+        * slot will become invalid in the next checkpoint cycle.
+        */
+       if (second_slot_contents.data.invalidated != RS_INVAL_NONE)
+           ereport(ERROR,
+                   errmsg("cannot copy replication slot \"%s\"",
+                          NameStr(*src_name)),
+                   errdetail("The source replication slot was invalidated during the copy operation."));
+
        /* Install copied values again */
        SpinLockAcquire(&MyReplicationSlot->mutex);
        MyReplicationSlot->effective_xmin = copy_effective_xmin;
index aeb79f51e713142c80c5fc67ca37289960f9af49..4eca17885d6ea064eae03871b57b8bbe1f2e24f9 100644 (file)
@@ -583,6 +583,15 @@ check_pg_recvlogical_stderr($handle,
    "can no longer get changes from replication slot \"vacuum_full_activeslot\""
 );
 
+# Attempt to copy an invalidated logical replication slot
+($result, $stdout, $stderr) = $node_standby->psql(
+   'postgres',
+   qq[select pg_copy_logical_replication_slot('vacuum_full_inactiveslot', 'vacuum_full_inactiveslot_copy');],
+   replication => 'database');
+ok( $stderr =~
+     /ERROR:  cannot copy invalidated replication slot "vacuum_full_inactiveslot"/,
+   "invalidated slot cannot be copied");
+
 # Turn hot_standby_feedback back on
 change_hot_standby_feedback_and_wait_for_xmins(1, 1);