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.
Attached is a v1 patch that adds aborted_txns and aborted_bytes to pg_stat_replication_slots (and pg_stat_get_replication_slot). It tracks decoded changes for transactions that roll back normally, abort at logical_decoding_work_mem, abort concurrently during streaming or prepare, roll back via ROLLBACK PREPARED, or are cleaned up after a crash via ReorderBufferAbortOld. A couple of design choices I would appreciate feedback on: 1. aborted_txns counts both top-level transactions and subtransactions (matching spill_txns), since rolled-back savepoints inside a committed transaction also do decoding work. Let me know if people prefer counting top-level transactions only (like total_txns). 2. aborted_bytes currently counts only streamable DML and message changes (excluding internal snapshot and invalidation changes) so that empty transactions receiving distributed snapshots are not counted. This does mean pure-DDL aborts are excluded, and aborted_bytes can be slightly smaller than spill_bytes if a transaction mixed DDL and DML. Happy to adjust if folks prefer counting all non-snapshot queued changes instead. 3. Aborts encountered before reaching a consistent snapshot or start_decoding_at are skipped so slot creation and restarts do not pollute or re-count stats. The patch includes docs and tests in contrib/test_decoding (including persistence across restart in 001_repl_stats.pl) and passes make check and contrib/test_decoding checks. Feedback and review comments are welcome. Regards, Rajesh Kumar Thandapani
From ce070689b0d6e6464e74faa1da8a62e9d3f2d676 Mon Sep 17 00:00:00 2001 From: Rajesh Kumar Thandapani <[email protected]> Date: Sun, 29 Nov 2026 16:56:53 +0000 Subject: [PATCH v1] 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 | 221 +++++++++++++++--- contrib/test_decoding/sql/stats.sql | 70 +++++- 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 | 15 +- .../replication/logical/reorderbuffer.c | 115 +++++++-- 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 | 20 ++ src/test/regress/expected/rules.out | 4 +- 14 files changed, 469 insertions(+), 72 deletions(-) diff --git a/contrib/test_decoding/expected/stats.out b/contrib/test_decoding/expected/stats.out index a9ead3c..30bbf4c 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,177 @@ 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 > 0 AS 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 | t | 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 > 0 AS 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 | t | t +(1 row) + +RESET logical_decoding_work_mem; +-- 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 > 0 AS 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 | t | 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 > 0 AS 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 | t | 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 6661dbc..85a5912 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,69 @@ 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 > 0 AS 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 > 0 AS aborted_txns, aborted_bytes > 0 AS aborted_bytes FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats3'; +RESET logical_decoding_work_mem; + +-- 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 > 0 AS 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 > 0 AS 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 47e26da..de0d7c3 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 68803c5..09252f7 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 6dce919..e5a53f3 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 031bb10..3bdbe72 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 9576098..b3dcbea 100644 --- a/src/backend/replication/logical/logical.c +++ b/src/backend/replication/logical/logical.c @@ -782,6 +782,9 @@ DecodingContextFindStartpoint(LogicalDecodingContext *ctx) void FreeDecodingContext(LogicalDecodingContext *ctx) { + /* Flush any remaining stats, e.g. changes skipped after an early abort. */ + UpdateDecodingStats(ctx); + if (ctx->callbacks.shutdown_cb != NULL) shutdown_cb_wrapper(ctx); @@ -2070,10 +2073,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 +2086,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 +2099,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 +2113,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 2917d97..229df89 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -278,6 +278,7 @@ static void ReorderBufferTruncateTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, bool txn_prepared); static void ReorderBufferMaybeMarkTXNStreamed(ReorderBuffer *rb, ReorderBufferTXN *txn); static bool ReorderBufferCheckAndTruncateAbortedTXN(ReorderBuffer *rb, ReorderBufferTXN *txn); +static inline bool ReorderBufferCanReportAbortStats(ReorderBuffer *rb); static void ReorderBufferCleanupSerializedTXNs(const char *slotname); static void ReorderBufferSerializedPath(char *path, ReplicationSlot *slot, TransactionId xid, XLogSegNo segno); @@ -396,6 +397,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 +817,24 @@ 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 real changes in decoded_bytes / + * abortedBytes. + */ + 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 +842,24 @@ ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, */ if (rbtxn_is_aborted(txn)) { + if (is_streamable) + { + sz = ReorderBufferChangeSize(change); + + 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; + } + /* * We don't need to update memory accounting for this change as we * have not added it to the queue yet. @@ -832,21 +868,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 +887,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 +1790,58 @@ 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) + { + if (!rbtxn_is_abort_counted(txn)) + { + rb->abortedTxns++; + txn->txn_flags |= RBTXN_ABORT_COUNTED; + } + + 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 @@ -1816,6 +1896,7 @@ ReorderBufferCheckAndTruncateAbortedTXN(ReorderBuffer *rb, ReorderBufferTXN *txn * and the toast reconstruction data. The full cleanup will happen as part * of decoding ABORT record of this transaction. */ + ReorderBufferAccountAbortedTXN(rb, txn); ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn)); ReorderBufferToastReset(rb, txn); @@ -2802,6 +2883,7 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, /* Remember the transaction is aborted. */ Assert(!rbtxn_is_committed(curtxn)); curtxn->txn_flags |= RBTXN_IS_ABORTED; + ReorderBufferAccountAbortedTXN(rb, curtxn); /* Mark the transaction is streamed if appropriate */ if (stream_started) @@ -3101,7 +3183,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 +3241,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 +3280,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 a32b70a..60daa6d 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 64b6f60..d976400 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 50fb182..3bb5f19 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 9b99062..31484b0 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 612a7a8..8c189f3 100644 --- a/src/include/replication/reorderbuffer.h +++ b/src/include/replication/reorderbuffer.h @@ -182,6 +182,7 @@ typedef struct ReorderBufferChange #define RBTXN_IS_COMMITTED 0x0400 #define RBTXN_IS_ABORTED 0x0800 #define RBTXN_DISTR_INVAL_OVERFLOWED 0x1000 +#define RBTXN_ABORT_COUNTED 0x2000 #define RBTXN_PREPARE_STATUS_MASK (RBTXN_IS_PREPARED | RBTXN_SKIPPED_PREPARE | RBTXN_SENT_PREPARE) @@ -277,6 +278,12 @@ typedef struct ReorderBufferChange ((txn)->txn_flags & RBTXN_DISTR_INVAL_OVERFLOWED) != 0 \ ) +/* Has this aborted transaction already been counted in decoding stats? */ +#define rbtxn_is_abort_counted(txn) \ +( \ + ((txn)->txn_flags & RBTXN_ABORT_COUNTED) != 0 \ +) + /* Is this a top-level transaction? */ #define rbtxn_is_toptxn(txn) \ ( \ @@ -466,6 +473,12 @@ typedef struct ReorderBufferTXN /* Size of top-transaction including sub-transactions. */ Size total_size; + /* + * Total bytes of decoded changes queued for this transaction (in-memory, + * spilled, or streamed) not yet accounted as aborted. + */ + uint64 decoded_bytes; + /* * Private data pointer of the output plugin. */ @@ -706,6 +719,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 cf00b58..54beba6 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.839.g22a8290db9-goog
