Skip to content

Let a logical slot hold the coordinator's WAL - #2450

Merged
tuhaihe merged 1 commit into
apache:mainfrom
my-ship-it:kernel-coordinator-wal-slot
Dec 4, 2026
Merged

tuhaihe merged 1 commit into
apache:mainfrom
my-ship-it:kernel-coordinator-wal-slot

Conversation

@my-ship-it

Copy link
Copy Markdown
Contributor

What does this PR do?

Lets a logical replication slot hold WAL on the coordinator.

KeepLogSeg() has two ways of deciding how far back WAL must be kept, and the
coordinator used only one of them:

keep = IS_QUERY_DISPATCHER() ?
    WalSndCtlGetXLogCleanUpTo() :        /* what connected walsenders confirmed */
    XLogGetReplicationSlotMinimumLSN();  /* what slots need */

The comment above it explains why — "coordinator needs a different way to
determine the keep point as replication slot is not created there"
. The
coordinator's standby is fed by a bare walsender with no slot behind it, so a
slot minimum of "nothing" would have left that standby unprotected.

Something does create a slot there now. A logical slot is a promise written
to disk precisely so that it outlives the connection, and the subscriber it
belongs to is expected to be away sometimes: disabled, its target down,
restarting after an error. The walsender bookkeeping exists only while somebody
is connected, so during every one of those gaps the coordinator had no reason
to keep any WAL at all. It recycled, and the next checkpoint then invalidated
the slot of every subscription that happened not to be running.

The segments never had this, which made it look stranger than it was: the same
subscription, the same slot name, dead on the coordinator and healthy on all
three segments.

What it does instead

Consult both and keep the earlier. Only logical slots join in —
ReplicationSlotsComputeLogicalRestartLSN(), not the general slot minimum:

  • the coordinator's physical replication has never been driven by slots, and
    this is not the place to change that;
  • that function already leaves out slots which have been invalidated, so a dead
    one cannot pin WAL for ever;
  • where there are no logical slots it answers InvalidXLogRecPtr and nothing
    about the existing behaviour changes.

Why the two are not simply Min()ed. InvalidXLogRecPtr means "nothing to
say" here, and it is zero — so Min() would read an abstention as the
strictest possible answer and keep no WAL at all, which is exactly the case
this is about. Each is tested for validity and the earliest of those that
answered wins.

Side effect worth knowing

This brings max_slot_wal_keep_size into play on a coordinator, where until now
it could do nothing: it caps a keep point computed from slots, and no such point
was ever computed there. A publisher that would rather drop a subscriber than
grow its WAL can now say so on the coordinator too.

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

None. Where no logical slot exists on the coordinator — which is every cluster
that is not a logical replication source — the computed keep point is unchanged,
because ReplicationSlotsComputeLogicalRestartLSN() returns InvalidXLogRecPtr
and the walsender answer is taken as before. Physical replication and the
coordinator's standby are untouched.

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Compiles clean on current main (xlog.o, no warnings)
  • Behaviour checked by hand: a logical slot on the coordinator of a
    3-segment cluster, its consumer stopped, then WAL generated and
    checkpoints forced. Before: the slot is invalidated at the next
    checkpoint while the segments' slots of the same name survive. After:
    it is retained, and max_slot_wal_keep_size bounds how far that goes.
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

A regression test for this needs a coordinator logical slot plus checkpoint
control plus a wait for invalidation; I have not written one, and would rather
say so than imply coverage that is not there.

Impact

Performance: one extra shared-memory read per KeepLogSeg() call on the
coordinator, i.e. per checkpoint.

User-facing changes: a logical slot on the coordinator survives its
subscriber being away. max_slot_wal_keep_size starts applying there.

Dependencies: none.

Checklist

@reshke

reshke commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Thats some sort of tech solution which doesn't look promising to me. Making Cloudberry retain more files on FS just for logical decoding is something that may become fragile to support.

My proposal is to take https://www.postgresql.org/message-id/EC645E05-27FD-4394-91F1-60B4F58DCE3E%40yandex-team.ru and enable for logical replication too (would be 1 if statement). So, server would simply fetch WAL from archive on purposes of decoding it. WHYT?

@my-ship-it

Copy link
Copy Markdown
Contributor Author

Thats some sort of tech solution which doesn't look promising to me. Making Cloudberry retain more files on FS just for logical decoding is something that may become fragile to support.

My proposal is to take https://www.postgresql.org/message-id/EC645E05-27FD-4394-91F1-60B4F58DCE3E%40yandex-team.ru and enable for logical replication too (would be 1 if statement). So, server would simply fetch WAL from archive on purposes of decoding it. WHYT?

Fair concern, but I think the scope here is narrower than it looks.

On the coordinator KeepLogSeg() never looks at logical slots at all — it ends up at segno = currSegNo, so WAL gets recycled whether or not a consumer is behind, and max_slot_wal_keep_size has nothing to cap. This patch doesn't ask for unbounded retention; it just puts the slot into the calculation so the coordinator behaves like the segments do, that GUC included.

Re wal_sender_restore: I'd like to see it land. But it works by letting a physical receiver survive without a slot pinning WAL, and logical has no such option — the protocol needs the slot, and by the time a slot is invalidated the catalog rows its catalog_xmin held back are already vacuumed, so getting the WAL back from an archive doesn't get you decoding back. Different problem, not a competing fix.

WAL retention on a coordinator does not follow replication slots.  It
follows what the connected walsenders have confirmed, because the
coordinator's standby is fed by a bare walsender with no slot behind it,
and a slot minimum of "nothing" would have left that standby unprotected.
The comment on it says as much: replication slot is not created there.

One is now.  A logical slot is a promise written to disk precisely so that
it outlives the connection, and the subscriber it belongs to is expected to
be away sometimes -- disabled, its target down, restarting after an error.
The walsender bookkeeping exists only while somebody is connected, so
during every one of those gaps the coordinator had no reason to keep any
WAL at all: it recycled, and the next checkpoint then invalidated the slot
of every subscription that happened not to be running.  The segments never
had this, which made it look stranger than it was -- the same subscription,
the same slot name, dead on the coordinator and healthy on all three
segments.

Consult both and keep the earlier.  Only logical slots join in: the
coordinator's physical replication has never been driven by slots and this
is not the place to change that, and ReplicationSlotsComputeLogicalRestartLSN()
already leaves out slots that have been invalidated, so a dead one cannot
pin WAL for ever.  Where there are no logical slots it answers
InvalidXLogRecPtr and nothing about this changes.

That last part is why the two cannot simply be Min()ed: InvalidXLogRecPtr
means "nothing to say" here, and it is zero, so Min() would read an
abstention as the strictest possible answer and keep no WAL at all --
which is exactly the case this is about.

The cap this brings into play is max_slot_wal_keep_size, which until now
could not do anything on a coordinator, because it caps a keep point
computed from slots and no such point was ever computed.  A publisher that
would rather drop a subscriber than grow its WAL can now say so there too.
@tuhaihe
tuhaihe force-pushed the kernel-coordinator-wal-slot branch from a099220 to 28ce598 Compare December 4, 2026 02:44
@tuhaihe
tuhaihe enabled auto-merge (rebase) December 4, 2026 02:45
@tuhaihe
tuhaihe merged commit ec69267 into apache:main Dec 4, 2026
178 of 179 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants