From 405bf08190b0d712145c66f445949b18ff0fa430 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 21 Nov 2022 15:13:25 +0000 Subject: [PATCH] Ignore invalidated slots while computing the oldest xmin Catalog_xmin will not advance when a slot is invalidated until the invalidated slot is dropped. This patch ignores invalidated slots while computing the oldest xmin. --- src/backend/replication/slot.c | 6 ++++++ src/backend/replication/slotfuncs.c | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index c036a2c37b..f015ceca51 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -847,6 +847,7 @@ ReplicationSlotsComputeRequiredXmin(bool already_locked) ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i]; TransactionId effective_xmin; TransactionId effective_catalog_xmin; + bool is_invalidated; if (!s->in_use) continue; @@ -854,8 +855,13 @@ ReplicationSlotsComputeRequiredXmin(bool already_locked) SpinLockAcquire(&s->mutex); effective_xmin = s->effective_xmin; effective_catalog_xmin = s->effective_catalog_xmin; + is_invalidated = !XLogRecPtrIsInvalid(s->data.invalidated_at); SpinLockRelease(&s->mutex); + /* ignore invalid slots while computing the oldest xmin */ + if (is_invalidated) + continue; + /* check the data xmin */ if (TransactionIdIsValid(effective_xmin) && (!TransactionIdIsValid(agg_xmin) || diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c index 16a3527903..5cf0c8e94b 100644 --- a/src/backend/replication/slotfuncs.c +++ b/src/backend/replication/slotfuncs.c @@ -318,11 +318,10 @@ pg_get_replication_slots(PG_FUNCTION_ARGS) * certain that the slot has been invalidated. Otherwise, test * availability from restart_lsn. */ - if (XLogRecPtrIsInvalid(slot_contents.data.restart_lsn) && - !XLogRecPtrIsInvalid(slot_contents.data.invalidated_at)) - walstate = WALAVAIL_REMOVED; - else + if (XLogRecPtrIsInvalid(slot_contents.data.invalidated_at)) walstate = GetWALAvailability(slot_contents.data.restart_lsn); + else + walstate = WALAVAIL_REMOVED; switch (walstate) { @@ -369,6 +368,7 @@ pg_get_replication_slots(PG_FUNCTION_ARGS) break; } } + Assert(XLogRecPtrIsInvalid(slot_contents.data.restart_lsn)); values[i++] = CStringGetTextDatum("lost"); break; } -- 2.25.1