Add support for runtime arguments in injection points
authorMichael Paquier <michael@paquier.xyz>
Fri, 9 May 2025 21:56:26 +0000 (06:56 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 9 May 2025 21:56:26 +0000 (06:56 +0900)
The macros INJECTION_POINT() and INJECTION_POINT_CACHED() are extended
with an optional argument that can be passed down to the callback
attached when an injection point is run, giving to callbacks the
possibility to manipulate a stack state given by the caller.  The
existing callbacks in modules injection_points and test_aio have their
declarations adjusted based on that.

da7226993fd4 (core AIO infrastructure) and 93bc3d75d8e1 (test_aio) and
been relying on a set of workarounds where a static variable called
pgaio_inj_cur_handle is used as runtime argument in the injection point
callbacks used by the AIO tests, in combination with a TRY/CATCH block
to reset the argument value.  The infrastructure introduced in this
commit will be reused for the AIO tests, simplifying them.

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

23 files changed:
doc/src/sgml/xfunc.sgml
src/backend/access/gin/ginbtree.c
src/backend/access/heap/heapam.c
src/backend/access/index/genam.c
src/backend/access/transam/multixact.c
src/backend/access/transam/xlog.c
src/backend/commands/indexcmds.c
src/backend/executor/nodeAgg.c
src/backend/libpq/be-secure-gssapi.c
src/backend/libpq/be-secure.c
src/backend/postmaster/autovacuum.c
src/backend/storage/aio/aio.c
src/backend/tcop/backend_startup.c
src/backend/tcop/postgres.c
src/backend/utils/cache/catcache.c
src/backend/utils/cache/inval.c
src/backend/utils/cache/typcache.c
src/backend/utils/init/postinit.c
src/backend/utils/misc/injection_point.c
src/include/utils/injection_point.h
src/test/modules/injection_points/injection_points.c
src/test/modules/test_aio/test_aio.c
src/test/modules/test_slru/test_multixact.c

index 3a73b02ccafa2f41cc6e310078f7b5b71736dab9..2d81afce8cb9b573ad3ee4874352d8f93e0eda54 100644 (file)
@@ -3829,15 +3829,17 @@ uint32 WaitEventExtensionNew(const char *wait_event_name)
      An injection point with a given <literal>name</literal> is declared using
      macro:
 <programlisting>
-INJECTION_POINT(name);
+INJECTION_POINT(name, arg);
 </programlisting>
 
      There are a few injection points already declared at strategic points
      within the server code. After adding a new injection point the code needs
      to be compiled in order for that injection point to be available in the
      binary. Add-ins written in C-language can declare injection points in
-     their own code using the same macro. The injection point names should
-     use lower-case characters, with terms separated by dashes.
+     their own code using the same macro. The injection point names should use
+     lower-case characters, with terms separated by
+     dashes. <literal>arg</literal> is an optional argument value given to the
+     callback at run-time.
     </para>
 
     <para>
@@ -3847,7 +3849,7 @@ INJECTION_POINT(name);
      a two-step approach with the following macros:
 <programlisting>
 INJECTION_POINT_LOAD(name);
-INJECTION_POINT_CACHED(name);
+INJECTION_POINT_CACHED(name, arg);
 </programlisting>
 
      Before entering the critical section,
@@ -3880,7 +3882,9 @@ extern void InjectionPointAttach(const char *name,
      <literal>InjectionPointCallback</literal>:
 <programlisting>
 static void
-custom_injection_callback(const char *name, const void *private_data)
+custom_injection_callback(const char *name,
+                          const void *private_data,
+                          void *arg)
 {
     uint32 wait_event_info = WaitEventInjectionPointNew(name);
 
@@ -3909,7 +3913,7 @@ if (IS_INJECTION_POINT_ATTACHED("before-foobar"))
     local_var = 123;
 
     /* also execute the callback */
-    INJECTION_POINT_CACHED("before-foobar");
+    INJECTION_POINT_CACHED("before-foobar", NULL);
 }
 #endif
 </programlisting>
index 26a0bdc2063a33810407d2f22c765baa175bb070..644d484ea53c6b62cdc30f9f1c3cdd436bc60e1f 100644 (file)
@@ -685,9 +685,9 @@ ginFinishSplit(GinBtree btree, GinBtreeStack *stack, bool freestack,
 
 #ifdef USE_INJECTION_POINTS
        if (GinPageIsLeaf(BufferGetPage(stack->buffer)))
-           INJECTION_POINT("gin-leave-leaf-split-incomplete");
+           INJECTION_POINT("gin-leave-leaf-split-incomplete", NULL);
        else
-           INJECTION_POINT("gin-leave-internal-split-incomplete");
+           INJECTION_POINT("gin-leave-internal-split-incomplete", NULL);
 #endif
 
        /* search parent to lock */
@@ -778,7 +778,7 @@ ginFinishSplit(GinBtree btree, GinBtreeStack *stack, bool freestack,
 static void
 ginFinishOldSplit(GinBtree btree, GinBtreeStack *stack, GinStatsData *buildStats, int access)
 {
-   INJECTION_POINT("gin-finish-incomplete-split");
+   INJECTION_POINT("gin-finish-incomplete-split", NULL);
    elog(DEBUG1, "finishing incomplete split of block %u in gin index \"%s\"",
         stack->blkno, RelationGetRelationName(btree->index));
 
index c1a4de14a59e24c31fa8085d6c9e51855d403012..9ec8cda1c6801bba3b549aa7e72da41bf822e4f2 100644 (file)
@@ -3304,7 +3304,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
    interesting_attrs = bms_add_members(interesting_attrs, id_attrs);
 
    block = ItemPointerGetBlockNumber(otid);
-   INJECTION_POINT("heap_update-before-pin");
+   INJECTION_POINT("heap_update-before-pin", NULL);
    buffer = ReadBuffer(relation, block);
    page = BufferGetPage(buffer);
 
index 8f532e14590e7c74440274e6455ffccd9902eef8..0cb27af13109592c86d698f0f56188612a84a976 100644 (file)
@@ -851,7 +851,7 @@ systable_inplace_update_begin(Relation relation,
        if (retries++ > 10000)
            elog(ERROR, "giving up after too many tries to overwrite row");
 
-       INJECTION_POINT("inplace-before-pin");
+       INJECTION_POINT("inplace-before-pin", NULL);
        scan = systable_beginscan(relation, indexId, indexOK, snapshot,
                                  nkeys, unconstify(ScanKeyData *, key));
        oldtup = systable_getnext(scan);
index ef91e70f41dc9f4234c0a927e8562bcbcf2afb47..3c06ac45532f8ca60449bad6e677553773319840 100644 (file)
@@ -872,7 +872,7 @@ MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
     */
    multi = GetNewMultiXactId(nmembers, &offset);
 
-   INJECTION_POINT_CACHED("multixact-create-from-members");
+   INJECTION_POINT_CACHED("multixact-create-from-members", NULL);
 
    /* Make an XLOG entry describing the new MXID. */
    xlrec.mid = multi;
@@ -1486,7 +1486,7 @@ retry:
            LWLockRelease(lock);
            CHECK_FOR_INTERRUPTS();
 
-           INJECTION_POINT("multixact-get-members-cv-sleep");
+           INJECTION_POINT("multixact-get-members-cv-sleep", NULL);
 
            ConditionVariableSleep(&MultiXactState->nextoff_cv,
                                   WAIT_EVENT_MULTIXACT_CREATION);
index 2d4c346473b7221100daf545c9a4d16baa20322a..1914859b2eed713e97437f8d77f3f0ac13728aa9 100644 (file)
@@ -7882,7 +7882,7 @@ CreateRestartPoint(int flags)
     * This location needs to be after CheckPointGuts() to ensure that some
     * work has already happened during this checkpoint.
     */
-   INJECTION_POINT("create-restart-point");
+   INJECTION_POINT("create-restart-point", NULL);
 
    /*
     * Remember the prior checkpoint's redo ptr for
index 33c2106c17cee4f05dd97fcf579765b2b8541ff2..d962fe392cd27a7503276da6410f34bf56aec0a8 100644 (file)
@@ -3892,9 +3892,9 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
 
 #ifdef USE_INJECTION_POINTS
        if (idx->safe)
-           INJECTION_POINT("reindex-conc-index-safe");
+           INJECTION_POINT("reindex-conc-index-safe", NULL);
        else
-           INJECTION_POINT("reindex-conc-index-not-safe");
+           INJECTION_POINT("reindex-conc-index-not-safe", NULL);
 #endif
 
        idx->tableId = RelationGetRelid(heapRel);
index f83fc16c5c8e2906146b8f77965ce059bfdbcd95..377e016d73225865fe9d0e0c7a05e430d20f3f68 100644 (file)
@@ -1492,7 +1492,7 @@ build_hash_tables(AggState *aggstate)
        if (IS_INJECTION_POINT_ATTACHED("hash-aggregate-oversize-table"))
        {
            nbuckets = memory / TupleHashEntrySize();
-           INJECTION_POINT_CACHED("hash-aggregate-oversize-table");
+           INJECTION_POINT_CACHED("hash-aggregate-oversize-table", NULL);
        }
 #endif
 
@@ -1882,7 +1882,7 @@ hash_agg_check_limits(AggState *aggstate)
        if (IS_INJECTION_POINT_ATTACHED("hash-aggregate-spill-1000"))
        {
            do_spill = true;
-           INJECTION_POINT_CACHED("hash-aggregate-spill-1000");
+           INJECTION_POINT_CACHED("hash-aggregate-spill-1000", NULL);
        }
    }
 #endif
@@ -1910,7 +1910,7 @@ hash_agg_check_limits(AggState *aggstate)
 static void
 hash_agg_enter_spill_mode(AggState *aggstate)
 {
-   INJECTION_POINT("hash-aggregate-enter-spill-mode");
+   INJECTION_POINT("hash-aggregate-enter-spill-mode", NULL);
    aggstate->hash_spill_mode = true;
    hashagg_recompile_expressions(aggstate, aggstate->table_filled, true);
 
@@ -2739,7 +2739,7 @@ agg_refill_hash_table(AggState *aggstate)
     */
    hashagg_recompile_expressions(aggstate, true, true);
 
-   INJECTION_POINT("hash-aggregate-process-batch");
+   INJECTION_POINT("hash-aggregate-process-batch", NULL);
    for (;;)
    {
        TupleTableSlot *spillslot = aggstate->hash_spill_rslot;
@@ -2995,7 +2995,7 @@ hashagg_spill_init(HashAggSpill *spill, LogicalTapeSet *tapeset, int used_bits,
    {
        npartitions = 1;
        partition_bits = 0;
-       INJECTION_POINT_CACHED("hash-aggregate-single-partition");
+       INJECTION_POINT_CACHED("hash-aggregate-single-partition", NULL);
    }
 #endif
 
index a3d610b1373da60a6100230051a18b8aa4a4f79e..717ba9824f914d595b0693a85f671efcfa189962 100644 (file)
@@ -500,7 +500,7 @@ secure_open_gssapi(Port *port)
                minor;
    gss_cred_id_t delegated_creds;
 
-   INJECTION_POINT("backend-gssapi-startup");
+   INJECTION_POINT("backend-gssapi-startup", NULL);
 
    /*
     * Allocate subsidiary Port data for GSSAPI operations.
index 91576f942857d703c8eaf0e41a88817898e23da4..d723e74e8137bea2f3da268432c2edc14ba2b83a 100644 (file)
@@ -131,7 +131,7 @@ secure_open_server(Port *port)
    }
    Assert(pq_buffer_remaining_data() == 0);
 
-   INJECTION_POINT("backend-ssl-startup");
+   INJECTION_POINT("backend-ssl-startup", NULL);
 
    r = be_tls_open_server(port);
 
index 16756152b71a5ab86c7e00aacf37c6de802f684b..4d4a1a3197ec6543bb75997038ebe04014b2a6c9 100644 (file)
@@ -1922,7 +1922,7 @@ do_autovacuum(void)
     * This injection point is put in a transaction block to work with a wait
     * that uses a condition variable.
     */
-   INJECTION_POINT("autovacuum-worker-start");
+   INJECTION_POINT("autovacuum-worker-start", NULL);
 
    /*
     * Compute the multixact age for which freezing is urgent.  This is
index 57b9cf3dcab365766b5f74c9c21ad40d5253f3f9..04268e77ec2ee2075a86ff3626c8e93a70d18e6a 100644 (file)
@@ -1275,7 +1275,7 @@ pgaio_io_call_inj(PgAioHandle *ioh, const char *injection_point)
 
    PG_TRY();
    {
-       InjectionPointCached(injection_point);
+       InjectionPointCached(injection_point, NULL);
    }
    PG_FINALLY();
    {
index dde8d5b35175e6373c78c74f39e8eee28a91ac82..a7d1fec981f881aa879e87827e1bfe247c6f1cec 100644 (file)
@@ -234,7 +234,7 @@ BackendInitialize(ClientSocket *client_sock, CAC_state cac)
 
    /* For testing client error handling */
 #ifdef USE_INJECTION_POINTS
-   INJECTION_POINT("backend-initialize");
+   INJECTION_POINT("backend-initialize", NULL);
    if (IS_INJECTION_POINT_ATTACHED("backend-initialize-v2-error"))
    {
        /*
index dc4c600922dffe6ef0a52431049198a11036440d..1ae51b1b3915756371c6923d163da4aa8467844b 100644 (file)
@@ -3482,7 +3482,7 @@ ProcessInterrupts(void)
        IdleInTransactionSessionTimeoutPending = false;
        if (IdleInTransactionSessionTimeout > 0)
        {
-           INJECTION_POINT("idle-in-transaction-session-timeout");
+           INJECTION_POINT("idle-in-transaction-session-timeout", NULL);
            ereport(FATAL,
                    (errcode(ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT),
                     errmsg("terminating connection due to idle-in-transaction timeout")));
@@ -3495,7 +3495,7 @@ ProcessInterrupts(void)
        TransactionTimeoutPending = false;
        if (TransactionTimeout > 0)
        {
-           INJECTION_POINT("transaction-timeout");
+           INJECTION_POINT("transaction-timeout", NULL);
            ereport(FATAL,
                    (errcode(ERRCODE_TRANSACTION_TIMEOUT),
                     errmsg("terminating connection due to transaction timeout")));
@@ -3508,7 +3508,7 @@ ProcessInterrupts(void)
        IdleSessionTimeoutPending = false;
        if (IdleSessionTimeout > 0)
        {
-           INJECTION_POINT("idle-session-timeout");
+           INJECTION_POINT("idle-session-timeout", NULL);
            ereport(FATAL,
                    (errcode(ERRCODE_IDLE_SESSION_TIMEOUT),
                     errmsg("terminating connection due to idle-session timeout")));
index 6e3cad454c0a6011f05ab0c6d046f44436343b27..657648996c235ef4002a926d1298e910efa3631f 100644 (file)
@@ -1926,7 +1926,7 @@ SearchCatCacheList(CatCache *cache,
            /* Injection point to help testing the recursive invalidation case */
            if (first_iter)
            {
-               INJECTION_POINT("catcache-list-miss-systable-scan-started");
+               INJECTION_POINT("catcache-list-miss-systable-scan-started", NULL);
                first_iter = false;
            }
 
index fa7b4d7e303f958fef2b5f714a389e9eabc139c2..02505c88b8e4cff64f8576616093736c8f537d47 100644 (file)
@@ -1207,7 +1207,7 @@ AtEOXact_Inval(bool isCommit)
    /* Must be at top of stack */
    Assert(transInvalInfo->my_level == 1 && transInvalInfo->parent == NULL);
 
-   INJECTION_POINT("transaction-end-process-inval");
+   INJECTION_POINT("transaction-end-process-inval", NULL);
 
    if (isCommit)
    {
index e359da09ec9f58e430d82b3bf47dfc6328b51f42..f9aec38a11fb37ce56b0a95eaed233fda22f0567 100644 (file)
@@ -952,7 +952,7 @@ lookup_type_cache(Oid type_id, int flags)
        load_domaintype_info(typentry);
    }
 
-   INJECTION_POINT("typecache-before-rel-type-cache-insert");
+   INJECTION_POINT("typecache-before-rel-type-cache-insert", NULL);
 
    Assert(in_progress_offset + 1 == in_progress_list_len);
    in_progress_list_len--;
index 28f09a27001b043411e27b41476b177f53d6d7f5..89d72cdd5ff51adece1a87b5b1fa13152b15521a 100644 (file)
@@ -747,7 +747,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
    if (!bootstrap)
    {
        pgstat_bestart_initial();
-       INJECTION_POINT("init-pre-auth");
+       INJECTION_POINT("init-pre-auth", NULL);
    }
 
    /*
index 97ab851f0a6323c48c5bf68d8cb378084f874041..f58ebc8ee522d2262600965a9478d83a55c4cc30 100644 (file)
@@ -541,14 +541,14 @@ InjectionPointLoad(const char *name)
  * Execute an injection point, if defined.
  */
 void
-InjectionPointRun(const char *name)
+InjectionPointRun(const char *name, void *arg)
 {
 #ifdef USE_INJECTION_POINTS
    InjectionPointCacheEntry *cache_entry;
 
    cache_entry = InjectionPointCacheRefresh(name);
    if (cache_entry)
-       cache_entry->callback(name, cache_entry->private_data);
+       cache_entry->callback(name, cache_entry->private_data, arg);
 #else
    elog(ERROR, "Injection points are not supported by this build");
 #endif
@@ -558,14 +558,14 @@ InjectionPointRun(const char *name)
  * Execute an injection point directly from the cache, if defined.
  */
 void
-InjectionPointCached(const char *name)
+InjectionPointCached(const char *name, void *arg)
 {
 #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);
+       cache_entry->callback(name, cache_entry->private_data, arg);
 #else
    elog(ERROR, "Injection points are not supported by this build");
 #endif
index 6ba64cd1ebc623faeac9b26e520f0a264c76fe25..a37958e1835fd144dac262aad5eeaf7352e2cefa 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)
+#define INJECTION_POINT(name, arg) InjectionPointRun(name, arg)
+#define INJECTION_POINT_CACHED(name, arg) InjectionPointCached(name, arg)
 #define IS_INJECTION_POINT_ATTACHED(name) IsInjectionPointAttached(name)
 #else
 #define INJECTION_POINT_LOAD(name) ((void) name)
-#define INJECTION_POINT(name) ((void) name)
-#define INJECTION_POINT_CACHED(name) ((void) name)
+#define INJECTION_POINT(name, arg) ((void) name)
+#define INJECTION_POINT_CACHED(name, arg) ((void) name)
 #define IS_INJECTION_POINT_ATTACHED(name) (false)
 #endif
 
@@ -30,7 +30,8 @@
  * Typedef for callback function launched by an injection point.
  */
 typedef void (*InjectionPointCallback) (const char *name,
-                                       const void *private_data);
+                                       const void *private_data,
+                                       void *arg);
 
 extern Size InjectionPointShmemSize(void);
 extern void InjectionPointShmemInit(void);
@@ -41,8 +42,8 @@ extern void InjectionPointAttach(const char *name,
                                 const void *private_data,
                                 int private_data_size);
 extern void InjectionPointLoad(const char *name);
-extern void InjectionPointRun(const char *name);
-extern void InjectionPointCached(const char *name);
+extern void InjectionPointRun(const char *name, void *arg);
+extern void InjectionPointCached(const char *name, void *arg);
 extern bool IsInjectionPointAttached(const char *name);
 extern bool InjectionPointDetach(const char *name);
 
index ad528d77752b58759cc34d9c7d50545a81c06509..44eda3caa0582c5a3da0d0b3ca2cc943835a2c79 100644 (file)
@@ -94,11 +94,14 @@ typedef struct InjectionPointSharedState
 static InjectionPointSharedState *inj_state = NULL;
 
 extern PGDLLEXPORT void injection_error(const char *name,
-                                       const void *private_data);
+                                       const void *private_data,
+                                       void *arg);
 extern PGDLLEXPORT void injection_notice(const char *name,
-                                        const void *private_data);
+                                        const void *private_data,
+                                        void *arg);
 extern PGDLLEXPORT void injection_wait(const char *name,
-                                      const void *private_data);
+                                      const void *private_data,
+                                      void *arg);
 
 /* track if injection points attached in this process are linked to it */
 static bool injection_point_local = false;
@@ -239,7 +242,7 @@ injection_points_cleanup(int code, Datum arg)
 
 /* Set of callbacks available to be attached to an injection point. */
 void
-injection_error(const char *name, const void *private_data)
+injection_error(const char *name, const void *private_data, void *arg)
 {
    InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
 
@@ -252,7 +255,7 @@ injection_error(const char *name, const void *private_data)
 }
 
 void
-injection_notice(const char *name, const void *private_data)
+injection_notice(const char *name, const void *private_data, void *arg)
 {
    InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
 
@@ -266,7 +269,7 @@ injection_notice(const char *name, const void *private_data)
 
 /* Wait on a condition variable, awaken by injection_points_wakeup() */
 void
-injection_wait(const char *name, const void *private_data)
+injection_wait(const char *name, const void *private_data, void *arg)
 {
    uint32      old_wait_counts = 0;
    int         index = -1;
@@ -405,7 +408,7 @@ injection_points_run(PG_FUNCTION_ARGS)
    char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
 
    pgstat_report_inj_fixed(0, 0, 1, 0, 0);
-   INJECTION_POINT(name);
+   INJECTION_POINT(name, NULL);
 
    PG_RETURN_VOID();
 }
@@ -420,7 +423,7 @@ injection_points_cached(PG_FUNCTION_ARGS)
    char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
 
    pgstat_report_inj_fixed(0, 0, 0, 1, 0);
-   INJECTION_POINT_CACHED(name);
+   INJECTION_POINT_CACHED(name, NULL);
 
    PG_RETURN_VOID();
 }
index 7745244b0e2471a1eb5855af8d4ae0a432a3c383..681d5d73d47307440be8d33214527773df525ac1 100644 (file)
@@ -674,11 +674,15 @@ batch_end(PG_FUNCTION_ARGS)
 }
 
 #ifdef USE_INJECTION_POINTS
-extern PGDLLEXPORT void inj_io_short_read(const char *name, const void *private_data);
-extern PGDLLEXPORT void inj_io_reopen(const char *name, const void *private_data);
+extern PGDLLEXPORT void inj_io_short_read(const char *name,
+                                         const void *private_data,
+                                         void *arg);
+extern PGDLLEXPORT void inj_io_reopen(const char *name,
+                                     const void *private_data,
+                                     void *arg);
 
 void
-inj_io_short_read(const char *name, const void *private_data)
+inj_io_short_read(const char *name, const void *private_data, void *arg)
 {
    PgAioHandle *ioh;
 
@@ -742,7 +746,7 @@ inj_io_short_read(const char *name, const void *private_data)
 }
 
 void
-inj_io_reopen(const char *name, const void *private_data)
+inj_io_reopen(const char *name, const void *private_data, void *arg)
 {
    ereport(LOG,
            errmsg("reopen injection point called, is enabled: %d",
index a9b9628c95874e0f386941ec8d682a8efcd01292..6c9b0420717cc59ce140f10a46b022fe3d1bf9d5 100644 (file)
@@ -46,7 +46,7 @@ test_read_multixact(PG_FUNCTION_ARGS)
    MultiXactId id = PG_GETARG_TRANSACTIONID(0);
    MultiXactMember *members;
 
-   INJECTION_POINT("test-multixact-read");
+   INJECTION_POINT("test-multixact-read", NULL);
    /* discard caches */
    AtEOXact_MultiXact();