diff options
-rw-r--r-- | doc/src/sgml/xfunc.sgml | 17 | ||||
-rw-r--r-- | src/backend/utils/misc/injection_point.c | 17 | ||||
-rw-r--r-- | src/include/utils/injection_point.h | 3 | ||||
-rw-r--r-- | src/test/modules/injection_points/expected/injection_points.out | 13 | ||||
-rw-r--r-- | src/test/modules/injection_points/injection_points--1.0.sql | 10 | ||||
-rw-r--r-- | src/test/modules/injection_points/injection_points.c | 14 | ||||
-rw-r--r-- | src/test/modules/injection_points/sql/injection_points.sql | 2 |
7 files changed, 69 insertions, 7 deletions
diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml index 756a9d07fb0..7e92e898460 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -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> diff --git a/src/backend/utils/misc/injection_point.c b/src/backend/utils/misc/injection_point.c index 84ad5e470d7..8ad0c27bc8a 100644 --- a/src/backend/utils/misc/injection_point.c +++ b/src/backend/utils/misc/injection_point.c @@ -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 +} diff --git a/src/include/utils/injection_point.h b/src/include/utils/injection_point.h index bd3a62425c3..a385e3df649 100644 --- a/src/include/utils/injection_point.h +++ b/src/include/utils/injection_point.h @@ -17,9 +17,11 @@ #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 */ diff --git a/src/test/modules/injection_points/expected/injection_points.out b/src/test/modules/injection_points/expected/injection_points.out index 2f60da900bb..f25bbe4966e 100644 --- a/src/test/modules/injection_points/expected/injection_points.out +++ b/src/test/modules/injection_points/expected/injection_points.out @@ -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 diff --git a/src/test/modules/injection_points/injection_points--1.0.sql b/src/test/modules/injection_points/injection_points--1.0.sql index e275c2cf5b6..0f280419a55 100644 --- a/src/test/modules/injection_points/injection_points--1.0.sql +++ b/src/test/modules/injection_points/injection_points--1.0.sql @@ -35,6 +35,16 @@ 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() -- -- Wakes up a waiting injection point. diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c index b6c8e893246..15f9d0233c3 100644 --- a/src/test/modules/injection_points/injection_points.c +++ b/src/test/modules/injection_points/injection_points.c @@ -334,6 +334,20 @@ injection_points_run(PG_FUNCTION_ARGS) } /* + * 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(). */ PG_FUNCTION_INFO_V1(injection_points_wakeup); diff --git a/src/test/modules/injection_points/sql/injection_points.sql b/src/test/modules/injection_points/sql/injection_points.sql index fabf0a8823b..e3a481d6044 100644 --- a/src/test/modules/injection_points/sql/injection_points.sql +++ b/src/test/modules/injection_points/sql/injection_points.sql @@ -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'); |