From 2e35c67f956891b2dd7c30fbac9a14e76377300a Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Fri, 23 Aug 2024 11:36:41 +0900 Subject: [PATCH] injection_point: Add injection_points.stats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This GUC controls if cumulative statistics are enabled or not in the module. Custom statistics require the module to be loaded with shared_preload_libraries, hence this GUC is made PGC_POSTMASTER. By default, the stats are disabled. 001_stats.pl is updated to enable the statistics, as it is the only area where these are required now. This will be used by an upcoming change for the injection point test added by 768a9fd5535f where stats should not be used, as the test runs a point callback in a critical section. And the module injection_points will need to be loaded with shared_preload_libraries there. Per discussion with Álvaro Herrera. Author: Michael Paquier Discussion: https://postgr.es/m/ZsUnJUlSOBNAzwW1@paquier.xyz --- .../injection_points/injection_points.c | 23 +++++++++++++++++++ .../injection_points/injection_stats.c | 8 +++---- .../injection_points/injection_stats.h | 3 +++ .../injection_points/injection_stats_fixed.c | 4 ++-- .../modules/injection_points/t/001_stats.pl | 7 ++++-- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c index abb1516e12..6bcde7b34e 100644 --- a/src/test/modules/injection_points/injection_points.c +++ b/src/test/modules/injection_points/injection_points.c @@ -28,6 +28,7 @@ #include "storage/lwlock.h" #include "storage/shmem.h" #include "utils/builtins.h" +#include "utils/guc.h" #include "utils/injection_point.h" #include "utils/memutils.h" #include "utils/wait_event.h" @@ -102,6 +103,15 @@ extern PGDLLEXPORT void injection_wait(const char *name, /* track if injection points attached in this process are linked to it */ static bool injection_point_local = false; +/* + * GUC variable + * + * This GUC is useful to control if statistics should be enabled or not + * during a test with injection points, like for example if a test relies + * on a callback run in a critical section where no allocation should happen. + */ +bool inj_stats_enabled = false; + /* Shared memory init callbacks */ static shmem_request_hook_type prev_shmem_request_hook = NULL; static shmem_startup_hook_type prev_shmem_startup_hook = NULL; @@ -513,6 +523,19 @@ _PG_init(void) if (!process_shared_preload_libraries_in_progress) return; + DefineCustomBoolVariable("injection_points.stats", + "Enables statistics for injection points.", + NULL, + &inj_stats_enabled, + false, + PGC_POSTMASTER, + 0, + NULL, + NULL, + NULL); + + MarkGUCPrefixReserved("injection_points"); + /* Shared memory initialization */ prev_shmem_request_hook = shmem_request_hook; shmem_request_hook = injection_shmem_request; diff --git a/src/test/modules/injection_points/injection_stats.c b/src/test/modules/injection_points/injection_stats.c index 78042074ff..582686a0a8 100644 --- a/src/test/modules/injection_points/injection_stats.c +++ b/src/test/modules/injection_points/injection_stats.c @@ -91,7 +91,7 @@ pgstat_fetch_stat_injentry(const char *name) { PgStat_StatInjEntry *entry = NULL; - if (!inj_stats_loaded) + if (!inj_stats_loaded || !inj_stats_enabled) return NULL; /* Compile the lookup key as a hash of the point name */ @@ -123,7 +123,7 @@ pgstat_create_inj(const char *name) PgStatShared_InjectionPoint *shstatent; /* leave if disabled */ - if (!inj_stats_loaded) + if (!inj_stats_loaded || !inj_stats_enabled) return; entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_INJECTION, InvalidOid, @@ -142,7 +142,7 @@ void pgstat_drop_inj(const char *name) { /* leave if disabled */ - if (!inj_stats_loaded) + if (!inj_stats_loaded || !inj_stats_enabled) return; if (!pgstat_drop_entry(PGSTAT_KIND_INJECTION, InvalidOid, @@ -164,7 +164,7 @@ pgstat_report_inj(const char *name) PgStat_StatInjEntry *statent; /* leave if disabled */ - if (!inj_stats_loaded) + if (!inj_stats_loaded || !inj_stats_enabled) return; entry_ref = pgstat_get_entry_ref_locked(PGSTAT_KIND_INJECTION, InvalidOid, diff --git a/src/test/modules/injection_points/injection_stats.h b/src/test/modules/injection_points/injection_stats.h index 126c110169..c48d533b4b 100644 --- a/src/test/modules/injection_points/injection_stats.h +++ b/src/test/modules/injection_points/injection_stats.h @@ -15,6 +15,9 @@ #ifndef INJECTION_STATS #define INJECTION_STATS +/* GUC variable */ +extern bool inj_stats_enabled; + /* injection_stats.c */ extern void pgstat_register_inj(void); extern void pgstat_create_inj(const char *name); diff --git a/src/test/modules/injection_points/injection_stats_fixed.c b/src/test/modules/injection_points/injection_stats_fixed.c index 82b07e5332..2fed178b7a 100644 --- a/src/test/modules/injection_points/injection_stats_fixed.c +++ b/src/test/modules/injection_points/injection_stats_fixed.c @@ -146,7 +146,7 @@ pgstat_report_inj_fixed(uint32 numattach, PgStatShared_InjectionPointFixed *stats_shmem; /* leave if disabled */ - if (!inj_fixed_loaded) + if (!inj_fixed_loaded || !inj_stats_enabled) return; stats_shmem = pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED); @@ -172,7 +172,7 @@ injection_points_stats_fixed(PG_FUNCTION_ARGS) bool nulls[5] = {0}; PgStat_StatInjFixedEntry *stats; - if (!inj_fixed_loaded) + if (!inj_fixed_loaded || !inj_stats_enabled) PG_RETURN_NULL(); pgstat_snapshot_fixed(PGSTAT_KIND_INJECTION_FIXED); diff --git a/src/test/modules/injection_points/t/001_stats.pl b/src/test/modules/injection_points/t/001_stats.pl index 0d72cd86df..7d6070e713 100644 --- a/src/test/modules/injection_points/t/001_stats.pl +++ b/src/test/modules/injection_points/t/001_stats.pl @@ -20,8 +20,11 @@ if ($ENV{enable_injection_points} ne 'yes') # Node initialization my $node = PostgreSQL::Test::Cluster->new('master'); $node->init; -$node->append_conf('postgresql.conf', - "shared_preload_libraries = 'injection_points'"); +$node->append_conf( + 'postgresql.conf', qq( +shared_preload_libraries = 'injection_points' +injection_points.stats = true +)); $node->start; $node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');