LCOV - differential code coverage report
Current view: top level - src/test/modules/injection_points - injection_points.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC ECB DUB DCB
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 98.2 % 220 216 1 3 1 41 174 1 1 29
Current Date: 2026-08-27 14:31:44 +0300 Functions: 100.0 % 30 30 7 23 1
Baseline: lcov-20260827-baseline Branches: 85.4 % 144 123 7 14 39 84
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 28 28 28
(30,360] days: 98.1 % 54 53 1 13 40 1
(360..) days: 97.8 % 138 135 3 1 134
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 6 6 1 5
(360..) days: 100.0 % 23 23 5 18
Branch coverage date bins:
(7,30] days: 86.1 % 36 31 5 31
(30,360] days: 80.0 % 30 24 2 4 8 16
(360..) days: 87.2 % 78 68 10 68

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*--------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * injection_points.c
                                  4                 :                :  *      Code for testing injection points.
                                  5                 :                :  *
                                  6                 :                :  * Injection points are able to trigger user-defined callbacks in pre-defined
                                  7                 :                :  * code paths.
                                  8                 :                :  *
                                  9                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 10                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 11                 :                :  *
                                 12                 :                :  * IDENTIFICATION
                                 13                 :                :  *      src/test/modules/injection_points/injection_points.c
                                 14                 :                :  *
                                 15                 :                :  * -------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : 
                                 18                 :                : #include "postgres.h"
                                 19                 :                : 
                                 20                 :                : #include "fmgr.h"
                                 21                 :                : #include "funcapi.h"
                                 22                 :                : #include "injection_points.h"
                                 23                 :                : #include "miscadmin.h"
                                 24                 :                : #include "nodes/pg_list.h"
                                 25                 :                : #include "nodes/value.h"
                                 26                 :                : #include "storage/dsm_registry.h"
                                 27                 :                : #include "storage/ipc.h"
                                 28                 :                : #include "storage/lwlock.h"
                                 29                 :                : #include "storage/shmem.h"
                                 30                 :                : #include "storage/spin.h"
                                 31                 :                : #include "utils/builtins.h"
                                 32                 :                : #include "utils/guc.h"
                                 33                 :                : #include "utils/injection_point.h"
                                 34                 :                : #include "utils/memutils.h"
                                 35                 :                : #include "utils/tuplestore.h"
                                 36                 :                : #include "utils/wait_event.h"
                                 37                 :                : 
  948 michael@paquier.xyz        38                 :CBC         236 : PG_MODULE_MAGIC;
                                 39                 :                : 
                                 40                 :                : /* Maximum number of waits usable in injection points at once */
                                 41                 :                : #define INJ_MAX_WAIT    8
                                 42                 :                : #define INJ_NAME_MAXLEN 64
                                 43                 :                : 
                                 44                 :                : /* Thresholds of waits */
                                 45                 :                : #define INJ_WAIT_INITIAL_US     10  /* 10us */
                                 46                 :                : #define INJ_WAIT_MAX_US         100000  /* 100ms */
                                 47                 :                : 
                                 48                 :                : /*
                                 49                 :                :  * List of injection points stored in TopMemoryContext attached
                                 50                 :                :  * locally to this process.
                                 51                 :                :  */
                                 52                 :                : static List *inj_list_local = NIL;
                                 53                 :                : 
                                 54                 :                : /*
                                 55                 :                :  * Shared state information for injection points.
                                 56                 :                :  *
                                 57                 :                :  * This state data can be initialized in two ways: dynamically with a DSM
                                 58                 :                :  * or when loading the module.
                                 59                 :                :  */
                                 60                 :                : typedef struct InjectionPointSharedState
                                 61                 :                : {
                                 62                 :                :     /* Protects access to other fields */
                                 63                 :                :     slock_t     lock;
                                 64                 :                : 
                                 65                 :                :     /* Counters advancing when injection_points_wakeup() is called */
                                 66                 :                :     pg_atomic_uint32 wait_counts[INJ_MAX_WAIT];
                                 67                 :                : 
                                 68                 :                :     /* Names of injection points attached to wait counters */
                                 69                 :                :     char        name[INJ_MAX_WAIT][INJ_NAME_MAXLEN];
                                 70                 :                : } InjectionPointSharedState;
                                 71                 :                : 
                                 72                 :                : /* Pointer to shared-memory state. */
                                 73                 :                : static InjectionPointSharedState *inj_state = NULL;
                                 74                 :                : 
                                 75                 :                : extern PGDLLEXPORT void injection_error(const char *name,
                                 76                 :                :                                         const void *private_data,
                                 77                 :                :                                         void *arg);
                                 78                 :                : extern PGDLLEXPORT void injection_notice(const char *name,
                                 79                 :                :                                          const void *private_data,
                                 80                 :                :                                          void *arg);
                                 81                 :                : extern PGDLLEXPORT void injection_wait(const char *name,
                                 82                 :                :                                        const void *private_data,
                                 83                 :                :                                        void *arg);
                                 84                 :                : 
                                 85                 :                : /* track if injection points attached in this process are linked to it */
                                 86                 :                : static bool injection_point_local = false;
                                 87                 :                : 
                                 88                 :                : static void injection_shmem_request(void *arg);
                                 89                 :                : static void injection_shmem_init(void *arg);
                                 90                 :                : 
                                 91                 :                : static const ShmemCallbacks injection_shmem_callbacks = {
                                 92                 :                :     .request_fn = injection_shmem_request,
                                 93                 :                :     .init_fn = injection_shmem_init,
                                 94                 :                : };
                                 95                 :                : 
                                 96                 :                : /*
                                 97                 :                :  * Routine for shared memory area initialization, used as a callback
                                 98                 :                :  * when initializing dynamically with a DSM or when loading the module.
                                 99                 :                :  */
                                100                 :                : static void
  255 nathan@postgresql.or      101                 :             20 : injection_point_init_state(void *ptr, void *arg)
                                102                 :                : {
  906 michael@paquier.xyz       103                 :             20 :     InjectionPointSharedState *state = (InjectionPointSharedState *) ptr;
                                104                 :                : 
                                105                 :             20 :     SpinLockInit(&state->lock);
                                106                 :             20 :     memset(state->name, 0, sizeof(state->name));
   50 michael@paquier.xyz       107         [ +  + ]:GNC         180 :     for (int i = 0; i < INJ_MAX_WAIT; i++)
                                108                 :            160 :         pg_atomic_init_u32(&state->wait_counts[i], 0);
  906 michael@paquier.xyz       109                 :CBC          20 : }
                                110                 :                : 
                                111                 :                : static void
  143 heikki.linnakangas@i      112                 :              3 : injection_shmem_request(void *arg)
                                113                 :                : {
                                114                 :              3 :     ShmemRequestStruct(.name = "injection_points",
                                115                 :                :                        .size = sizeof(InjectionPointSharedState),
                                116                 :                :                        .ptr = (void **) &inj_state,
                                117                 :                :         );
  734 michael@paquier.xyz       118                 :              3 : }
                                119                 :                : 
                                120                 :                : static void
  143 heikki.linnakangas@i      121                 :              3 : injection_shmem_init(void *arg)
                                122                 :                : {
                                123                 :                :     /*
                                124                 :                :      * First time through, so initialize.  This is shared with the dynamic
                                125                 :                :      * initialization using a DSM.
                                126                 :                :      */
                                127                 :              3 :     injection_point_init_state(inj_state, NULL);
  734 michael@paquier.xyz       128                 :              3 : }
                                129                 :                : 
                                130                 :                : /*
                                131                 :                :  * Initialize shared memory area for this module through DSM.
                                132                 :                :  */
                                133                 :                : static void
  906                           134                 :            159 : injection_init_shmem(void)
                                135                 :                : {
                                136                 :                :     bool        found;
                                137                 :                : 
                                138         [ -  + ]:            159 :     if (inj_state != NULL)
  906 michael@paquier.xyz       139                 :UBC           0 :         return;
                                140                 :                : 
  906 michael@paquier.xyz       141                 :CBC         159 :     inj_state = GetNamedDSMSegment("injection_points",
                                142                 :                :                                    sizeof(InjectionPointSharedState),
                                143                 :                :                                    injection_point_init_state,
                                144                 :                :                                    &found, NULL);
                                145                 :                : }
                                146                 :                : 
                                147                 :                : /*
                                148                 :                :  * Check runtime conditions associated to an injection point.
                                149                 :                :  *
                                150                 :                :  * Returns true if the named injection point is allowed to run, and false
                                151                 :                :  * otherwise.
                                152                 :                :  */
                                153                 :                : static bool
   13 michael@paquier.xyz       154                 :GNC         307 : injection_point_allowed(const InjectionPointCondition *condition,
                                155                 :                :                         const char *arg)
                                156                 :                : {
                                157                 :                :     /* Does not match the condition PID? */
                                158         [ +  + ]:            307 :     if ((condition->type & INJ_CONDITION_PID) &&
                                159         [ +  + ]:            264 :         MyProcPid != condition->pid)
                                160                 :            124 :         return false;
                                161                 :                : 
                                162                 :                :     /* Does not match the condition string? */
                                163   [ +  +  +  + ]:            183 :     if ((condition->type & INJ_CONDITION_STRING) &&
                                164         [ +  + ]:              4 :         (arg == NULL || strcmp(condition->str, arg) != 0))
                                165                 :              2 :         return false;
                                166                 :                : 
                                167                 :            181 :     return true;
                                168                 :                : }
                                169                 :                : 
                                170                 :                : /*
                                171                 :                :  * before_shmem_exit callback to remove injection points linked to a
                                172                 :                :  * specific process.
                                173                 :                :  */
                                174                 :                : static void
  871 michael@paquier.xyz       175                 :CBC          79 : injection_points_cleanup(int code, Datum arg)
                                176                 :                : {
                                177                 :                :     ListCell   *lc;
                                178                 :                : 
                                179                 :                :     /* Leave if nothing is tracked locally */
                                180         [ -  + ]:             79 :     if (!injection_point_local)
  871 michael@paquier.xyz       181                 :UBC           0 :         return;
                                182                 :                : 
                                183                 :                :     /* Detach all the local points */
  837 michael@paquier.xyz       184   [ +  +  +  +  :CBC         241 :     foreach(lc, inj_list_local)
                                              +  + ]
                                185                 :                :     {
                                186                 :            162 :         char       *name = strVal(lfirst(lc));
                                187                 :                : 
                                188                 :            162 :         (void) InjectionPointDetach(name);
                                189                 :                :     }
                                190                 :                : }
                                191                 :                : 
                                192                 :                : /* Set of callbacks available to be attached to an injection point. */
                                193                 :                : void
  474                           194                 :             14 : injection_error(const char *name, const void *private_data, void *arg)
                                195                 :                : {
  227 peter@eisentraut.org      196                 :             14 :     const InjectionPointCondition *condition = private_data;
                                197                 :             14 :     char       *argstr = arg;
                                198                 :                : 
   13 michael@paquier.xyz       199         [ -  + ]:GNC          14 :     if (!injection_point_allowed(condition, argstr))
  871 michael@paquier.xyz       200                 :UBC           0 :         return;
                                201                 :                : 
  474 michael@paquier.xyz       202         [ +  + ]:CBC          14 :     if (argstr)
                                203         [ +  - ]:              1 :         elog(ERROR, "error triggered for injection point %s (%s)",
                                204                 :                :              name, argstr);
                                205                 :                :     else
                                206         [ +  - ]:             13 :         elog(ERROR, "error triggered for injection point %s", name);
                                207                 :                : }
                                208                 :                : 
                                209                 :                : void
                                210                 :             77 : injection_notice(const char *name, const void *private_data, void *arg)
                                211                 :                : {
  227 peter@eisentraut.org      212                 :             77 :     const InjectionPointCondition *condition = private_data;
                                213                 :             77 :     char       *argstr = arg;
                                214                 :                : 
   13 michael@paquier.xyz       215         [ +  + ]:GNC          77 :     if (!injection_point_allowed(condition, argstr))
  871 michael@paquier.xyz       216                 :GBC           2 :         return;
                                217                 :                : 
  474 michael@paquier.xyz       218         [ +  + ]:CBC          75 :     if (argstr)
                                219         [ +  + ]:              4 :         elog(NOTICE, "notice triggered for injection point %s (%s)",
                                220                 :                :              name, argstr);
                                221                 :                :     else
                                222         [ +  - ]:             71 :         elog(NOTICE, "notice triggered for injection point %s", name);
                                223                 :                : }
                                224                 :                : 
                                225                 :                : /*
                                226                 :                :  * Error cleanup callback for injection point waits.
                                227                 :                :  */
                                228                 :                : static void
   35                           229                 :             92 : injection_wait_cleanup(int code, Datum arg)
                                230                 :                : {
                                231                 :             92 :     int         index = DatumGetInt32(arg);
                                232                 :                : 
                                233                 :             92 :     SpinLockAcquire(&inj_state->lock);
                                234                 :             92 :     inj_state->name[index][0] = '\0';
                                235                 :             92 :     SpinLockRelease(&inj_state->lock);
                                236                 :             92 : }
                                237                 :                : 
                                238                 :                : /* Wait until injection_points_wakeup() is called */
                                239                 :                : void
  474                           240                 :            216 : injection_wait(const char *name, const void *private_data, void *arg)
                                241                 :                : {
  906                           242                 :            216 :     uint32      old_wait_counts = 0;
                                243                 :            216 :     int         index = -1;
                                244                 :            216 :     uint32      injection_wait_event = 0;
  227 peter@eisentraut.org      245                 :            216 :     const InjectionPointCondition *condition = private_data;
   13 michael@paquier.xyz       246                 :GNC         216 :     char       *argstr = arg;
   50                           247                 :            216 :     int         delay_us = 0;
                                248                 :                : 
  906 michael@paquier.xyz       249         [ +  + ]:CBC         216 :     if (inj_state == NULL)
                                250                 :             34 :         injection_init_shmem();
                                251                 :                : 
   13 michael@paquier.xyz       252         [ +  + ]:GNC         216 :     if (!injection_point_allowed(condition, argstr))
  871 michael@paquier.xyz       253                 :CBC         124 :         return;
                                254                 :                : 
                                255                 :                :     /*
                                256                 :                :      * Use the injection point name for this custom wait event.  Note that
                                257                 :                :      * this custom wait event name is not released, but we don't care much for
                                258                 :                :      * testing as this should be short-lived.
                                259                 :                :      */
  791 noah@leadboat.com         260                 :             92 :     injection_wait_event = WaitEventInjectionPointNew(name);
                                261                 :                : 
                                262                 :                :     /*
                                263                 :                :      * Find a free slot to wait for, and register this injection point's name.
                                264                 :                :      */
  906 michael@paquier.xyz       265                 :             92 :     SpinLockAcquire(&inj_state->lock);
                                266         [ +  - ]:            125 :     for (int i = 0; i < INJ_MAX_WAIT; i++)
                                267                 :                :     {
                                268         [ +  + ]:            125 :         if (inj_state->name[i][0] == '\0')
                                269                 :                :         {
                                270                 :             92 :             index = i;
                                271                 :             92 :             strlcpy(inj_state->name[i], name, INJ_NAME_MAXLEN);
   50 michael@paquier.xyz       272                 :GNC          92 :             old_wait_counts = pg_atomic_read_u32(&inj_state->wait_counts[i]);
  906 michael@paquier.xyz       273                 :CBC          92 :             break;
                                274                 :                :         }
                                275                 :                :     }
                                276                 :             92 :     SpinLockRelease(&inj_state->lock);
                                277                 :                : 
                                278         [ -  + ]:             92 :     if (index < 0)
   50 michael@paquier.xyz       279         [ #  # ]:UNC           0 :         elog(ERROR, "could not find free slot for wait of injection point %s",
                                280                 :                :              name);
                                281                 :                : 
                                282                 :                :     /*
                                283                 :                :      * Wait until the counter is bumped by injection_points_wakeup().
                                284                 :                :      *
                                285                 :                :      * This loop starts with a short delay for responsiveness, enlarged to
                                286                 :                :      * ease the CPU workload in slower environments.
                                287                 :                :      */
   50 michael@paquier.xyz       288                 :GNC          92 :     delay_us = INJ_WAIT_INITIAL_US;
                                289                 :                : 
                                290                 :             92 :     pgstat_report_wait_start(injection_wait_event);
   35 michael@paquier.xyz       291         [ +  + ]:CBC          92 :     PG_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index));
                                292                 :                :     {
   35 michael@paquier.xyz       293         [ +  + ]:GNC        1404 :         while (pg_atomic_read_u32(&inj_state->wait_counts[index]) == old_wait_counts)
   35 michael@paquier.xyz       294                 :ECB       (127) :         {
   35 michael@paquier.xyz       295         [ +  + ]:GNC        1317 :             CHECK_FOR_INTERRUPTS();
                                296                 :           1312 :             pg_usleep(delay_us);
                                297         [ +  + ]:           1312 :             if (delay_us < INJ_WAIT_MAX_US)
                                298                 :           1245 :                 delay_us = Min(delay_us * 2, INJ_WAIT_MAX_US);
                                299                 :                :         }
                                300                 :                :     }
   35 michael@paquier.xyz       301         [ -  + ]:CBC          90 :     PG_END_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index));
   50 michael@paquier.xyz       302                 :GNC          87 :     pgstat_report_wait_end();
                                303                 :                : 
                                304                 :                :     /* Remove this injection point from the waiters. */
   35 michael@paquier.xyz       305                 :CBC          87 :     injection_wait_cleanup(0, Int32GetDatum(index));
                                306                 :                : }
                                307                 :                : 
                                308                 :                : /*
                                309                 :                :  * SQL function for creating an injection point.
                                310                 :                :  */
  948                           311                 :            170 : PG_FUNCTION_INFO_V1(injection_points_attach);
                                312                 :                : Datum
                                313                 :            154 : injection_points_attach(PG_FUNCTION_ARGS)
                                314                 :                : {
                                315                 :                :     char       *name;
                                316                 :                :     char       *action;
                                317                 :                :     char       *str;
                                318                 :                :     char       *function;
  837                           319                 :            154 :     InjectionPointCondition condition = {0};
                                320                 :                : 
   13 michael@paquier.xyz       321         [ +  + ]:GNC         154 :     if (PG_ARGISNULL(0))
                                322         [ +  - ]:              1 :         elog(ERROR, "injection point name must not be null");
                                323                 :                : 
                                324         [ +  + ]:            153 :     if (PG_ARGISNULL(1))
                                325         [ +  - ]:              1 :         elog(ERROR, "injection point action must not be null");
                                326                 :                : 
                                327                 :            152 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                328                 :            152 :     action = text_to_cstring(PG_GETARG_TEXT_PP(1));
                                329                 :            304 :     str = PG_ARGISNULL(2) ? NULL
                                330         [ +  + ]:            152 :         : text_to_cstring(PG_GETARG_TEXT_PP(2));
                                331                 :                : 
  948 michael@paquier.xyz       332         [ +  + ]:CBC         152 :     if (strcmp(action, "error") == 0)
                                333                 :             21 :         function = "injection_error";
                                334         [ +  + ]:            131 :     else if (strcmp(action, "notice") == 0)
                                335                 :             32 :         function = "injection_notice";
  906                           336         [ +  + ]:             99 :     else if (strcmp(action, "wait") == 0)
                                337                 :             98 :         function = "injection_wait";
                                338                 :                :     else
  948                           339         [ +  - ]:              1 :         elog(ERROR, "incorrect action \"%s\" for injection point creation", action);
                                340                 :                : 
  871                           341         [ +  + ]:            151 :     if (injection_point_local)
                                342                 :                :     {
   13 michael@paquier.xyz       343                 :GNC         111 :         condition.type |= INJ_CONDITION_PID;
  837 michael@paquier.xyz       344                 :CBC         111 :         condition.pid = MyProcPid;
                                345                 :                :     }
                                346                 :                : 
   13 michael@paquier.xyz       347         [ +  + ]:GNC         151 :     if (str)
                                348                 :                :     {
                                349         [ +  + ]:              5 :         if (str[0] == '\0')
                                350         [ +  - ]:              1 :             elog(ERROR, "injection point condition string must not be empty");
                                351         [ +  + ]:              4 :         if (strlen(str) >= INJ_DATA_MAXLEN)
                                352         [ +  - ]:              1 :             elog(ERROR, "injection point condition string too long (maximum of %d characters)",
                                353                 :                :                  INJ_DATA_MAXLEN - 1);
                                354                 :              3 :         condition.type |= INJ_CONDITION_STRING;
                                355                 :              3 :         strlcpy(condition.str, str, INJ_DATA_MAXLEN);
                                356                 :                :     }
                                357                 :                : 
  837 michael@paquier.xyz       358                 :CBC         149 :     InjectionPointAttach(name, "injection_points", function, &condition,
                                359                 :                :                          sizeof(InjectionPointCondition));
                                360                 :                : 
                                361         [ +  + ]:            149 :     if (injection_point_local)
                                362                 :                :     {
                                363                 :                :         MemoryContext oldctx;
                                364                 :                : 
                                365                 :                :         /* Local injection point, so track it for automated cleanup */
                                366                 :            111 :         oldctx = MemoryContextSwitchTo(TopMemoryContext);
                                367                 :            111 :         inj_list_local = lappend(inj_list_local, makeString(pstrdup(name)));
                                368                 :            111 :         MemoryContextSwitchTo(oldctx);
                                369                 :                :     }
                                370                 :                : 
  948                           371                 :            149 :     PG_RETURN_VOID();
                                372                 :                : }
                                373                 :                : 
                                374                 :                : /*
                                375                 :                :  * SQL function for creating an injection point with library name, function
                                376                 :                :  * name and private data.
                                377                 :                :  */
  290                           378                 :             59 : PG_FUNCTION_INFO_V1(injection_points_attach_func);
                                379                 :                : Datum
                                380                 :              8 : injection_points_attach_func(PG_FUNCTION_ARGS)
                                381                 :                : {
                                382                 :                :     char       *name;
                                383                 :                :     char       *lib_name;
                                384                 :                :     char       *function;
                                385                 :              8 :     bytea      *private_data = NULL;
                                386                 :              8 :     int         private_data_size = 0;
                                387                 :                : 
                                388         [ +  + ]:              8 :     if (PG_ARGISNULL(0))
                                389         [ +  - ]:              1 :         elog(ERROR, "injection point name cannot be NULL");
                                390         [ +  + ]:              7 :     if (PG_ARGISNULL(1))
                                391         [ +  - ]:              1 :         elog(ERROR, "injection point library cannot be NULL");
                                392         [ +  + ]:              6 :     if (PG_ARGISNULL(2))
                                393         [ +  - ]:              1 :         elog(ERROR, "injection point function cannot be NULL");
                                394                 :                : 
                                395                 :              5 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                396                 :              5 :     lib_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
                                397                 :              5 :     function = text_to_cstring(PG_GETARG_TEXT_PP(2));
                                398                 :                : 
                                399         [ +  + ]:              5 :     if (!PG_ARGISNULL(3))
                                400                 :                :     {
                                401                 :              1 :         private_data = PG_GETARG_BYTEA_PP(3);
                                402                 :              1 :         private_data_size = VARSIZE_ANY_EXHDR(private_data);
                                403                 :                :     }
                                404                 :                : 
                                405         [ +  + ]:              5 :     if (private_data != NULL)
                                406                 :              1 :         InjectionPointAttach(name, lib_name, function, VARDATA_ANY(private_data),
                                407                 :                :                              private_data_size);
                                408                 :                :     else
                                409                 :              4 :         InjectionPointAttach(name, lib_name, function, NULL,
                                410                 :                :                              0);
                                411                 :              1 :     PG_RETURN_VOID();
                                412                 :                : }
                                413                 :                : 
                                414                 :                : /*
                                415                 :                :  * SQL function for loading an injection point.
                                416                 :                :  */
  783                           417                 :             59 : PG_FUNCTION_INFO_V1(injection_points_load);
                                418                 :                : Datum
                                419                 :              2 : injection_points_load(PG_FUNCTION_ARGS)
                                420                 :                : {
                                421                 :              2 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                422                 :                : 
                                423         [ +  + ]:              2 :     if (inj_state == NULL)
                                424                 :              1 :         injection_init_shmem();
                                425                 :                : 
                                426                 :              2 :     INJECTION_POINT_LOAD(name);
                                427                 :                : 
                                428                 :              2 :     PG_RETURN_VOID();
                                429                 :                : }
                                430                 :                : 
                                431                 :                : /*
                                432                 :                :  * SQL function for triggering an injection point.
                                433                 :                :  */
  948                           434                 :             68 : PG_FUNCTION_INFO_V1(injection_points_run);
                                435                 :                : Datum
                                436                 :             35 : injection_points_run(PG_FUNCTION_ARGS)
                                437                 :                : {
                                438                 :                :     char       *name;
  474                           439                 :             35 :     char       *arg = NULL;
                                440                 :                : 
                                441         [ +  + ]:             35 :     if (PG_ARGISNULL(0))
                                442                 :              1 :         PG_RETURN_VOID();
                                443                 :             34 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                444                 :                : 
                                445         [ +  + ]:             34 :     if (!PG_ARGISNULL(1))
                                446                 :              4 :         arg = text_to_cstring(PG_GETARG_TEXT_PP(1));
                                447                 :                : 
                                448                 :             34 :     INJECTION_POINT(name, arg);
                                449                 :                : 
  948                           450                 :             26 :     PG_RETURN_VOID();
                                451                 :                : }
                                452                 :                : 
                                453                 :                : /*
                                454                 :                :  * SQL function for triggering an injection point from cache.
                                455                 :                :  */
  770                           456                 :             60 : PG_FUNCTION_INFO_V1(injection_points_cached);
                                457                 :                : Datum
                                458                 :              5 : injection_points_cached(PG_FUNCTION_ARGS)
                                459                 :                : {
                                460                 :                :     char       *name;
  474                           461                 :              5 :     char       *arg = NULL;
                                462                 :                : 
                                463         [ +  + ]:              5 :     if (PG_ARGISNULL(0))
                                464                 :              1 :         PG_RETURN_VOID();
                                465                 :              4 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                466                 :                : 
                                467         [ +  + ]:              4 :     if (!PG_ARGISNULL(1))
                                468                 :              1 :         arg = text_to_cstring(PG_GETARG_TEXT_PP(1));
                                469                 :                : 
                                470                 :              4 :     INJECTION_POINT_CACHED(name, arg);
                                471                 :                : 
  770                           472                 :              4 :     PG_RETURN_VOID();
                                473                 :                : }
                                474                 :                : 
                                475                 :                : /*
                                476                 :                :  * SQL function for waking up an injection point waiting in injection_wait().
                                477                 :                :  */
  906                           478                 :            149 : PG_FUNCTION_INFO_V1(injection_points_wakeup);
                                479                 :                : Datum
                                480                 :             97 : injection_points_wakeup(PG_FUNCTION_ARGS)
                                481                 :                : {
                                482                 :             97 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                483                 :             97 :     int         index = -1;
                                484                 :                : 
                                485         [ +  + ]:             97 :     if (inj_state == NULL)
                                486                 :             67 :         injection_init_shmem();
                                487                 :                : 
                                488                 :                :     /* Find the injection point then bump its wait counter */
                                489                 :             97 :     SpinLockAcquire(&inj_state->lock);
                                490         [ +  + ]:            140 :     for (int i = 0; i < INJ_MAX_WAIT; i++)
                                491                 :                :     {
                                492         [ +  + ]:            139 :         if (strcmp(name, inj_state->name[i]) == 0)
                                493                 :                :         {
                                494                 :             96 :             index = i;
                                495                 :             96 :             break;
                                496                 :                :         }
                                497                 :                :     }
                                498         [ +  + ]:             97 :     if (index < 0)
                                499                 :                :     {
                                500                 :              1 :         SpinLockRelease(&inj_state->lock);
                                501         [ +  - ]:              1 :         elog(ERROR, "could not find injection point %s to wake up", name);
                                502                 :                :     }
                                503                 :             96 :     SpinLockRelease(&inj_state->lock);
                                504                 :                : 
   50 michael@paquier.xyz       505                 :GNC          96 :     pg_atomic_fetch_add_u32(&inj_state->wait_counts[index], 1);
  906 michael@paquier.xyz       506                 :CBC          96 :     PG_RETURN_VOID();
                                507                 :                : }
                                508                 :                : 
                                509                 :                : /*
                                510                 :                :  * injection_points_set_local
                                511                 :                :  *
                                512                 :                :  * Track if any injection point created in this process ought to run only
                                513                 :                :  * in this process.  Such injection points are detached automatically when
                                514                 :                :  * this process exits.  This is useful to make test suites concurrent-safe.
                                515                 :                :  */
  871                           516                 :            137 : PG_FUNCTION_INFO_V1(injection_points_set_local);
                                517                 :                : Datum
                                518                 :             79 : injection_points_set_local(PG_FUNCTION_ARGS)
                                519                 :                : {
                                520                 :                :     /* Enable flag to add a runtime condition based on this process ID */
                                521                 :             79 :     injection_point_local = true;
                                522                 :                : 
                                523         [ +  + ]:             79 :     if (inj_state == NULL)
                                524                 :             57 :         injection_init_shmem();
                                525                 :                : 
                                526                 :                :     /*
                                527                 :                :      * Register a before_shmem_exit callback to remove any injection points
                                528                 :                :      * linked to this process.
                                529                 :                :      */
                                530                 :             79 :     before_shmem_exit(injection_points_cleanup, (Datum) 0);
                                531                 :                : 
                                532                 :             79 :     PG_RETURN_VOID();
                                533                 :                : }
                                534                 :                : 
                                535                 :                : /*
                                536                 :                :  * SQL function for dropping an injection point.
                                537                 :                :  */
  948                           538                 :            156 : PG_FUNCTION_INFO_V1(injection_points_detach);
                                539                 :                : Datum
                                540                 :            131 : injection_points_detach(PG_FUNCTION_ARGS)
                                541                 :                : {
                                542                 :            131 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                543                 :                : 
  837                           544         [ +  + ]:            131 :     if (!InjectionPointDetach(name))
                                545         [ +  - ]:              1 :         elog(ERROR, "could not detach injection point \"%s\"", name);
                                546                 :                : 
                                547                 :                :     /* Remove point from local list, if required */
                                548         [ +  + ]:            130 :     if (inj_list_local != NIL)
                                549                 :                :     {
                                550                 :                :         MemoryContext oldctx;
                                551                 :                : 
                                552                 :             32 :         oldctx = MemoryContextSwitchTo(TopMemoryContext);
                                553                 :             32 :         inj_list_local = list_delete(inj_list_local, makeString(name));
                                554                 :             32 :         MemoryContextSwitchTo(oldctx);
                                555                 :                :     }
                                556                 :                : 
  948                           557                 :            130 :     PG_RETURN_VOID();
                                558                 :                : }
                                559                 :                : 
                                560                 :                : /*
                                561                 :                :  * SQL function for listing all the injection points attached.
                                562                 :                :  */
  413                           563                 :             60 : PG_FUNCTION_INFO_V1(injection_points_list);
                                564                 :                : Datum
                                565                 :              3 : injection_points_list(PG_FUNCTION_ARGS)
                                566                 :                : {
                                567                 :                : #define NUM_INJECTION_POINTS_LIST 3
                                568                 :              3 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
                                569                 :                :     List       *inj_points;
                                570                 :                :     ListCell   *lc;
                                571                 :                : 
                                572                 :                :     /* Build a tuplestore to return our results in */
                                573                 :              3 :     InitMaterializedSRF(fcinfo, 0);
                                574                 :                : 
                                575                 :              3 :     inj_points = InjectionPointList();
                                576                 :                : 
                                577   [ +  +  +  +  :              7 :     foreach(lc, inj_points)
                                              +  + ]
                                578                 :                :     {
                                579                 :                :         Datum       values[NUM_INJECTION_POINTS_LIST];
                                580                 :                :         bool        nulls[NUM_INJECTION_POINTS_LIST];
                                581                 :              4 :         InjectionPointData *inj_point = lfirst(lc);
                                582                 :                : 
                                583                 :              4 :         memset(values, 0, sizeof(values));
                                584                 :              4 :         memset(nulls, 0, sizeof(nulls));
                                585                 :                : 
                                586                 :              4 :         values[0] = PointerGetDatum(cstring_to_text(inj_point->name));
                                587                 :              4 :         values[1] = PointerGetDatum(cstring_to_text(inj_point->library));
                                588                 :              4 :         values[2] = PointerGetDatum(cstring_to_text(inj_point->function));
                                589                 :                : 
                                590                 :                :         /* shove row into tuplestore */
                                591                 :              4 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
                                592                 :                :     }
                                593                 :                : 
                                594                 :              3 :     return (Datum) 0;
                                595                 :                : #undef NUM_INJECTION_POINTS_LIST
                                596                 :                : }
                                597                 :                : 
                                598                 :                : void
  752                           599                 :            236 : _PG_init(void)
                                600                 :                : {
                                601         [ +  + ]:            236 :     if (!process_shared_preload_libraries_in_progress)
                                602                 :            233 :         return;
                                603                 :                : 
  143 heikki.linnakangas@i      604                 :              3 :     RegisterShmemCallbacks(&injection_shmem_callbacks);
                                605                 :                : }
        

Generated by: LCOV version 2.0-1