diff --git a/contrib/test_decoding/expected/stats.out b/contrib/test_decoding/expected/stats.out
index bc8e601eab..8be5360807 100644
--- a/contrib/test_decoding/expected/stats.out
+++ b/contrib/test_decoding/expected/stats.out
@@ -95,8 +95,8 @@ SELECT slot_name, spill_txns, spill_count, total_txns, total_bytes FROM pg_stat_
  regression_slot |          0 |           0 |          0 |           0
 (1 row)
 
--- decode and check stats again.
-SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
+-- decode and check stats again. consume all changes.
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
  count 
 -------
   5002
@@ -120,8 +120,24 @@ SELECT pg_stat_reset_replication_slot('regression_slot');
  
 (1 row)
 
--- non-spilled xact
+SELECT wait_for_decode_stats(true, false);
+ wait_for_decode_stats 
+-----------------------
+ 
+(1 row)
+
+-- Test for non-spilled xact. Since the logical decoding skips sending
+-- uninteresting transactions to the decoding output plugin, which could
+-- be counted as spill_txns/count but not as total_txns/bytes, we increase
+-- logical_decoding_work_mem to ensure not spill transactions.
+SET logical_decoding_work_mem TO '64MB';
 INSERT INTO stats_test values(generate_series(1, 10));
+SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
+ count 
+-------
+    12
+(1 row)
+
 SELECT wait_for_decode_stats(false, false);
  wait_for_decode_stats 
 -----------------------
@@ -131,9 +147,10 @@ SELECT wait_for_decode_stats(false, false);
 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 FROM pg_stat_replication_slots;
     slot_name    | spill_txns | spill_count | total_txns | total_bytes 
 -----------------+------------+-------------+------------+-------------
- regression_slot | f          | f           | t          | t
+ regression_slot | t          | t           | t          | t
 (1 row)
 
+RESET logical_decoding_work_mem;
 -- Ensure stats can be repeatedly accessed using the same stats snapshot. See
 -- https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i%40alap3.anarazel.de
 BEGIN;
diff --git a/contrib/test_decoding/sql/stats.sql b/contrib/test_decoding/sql/stats.sql
index 8c34aeced1..41bfd364e7 100644
--- a/contrib/test_decoding/sql/stats.sql
+++ b/contrib/test_decoding/sql/stats.sql
@@ -67,17 +67,24 @@ SELECT pg_stat_reset_replication_slot('regression_slot');
 SELECT wait_for_decode_stats(true, true);
 SELECT slot_name, spill_txns, spill_count, total_txns, total_bytes FROM pg_stat_replication_slots;
 
--- decode and check stats again.
-SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
+-- decode and check stats again. consume all changes.
+SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
 SELECT wait_for_decode_stats(false, true);
 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 FROM pg_stat_replication_slots;
 
 SELECT pg_stat_reset_replication_slot('regression_slot');
+SELECT wait_for_decode_stats(true, false);
 
--- non-spilled xact
+-- Test for non-spilled xact. Since the logical decoding skips sending
+-- uninteresting transactions to the decoding output plugin, which could
+-- be counted as spill_txns/count but not as total_txns/bytes, we increase
+-- logical_decoding_work_mem to ensure not spill transactions.
+SET logical_decoding_work_mem TO '64MB';
 INSERT INTO stats_test values(generate_series(1, 10));
+SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
 SELECT wait_for_decode_stats(false, false);
 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 FROM pg_stat_replication_slots;
+RESET logical_decoding_work_mem;
 
 -- Ensure stats can be repeatedly accessed using the same stats snapshot. See
 -- https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i%40alap3.anarazel.de
