Skip to content

Commit caa463e

Browse files
Fix stream abort for a transaction that was never streamed.
Commit 072ee84 taught logical decoding to discard the changes of a transaction that is already known to be aborted when it is picked for eviction. That path reuses ReorderBufferTruncateTXN(), which marks every subtransaction that still has in-memory changes as streamed. Since nothing is streamed in that path, and the top-level transaction is never marked, a subtransaction ends up flagged as streamed even though the output plugin has never seen it. Decoding the subsequent abort record then makes ReorderBufferAbort() invoke the stream_abort callback for that subtransaction. For pgoutput this sends a Stream Abort ('A') message to a subscriber that requested streaming = off, and it does so regardless of the negotiated protocol version, so even a client speaking a version that predates transaction streaming receives a message it cannot parse. test_decoding dereferences a NULL pointer and crashes, since it allocates its per-transaction state in the begin or stream start callback, neither of which runs for a transaction discarded as aborted. This commit fixes this by marking a subtransaction as streamed only when it has changes and its top-level transaction is already marked as streamed. All streaming call sites mark the top-level transaction before truncating it, so their behavior is unchanged, while the abort-discard path never marks the top-level transaction and therefore now leaves its subtransactions unmarked. Backpatch to v18, where commit 072ee84 was introduced. Bug: #19616 Reported-by: Tyler Smart <[email protected]> Author: Andrey Rachitskiy <[email protected]> Reviewed-by: Hayato Kuroda <[email protected]> Reviewed-by: Fujii Masao <[email protected]> Reviewed-by: Masahiko Sawada <[email protected]> Discussion: https://postgr.es/m/[email protected] Backpatch-through: 18
1 parent 607c903 commit caa463e

3 files changed

Lines changed: 64 additions & 14 deletions

File tree

‎contrib/test_decoding/expected/stream.out‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL,
134134
(1 row)
135135

136136
RESET debug_logical_replication_streaming;
137+
-- bug #19616
138+
--
139+
-- An aborted top-level transaction that is discarded at eviction must not
140+
-- leave its subtransactions marked as streamed. Otherwise, decoding its
141+
-- abort record invokes the stream abort callback for a subtransaction the
142+
-- output plugin has never seen, even though streaming was never requested.
143+
-- The trailing committed transaction is required to flush the ROLLBACK
144+
-- record; without it decoding would stop before reaching the abort.
145+
BEGIN;
146+
SAVEPOINT s;
147+
INSERT INTO stream_test VALUES ('subxact-change');
148+
RELEASE SAVEPOINT s;
149+
INSERT INTO stream_test SELECT 'toplevel-change' || g.i FROM generate_series(1, 5000) g(i);
150+
ROLLBACK;
151+
INSERT INTO stream_test VALUES ('after-abort');
152+
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
153+
data
154+
----------------------------------------------------------------------------
155+
BEGIN
156+
table public.stream_test: INSERT: data[text]:'after-abort' i[integer]:null
157+
COMMIT
158+
(3 rows)
159+
137160
DROP TABLE stream_test;
138161
SELECT pg_drop_replication_slot('regression_slot');
139162
pg_drop_replication_slot

‎contrib/test_decoding/sql/stream.sql‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,22 @@ COMMIT;
6565
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1');
6666
RESET debug_logical_replication_streaming;
6767

68+
-- bug #19616
69+
--
70+
-- An aborted top-level transaction that is discarded at eviction must not
71+
-- leave its subtransactions marked as streamed. Otherwise, decoding its
72+
-- abort record invokes the stream abort callback for a subtransaction the
73+
-- output plugin has never seen, even though streaming was never requested.
74+
-- The trailing committed transaction is required to flush the ROLLBACK
75+
-- record; without it decoding would stop before reaching the abort.
76+
BEGIN;
77+
SAVEPOINT s;
78+
INSERT INTO stream_test VALUES ('subxact-change');
79+
RELEASE SAVEPOINT s;
80+
INSERT INTO stream_test SELECT 'toplevel-change' || g.i FROM generate_series(1, 5000) g(i);
81+
ROLLBACK;
82+
INSERT INTO stream_test VALUES ('after-abort');
83+
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
84+
6885
DROP TABLE stream_test;
6986
SELECT pg_drop_replication_slot('regression_slot');

‎src/backend/replication/logical/reorderbuffer.c‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,26 +2130,36 @@ ReorderBufferSaveTXNSnapshot(ReorderBuffer *rb, ReorderBufferTXN *txn,
21302130
}
21312131

21322132
/*
2133-
* Mark the given transaction as streamed if it's a top-level transaction
2134-
* or has changes.
2133+
* Mark the given transaction as streamed, if appropriate.
2134+
*
2135+
* A top-level transaction is always marked. A subtransaction is marked
2136+
* only when it has changes and its top-level transaction is already
2137+
* marked as streamed.
21352138
*/
21362139
static void
21372140
ReorderBufferMaybeMarkTXNStreamed(ReorderBuffer *rb, ReorderBufferTXN *txn)
21382141
{
21392142
/*
2140-
* The top-level transaction, is marked as streamed always, even if it
2141-
* does not contain any changes (that is, when all the changes are in
2142-
* subtransactions).
2143-
*
2144-
* For subtransactions, we only mark them as streamed when there are
2145-
* changes in them.
2146-
*
2147-
* We do it this way because of aborts - we don't want to send aborts for
2148-
* XIDs the downstream is not aware of. And of course, it always knows
2149-
* about the top-level xact (we send the XID in all messages), but we
2150-
* never stream XIDs of empty subxacts.
2143+
* The top-level transaction is marked as streamed always, even if it does
2144+
* not contain any changes (that is, when all the changes are in
2145+
* subtransactions). The downstream always knows about it, since we send
2146+
* its XID in every message.
2147+
*/
2148+
if (rbtxn_is_toptxn(txn))
2149+
{
2150+
/* We only reach here when streaming is supported. */
2151+
Assert(ReorderBufferCanStream(rb));
2152+
txn->txn_flags |= RBTXN_IS_STREAMED;
2153+
return;
2154+
}
2155+
2156+
/*
2157+
* A subtransaction is marked only when it has changes, and only when its
2158+
* top-level transaction has already been marked as streamed. We never
2159+
* stream XIDs of empty subxacts, and we must not send an abort for an XID
2160+
* the downstream has never heard of.
21512161
*/
2152-
if (rbtxn_is_toptxn(txn) || (txn->nentries_mem != 0))
2162+
if (txn->nentries_mem != 0 && rbtxn_is_streamed(rbtxn_get_toptxn(txn)))
21532163
txn->txn_flags |= RBTXN_IS_STREAMED;
21542164
}
21552165

0 commit comments

Comments
 (0)