On Wed, Dec 2, 2026 at 11:59 AM Ajin Cherian <[email protected]> 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
From c3d4ce1540386f7f6c8da88728e8b72d972f190f Mon Sep 17 00:00:00 2001
From: Rajesh Kumar Thandapani <[email protected]>
Date: Sun, 29 Nov 2026 16:56:53 +0000
Subject: [PATCH v2] Add aborted_txns and aborted_bytes to
 pg_stat_replication_slots.

Logical decoding must read, decode, and buffer WAL changes for in-progress
transactions before knowing whether they will commit or abort.  Previously,
pg_stat_replication_slots only tracked transactions sent to the output
plugin (total_txns and total_bytes), leaving aborted transactions hidden.
Since commit 072ee847ad4 discards already-aborted transactions at
logical_decoding_work_mem without spilling or streaming and commit
d3b6183dd98 exposes mem_exceeded_count, users could see mem_exceeded_count
increasing while spill_txns, stream_txns, and total_txns remained zero, or
see spill/stream counters inflated by transactions that later rolled back.

This commit adds aborted_txns and aborted_bytes to pg_stat_replication_slots
(and pg_stat_get_replication_slot()).  aborted_txns counts both top-level
transactions and subtransactions with decoded streamable changes (matching
spill_txns), and aborted_bytes tracks their decoded change bytes, including
changes previously spilled or streamed as well as changes discarded early or
skipped after abort detection.  Transactions without streamable changes in
the slot's target database/origin are excluded, and aborts encountered before
reaching a consistent snapshot or start_decoding_at are not counted.

Bumps PGSTAT_FILE_FORMAT_ID (catversion will also need to be bumped on
commit).
---
 contrib/test_decoding/expected/stats.out      | 299 ++++++++++++++++--
 contrib/test_decoding/sql/stats.sql           |  95 +++++-
 contrib/test_decoding/t/001_repl_stats.pl     |  22 +-
 doc/src/sgml/monitoring.sgml                  |  35 +-
 src/backend/catalog/system_views.sql          |   2 +
 src/backend/replication/logical/decode.c      |   1 +
 src/backend/replication/logical/logical.c     |  12 +-
 .../replication/logical/reorderbuffer.c       |  91 +++++-
 src/backend/utils/activity/pgstat_replslot.c  |   2 +
 src/backend/utils/adt/pgstatfuncs.c           |  24 +-
 src/include/catalog/pg_proc.dat               |   6 +-
 src/include/pgstat.h                          |   4 +-
 src/include/replication/reorderbuffer.h       |  13 +
 src/test/regress/expected/rules.out           |   4 +-
 14 files changed, 538 insertions(+), 72 deletions(-)

diff --git a/contrib/test_decoding/expected/stats.out b/contrib/test_decoding/expected/stats.out
index a9ead3c4..4ebbd044 100644
--- a/contrib/test_decoding/expected/stats.out
+++ b/contrib/test_decoding/expected/stats.out
@@ -37,12 +37,12 @@ SELECT pg_stat_force_next_flush();
  
 (1 row)
 
-SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
-       slot_name        | spill_txns | spill_count | total_txns | total_bytes | mem_exceeded_count 
-------------------------+------------+-------------+------------+-------------+--------------------
- regression_slot_stats1 | t          | t           | t          | t           | t
- regression_slot_stats2 | t          | t           | t          | t           | t
- regression_slot_stats3 | t          | t           | t          | t           | t
+SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots ORDER BY slot_name;
+       slot_name        | spill_txns | spill_count | total_txns | total_bytes | mem_exceeded_count | aborted_txns | aborted_bytes 
+------------------------+------------+-------------+------------+-------------+--------------------+--------------+---------------
+ regression_slot_stats1 | t          | t           | t          | t           | t                  | t            | t
+ regression_slot_stats2 | t          | t           | t          | t           | t                  | t            | t
+ regression_slot_stats3 | t          | t           | t          | t           | t                  | t            | t
 (3 rows)
 
 RESET logical_decoding_work_mem;
@@ -53,12 +53,12 @@ SELECT pg_stat_reset_replication_slot('regression_slot_stats1');
  
 (1 row)
 
-SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
-       slot_name        | spill_txns | spill_count | total_txns | total_bytes | mem_exceeded_count 
-------------------------+------------+-------------+------------+-------------+--------------------
- regression_slot_stats1 | t          | t           | f          | f           | t
- regression_slot_stats2 | t          | t           | t          | t           | t
- regression_slot_stats3 | t          | t           | t          | t           | t
+SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots ORDER BY slot_name;
+       slot_name        | spill_txns | spill_count | total_txns | total_bytes | mem_exceeded_count | aborted_txns | aborted_bytes 
+------------------------+------------+-------------+------------+-------------+--------------------+--------------+---------------
+ regression_slot_stats1 | t          | t           | f          | f           | t                  | t            | t
+ regression_slot_stats2 | t          | t           | t          | t           | t                  | t            | t
+ regression_slot_stats3 | t          | t           | t          | t           | t                  | t            | t
 (3 rows)
 
 -- reset stats for all slots
@@ -68,27 +68,27 @@ SELECT pg_stat_reset_replication_slot(NULL);
  
 (1 row)
 
-SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
-       slot_name        | spill_txns | spill_count | total_txns | total_bytes | mem_exceeded_count 
-------------------------+------------+-------------+------------+-------------+--------------------
- regression_slot_stats1 | t          | t           | f          | f           | t
- regression_slot_stats2 | t          | t           | f          | f           | t
- regression_slot_stats3 | t          | t           | f          | f           | t
+SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots ORDER BY slot_name;
+       slot_name        | spill_txns | spill_count | total_txns | total_bytes | mem_exceeded_count | aborted_txns | aborted_bytes 
+------------------------+------------+-------------+------------+-------------+--------------------+--------------+---------------
+ regression_slot_stats1 | t          | t           | f          | f           | t                  | t            | t
+ regression_slot_stats2 | t          | t           | f          | f           | t                  | t            | t
+ regression_slot_stats3 | t          | t           | f          | f           | t                  | t            | t
 (3 rows)
 
 -- verify accessing/resetting stats for non-existent slot does something reasonable
 SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
-  slot_name   | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | mem_exceeded_count | total_txns | total_bytes | slotsync_skip_count | slotsync_last_skip | stats_reset 
---------------+------------+-------------+-------------+-------------+--------------+--------------+--------------------+------------+-------------+---------------------+--------------------+-------------
- do-not-exist |          0 |           0 |           0 |           0 |            0 |            0 |                  0 |          0 |           0 |                   0 |                    | 
+  slot_name   | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | mem_exceeded_count | total_txns | total_bytes | aborted_txns | aborted_bytes | slotsync_skip_count | slotsync_last_skip | stats_reset 
+--------------+------------+-------------+-------------+-------------+--------------+--------------+--------------------+------------+-------------+--------------+---------------+---------------------+--------------------+-------------
+ do-not-exist |          0 |           0 |           0 |           0 |            0 |            0 |                  0 |          0 |           0 |            0 |             0 |                   0 |                    | 
 (1 row)
 
 SELECT pg_stat_reset_replication_slot('do-not-exist');
 ERROR:  replication slot "do-not-exist" does not exist
 SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
-  slot_name   | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | mem_exceeded_count | total_txns | total_bytes | slotsync_skip_count | slotsync_last_skip | stats_reset 
---------------+------------+-------------+-------------+-------------+--------------+--------------+--------------------+------------+-------------+---------------------+--------------------+-------------
- do-not-exist |          0 |           0 |           0 |           0 |            0 |            0 |                  0 |          0 |           0 |                   0 |                    | 
+  slot_name   | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | mem_exceeded_count | total_txns | total_bytes | aborted_txns | aborted_bytes | slotsync_skip_count | slotsync_last_skip | stats_reset 
+--------------+------------+-------------+-------------+-------------+--------------+--------------+--------------------+------------+-------------+--------------+---------------+---------------------+--------------------+-------------
+ do-not-exist |          0 |           0 |           0 |           0 |            0 |            0 |                  0 |          0 |           0 |            0 |             0 |                   0 |                    | 
 (1 row)
 
 -- spilling the xact
@@ -168,12 +168,255 @@ SELECT pg_stat_force_next_flush();
  
 (1 row)
 
-SELECT slot_name, spill_txns, spill_count, mem_exceeded_count > 0 as mem_exceeded_count FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats4_twophase';
-            slot_name            | spill_txns | spill_count | mem_exceeded_count 
----------------------------------+------------+-------------+--------------------
- regression_slot_stats4_twophase |          0 |           0 | t
+SELECT slot_name, spill_txns, spill_count, mem_exceeded_count > 0 AS mem_exceeded_count, aborted_txns > 0 AS aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats4_twophase';
+            slot_name            | spill_txns | spill_count | mem_exceeded_count | aborted_txns | aborted_bytes 
+---------------------------------+------------+-------------+--------------------+--------------+---------------
+ regression_slot_stats4_twophase |          0 |           0 | t                  | t            | t
 (1 row)
 
+-- aborted transaction stats (reuse regression_slot_stats3)
+SET logical_decoding_work_mem = '64MB';
+SELECT count(*) > 0 FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+ ?column? 
+----------
+ t
+(1 row)
+
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+ pg_stat_reset_replication_slot 
+--------------------------------
+ 
+(1 row)
+
+-- rollback with no decodable changes (DDL only) should not bump aborted stats
+BEGIN;
+CREATE TABLE stats_ddl_abort(data text);
+ROLLBACK;
+INSERT INTO stats_test VALUES ('flush-ddl-abort');
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+     3
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT slot_name, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | aborted_txns | aborted_bytes 
+------------------------+--------------+---------------
+ regression_slot_stats3 | t            | t
+(1 row)
+
+-- in-memory rollback (CHECKPOINT flushes the ABORT record without a committed xact)
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+ pg_stat_reset_replication_slot 
+--------------------------------
+ 
+(1 row)
+
+BEGIN;
+INSERT INTO stats_test VALUES ('abort-in-mem');
+ROLLBACK;
+CHECKPOINT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+     0
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT slot_name, spill_txns = 0 AS spill_txns, total_txns = 0 AS total_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | spill_txns | total_txns | aborted_txns | aborted_bytes 
+------------------------+------------+------------+--------------+---------------
+ regression_slot_stats3 | t          | t          |            1 | t
+(1 row)
+
+-- reset clears aborted stats
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+ pg_stat_reset_replication_slot 
+--------------------------------
+ 
+(1 row)
+
+SELECT slot_name, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | aborted_txns | aborted_bytes 
+------------------------+--------------+---------------
+ regression_slot_stats3 | t            | t
+(1 row)
+
+-- savepoint rollback inside committed xact bumps both total and aborted stats
+BEGIN;
+INSERT INTO stats_test VALUES ('top-commit');
+SAVEPOINT s1;
+INSERT INTO stats_test VALUES ('sub-abort');
+ROLLBACK TO SAVEPOINT s1;
+COMMIT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+     3
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT slot_name, total_txns > 0 AS total_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | total_txns | aborted_txns | aborted_bytes 
+------------------------+------------+--------------+---------------
+ regression_slot_stats3 | t          |            1 | t
+(1 row)
+
+-- stopping decoding after early abort discard but before ABORT must not
+-- double-count when resumed, and must match an identical in-memory rollback
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+ pg_stat_reset_replication_slot 
+--------------------------------
+ 
+(1 row)
+
+BEGIN;
+INSERT INTO stats_test SELECT repeat('x', 100) FROM generate_series(1, 1000);
+ROLLBACK;
+CHECKPOINT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+     0
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT aborted_bytes AS expected_aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3' \gset
+SELECT slot_name, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | aborted_txns | aborted_bytes 
+------------------------+--------------+---------------
+ regression_slot_stats3 |            1 | t
+(1 row)
+
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+ pg_stat_reset_replication_slot 
+--------------------------------
+ 
+(1 row)
+
+RESET logical_decoding_work_mem;
+BEGIN;
+INSERT INTO stats_test SELECT repeat('x', 100) FROM generate_series(1, 1000);
+SELECT pg_current_wal_insert_lsn() AS mid_lsn \gset
+ROLLBACK;
+CHECKPOINT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', :'mid_lsn', NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+     0
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT slot_name, spill_txns, mem_exceeded_count > 0 AS mem_exceeded_count, aborted_txns, aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | spill_txns | mem_exceeded_count | aborted_txns | aborted_bytes 
+------------------------+------------+--------------------+--------------+---------------
+ regression_slot_stats3 |          0 | t                  |            0 |             0
+(1 row)
+
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+     0
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT slot_name, spill_txns, aborted_txns, aborted_bytes = :expected_aborted_bytes AS exact_aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | spill_txns | aborted_txns | exact_aborted_bytes 
+------------------------+------------+--------------+---------------------
+ regression_slot_stats3 |          0 |            1 | t
+(1 row)
+
+-- spilled-then-aborted xact (immediate mode forces spilling before abort)
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+ pg_stat_reset_replication_slot 
+--------------------------------
+ 
+(1 row)
+
+SET debug_logical_replication_streaming = 'immediate';
+BEGIN;
+INSERT INTO stats_test SELECT 'abort-spill-' || g.i FROM generate_series(1, 50) g(i);
+ROLLBACK;
+INSERT INTO stats_test VALUES ('flush-abort-spill');
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+     3
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT slot_name, spill_txns > 0 AS spill_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | spill_txns | aborted_txns | aborted_bytes 
+------------------------+------------+--------------+---------------
+ regression_slot_stats3 | t          |            1 | t
+(1 row)
+
+-- streamed-then-aborted xact
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+ pg_stat_reset_replication_slot 
+--------------------------------
+ 
+(1 row)
+
+BEGIN;
+INSERT INTO stats_test SELECT 'abort-stream-' || g.i FROM generate_series(1, 50) g(i);
+ROLLBACK;
+INSERT INTO stats_test VALUES ('flush-abort-stream');
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'stream-changes', '1', 'skip-empty-xacts', '1');
+ count 
+-------
+     4
+(1 row)
+
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+ 
+(1 row)
+
+SELECT slot_name, stream_txns > 0 AS stream_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+       slot_name        | stream_txns | aborted_txns | aborted_bytes 
+------------------------+-------------+--------------+---------------
+ regression_slot_stats3 | t           |            1 | t
+(1 row)
+
+RESET debug_logical_replication_streaming;
 DROP TABLE stats_test;
 SELECT pg_drop_replication_slot('regression_slot_stats1'),
     pg_drop_replication_slot('regression_slot_stats2'),
diff --git a/contrib/test_decoding/sql/stats.sql b/contrib/test_decoding/sql/stats.sql
index 6661dbcb..802d0793 100644
--- a/contrib/test_decoding/sql/stats.sql
+++ b/contrib/test_decoding/sql/stats.sql
@@ -15,16 +15,16 @@ SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats1', NULL,
 SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats2', NULL, NULL, 'skip-empty-xacts', '1');
 SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
 SELECT pg_stat_force_next_flush();
-SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
+SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots ORDER BY slot_name;
 RESET logical_decoding_work_mem;
 
 -- reset stats for one slot, others should be unaffected
 SELECT pg_stat_reset_replication_slot('regression_slot_stats1');
-SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
+SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots ORDER BY slot_name;
 
 -- reset stats for all slots
 SELECT pg_stat_reset_replication_slot(NULL);
-SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
+SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots ORDER BY slot_name;
 
 -- verify accessing/resetting stats for non-existent slot does something reasonable
 SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
@@ -68,7 +68,94 @@ SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats4_twophas
 -- mem_exceeded_count should theoretically be 1 but we check if >0 here since it's
 -- more flexible for potential future changes and adequate for the testing purpose.
 SELECT pg_stat_force_next_flush();
-SELECT slot_name, spill_txns, spill_count, mem_exceeded_count > 0 as mem_exceeded_count FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats4_twophase';
+SELECT slot_name, spill_txns, spill_count, mem_exceeded_count > 0 AS mem_exceeded_count, aborted_txns > 0 AS aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats4_twophase';
+
+-- aborted transaction stats (reuse regression_slot_stats3)
+SET logical_decoding_work_mem = '64MB';
+SELECT count(*) > 0 FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+
+-- rollback with no decodable changes (DDL only) should not bump aborted stats
+BEGIN;
+CREATE TABLE stats_ddl_abort(data text);
+ROLLBACK;
+INSERT INTO stats_test VALUES ('flush-ddl-abort');
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT slot_name, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+
+-- in-memory rollback (CHECKPOINT flushes the ABORT record without a committed xact)
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+BEGIN;
+INSERT INTO stats_test VALUES ('abort-in-mem');
+ROLLBACK;
+CHECKPOINT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT slot_name, spill_txns = 0 AS spill_txns, total_txns = 0 AS total_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+
+-- reset clears aborted stats
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+SELECT slot_name, aborted_txns = 0 AS aborted_txns, aborted_bytes = 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+
+-- savepoint rollback inside committed xact bumps both total and aborted stats
+BEGIN;
+INSERT INTO stats_test VALUES ('top-commit');
+SAVEPOINT s1;
+INSERT INTO stats_test VALUES ('sub-abort');
+ROLLBACK TO SAVEPOINT s1;
+COMMIT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT slot_name, total_txns > 0 AS total_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+
+-- stopping decoding after early abort discard but before ABORT must not
+-- double-count when resumed, and must match an identical in-memory rollback
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+BEGIN;
+INSERT INTO stats_test SELECT repeat('x', 100) FROM generate_series(1, 1000);
+ROLLBACK;
+CHECKPOINT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT aborted_bytes AS expected_aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3' \gset
+SELECT slot_name, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+RESET logical_decoding_work_mem;
+BEGIN;
+INSERT INTO stats_test SELECT repeat('x', 100) FROM generate_series(1, 1000);
+SELECT pg_current_wal_insert_lsn() AS mid_lsn \gset
+ROLLBACK;
+CHECKPOINT;
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', :'mid_lsn', NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT slot_name, spill_txns, mem_exceeded_count > 0 AS mem_exceeded_count, aborted_txns, aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT slot_name, spill_txns, aborted_txns, aborted_bytes = :expected_aborted_bytes AS exact_aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+
+-- spilled-then-aborted xact (immediate mode forces spilling before abort)
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+SET debug_logical_replication_streaming = 'immediate';
+BEGIN;
+INSERT INTO stats_test SELECT 'abort-spill-' || g.i FROM generate_series(1, 50) g(i);
+ROLLBACK;
+INSERT INTO stats_test VALUES ('flush-abort-spill');
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT slot_name, spill_txns > 0 AS spill_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+
+-- streamed-then-aborted xact
+SELECT pg_stat_reset_replication_slot('regression_slot_stats3');
+BEGIN;
+INSERT INTO stats_test SELECT 'abort-stream-' || g.i FROM generate_series(1, 50) g(i);
+ROLLBACK;
+INSERT INTO stats_test VALUES ('flush-abort-stream');
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'stream-changes', '1', 'skip-empty-xacts', '1');
+SELECT pg_stat_force_next_flush();
+SELECT slot_name, stream_txns > 0 AS stream_txns, aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3';
+RESET debug_logical_replication_streaming;
 
 DROP TABLE stats_test;
 SELECT pg_drop_replication_slot('regression_slot_stats1'),
diff --git a/contrib/test_decoding/t/001_repl_stats.pl b/contrib/test_decoding/t/001_repl_stats.pl
index 47e26dab..de0d7c34 100644
--- a/contrib/test_decoding/t/001_repl_stats.pl
+++ b/contrib/test_decoding/t/001_repl_stats.pl
@@ -26,7 +26,9 @@ sub test_slot_stats
 	my $result = $node->safe_psql(
 		'postgres', qq[
 		SELECT slot_name, total_txns > 0 AS total_txn,
-			   total_bytes > 0 AS total_bytes
+			   total_bytes > 0 AS total_bytes,
+			   aborted_txns > 0 AS aborted_txns,
+			   aborted_bytes > 0 AS aborted_bytes
 			   FROM pg_stat_replication_slots
 			   ORDER BY slot_name]);
 	is($result, $expected, $msg);
@@ -44,7 +46,10 @@ $node->safe_psql(
 	SELECT pg_create_logical_replication_slot('regression_slot4', 'test_decoding');
 ]);
 
-# Insert some data.
+# Insert some data (both aborted and committed transactions; the committed
+# transaction afterwards ensures WAL including the ABORT record is flushed).
+$node->safe_psql('postgres',
+	"BEGIN; INSERT INTO test_repl_stat values(generate_series(6, 10)); ROLLBACK;");
 $node->safe_psql('postgres',
 	"INSERT INTO test_repl_stat values(generate_series(1, 5));");
 
@@ -65,7 +70,8 @@ $node->poll_query_until(
 	'postgres', qq[
 	SELECT count(slot_name) >= 4 FROM pg_stat_replication_slots
 	WHERE slot_name ~ 'regression_slot'
-	AND total_txns > 0 AND total_bytes > 0;
+	AND total_txns > 0 AND total_bytes > 0
+	AND aborted_txns > 0 AND aborted_bytes > 0;
 ]) or die "Timed out while waiting for statistics to be updated";
 
 # Test to drop one of the replication slot and verify replication statistics data is
@@ -80,9 +86,9 @@ $node->start;
 # restart.
 test_slot_stats(
 	$node,
-	qq(regression_slot1|t|t
-regression_slot2|t|t
-regression_slot3|t|t),
+	qq(regression_slot1|t|t|t|t
+regression_slot2|t|t|t|t
+regression_slot3|t|t|t|t),
 	'check replication statistics are updated');
 
 # Test to remove one of the replication slots and adjust
@@ -104,8 +110,8 @@ $node->start;
 # restart.
 test_slot_stats(
 	$node,
-	qq(regression_slot1|t|t
-regression_slot2|t|t),
+	qq(regression_slot1|t|t|t|t
+regression_slot2|t|t|t|t),
 	'check replication statistics after removing the slot file');
 
 # cleanup
diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index 68803c58..09252f76 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -1837,7 +1837,8 @@ description | Waiting for a newly initialized WAL file to reach durable storage
         Number of decoded transactions sent to the decoding output plugin for
         this slot. This counts top-level transactions only, and is not incremented
         for subtransactions. Note that this includes the transactions that are
-        streamed and/or spilled.
+        streamed and/or spilled, but excludes aborted transactions that were
+        not streamed or prepared.
        </para></entry>
      </row>
 
@@ -1848,7 +1849,37 @@ description | Waiting for a newly initialized WAL file to reach durable storage
        <para>
         Amount of transaction data decoded for sending transactions to the
         decoding output plugin while decoding changes from WAL for this slot.
-        Note that this includes data that is streamed and/or spilled.
+        Note that this includes data that is streamed and/or spilled, but
+        excludes data from aborted transactions that were not streamed or
+        prepared.
+       </para>
+      </entry>
+     </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+        <structfield>aborted_txns</structfield> <type>bigint</type>
+       </para>
+       <para>
+        Number of aborted transactions with decoded changes encountered while
+        decoding changes from WAL for this slot. The counter gets incremented
+        for both top-level transactions and subtransactions. Note that this
+        includes transactions that were spilled, streamed, prepared, or
+        discarded early upon exceeding
+        <literal>logical_decoding_work_mem</literal>.
+       </para>
+      </entry>
+     </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+        <structfield>aborted_bytes</structfield> <type>bigint</type>
+       </para>
+       <para>
+        Amount of decoded transaction data belonging to aborted transactions
+        encountered while decoding changes from WAL for this slot. Note that
+        this includes data that was spilled, streamed, prepared, discarded from
+        memory, or skipped after detecting a transaction abort.
        </para>
       </entry>
      </row>
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 6dce9192..e5a53f31 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -1139,6 +1139,8 @@ CREATE VIEW pg_stat_replication_slots AS
             s.mem_exceeded_count,
             s.total_txns,
             s.total_bytes,
+            s.aborted_txns,
+            s.aborted_bytes,
             s.slotsync_skip_count,
             s.slotsync_last_skip,
             s.stats_reset
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index 031bb100..3bdbe72e 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -399,6 +399,7 @@ standby_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 				 * transactions are in progress.
 				 */
 				ReorderBufferAbortOld(ctx->reorder, running->oldestRunningXid);
+				UpdateDecodingStats(ctx);
 			}
 			break;
 		case XLOG_STANDBY_LOCK:
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 95760981..c72d9bc6 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2070,10 +2070,10 @@ UpdateDecodingStats(LogicalDecodingContext *ctx)
 
 	/* Nothing to do if we don't have any replication stats to be sent. */
 	if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0 &&
-		rb->memExceededCount <= 0)
+		rb->memExceededCount <= 0 && rb->abortedBytes <= 0)
 		return;
 
-	elog(DEBUG2, "UpdateDecodingStats: updating stats %p %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
+	elog(DEBUG2, "UpdateDecodingStats: updating stats %p %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
 		 rb,
 		 rb->spillTxns,
 		 rb->spillCount,
@@ -2083,7 +2083,9 @@ UpdateDecodingStats(LogicalDecodingContext *ctx)
 		 rb->streamBytes,
 		 rb->memExceededCount,
 		 rb->totalTxns,
-		 rb->totalBytes);
+		 rb->totalBytes,
+		 rb->abortedTxns,
+		 rb->abortedBytes);
 
 	repSlotStat.spill_txns = rb->spillTxns;
 	repSlotStat.spill_count = rb->spillCount;
@@ -2094,6 +2096,8 @@ UpdateDecodingStats(LogicalDecodingContext *ctx)
 	repSlotStat.mem_exceeded_count = rb->memExceededCount;
 	repSlotStat.total_txns = rb->totalTxns;
 	repSlotStat.total_bytes = rb->totalBytes;
+	repSlotStat.aborted_txns = rb->abortedTxns;
+	repSlotStat.aborted_bytes = rb->abortedBytes;
 
 	pgstat_report_replslot(ctx->slot, &repSlotStat);
 
@@ -2106,6 +2110,8 @@ UpdateDecodingStats(LogicalDecodingContext *ctx)
 	rb->memExceededCount = 0;
 	rb->totalTxns = 0;
 	rb->totalBytes = 0;
+	rb->abortedTxns = 0;
+	rb->abortedBytes = 0;
 }
 
 /*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 2917d971..612e359b 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -396,6 +396,8 @@ ReorderBufferAllocate(void)
 	buffer->memExceededCount = 0;
 	buffer->totalTxns = 0;
 	buffer->totalBytes = 0;
+	buffer->abortedTxns = 0;
+	buffer->abortedBytes = 0;
 
 	buffer->current_restart_decoding_lsn = InvalidXLogRecPtr;
 
@@ -814,9 +816,23 @@ ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
 						 ReorderBufferChange *change, bool toast_insert)
 {
 	ReorderBufferTXN *txn;
+	bool		is_streamable;
+	Size		sz;
 
 	txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
 
+	/*
+	 * The changes that are sent downstream are considered streamable.  We
+	 * remember such transactions so that only those will later be considered
+	 * for streaming, and only count these changes in decoded_bytes.
+	 */
+	is_streamable = (change->action == REORDER_BUFFER_CHANGE_INSERT ||
+					 change->action == REORDER_BUFFER_CHANGE_UPDATE ||
+					 change->action == REORDER_BUFFER_CHANGE_DELETE ||
+					 change->action == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT ||
+					 change->action == REORDER_BUFFER_CHANGE_TRUNCATE ||
+					 change->action == REORDER_BUFFER_CHANGE_MESSAGE);
+
 	/*
 	 * If we have detected that the transaction is aborted while streaming the
 	 * previous changes or by checking its CLOG, there is no point in
@@ -824,6 +840,9 @@ ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
 	 */
 	if (rbtxn_is_aborted(txn))
 	{
+		if (is_streamable)
+			txn->decoded_bytes += ReorderBufferChangeSize(change);
+
 		/*
 		 * We don't need to update memory accounting for this change as we
 		 * have not added it to the queue yet.
@@ -832,21 +851,14 @@ ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
 		return;
 	}
 
-	/*
-	 * The changes that are sent downstream are considered streamable.  We
-	 * remember such transactions so that only those will later be considered
-	 * for streaming.
-	 */
-	if (change->action == REORDER_BUFFER_CHANGE_INSERT ||
-		change->action == REORDER_BUFFER_CHANGE_UPDATE ||
-		change->action == REORDER_BUFFER_CHANGE_DELETE ||
-		change->action == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT ||
-		change->action == REORDER_BUFFER_CHANGE_TRUNCATE ||
-		change->action == REORDER_BUFFER_CHANGE_MESSAGE)
+	sz = ReorderBufferChangeSize(change);
+
+	if (is_streamable)
 	{
 		ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn);
 
 		toptxn->txn_flags |= RBTXN_HAS_STREAMABLE_CHANGE;
+		txn->decoded_bytes += sz;
 	}
 
 	change->lsn = lsn;
@@ -858,8 +870,7 @@ ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
 	txn->nentries_mem++;
 
 	/* update memory accounting information */
-	ReorderBufferChangeMemoryUpdate(rb, change, NULL, true,
-									ReorderBufferChangeSize(change));
+	ReorderBufferChangeMemoryUpdate(rb, change, NULL, true, sz);
 
 	/* process partial change */
 	ReorderBufferProcessPartialChange(rb, txn, change, toast_insert);
@@ -1762,6 +1773,53 @@ ReorderBufferTruncateTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, bool txn_prep
 	txn->nentries = 0;
 }
 
+/*
+ * Returns true if we should report abort statistics at the current WAL
+ * position (i.e. we have a consistent snapshot and have reached
+ * start_decoding_at).
+ */
+static inline bool
+ReorderBufferCanReportAbortStats(ReorderBuffer *rb)
+{
+	LogicalDecodingContext *ctx = rb->private_data;
+	SnapBuild  *builder = ctx->snapshot_builder;
+
+	return SnapBuildCurrentState(builder) >= SNAPBUILD_CONSISTENT &&
+		!SnapBuildXactNeedsSkip(builder, ctx->reader->ReadRecPtr);
+}
+
+/*
+ * Account decoded transaction data belonging to an aborted transaction (and
+ * any attached subtransactions) in the reorder buffer statistics.
+ */
+static void
+ReorderBufferAccountAbortedTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
+{
+	dlist_iter	iter;
+
+	if (!ReorderBufferCanReportAbortStats(rb))
+		return;
+
+	dlist_foreach(iter, &txn->subtxns)
+	{
+		ReorderBufferTXN *subtxn;
+
+		subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
+
+		Assert(rbtxn_is_known_subxact(subtxn));
+		Assert(subtxn->nsubtxns == 0);
+
+		ReorderBufferAccountAbortedTXN(rb, subtxn);
+	}
+
+	if (txn->decoded_bytes > 0)
+	{
+		rb->abortedTxns++;
+		rb->abortedBytes += txn->decoded_bytes;
+		txn->decoded_bytes = 0;
+	}
+}
+
 /*
  * Check the transaction status by CLOG lookup and discard all changes if
  * the transaction is aborted. The transaction status is cached in
@@ -3101,7 +3159,10 @@ ReorderBufferFinishPrepared(ReorderBuffer *rb, TransactionId xid,
 	if (is_commit)
 		rb->commit_prepared(rb, txn, commit_lsn);
 	else
+	{
 		rb->rollback_prepared(rb, txn, prepare_end_lsn, prepare_time);
+		ReorderBufferAccountAbortedTXN(rb, txn);
+	}
 
 	/* cleanup: make sure there's no cache pollution */
 	ReorderBufferExecuteInvalidations(txn->ninvalidations,
@@ -3156,6 +3217,8 @@ ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
 	/* cosmetic... */
 	txn->final_lsn = lsn;
 
+	ReorderBufferAccountAbortedTXN(rb, txn);
+
 	/* remove potential on-disk data, and deallocate */
 	ReorderBufferCleanupTXN(rb, txn);
 }
@@ -3193,6 +3256,8 @@ ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
 			if (rbtxn_is_streamed(txn))
 				rb->stream_abort(rb, txn, InvalidXLogRecPtr);
 
+			ReorderBufferAccountAbortedTXN(rb, txn);
+
 			/* remove potential on-disk data, and deallocate this tx */
 			ReorderBufferCleanupTXN(rb, txn);
 		}
diff --git a/src/backend/utils/activity/pgstat_replslot.c b/src/backend/utils/activity/pgstat_replslot.c
index a32b70a0..60daa6d0 100644
--- a/src/backend/utils/activity/pgstat_replslot.c
+++ b/src/backend/utils/activity/pgstat_replslot.c
@@ -96,6 +96,8 @@ pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *re
 	REPLSLOT_ACC(mem_exceeded_count);
 	REPLSLOT_ACC(total_txns);
 	REPLSLOT_ACC(total_bytes);
+	REPLSLOT_ACC(aborted_txns);
+	REPLSLOT_ACC(aborted_bytes);
 #undef REPLSLOT_ACC
 
 	pgstat_unlock_entry(entry_ref);
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 64b6f605..d9764000 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -2330,7 +2330,7 @@ pg_stat_get_archiver(PG_FUNCTION_ARGS)
 Datum
 pg_stat_get_replication_slot(PG_FUNCTION_ARGS)
 {
-#define PG_STAT_GET_REPLICATION_SLOT_COLS 13
+#define PG_STAT_GET_REPLICATION_SLOT_COLS 15
 	text	   *slotname_text = PG_GETARG_TEXT_P(0);
 	NameData	slotname;
 	TupleDesc	tupdesc;
@@ -2361,11 +2361,15 @@ pg_stat_get_replication_slot(PG_FUNCTION_ARGS)
 					   INT8OID, -1, 0);
 	TupleDescInitEntry(tupdesc, (AttrNumber) 10, "total_bytes",
 					   INT8OID, -1, 0);
-	TupleDescInitEntry(tupdesc, (AttrNumber) 11, "slotsync_skip_count",
+	TupleDescInitEntry(tupdesc, (AttrNumber) 11, "aborted_txns",
 					   INT8OID, -1, 0);
-	TupleDescInitEntry(tupdesc, (AttrNumber) 12, "slotsync_last_skip",
+	TupleDescInitEntry(tupdesc, (AttrNumber) 12, "aborted_bytes",
+					   INT8OID, -1, 0);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 13, "slotsync_skip_count",
+					   INT8OID, -1, 0);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 14, "slotsync_last_skip",
 					   TIMESTAMPTZOID, -1, 0);
-	TupleDescInitEntry(tupdesc, (AttrNumber) 13, "stats_reset",
+	TupleDescInitEntry(tupdesc, (AttrNumber) 15, "stats_reset",
 					   TIMESTAMPTZOID, -1, 0);
 	TupleDescFinalize(tupdesc);
 	BlessTupleDesc(tupdesc);
@@ -2392,17 +2396,19 @@ pg_stat_get_replication_slot(PG_FUNCTION_ARGS)
 	values[7] = Int64GetDatum(slotent->mem_exceeded_count);
 	values[8] = Int64GetDatum(slotent->total_txns);
 	values[9] = Int64GetDatum(slotent->total_bytes);
-	values[10] = Int64GetDatum(slotent->slotsync_skip_count);
+	values[10] = Int64GetDatum(slotent->aborted_txns);
+	values[11] = Int64GetDatum(slotent->aborted_bytes);
+	values[12] = Int64GetDatum(slotent->slotsync_skip_count);
 
 	if (slotent->slotsync_last_skip == 0)
-		nulls[11] = true;
+		nulls[13] = true;
 	else
-		values[11] = TimestampTzGetDatum(slotent->slotsync_last_skip);
+		values[13] = TimestampTzGetDatum(slotent->slotsync_last_skip);
 
 	if (slotent->stat_reset_timestamp == 0)
-		nulls[12] = true;
+		nulls[14] = true;
 	else
-		values[12] = TimestampTzGetDatum(slotent->stat_reset_timestamp);
+		values[14] = TimestampTzGetDatum(slotent->stat_reset_timestamp);
 
 	/* Returns the record as Datum */
 	PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 18b4be83..1805f329 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -5729,9 +5729,9 @@
 { oid => '6169', descr => 'statistics: information about replication slot',
   proname => 'pg_stat_get_replication_slot', provolatile => 's',
   proparallel => 'r', prorettype => 'record', proargtypes => 'text',
-  proallargtypes => '{text,text,int8,int8,int8,int8,int8,int8,int8,int8,int8,int8,timestamptz,timestamptz}',
-  proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o}',
-  proargnames => '{slot_name,slot_name,spill_txns,spill_count,spill_bytes,stream_txns,stream_count,stream_bytes,mem_exceeded_count,total_txns,total_bytes,slotsync_skip_count,slotsync_last_skip,stats_reset}',
+  proallargtypes => '{text,text,int8,int8,int8,int8,int8,int8,int8,int8,int8,int8,int8,int8,timestamptz,timestamptz}',
+  proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}',
+  proargnames => '{slot_name,slot_name,spill_txns,spill_count,spill_bytes,stream_txns,stream_count,stream_bytes,mem_exceeded_count,total_txns,total_bytes,aborted_txns,aborted_bytes,slotsync_skip_count,slotsync_last_skip,stats_reset}',
   prosrc => 'pg_stat_get_replication_slot' },
 
 { oid => '6230', descr => 'statistics: check if a stats object exists',
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 9b99062f..31484b06 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -275,7 +275,7 @@ typedef struct PgStat_TableXactStatus
  * ------------------------------------------------------------
  */
 
-#define PGSTAT_FILE_FORMAT_ID	0x01A5BCBD
+#define PGSTAT_FILE_FORMAT_ID	0x01A5BCBE
 
 typedef struct PgStat_ArchiverStats
 {
@@ -479,6 +479,8 @@ typedef struct PgStat_StatReplSlotEntry
 	PgStat_Counter mem_exceeded_count;
 	PgStat_Counter total_txns;
 	PgStat_Counter total_bytes;
+	PgStat_Counter aborted_txns;
+	PgStat_Counter aborted_bytes;
 	PgStat_Counter slotsync_skip_count;
 	TimestampTz slotsync_last_skip;
 	TimestampTz stat_reset_timestamp;
diff --git a/src/include/replication/reorderbuffer.h b/src/include/replication/reorderbuffer.h
index 612a7a81..5b0622a4 100644
--- a/src/include/replication/reorderbuffer.h
+++ b/src/include/replication/reorderbuffer.h
@@ -466,6 +466,12 @@ typedef struct ReorderBufferTXN
 	/* Size of top-transaction including sub-transactions. */
 	Size		total_size;
 
+	/*
+	 * Total bytes of decoded streamable changes for this transaction
+	 * (in-memory, spilled, streamed, or skipped after early abort detection).
+	 */
+	uint64		decoded_bytes;
+
 	/*
 	 * Private data pointer of the output plugin.
 	 */
@@ -706,6 +712,13 @@ struct ReorderBuffer
 	 */
 	int64		totalTxns;		/* total number of transactions sent */
 	int64		totalBytes;		/* total amount of data decoded */
+
+	/*
+	 * Statistics about aborted transactions decoded. Includes both toplevel
+	 * transactions and subtransactions.
+	 */
+	int64		abortedTxns;	/* number of aborted transactions */
+	int64		abortedBytes;	/* amount of data decoded for aborted txns */
 };
 
 
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index cf00b58f..54beba66 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -2278,11 +2278,13 @@ pg_stat_replication_slots| SELECT s.slot_name,
     s.mem_exceeded_count,
     s.total_txns,
     s.total_bytes,
+    s.aborted_txns,
+    s.aborted_bytes,
     s.slotsync_skip_count,
     s.slotsync_last_skip,
     s.stats_reset
    FROM pg_replication_slots r,
-    LATERAL pg_stat_get_replication_slot((r.slot_name)::text) s(slot_name, spill_txns, spill_count, spill_bytes, stream_txns, stream_count, stream_bytes, mem_exceeded_count, total_txns, total_bytes, slotsync_skip_count, slotsync_last_skip, stats_reset)
+    LATERAL pg_stat_get_replication_slot((r.slot_name)::text) s(slot_name, spill_txns, spill_count, spill_bytes, stream_txns, stream_count, stream_bytes, mem_exceeded_count, total_txns, total_bytes, aborted_txns, aborted_bytes, slotsync_skip_count, slotsync_last_skip, stats_reset)
   WHERE (r.datoid IS NOT NULL);
 pg_stat_slru| SELECT name,
     blks_zeroed,
-- 
2.56.1.911.g883d8bcb20-goog

Reply via email to