injection_point: Add injection_points.stats

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 768a9fd553 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
This commit is contained in:
Michael Paquier 2024-08-23 11:36:41 +09:00
parent b2b023aa37
commit 2e35c67f95
5 changed files with 37 additions and 8 deletions

View 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;

View 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,

View 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);

View 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);

View 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;');