injection_point: Add injection_points.stats
authorMichael Paquier <[email protected]>
Fri, 23 Aug 2024 02:36:41 +0000 (11:36 +0900)
committerMichael Paquier <[email protected]>
Fri, 23 Aug 2024 02:36:41 +0000 (11:36 +0900)
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/[email protected]

src/test/modules/injection_points/injection_points.c
src/test/modules/injection_points/injection_stats.c
src/test/modules/injection_points/injection_stats.h
src/test/modules/injection_points/injection_stats_fixed.c
src/test/modules/injection_points/t/001_stats.pl

index abb1516e124a57ace4bc89d0776e906d25d76c4d..6bcde7b34e8014b31af8bdaca236fe343116bc46 100644 (file)
@@ -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;
index 78042074ffae81f46ed9a36cfcca1f8880c0af6d..582686a0a83b4ae0f65c0cc9fecbfe98dea4d025 100644 (file)
@@ -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,
index 126c1101691779c034e856fbd87c4fdce0995994..c48d533b4b7ed88a7b8500deef312c310ca7b214 100644 (file)
@@ -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);
index 82b07e5332f2be7a199db1456ab83a728f2ef614..2fed178b7a65696e3f0954d7440ee98551513cec 100644 (file)
@@ -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);
index 0d72cd86df7f60553747fa850815884090181d2e..7d6070e7137c033c4107203d8252998c3c9a1c2a 100644 (file)
@@ -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;');