From a632cd354d35e1c3352061c375b4458876c9f10a Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Fri, 31 Jan 2025 12:41:39 +0900 Subject: [PATCH] injection_points: Add routine able to drop all stats This serves as an example of how to use the new function introduced in ce5c620fb625, pgstat_drop_matching_entries(), with a callback able to filter the entries dropped. A SQL function named injection_points_stats_drop() is added with some tests. Author: Lukas Fitti Discussion: https://postgr.es/m/CAP53PkwuFbo3NkwZgxwNRMjMfqPEqidD-SggaoQ4ijotBVLJAA@mail.gmail.com --- .../injection_points--1.0.sql | 10 ++++++++++ .../injection_points/injection_stats.c | 19 +++++++++++++++++++ .../modules/injection_points/t/001_stats.pl | 13 +++++++++++++ 3 files changed, 42 insertions(+) diff --git a/src/test/modules/injection_points/injection_points--1.0.sql b/src/test/modules/injection_points/injection_points--1.0.sql index c445bf64e62..5d83f08811b 100644 --- a/src/test/modules/injection_points/injection_points--1.0.sql +++ b/src/test/modules/injection_points/injection_points--1.0.sql @@ -85,6 +85,16 @@ RETURNS bigint AS 'MODULE_PATHNAME', 'injection_points_stats_numcalls' LANGUAGE C STRICT; +-- +-- injection_points_stats_drop() +-- +-- Drop all statistics of injection points. +-- +CREATE FUNCTION injection_points_stats_drop() +RETURNS void +AS 'MODULE_PATHNAME', 'injection_points_stats_drop' +LANGUAGE C STRICT; + -- -- injection_points_stats_fixed() -- diff --git a/src/test/modules/injection_points/injection_stats.c b/src/test/modules/injection_points/injection_stats.c index 5db62bca66f..14903c629e0 100644 --- a/src/test/modules/injection_points/injection_stats.c +++ b/src/test/modules/injection_points/injection_stats.c @@ -197,3 +197,22 @@ injection_points_stats_numcalls(PG_FUNCTION_ARGS) PG_RETURN_INT64(entry->numcalls); } + +/* Only used by injection_points_stats_drop() */ +static bool +match_inj_entries(PgStatShared_HashEntry *entry, Datum match_data) +{ + return entry->key.kind == PGSTAT_KIND_INJECTION; +} + +/* + * SQL function that drops all injection point statistics. + */ +PG_FUNCTION_INFO_V1(injection_points_stats_drop); +Datum +injection_points_stats_drop(PG_FUNCTION_ARGS) +{ + pgstat_drop_matching_entries(match_inj_entries, 0); + + PG_RETURN_VOID(); +} diff --git a/src/test/modules/injection_points/t/001_stats.pl b/src/test/modules/injection_points/t/001_stats.pl index d4539fe8727..25de5fc46fe 100644 --- a/src/test/modules/injection_points/t/001_stats.pl +++ b/src/test/modules/injection_points/t/001_stats.pl @@ -69,6 +69,19 @@ $fixedstats = $node->safe_psql('postgres', "SELECT * FROM injection_points_stats_fixed();"); is($fixedstats, '0|0|0|0|0', 'fixed stats after crash'); +# On drop all stats are gone +$node->safe_psql('postgres', + "SELECT injection_points_attach('stats-notice', 'notice');"); +$node->safe_psql('postgres', "SELECT injection_points_run('stats-notice');"); +$node->safe_psql('postgres', "SELECT injection_points_run('stats-notice');"); +$numcalls = $node->safe_psql('postgres', + "SELECT injection_points_stats_numcalls('stats-notice');"); +is($numcalls, '2', 'number of stats calls'); +$node->safe_psql('postgres', "SELECT injection_points_stats_drop();"); +$numcalls = $node->safe_psql('postgres', + "SELECT injection_points_stats_numcalls('stats-notice');"); +is($numcalls, '', 'no stats after drop via SQL function'); + # Stop the server, disable the module, then restart. The server # should be able to come up. $node->stop;