Re: Add aborted_txns and aborted_bytes to pg_stat_replication_slots

From: Rajesh Kumar Thandapani <rajeshkumarth(at)google(dot)com>
To: Ajin Cherian <itsajin(at)gmail(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 11:37:45
Message-ID: CAJoZ81s44cTyq-Oh9DtNreh02iucNsXS5BaNWJ9iw65OKi5h0g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Dec 2, 2026 at 11:59 AM Ajin Cherian <itsajin(at)gmail(dot)com> wrote:
>
> 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.
> ...
> 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.

Thanks for the review and for testing the simplification locally! You are
right - since decoded_bytes survives ReorderBufferTruncateTXN(), spilling,
and streaming, accounting only at the three final cleanup points
(ReorderBufferAbort, ReorderBufferAbortOld, and the rollback branch of
ReorderBufferFinishPrepared) is much cleaner. In v2 I removed the three
early accounting points along with RBTXN_ABORT_COUNTED and
rbtxn_is_abort_counted(), and kept only decoded_bytes accumulation in the
rbtxn_is_aborted() path of ReorderBufferQueueChange().

> 2.
> The early accounting can also count the same transaction twice.
> ...
> 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.

Good catch on the mid-transaction stop/resume double-counting bug. Moving
accounting to the three cleanup points fixes it so the transaction is only
counted once when its abort record is reached.

In v2 I updated the new rollback tests in contrib/test_decoding/sql/stats.sql
to assert exact aborted_txns values, and added a test based on your repro
that stops decoding at mid_lsn after early abort discard (asserting
aborted_txns = 0 and aborted_bytes = 0) and then resumes through ROLLBACK
(asserting aborted_txns = 1 and matching aborted_bytes against an identical
in-memory rollback so the byte check stays portable across 32-bit and 64-bit
platforms).

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

Removed from FreeDecodingContext() in v2 as well, since DecodeAbort() and
standby_decode() already flush stats right after the three remaining
accounting sites.

Attached is the updated v2 patch (rebased on current master), passing make
check and contrib/test_decoding checks.

Regards,
Rajesh Kumar Thandapani

Attachment Content-Type Size
v2-0001-Add-aborted_txns-and-aborted_bytes-to-pg_stat_rep.patch application/x-patch 48.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2026-12-02 11:41:48 Re: [PATCH 0/2] Parallelizing the archiver
Previous Message Etsuro Fujita 2026-12-02 11:31:40 Re: postgres_fdw: statistics import can silently fall back to sampling across encodings