injection_points: Add routine able to drop all stats
authorMichael Paquier <michael@paquier.xyz>
Fri, 31 Jan 2025 03:41:39 +0000 (12:41 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 31 Jan 2025 03:41:39 +0000 (12:41 +0900)
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

src/test/modules/injection_points/injection_points--1.0.sql
src/test/modules/injection_points/injection_stats.c
src/test/modules/injection_points/t/001_stats.pl

index c445bf64e62387ceac8fe00696086c648a0de4b2..5d83f08811b0bfd462d63bb2a696a5d5648d39da 100644 (file)
@@ -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()
 --
index 5db62bca66fdeae682c7ca20ac916be4dfb3a0d2..14903c629e0d1f1cdb6b8bc1a585b43d2c5bf539 100644 (file)
@@ -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();
+}
index d4539fe8727d335f7338e7ba6c3fd4950ca89657..25de5fc46fe4adada6346237680c161d6adc8bc1 100644 (file)
@@ -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;