]> granicus.if.org Git - postgresql/commitdiff
Block replication slot advance for these not yet reserving WAL
authorMichael Paquier <michael@paquier.xyz>
Tue, 10 Jul 2018 23:56:24 +0000 (08:56 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 10 Jul 2018 23:56:24 +0000 (08:56 +0900)
Such replication slots are physical slots freshly created without WAL
being reserved, which is the default behavior, which have not been used
yet as WAL consumption resources to retain WAL.  This prevents advancing
a slot to a position older than any WAL available, which could falsify
calculations for WAL segment recycling.

This also cleans up a bit the code, as ReplicationSlotRelease() would be
called on ERROR, and improves error messages.

Reported-by: Kyotaro Horiguchi
Author: Michael Paquier
Reviewed-by: Andres Freund, Álvaro Herrera, Kyotaro Horiguchi
Discussion: https://postgr.es/m/20180626071305.GH31353@paquier.xyz

contrib/test_decoding/expected/slot.out
contrib/test_decoding/sql/slot.sql
doc/src/sgml/catalogs.sgml
src/backend/replication/slotfuncs.c

index 21e9d56f73b739503acd1447ee2639c9f7d947fa..2737a8a301bfbe7fb6517bc3b9f9a91553e5e463 100644 (file)
@@ -131,3 +131,20 @@ SELECT pg_drop_replication_slot('regression_slot1');
 ERROR:  replication slot "regression_slot1" does not exist
 SELECT pg_drop_replication_slot('regression_slot2');
 ERROR:  replication slot "regression_slot2" does not exist
+-- slot advance with physical slot, error with non-reserved slot
+SELECT slot_name FROM pg_create_physical_replication_slot('regression_slot3');
+    slot_name     
+------------------
+ regression_slot3
+(1 row)
+
+SELECT pg_replication_slot_advance('regression_slot3', '0/0'); -- invalid LSN
+ERROR:  invalid target wal lsn
+SELECT pg_replication_slot_advance('regression_slot3', '0/1'); -- error
+ERROR:  cannot advance replication slot that has not previously reserved WAL
+SELECT pg_drop_replication_slot('regression_slot3');
+ pg_drop_replication_slot 
+--------------------------
+(1 row)
+
index 706340c1d8d25a12a8d49d1e72d51671418c0bb0..24cdf7155d750a850adf7539002f6988a940687b 100644 (file)
@@ -68,3 +68,9 @@ SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot1', 'test_
 -- both should error as they should be dropped on error
 SELECT pg_drop_replication_slot('regression_slot1');
 SELECT pg_drop_replication_slot('regression_slot2');
+
+-- slot advance with physical slot, error with non-reserved slot
+SELECT slot_name FROM pg_create_physical_replication_slot('regression_slot3');
+SELECT pg_replication_slot_advance('regression_slot3', '0/0'); -- invalid LSN
+SELECT pg_replication_slot_advance('regression_slot3', '0/1'); -- error
+SELECT pg_drop_replication_slot('regression_slot3');
index 3ed9021c2fc840b37fcbfed04d8a7c790d9c2a52..4851bc2e248e86ef9573a17344d531df30e21dfb 100644 (file)
@@ -9867,7 +9867,8 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
       <entry></entry>
       <entry>The address (<literal>LSN</literal>) of oldest WAL which still
       might be required by the consumer of this slot and thus won't be
-      automatically removed during checkpoints.
+      automatically removed during checkpoints.  <literal>NULL</literal>
+      if the <literal>LSN</literal> of this slot has never been reserved.
       </entry>
      </row>
 
index 2806e1076caa31c82c98940c1acbb3bca75fdb23..23af32355b79fc09dfdaeaaddcf08e3cf00329e9 100644 (file)
@@ -483,6 +483,12 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
        /* Acquire the slot so we "own" it */
        ReplicationSlotAcquire(NameStr(*slotname), true);
 
+       /* A slot whose restart_lsn has never been reserved cannot be advanced */
+       if (XLogRecPtrIsInvalid(MyReplicationSlot->data.restart_lsn))
+               ereport(ERROR,
+                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot advance replication slot that has not previously reserved WAL")));
+
        /*
         * Check if the slot is not moving backwards.  Physical slots rely simply
         * on restart_lsn as a minimum point, while logical slots have confirmed
@@ -495,14 +501,11 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
                minlsn = MyReplicationSlot->data.restart_lsn;
 
        if (moveto < minlsn)
-       {
-               ReplicationSlotRelease();
                ereport(ERROR,
-                               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("cannot move slot to %X/%X, minimum is %X/%X",
+                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot advance replication slot to %X/%X, minimum is %X/%X",
                                                (uint32) (moveto >> 32), (uint32) moveto,
                                                (uint32) (minlsn >> 32), (uint32) minlsn)));
-       }
 
        /* Do the actual slot update, depending on the slot type */
        if (OidIsValid(MyReplicationSlot->data.database))