Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 56a7147

Browse files
committed
Block replication slot advance for these not yet reserving WAL
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
1 parent b6e3a3a commit 56a7147

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

contrib/test_decoding/expected/slot.out

+17
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,20 @@ SELECT pg_drop_replication_slot('regression_slot1');
131131
ERROR: replication slot "regression_slot1" does not exist
132132
SELECT pg_drop_replication_slot('regression_slot2');
133133
ERROR: replication slot "regression_slot2" does not exist
134+
-- slot advance with physical slot, error with non-reserved slot
135+
SELECT slot_name FROM pg_create_physical_replication_slot('regression_slot3');
136+
slot_name
137+
------------------
138+
regression_slot3
139+
(1 row)
140+
141+
SELECT pg_replication_slot_advance('regression_slot3', '0/0'); -- invalid LSN
142+
ERROR: invalid target wal lsn
143+
SELECT pg_replication_slot_advance('regression_slot3', '0/1'); -- error
144+
ERROR: cannot advance replication slot that has not previously reserved WAL
145+
SELECT pg_drop_replication_slot('regression_slot3');
146+
pg_drop_replication_slot
147+
--------------------------
148+
149+
(1 row)
150+

contrib/test_decoding/sql/slot.sql

+6
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,9 @@ SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot1', 'test_
6868
-- both should error as they should be dropped on error
6969
SELECT pg_drop_replication_slot('regression_slot1');
7070
SELECT pg_drop_replication_slot('regression_slot2');
71+
72+
-- slot advance with physical slot, error with non-reserved slot
73+
SELECT slot_name FROM pg_create_physical_replication_slot('regression_slot3');
74+
SELECT pg_replication_slot_advance('regression_slot3', '0/0'); -- invalid LSN
75+
SELECT pg_replication_slot_advance('regression_slot3', '0/1'); -- error
76+
SELECT pg_drop_replication_slot('regression_slot3');

doc/src/sgml/catalogs.sgml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9867,7 +9867,8 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
98679867
<entry></entry>
98689868
<entry>The address (<literal>LSN</literal>) of oldest WAL which still
98699869
might be required by the consumer of this slot and thus won't be
9870-
automatically removed during checkpoints.
9870+
automatically removed during checkpoints. <literal>NULL</literal>
9871+
if the <literal>LSN</literal> of this slot has never been reserved.
98719872
</entry>
98729873
</row>
98739874

src/backend/replication/slotfuncs.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
483483
/* Acquire the slot so we "own" it */
484484
ReplicationSlotAcquire(NameStr(*slotname), true);
485485

486+
/* A slot whose restart_lsn has never been reserved cannot be advanced */
487+
if (XLogRecPtrIsInvalid(MyReplicationSlot->data.restart_lsn))
488+
ereport(ERROR,
489+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
490+
errmsg("cannot advance replication slot that has not previously reserved WAL")));
491+
486492
/*
487493
* Check if the slot is not moving backwards. Physical slots rely simply
488494
* on restart_lsn as a minimum point, while logical slots have confirmed
@@ -495,14 +501,11 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
495501
minlsn = MyReplicationSlot->data.restart_lsn;
496502

497503
if (moveto < minlsn)
498-
{
499-
ReplicationSlotRelease();
500504
ereport(ERROR,
501-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
502-
errmsg("cannot move slot to %X/%X, minimum is %X/%X",
505+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
506+
errmsg("cannot advance replication slot to %X/%X, minimum is %X/%X",
503507
(uint32) (moveto >> 32), (uint32) moveto,
504508
(uint32) (minlsn >> 32), (uint32) minlsn)));
505-
}
506509

507510
/* Do the actual slot update, depending on the slot type */
508511
if (OidIsValid(MyReplicationSlot->data.database))

0 commit comments

Comments
 (0)