Add INJECTION_POINT_CACHED() to run injection points directly from cache
authorMichael Paquier <michael@paquier.xyz>
Thu, 18 Jul 2024 00:50:41 +0000 (09:50 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 18 Jul 2024 00:50:41 +0000 (09:50 +0900)
This new macro is able to perform a direct lookup from the local cache
of injection points (refreshed each time a point is loaded or run),
without touching the shared memory state of injection points at all.

This works in combination with INJECTION_POINT_LOAD(), and it is better
than INJECTION_POINT() in a critical section due to the fact that it
would avoid all memory allocations should a concurrent detach happen
since a LOAD(), as it retrieves a callback from the backend-private
memory.

The documentation is updated to describe in more details how to use this
new macro with a load.  Some tests are added to the module
injection_points based on a new SQL function that acts as a wrapper of
INJECTION_POINT_CACHED().

Based on a suggestion from Heikki Linnakangas.

Author: Heikki Linnakangas, Michael Paquier
Discussion: https://postgr.es/m/58d588d0-e63f-432f-9181-bed29313dece@iki.fi

doc/src/sgml/xfunc.sgml
src/backend/utils/misc/injection_point.c
src/include/utils/injection_point.h
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 756a9d07fb00998dcdad776c4240ac1d5a73af28..7e92e898460f7acb6681b58946af044da43c6f01 100644 (file)
@@ -3619,17 +3619,20 @@ INJECTION_POINT(name);
     </para>
 
     <para>
-     An injection point with a given <literal>name</literal> can be loaded
-     using macro:
+     Executing an injection point can require allocating a small amount of
+     memory, which can fail. If you need to have an injection point in a
+     critical section where dynamic allocations are not allowed, you can use
+     a two-step approach with the following macros:
 <programlisting>
 INJECTION_POINT_LOAD(name);
+INJECTION_POINT_CACHED(name);
 </programlisting>
 
-     This will load the injection point callback into the process cache,
-     doing all memory allocations at this stage without running the callback.
-     This is useful when an injection point is attached in a critical section
-     where no memory can be allocated: load the injection point outside the
-     critical section, then run it in the critical section.
+     Before entering the critical section,
+     call <function>INJECTION_POINT_LOAD</function>. It checks the shared
+     memory state, and loads the callback into backend-private memory if it is
+     active. Inside the critical section, use
+     <function>INJECTION_POINT_CACHED</function> to execute the callback.
     </para>
 
     <para>
index 84ad5e470d7eb723bf0a8501033ba35b08970c4f..8ad0c27bc8a18bf748d352449a0fa11588e20c31 100644 (file)
@@ -553,3 +553,20 @@ InjectionPointRun(const char *name)
        elog(ERROR, "Injection points are not supported by this build");
 #endif
 }
+
+/*
+ * Execute an injection point directly from the cache, if defined.
+ */
+void
+InjectionPointCached(const char *name)
+{
+#ifdef USE_INJECTION_POINTS
+       InjectionPointCacheEntry *cache_entry;
+
+       cache_entry = injection_point_cache_get(name);
+       if (cache_entry)
+               cache_entry->callback(name, cache_entry->private_data);
+#else
+       elog(ERROR, "Injection points are not supported by this build");
+#endif
+}
index bd3a62425c3e7b294738426aa6c9239d9b4cbfc1..a385e3df64965d7b8378a51cfe3f1cb3aa7a7098 100644 (file)
 #ifdef USE_INJECTION_POINTS
 #define INJECTION_POINT_LOAD(name) InjectionPointLoad(name)
 #define INJECTION_POINT(name) InjectionPointRun(name)
+#define INJECTION_POINT_CACHED(name) InjectionPointCached(name)
 #else
 #define INJECTION_POINT_LOAD(name) ((void) name)
 #define INJECTION_POINT(name) ((void) name)
+#define INJECTION_POINT_CACHED(name) ((void) name)
 #endif
 
 /*
@@ -38,6 +40,7 @@ extern void InjectionPointAttach(const char *name,
                                                                 int private_data_size);
 extern void InjectionPointLoad(const char *name);
 extern void InjectionPointRun(const char *name);
+extern void InjectionPointCached(const char *name);
 extern bool InjectionPointDetach(const char *name);
 
 #endif                                                 /* INJECTION_POINT_H */
index 2f60da900bb0cbb4d8bf8d358898be1b2c1024ab..f25bbe4966ee6e953e0d01cbc2021ab3d23443de 100644 (file)
@@ -129,6 +129,12 @@ SELECT injection_points_detach('TestInjectionLog2');
 (1 row)
 
 -- Loading
+SELECT injection_points_cached('TestInjectionLogLoad'); -- nothing in cache
+ injection_points_cached 
+-------------------------
+(1 row)
+
 SELECT injection_points_load('TestInjectionLogLoad'); -- nothing
  injection_points_load 
 -----------------------
@@ -147,6 +153,13 @@ SELECT injection_points_load('TestInjectionLogLoad'); -- nothing happens
  
 (1 row)
 
+SELECT injection_points_cached('TestInjectionLogLoad'); -- runs from cache
+NOTICE:  notice triggered for injection point TestInjectionLogLoad
+ injection_points_cached 
+-------------------------
+(1 row)
+
 SELECT injection_points_run('TestInjectionLogLoad'); -- runs from cache
 NOTICE:  notice triggered for injection point TestInjectionLogLoad
  injection_points_run 
index e275c2cf5b6d16b5129bfb37fc397af2c59b6ccb..0f280419a55815d4e84fba385a89a100b48f3f56 100644 (file)
@@ -34,6 +34,16 @@ RETURNS void
 AS 'MODULE_PATHNAME', 'injection_points_run'
 LANGUAGE C STRICT 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)
+RETURNS void
+AS 'MODULE_PATHNAME', 'injection_points_cached'
+LANGUAGE C STRICT PARALLEL UNSAFE;
+
 --
 -- injection_points_wakeup()
 --
index b6c8e89324625c1031e578825a74bb2b79df3240..15f9d0233c3cf71c9acafb98c9691e7917335b42 100644 (file)
@@ -333,6 +333,20 @@ injection_points_run(PG_FUNCTION_ARGS)
        PG_RETURN_VOID();
 }
 
+/*
+ * SQL function for triggering an injection point from cache.
+ */
+PG_FUNCTION_INFO_V1(injection_points_cached);
+Datum
+injection_points_cached(PG_FUNCTION_ARGS)
+{
+       char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
+
+       INJECTION_POINT_CACHED(name);
+
+       PG_RETURN_VOID();
+}
+
 /*
  * SQL function for waking up an injection point waiting in injection_wait().
  */
index fabf0a8823b5976fb6282cf5634c7dd47296b501..e3a481d6044928c707e8e4a238becb1bb355a621 100644 (file)
@@ -42,9 +42,11 @@ SELECT injection_points_run('TestInjectionLog2'); -- notice
 SELECT injection_points_detach('TestInjectionLog2');
 
 -- Loading
+SELECT injection_points_cached('TestInjectionLogLoad'); -- nothing in cache
 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_run('TestInjectionLogLoad'); -- runs from cache
 SELECT injection_points_detach('TestInjectionLogLoad');