Skip to content

Execute the invalidations of a transaction wherever it ends - #2409

Open
my-ship-it wants to merge 1 commit into
apache:mainfrom
my-ship-it:kernel-invalidations-at-end
Open

my-ship-it wants to merge 1 commit into
apache:mainfrom
my-ship-it:kernel-invalidations-at-end

Conversation

@my-ship-it

Copy link
Copy Markdown
Contributor

What does this PR do?

Executes a transaction's invalidation messages wherever the transaction ends,
instead of only when it happens to have a base snapshot.

Four places end a transaction whose contents were not decoded —
ReorderBufferForget, ReorderBufferInvalidate, ReorderBufferReplay and
ReorderBufferStreamTXN — and all four declined to execute its invalidation
messages: the first two by testing txn->base_snapshot != NULL, the other two
by asserting that a transaction with nothing decoded has no messages either.

The snapshot requirement has not existed since 2016. Invalidations used to
be executed under the transaction's own historic snapshot, so there had to be
one. Upstream c6ff84b06a6 changed them to be executed outside a valid
transaction — entries are marked invalid without the catalog being read, so no
snapshot is needed. The conditions and the assertions stayed behind, and
a271a1b50e9 carried them into ReorderBufferInvalidate when prepare-time
decoding arrived in PG14.

Why it bites here

On the commit path the old condition still happens to hold, because
SnapBuildCommitTxn() hands a catalog-modifying transaction a base snapshot
for exactly this purpose (snapbuild.c: "we might need to execute
invalidations, add snapshot"
), and DecodeCommit calls it before deciding to
skip.

DecodePrepare calls no such thing. A prepare is not a commit, so a
prepared transaction we are not decoding arrives with invalidations and no
snapshot — and on a Cloudberry segment that is an ordinary DDL in another
database, which is to say constantly, because ordinary DDL here is two-phase.
Measured on a 3-segment cluster with a logical slot open: 313 core files in
three minutes
, all from Assert(txn->ninvalidations == 0).

ReorderBufferReplay is the worse of the two assertions: a transaction with
nothing decoded is not a transaction with nothing to do, and the branch goes on
to free it, so the messages are not merely skipped but lost. The streaming one
only returns, leaving them for commit.

This is upstream code, and the hole is upstream's too

Nothing in these four sites is Cloudberry's. Upstream reaches the same place
through DecodePrepare → ReorderBufferInvalidate, where the stale condition
is an if rather than an Assert — so upstream does not crash, it silently
drops the invalidations
and leaves the backend holding catalog cache entries
it should have dropped. Same defect, different symptom; two-phase decoding is
rare upstream and constant here.

I intend to propose the same change to pgsql-hackers, argued from the dropped
invalidations rather than from our assertion. If reviewers would rather wait and
take it through the next merge instead of carrying it here, say so — I would
rather land it here first, because on this side it is a crash and not a
latent one.

Type of Change

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

Test Plan

No new test: the fix removes conditions, and the case that reaches them needs a
two-phase logical slot plus concurrent DDL in another database — reproducible,
but not as a regression test without a lot of scaffolding. What was done:

  • Unit tests added/updated
  • Integration tests added/updated
  • Compiles clean on current main (reorderbuffer.o, no warnings)
  • Reproduction: a 3-segment cluster with wal_level = logical and a
    two-phase slot, DDL running in another database. Before: assertion
    failures at the rate given above. After: none, over a multi-hour run.
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance: none. The removed test was a branch on a pointer; executing
invalidation messages is what this path was always supposed to do.

User-facing changes: a logical decoding session no longer asserts on a
prepared transaction it is not decoding, and no longer leaves stale catalog
cache entries behind on the paths where it previously skipped the messages.

Dependencies: none.

Checklist

A transaction we do not decode may still have changed the catalog, and
its invalidation messages have to be executed anyway or this backend
keeps cache entries it has no business keeping.  Four places end such a
transaction -- ReorderBufferForget, ReorderBufferInvalidate,
ReorderBufferReplay and ReorderBufferStreamTXN -- and all four declined
to do it unless the transaction had a base snapshot: the first two by
testing for one, the other two by asserting that a transaction with
nothing decoded has no messages either.

The snapshot was once required.  In the original implementation the
messages were executed under the transaction's own historic snapshot --
SetupHistoricSnapshot(txn->base_snapshot, txn->tuplecid_hash) -- because
executing one could read the catalog, and a transaction without a
snapshot had none to offer.  The assertions beside it said that this was
not supposed to happen.

c6ff84b took the requirement away.  Invalidations are executed
outside a valid transaction now, which is the whole point of doing it
that way: entries are marked invalid without the catalog being read, so
no snapshot has to be set up.  The conditions and the assertions stayed
behind, and a271a1b carried them into ReorderBufferInvalidate when
prepare-time decoding arrived.

On the commit path they still hold, because SnapBuildCommitTxn hands a
catalog-modifying transaction a base snapshot for this exact purpose --
"we might need to execute invalidations, add snapshot" -- and
DecodeCommit calls it before deciding to skip.  A prepare is not a
commit and nothing calls it there, so a prepared transaction we are not
decoding arrives with invalidations and no snapshot, and the assertion
that was meant to be unreachable fires.  On a Cloudberry segment that is
an ordinary DDL in another database, which is to say constantly.

ReorderBufferReplay is the worse of the two assertions: a transaction
with nothing decoded is not a transaction with nothing to do, and the
branch goes on to free it, so the messages are not merely skipped but
lost.  The streaming one only returns, leaving them to be executed at
commit.

So all four execute them whenever there are any, and the conditions that
no longer decide anything go.  That leaves one rule rather than four
sites agreeing by coincidence: invalidation messages are executed
wherever a transaction ends, whether or not anything of it was decoded.
@reshke

reshke commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Sooo, since when we are supporting logical decoding? Is there some project ongoing?

@my-ship-it

Copy link
Copy Markdown
Contributor Author

Sooo, since when we are supporting logical decoding? Is there some project ongoing?

Thanks for your interest. I have implemented table subscription and logical decoding on MPP using an extension and will share it later. Some small kernel adjustments are required to support the extension, allowing third-party extensions to capture distributed transaction boundaries.

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.

2 participants