From 997639c067a8af1ae11c58d7ef74ef42dd7cab32 Mon Sep 17 00:00:00 2001 From: Diego Fronza Date: Fri, 12 Nov 2021 10:58:56 -0300 Subject: [PATCH 1/2] PG-272: Fix server crash when calling pg_stat_monitor_reset(). The loop that resets the query buffers was incorrecly using MAX_BUCKETS to indicate the number of buckets to clear, which defaults to 10. If a user lowers this value the loop would access a pointer beyond the number of query buffers allocated. Fix the problem by using the correct PGSM_MAX_BUCKETS GUC as the limit to the loop. --- pg_stat_monitor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 41c23c8..4afee9d 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -1579,7 +1579,7 @@ pg_stat_monitor_reset(PG_FUNCTION_ARGS) LWLockAcquire(pgss->lock, LW_EXCLUSIVE); hash_entry_dealloc(-1, -1, NULL); /* Reset query buffers. */ - for (size_t i = 0; i < MAX_BUCKETS; ++i) + for (size_t i = 0; i < PGSM_MAX_BUCKETS; ++i) { *(uint64 *)pgss_qbuf[i] = 0; } From 3be31d67e925251f997ae3cec8b91289a79bac7b Mon Sep 17 00:00:00 2001 From: Diego Fronza Date: Fri, 12 Nov 2021 14:42:17 -0300 Subject: [PATCH 2/2] PG-275: Fix regression tests. Removal of redundant file guc_1.out. Adjusted guc.out to match query planning disabled by default. --- regression/expected/guc.out | 3 +-- regression/expected/guc_1.out | 38 ----------------------------------- 2 files changed, 1 insertion(+), 40 deletions(-) delete mode 100644 regression/expected/guc_1.out diff --git a/regression/expected/guc.out b/regression/expected/guc.out index 0da2986..ef3da0b 100644 --- a/regression/expected/guc.out +++ b/regression/expected/guc.out @@ -26,9 +26,8 @@ SELECT * FROM pg_stat_monitor_settings ORDER BY name COLLATE "C"; pg_stat_monitor.pgsm_overflow_target | 1 | 1 | Sets the overflow target for pg_stat_monitor | 0 | 1 | 1 pg_stat_monitor.pgsm_query_max_len | 1024 | 1024 | Sets the maximum length of query. | 1024 | 2147483647 | 1 pg_stat_monitor.pgsm_query_shared_buffer | 20 | 20 | Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor. | 1 | 10000 | 1 - pg_stat_monitor.pgsm_track_planning | 1 | 1 | Selects whether planning statistics are tracked. | 0 | 0 | 0 pg_stat_monitor.pgsm_track_utility | 1 | 1 | Selects whether utility commands are tracked. | 0 | 0 | 0 -(14 rows) +(13 rows) SELECT pg_stat_monitor_reset(); pg_stat_monitor_reset diff --git a/regression/expected/guc_1.out b/regression/expected/guc_1.out deleted file mode 100644 index 1425109..0000000 --- a/regression/expected/guc_1.out +++ /dev/null @@ -1,38 +0,0 @@ -CREATE EXTENSION pg_stat_monitor; -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - -select pg_sleep(.5); - pg_sleep ----------- - -(1 row) - -SELECT * FROM pg_stat_monitor_settings ORDER BY name COLLATE "C"; - name | value | default_value | description | minimum | maximum | restart -------------------------------------------+--------+---------------+----------------------------------------------------------------------------------------------------------+---------+------------+--------- - pg_stat_monitor.pgsm_bucket_time | 60 | 60 | Sets the time in seconds per bucket. | 1 | 2147483647 | 1 - pg_stat_monitor.pgsm_enable | 1 | 1 | Enable/Disable statistics collector. | 0 | 0 | 0 - pg_stat_monitor.pgsm_enable_query_plan | 0 | 0 | Enable/Disable query plan monitoring | 0 | 0 | 0 - pg_stat_monitor.pgsm_histogram_buckets | 10 | 10 | Sets the maximum number of histogram buckets | 2 | 2147483647 | 1 - pg_stat_monitor.pgsm_histogram_max | 100000 | 100000 | Sets the time in millisecond. | 10 | 2147483647 | 1 - pg_stat_monitor.pgsm_histogram_min | 0 | 0 | Sets the time in millisecond. | 0 | 2147483647 | 1 - pg_stat_monitor.pgsm_max | 100 | 100 | Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor. | 1 | 1000 | 1 - pg_stat_monitor.pgsm_max_buckets | 10 | 10 | Sets the maximum number of buckets. | 1 | 10 | 1 - pg_stat_monitor.pgsm_normalized_query | 1 | 1 | Selects whether save query in normalized format. | 0 | 0 | 0 - pg_stat_monitor.pgsm_overflow_target | 0 | 1 | Sets the overflow target for pg_stat_monitor | 0 | 1 | 1 - pg_stat_monitor.pgsm_query_max_len | 1024 | 1024 | Sets the maximum length of query. | 1024 | 2147483647 | 1 - pg_stat_monitor.pgsm_query_shared_buffer | 20 | 20 | Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor. | 1 | 10000 | 1 - pg_stat_monitor.pgsm_track_utility | 1 | 1 | Selects whether utility commands are tracked. | 0 | 0 | 0 -(13 rows) - -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - -DROP EXTENSION pg_stat_monitor;