Report
Logical decoding fails Assert("txn->ninvalidations == 0") in ReorderBufferForget() when decoding restarts inside a transaction whose subtransaction is first seen via an XLOG_XACT_INVALIDATIONS record; every later slot read crashes again
- Severity
- 25
- Blamed commit
- c55040ccd017962b7b8d3fbcdc184aa90c722a21
On 7 branches
REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
Logical decoding can fail the assertion txn->ninvalidations == 0 in
ReorderBufferForget() after a restart. It happens when the slot's
restart_lsn lies inside a transaction that commits before
confirmed_flush and whose subtransaction, after restart_lsn, writes
only an XLOG_XACT_INVALIDATIONS record (no data change).
When decoding restarts at restart_lsn, the record that links the subtransaction to its top-level transaction comes earlier in WAL and is not read. The invalidation record therefore creates a separate ReorderBufferTXN for the subxid, and ReorderBufferAddInvalidations() accumulates the messages on that txn. The txn has no base snapshot. When the top-level commit is read, it is before start_decoding_at, so DecodeCommit() calls ReorderBufferForget() for each subxid. For this subxid, base_snapshot is NULL but ninvalidations > 0, which hits the assertion.
The slot keeps the same restart_lsn and confirmed_flush after the crash, so on an assertion-enabled build every later read of the slot crashes the server again. Production builds skip the assertion and silently drop the subtransaction's invalidation messages. That only happens for transactions that commit before the point where output starts, and no visible effect beyond the assertion was found.
Reproduction
Server with wal_level=logical, assertions enabled, test_decoding available. Run the script against an empty database:
#!/bin/bash
P="psql -X -q -At -d postgres"
$P <<'SQL'
CREATE TABLE d (a int);
CREATE TABLE e (a int); -- stays empty
CREATE TABLE f (a int);
SELECT 'slot created'
FROM pg_create_logical_replication_slot('rs', 'test_decoding');
SQL
# A: the subtransaction's first record (carrying the top-level xid)
# is written now; after the pause it only writes an in-place update
# and an XLOG_XACT_INVALIDATIONS record (ANALYZE of an empty table).
$P -c "BEGIN; SAVEPOINT s; INSERT INTO d VALUES (1);
SELECT pg_sleep(2); ANALYZE e; RELEASE s; COMMIT;" >/dev/null &
sleep 0.5
# B: long-running; its first record follows snapshot R below.
$P -c "BEGIN; SELECT pg_sleep(1); INSERT INTO d VALUES (2);
SELECT pg_sleep(3); COMMIT;" >/dev/null &
sleep 0.5
$P -c "SELECT pg_log_standby_snapshot()" >/dev/null # R
sleep 2.5 # A committed, B running
$P -c "SELECT pg_log_standby_snapshot()" >/dev/null # R2
$P -c "INSERT INTO f VALUES (1)" # flush WAL
G="SELECT count(*) FROM pg_logical_slot_get_changes('rs', NULL, NULL)"
# Decodes A; restart_lsn moves to R (B's restart point), which lies
# inside A, and confirmed_flush moves past A's commit.
$P -c "$G"
# Restarts at R: the subtransaction is first seen through its
# invalidation record, then A's commit is skipped.
$P -c "$G"
wait
Expected: the second call returns 0 (A is already confirmed).
Actual: the first call returns 6, the second crashes the server:
TRAP: failed Assert("txn->ninvalidations == 0"),
File: "reorderbuffer.c", Line: 3245
ReorderBufferForget+0xea
xact_decode+0x19b
LogicalDecodingProcessRecord+0xa8
pg_logical_slot_get_changes+0x22
After crash recovery, each further pg_logical_slot_get_changes() on the slot crashes again in the same place.
The ANALYZE of an empty table is just a convenient way to get an invalidation record with no decodable change (it only writes an XLOG_HEAP_INPLACE record, which does not set a base snapshot). A random stress test that restarted decoding often (small upto_nchanges, frequent running-xacts records) while sessions ran ALTER TABLE in savepoints hit the same assertion without it.
Analysis
- decode.c, xact_decode(), XLOG_XACT_INVALIDATIONS: calls ReorderBufferAddInvalidations() for the record's xid. ReorderBufferAddInvalidations() stores the messages on the top-level txn "if available"; when the subxact-to-top assignment has not been seen (it was before restart_lsn), the subxid's own txn gets them.
- Nothing gives that txn a base snapshot: XLOG_HEAP_INPLACE does not call SnapBuildProcessChange(), and SnapBuildCommitTxn() sets a base snapshot only for the top-level xid.
- DecodeCommit(): the commit LSN is below start_decoding_at, so
ReorderBufferForget() runs for every subxid first. Its
else Assert(txn->ninvalidations == 0)assumes that a txn with invalidations always has a base snapshot. ReorderBufferInvalidate() has the same pattern.
The assertion dates from the original logical decoding code. It became reachable with commit c55040ccd017962b7b8d3fbcdc184aa90c722a21 ("WAL Log invalidations at command end with wal_level=logical"). That commit added XLOG_XACT_INVALIDATIONS and the accumulation of its messages per (sub)transaction. Before it, invalidations were only added from the commit record, for the top-level xid.
Possible fixes: relax the assertion for transactions that are being skipped at startup, or move the accumulated messages to the top-level txn when ReorderBufferAssignChild() learns the assignment (at the latest from the commit record's subxact list).
Notes
Reproduced on master at 00f270198828c57b764f60465c439a56cbd6e33a (assertion-enabled build). Only assertion-enabled builds crash. Restart points that fall inside a still-running transaction are normal; they happen whenever an older transaction is open when a newer one starts.
Branches
Reproduced on master. The blamed commit is in REL_14_STABLE and every later branch; older branches were not tested.