injection_points: Add support and tests for runtime arguments
authorMichael Paquier <michael@paquier.xyz>
Fri, 9 May 2025 22:40:25 +0000 (07:40 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 9 May 2025 22:40:25 +0000 (07:40 +0900)
This commit provides some test coverage for the runtime arguments of
injection points, for both INJECTION_POINT_CACHED() and
INJECTION_POINT(), as extended in 371f2db8b05e.

The SQL functions injection_points_cached() and injection_points_run()
are extended so as it is possible to pass an optional string value to
them.

Reviewed-by: Greg Burd <greg@burd.me>
Discussion: https://postgr.es/m/Z_y9TtnXubvYAApS@paquier.xyz

src/test/modules/injection_points/expected/injection_points.out
src/test/modules/injection_points/injection_points--1.0.sql
src/test/modules/injection_points/injection_points.c
src/test/modules/injection_points/sql/injection_points.sql

index f25bbe4966ee6e953e0d01cbc2021ab3d23443de..43bcdd01582f711cc71aa7d799aa96b348ca497e 100644 (file)
@@ -6,6 +6,19 @@ CREATE FUNCTION wait_pid(int)
   RETURNS void
   AS :'regresslib'
   LANGUAGE C STRICT;
+-- Non-strict checks
+SELECT injection_points_run(NULL);
+ injection_points_run 
+----------------------
+(1 row)
+
+SELECT injection_points_cached(NULL);
+ injection_points_cached 
+-------------------------
+(1 row)
+
 SELECT injection_points_attach('TestInjectionBooh', 'booh');
 ERROR:  incorrect action "booh" for injection point creation
 SELECT injection_points_attach('TestInjectionError', 'error');
@@ -39,6 +52,20 @@ NOTICE:  notice triggered for injection point TestInjectionLog2
  
 (1 row)
 
+SELECT injection_points_run('TestInjectionLog2', NULL); -- notice
+NOTICE:  notice triggered for injection point TestInjectionLog2
+ injection_points_run 
+----------------------
+(1 row)
+
+SELECT injection_points_run('TestInjectionLog2', 'foobar'); -- notice + arg
+NOTICE:  notice triggered for injection point TestInjectionLog2 (foobar)
+ injection_points_run 
+----------------------
+(1 row)
+
 SELECT injection_points_run('TestInjectionLog'); -- notice
 NOTICE:  notice triggered for injection point TestInjectionLog
  injection_points_run 
@@ -48,6 +75,10 @@ NOTICE:  notice triggered for injection point TestInjectionLog
 
 SELECT injection_points_run('TestInjectionError'); -- error
 ERROR:  error triggered for injection point TestInjectionError
+SELECT injection_points_run('TestInjectionError', NULL); -- error
+ERROR:  error triggered for injection point TestInjectionError
+SELECT injection_points_run('TestInjectionError', 'foobar2'); -- error + arg
+ERROR:  error triggered for injection point TestInjectionError (foobar2)
 -- Re-load cache and run again.
 \c
 SELECT injection_points_run('TestInjectionLog2'); -- notice
@@ -160,6 +191,20 @@ NOTICE:  notice triggered for injection point TestInjectionLogLoad
  
 (1 row)
 
+SELECT injection_points_cached('TestInjectionLogLoad', NULL); -- runs from cache
+NOTICE:  notice triggered for injection point TestInjectionLogLoad
+ injection_points_cached 
+-------------------------
+(1 row)
+
+SELECT injection_points_cached('TestInjectionLogLoad', 'foobar'); -- runs from cache
+NOTICE:  notice triggered for injection point TestInjectionLogLoad (foobar)
+ injection_points_cached 
+-------------------------
+(1 row)
+
 SELECT injection_points_run('TestInjectionLogLoad'); -- runs from cache
 NOTICE:  notice triggered for injection point TestInjectionLogLoad
  injection_points_run 
index 5d83f08811b0bfd462d63bb2a696a5d5648d39da..cc76b1bf99ae6f2ec496a518528c635ce8ad5b4d 100644 (file)
@@ -29,20 +29,22 @@ LANGUAGE C STRICT PARALLEL UNSAFE;
 --
 -- Executes the action attached to the injection point.
 --
-CREATE FUNCTION injection_points_run(IN point_name TEXT)
+CREATE FUNCTION injection_points_run(IN point_name TEXT,
+    IN arg TEXT DEFAULT NULL)
 RETURNS void
 AS 'MODULE_PATHNAME', 'injection_points_run'
-LANGUAGE C STRICT PARALLEL UNSAFE;
+LANGUAGE C PARALLEL UNSAFE;
 
 --
 -- injection_points_cached()
 --
 -- Executes the action attached to the injection point, from local cache.
 --
-CREATE FUNCTION injection_points_cached(IN point_name TEXT)
+CREATE FUNCTION injection_points_cached(IN point_name TEXT,
+    IN arg TEXT DEFAULT NULL)
 RETURNS void
 AS 'MODULE_PATHNAME', 'injection_points_cached'
-LANGUAGE C STRICT PARALLEL UNSAFE;
+LANGUAGE C PARALLEL UNSAFE;
 
 --
 -- injection_points_wakeup()
index 44eda3caa0582c5a3da0d0b3ca2cc943835a2c79..3da0cbc10e08fafae1f284085122f55e510007ae 100644 (file)
@@ -245,26 +245,36 @@ void
 injection_error(const char *name, const void *private_data, void *arg)
 {
    InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
+   char       *argstr = (char *) arg;
 
    if (!injection_point_allowed(condition))
        return;
 
    pgstat_report_inj(name);
 
-   elog(ERROR, "error triggered for injection point %s", name);
+   if (argstr)
+       elog(ERROR, "error triggered for injection point %s (%s)",
+            name, argstr);
+   else
+       elog(ERROR, "error triggered for injection point %s", name);
 }
 
 void
 injection_notice(const char *name, const void *private_data, void *arg)
 {
    InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
+   char       *argstr = (char *) arg;
 
    if (!injection_point_allowed(condition))
        return;
 
    pgstat_report_inj(name);
 
-   elog(NOTICE, "notice triggered for injection point %s", name);
+   if (argstr)
+       elog(NOTICE, "notice triggered for injection point %s (%s)",
+            name, argstr);
+   else
+       elog(NOTICE, "notice triggered for injection point %s", name);
 }
 
 /* Wait on a condition variable, awaken by injection_points_wakeup() */
@@ -405,10 +415,18 @@ PG_FUNCTION_INFO_V1(injection_points_run);
 Datum
 injection_points_run(PG_FUNCTION_ARGS)
 {
-   char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
+   char       *name;
+   char       *arg = NULL;
+
+   if (PG_ARGISNULL(0))
+       PG_RETURN_VOID();
+   name = text_to_cstring(PG_GETARG_TEXT_PP(0));
+
+   if (!PG_ARGISNULL(1))
+       arg = text_to_cstring(PG_GETARG_TEXT_PP(1));
 
    pgstat_report_inj_fixed(0, 0, 1, 0, 0);
-   INJECTION_POINT(name, NULL);
+   INJECTION_POINT(name, arg);
 
    PG_RETURN_VOID();
 }
@@ -420,10 +438,18 @@ PG_FUNCTION_INFO_V1(injection_points_cached);
 Datum
 injection_points_cached(PG_FUNCTION_ARGS)
 {
-   char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
+   char       *name;
+   char       *arg = NULL;
+
+   if (PG_ARGISNULL(0))
+       PG_RETURN_VOID();
+   name = text_to_cstring(PG_GETARG_TEXT_PP(0));
+
+   if (!PG_ARGISNULL(1))
+       arg = text_to_cstring(PG_GETARG_TEXT_PP(1));
 
    pgstat_report_inj_fixed(0, 0, 0, 1, 0);
-   INJECTION_POINT_CACHED(name, NULL);
+   INJECTION_POINT_CACHED(name, arg);
 
    PG_RETURN_VOID();
 }
index e3a481d6044928c707e8e4a238becb1bb355a621..d9748331c77155c3be03d03295907b888216a468 100644 (file)
@@ -9,6 +9,10 @@ CREATE FUNCTION wait_pid(int)
   AS :'regresslib'
   LANGUAGE C STRICT;
 
+-- Non-strict checks
+SELECT injection_points_run(NULL);
+SELECT injection_points_cached(NULL);
+
 SELECT injection_points_attach('TestInjectionBooh', 'booh');
 SELECT injection_points_attach('TestInjectionError', 'error');
 SELECT injection_points_attach('TestInjectionLog', 'notice');
@@ -16,8 +20,12 @@ SELECT injection_points_attach('TestInjectionLog2', 'notice');
 
 SELECT injection_points_run('TestInjectionBooh'); -- nothing
 SELECT injection_points_run('TestInjectionLog2'); -- notice
+SELECT injection_points_run('TestInjectionLog2', NULL); -- notice
+SELECT injection_points_run('TestInjectionLog2', 'foobar'); -- notice + arg
 SELECT injection_points_run('TestInjectionLog'); -- notice
 SELECT injection_points_run('TestInjectionError'); -- error
+SELECT injection_points_run('TestInjectionError', NULL); -- error
+SELECT injection_points_run('TestInjectionError', 'foobar2'); -- error + arg
 
 -- Re-load cache and run again.
 \c
@@ -47,6 +55,8 @@ SELECT injection_points_load('TestInjectionLogLoad'); -- nothing
 SELECT injection_points_attach('TestInjectionLogLoad', 'notice');
 SELECT injection_points_load('TestInjectionLogLoad'); -- nothing happens
 SELECT injection_points_cached('TestInjectionLogLoad'); -- runs from cache
+SELECT injection_points_cached('TestInjectionLogLoad', NULL); -- runs from cache
+SELECT injection_points_cached('TestInjectionLogLoad', 'foobar'); -- runs from cache
 SELECT injection_points_run('TestInjectionLogLoad'); -- runs from cache
 SELECT injection_points_detach('TestInjectionLogLoad');