Re: Add aborted_txns and aborted_bytes to pg_stat_replication_slots

From: Ajin Cherian <itsajin(at)gmail(dot)com>
To: Rajesh Kumar Thandapani <rajeshkumarth(at)google(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Add aborted_txns and aborted_bytes to pg_stat_replication_slots
Date: 2026-12-02 00:59:54
Message-ID: CAFPTHDasBmwsoGiC=2vgusSWogMFM4AeQjT1z9JgSumLsT8RdA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Dec 2, 2026 at 2:24 AM Rajesh Kumar Thandapani
<rajeshkumarth(at)google(dot)com> wrote:
>
> Hi all,
>
> Logical decoding has to read and buffer WAL changes for in-progress
> transactions before knowing whether they will commit or abort. Right now
> pg_stat_replication_slots only reports transactions sent to the output
> plugin (total_txns and total_bytes), so aborted transactions are invisible.
> With commit 072ee847ad4 discarding already-aborted transactions at
> logical_decoding_work_mem and commit d3b6183dd98 adding mem_exceeded_count,
> users can see mem_exceeded_count going up while spill_txns, stream_txns,
> and total_txns stay at zero, or see spill and stream counters inflated by
> transactions that later rolled back.

> Feedback and review comments are welcome.
>

Subject: Re: Add aborted_txns and aborted_bytes to pg_stat_replication_slots

Hi Rajesh,

Thanks for the patch. I applied v1 on HEAD and ran the test_decoding
regression and isolation tests; all pass. Some comments on where the
aborted stats are accounted.

1.
v1 accounts aborted stats at six places: the aborted path in
ReorderBufferQueueChange(), ReorderBufferCheckAndTruncateAbortedTXN(),
the concurrent-abort catch block in ReorderBufferProcessTXN(),
ReorderBufferFinishPrepared(), ReorderBufferAbort() and
ReorderBufferAbortOld(). As far as I can see, only the last three are
needed. An aborted transaction is finally cleaned up in one of them:
ReorderBufferAbort() for an ABORT record (including rolled-back
subtransactions, which have their own abort records),
ReorderBufferAbortOld() for transactions that went away without an
ABORT record, and the rollback branch of ReorderBufferFinishPrepared()
for ROLLBACK PREPARED. Since the new txn->decoded_bytes is not reset
by ReorderBufferTruncateTXN(), spilling or streaming, the bytes are
still there at that point, so the early-detection points don't need to
account anything.

So these could be removed:

+ if (ReorderBufferCanReportAbortStats(rb))
+ {
+ if (!rbtxn_is_abort_counted(txn))
+ {
+ rb->abortedTxns++;
+ txn->txn_flags |= RBTXN_ABORT_COUNTED;
+ }
+ rb->abortedBytes += txn->decoded_bytes + sz;
+ txn->decoded_bytes = 0;
+ }
+ else
+ txn->decoded_bytes += sz;

(in ReorderBufferQueueChange(), just keep txn->decoded_bytes += sz for
streamable changes)

+ ReorderBufferAccountAbortedTXN(rb, txn);
ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn));

+ ReorderBufferAccountAbortedTXN(rb, curtxn);

With those gone, RBTXN_ABORT_COUNTED and rbtxn_is_abort_counted()
should no longer be needed either, since a transaction would only be
accounted for when it is cleaned up. I tried this locally: the
test_decoding regression and isolation tests, including all the new
tests in stats.sql, still pass, and for an in-memory rollback and an
early-discarded (logical_decoding_work_mem = 64kB) rollback I get the
same aborted_txns and aborted_bytes as with v1.

2.
The early accounting can also count the same transaction twice.
ReorderBufferCanReportAbortStats() checks ctx->reader->ReadRecPtr
against start_decoding_at, which at the early-discard point is the
position of the change being decoded, not the transaction's abort LSN.
If decoding stops after the early discard but before the ABORT record,
confirmed_flush moves past the point where we counted while
restart_lsn stays before the transaction. The next decoding call
re-reads its changes (not counted, as they were before
start_decoding_at, but accumulated in decoded_bytes) and then counts
the whole transaction again at the ABORT record. Here is a repro:

CREATE TABLE dc_test (a int, b text);
SELECT 'init' FROM pg_create_logical_replication_slot('dc_slot',
'test_decoding');
BEGIN;
INSERT INTO dc_test SELECT g, repeat('x', 100) FROM generate_series(1, 5000) g;
SELECT pg_current_wal_insert_lsn() AS mid_lsn \gset
ROLLBACK;
CHECKPOINT;
SET logical_decoding_work_mem = '64kB';
SELECT count(*) FROM pg_logical_slot_get_changes('dc_slot', :'mid_lsn', NULL);
SELECT aborted_txns, aborted_bytes FROM pg_stat_replication_slots
WHERE slot_name = 'dc_slot';
SELECT count(*) FROM pg_logical_slot_get_changes('dc_slot', NULL, NULL);
SELECT aborted_txns, aborted_bytes FROM pg_stat_replication_slots
WHERE slot_name = 'dc_slot';

With v1, after the first call aborted_txns = 1 and aborted_bytes =
1205000; after the second, aborted_txns = 2 and aborted_bytes =
2410000, for a single rolled-back transaction. The same sequence with
COMMIT instead of ROLLBACK gives total_txns = 1. I used upto_lsn here
as an easy way to stop decoding in that window; I'd expect a walsender
restart at the same point to behave the same way, but I haven't tested
that. With accounting only at the three places in comment 1, the same
repro gives aborted_txns = 1 and aborted_bytes = 1205000.

It would be good to add a test along these lines that checks for exact
values; the new tests all check for > 0, so they wouldn't catch this.

3.
The UpdateDecodingStats() call added to FreeDecodingContext() doesn't
seem to be needed once the early accounting is removed:

+ /* Flush any remaining stats, e.g. changes skipped after an early abort. */
+ UpdateDecodingStats(ctx);

DecodeAbort() already calls UpdateDecodingStats() after
ReorderBufferAbort()/ReorderBufferFinishPrepared(), and v1 adds one
after ReorderBufferAbortOld() in standby_decode(), so the remaining
accounting points are each followed by a flush. I removed it along
with the changes in comment 1, and the tests mentioned above still
pass.

Regards,
Ajin Cherian
Fujitsu Australia

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Melanie Plageman 2026-12-02 01:03:18 Re: Checkpointer write combining
Previous Message shihao zhong 2026-12-02 00:57:11 Re: pg_upgrade's get_rel_infos_query() query can sometimes be slow