summaryrefslogtreecommitdiff
path: root/contrib/test_decoding
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2021-03-17 16:18:37 -0700
committerAndres Freund <andres@anarazel.de>2021-03-17 16:21:46 -0700
commit5f79580ad69f6e696365bdc63bc265f45bd77211 (patch)
treee00826de1ff309ad8bddbdec330131058209ff4d /contrib/test_decoding
parent70945649d734d16be22c3d1d90dd8c3d3c1e9d89 (diff)
Fix memory lifetime issues of replication slot stats.
When accessing replication slot stats, introduced in 98681675002d, pgstat_read_statsfiles() reads the data into newly allocated memory. Unfortunately the current memory context at that point is the callers, leading to leaks and use-after-free dangers. The fix is trivial, explicitly use pgStatLocalContext. There's some potential for further improvements, but that's outside of the scope of this bugfix. No backpatch necessary, feature is only in HEAD. Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i@alap3.anarazel.de
Diffstat (limited to 'contrib/test_decoding')
-rw-r--r--contrib/test_decoding/expected/stats.out16
-rw-r--r--contrib/test_decoding/sql/stats.sql7
2 files changed, 23 insertions, 0 deletions
diff --git a/contrib/test_decoding/expected/stats.out b/contrib/test_decoding/expected/stats.out
index dafca965201..bca36fa9030 100644
--- a/contrib/test_decoding/expected/stats.out
+++ b/contrib/test_decoding/expected/stats.out
@@ -101,6 +101,22 @@ SELECT slot_name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count F
regression_slot | t | t
(1 row)
+-- Ensure stats can be repeatedly accessed using the same stats snapshot. See
+-- https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i%40alap3.anarazel.de
+BEGIN;
+SELECT slot_name FROM pg_stat_replication_slots;
+ slot_name
+-----------------
+ regression_slot
+(1 row)
+
+SELECT slot_name FROM pg_stat_replication_slots;
+ slot_name
+-----------------
+ regression_slot
+(1 row)
+
+COMMIT;
DROP FUNCTION wait_for_decode_stats(bool);
DROP TABLE stats_test;
SELECT pg_drop_replication_slot('regression_slot');
diff --git a/contrib/test_decoding/sql/stats.sql b/contrib/test_decoding/sql/stats.sql
index 182df84030d..51294e48e87 100644
--- a/contrib/test_decoding/sql/stats.sql
+++ b/contrib/test_decoding/sql/stats.sql
@@ -59,6 +59,13 @@ SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL,
SELECT wait_for_decode_stats(false);
SELECT slot_name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count FROM pg_stat_replication_slots;
+-- Ensure stats can be repeatedly accessed using the same stats snapshot. See
+-- https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i%40alap3.anarazel.de
+BEGIN;
+SELECT slot_name FROM pg_stat_replication_slots;
+SELECT slot_name FROM pg_stat_replication_slots;
+COMMIT;
+
DROP FUNCTION wait_for_decode_stats(bool);
DROP TABLE stats_test;
SELECT pg_drop_replication_slot('regression_slot');