LCOV - code coverage report
Current view: top level - src/test/modules/injection_points - injection_points.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 98.2 % 220 216
Test Date: 2026-10-15 20:15:50 Functions: 100.0 % 30 30
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 85.4 % 144 123

             Branch data     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                 :             : 
      38                 :         304 : 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
     101                 :          26 : injection_point_init_state(void *ptr, void *arg)
     102                 :             : {
     103                 :          26 :     InjectionPointSharedState *state = (InjectionPointSharedState *) ptr;
     104                 :             : 
     105                 :          26 :     SpinLockInit(&state->lock);
     106                 :          26 :     memset(state->name, 0, sizeof(state->name));
     107         [ +  + ]:         234 :     for (int i = 0; i < INJ_MAX_WAIT; i++)
     108                 :         208 :         pg_atomic_init_u32(&state->wait_counts[i], 0);
     109                 :          26 : }
     110                 :             : 
     111                 :             : static void
     112                 :           3 : injection_shmem_request(void *arg)
     113                 :             : {
     114                 :           3 :     ShmemRequestStruct(.name = "injection_points",
     115                 :             :                        .size = sizeof(InjectionPointSharedState),
     116                 :             :                        .ptr = (void **) &inj_state,
     117                 :             :         );
     118                 :           3 : }
     119                 :             : 
     120                 :             : static void
     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);
     128                 :           3 : }
     129                 :             : 
     130                 :             : /*
     131                 :             :  * Initialize shared memory area for this module through DSM.
     132                 :             :  */
     133                 :             : static void
     134                 :         182 : injection_init_shmem(void)
     135                 :             : {
     136                 :             :     bool        found;
     137                 :             : 
     138         [ -  + ]:         182 :     if (inj_state != NULL)
     139                 :           0 :         return;
     140                 :             : 
     141                 :         182 :     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
     154                 :         340 : injection_point_allowed(const InjectionPointCondition *condition,
     155                 :             :                         const char *arg)
     156                 :             : {
     157                 :             :     /* Does not match the condition PID? */
     158         [ +  + ]:         340 :     if ((condition->type & INJ_CONDITION_PID) &&
     159         [ +  + ]:         284 :         MyProcPid != condition->pid)
     160                 :         128 :         return false;
     161                 :             : 
     162                 :             :     /* Does not match the condition string? */
     163   [ +  +  +  + ]:         212 :     if ((condition->type & INJ_CONDITION_STRING) &&
     164         [ +  + ]:           5 :         (arg == NULL || strcmp(condition->str, arg) != 0))
     165                 :           2 :         return false;
     166                 :             : 
     167                 :         210 :     return true;
     168                 :             : }
     169                 :             : 
     170                 :             : /*
     171                 :             :  * before_shmem_exit callback to remove injection points linked to a
     172                 :             :  * specific process.
     173                 :             :  */
     174                 :             : static void
     175                 :          94 : injection_points_cleanup(int code, Datum arg)
     176                 :             : {
     177                 :             :     ListCell   *lc;
     178                 :             : 
     179                 :             :     /* Leave if nothing is tracked locally */
     180         [ -  + ]:          94 :     if (!injection_point_local)
     181                 :           0 :         return;
     182                 :             : 
     183                 :             :     /* Detach all the local points */
     184   [ +  +  +  +  :         277 :     foreach(lc, inj_list_local)
                   +  + ]
     185                 :             :     {
     186                 :         183 :         char       *name = strVal(lfirst(lc));
     187                 :             : 
     188                 :         183 :         (void) InjectionPointDetach(name);
     189                 :             :     }
     190                 :             : }
     191                 :             : 
     192                 :             : /* Set of callbacks available to be attached to an injection point. */
     193                 :             : void
     194                 :          17 : injection_error(const char *name, const void *private_data, void *arg)
     195                 :             : {
     196                 :          17 :     const InjectionPointCondition *condition = private_data;
     197                 :          17 :     char       *argstr = arg;
     198                 :             : 
     199         [ -  + ]:          17 :     if (!injection_point_allowed(condition, argstr))
     200                 :           0 :         return;
     201                 :             : 
     202         [ +  + ]:          17 :     if (argstr)
     203         [ +  - ]:           1 :         elog(ERROR, "error triggered for injection point %s (%s)",
     204                 :             :              name, argstr);
     205                 :             :     else
     206         [ +  - ]:          16 :         elog(ERROR, "error triggered for injection point %s", name);
     207                 :             : }
     208                 :             : 
     209                 :             : void
     210                 :          78 : injection_notice(const char *name, const void *private_data, void *arg)
     211                 :             : {
     212                 :          78 :     const InjectionPointCondition *condition = private_data;
     213                 :          78 :     char       *argstr = arg;
     214                 :             : 
     215         [ +  + ]:          78 :     if (!injection_point_allowed(condition, argstr))
     216                 :           2 :         return;
     217                 :             : 
     218         [ +  + ]:          76 :     if (argstr)
     219         [ +  + ]:           4 :         elog(NOTICE, "notice triggered for injection point %s (%s)",
     220                 :             :              name, argstr);
     221                 :             :     else
     222         [ +  - ]:          72 :         elog(NOTICE, "notice triggered for injection point %s", name);
     223                 :             : }
     224                 :             : 
     225                 :             : /*
     226                 :             :  * Error cleanup callback for injection point waits.
     227                 :             :  */
     228                 :             : static void
     229                 :         117 : injection_wait_cleanup(int code, Datum arg)
     230                 :             : {
     231                 :         117 :     int         index = DatumGetInt32(arg);
     232                 :             : 
     233                 :         117 :     SpinLockAcquire(&inj_state->lock);
     234                 :         117 :     inj_state->name[index][0] = '\0';
     235                 :         117 :     SpinLockRelease(&inj_state->lock);
     236                 :         117 : }
     237                 :             : 
     238                 :             : /* Wait until injection_points_wakeup() is called */
     239                 :             : void
     240                 :         245 : injection_wait(const char *name, const void *private_data, void *arg)
     241                 :             : {
     242                 :         245 :     uint32      old_wait_counts = 0;
     243                 :         245 :     int         index = -1;
     244                 :         245 :     uint32      injection_wait_event = 0;
     245                 :         245 :     const InjectionPointCondition *condition = private_data;
     246                 :         245 :     char       *argstr = arg;
     247                 :         245 :     int         delay_us = 0;
     248                 :             : 
     249                 :             :     /*
     250                 :             :      * Check the condition before attaching to the shared state: attaching
     251                 :             :      * allocates memory, which a process that is not meant to wait here must
     252                 :             :      * not do if the injection point is in a critical section.
     253                 :             :      */
     254         [ +  + ]:         245 :     if (!injection_point_allowed(condition, argstr))
     255                 :         128 :         return;
     256                 :             : 
     257         [ +  + ]:         117 :     if (inj_state == NULL)
     258                 :          25 :         injection_init_shmem();
     259                 :             : 
     260                 :             :     /*
     261                 :             :      * Use the injection point name for this custom wait event.  Note that
     262                 :             :      * this custom wait event name is not released, but we don't care much for
     263                 :             :      * testing as this should be short-lived.
     264                 :             :      */
     265                 :         117 :     injection_wait_event = WaitEventInjectionPointNew(name);
     266                 :             : 
     267                 :             :     /*
     268                 :             :      * Find a free slot to wait for, and register this injection point's name.
     269                 :             :      */
     270                 :         117 :     SpinLockAcquire(&inj_state->lock);
     271         [ +  - ]:         149 :     for (int i = 0; i < INJ_MAX_WAIT; i++)
     272                 :             :     {
     273         [ +  + ]:         149 :         if (inj_state->name[i][0] == '\0')
     274                 :             :         {
     275                 :         117 :             index = i;
     276                 :         117 :             strlcpy(inj_state->name[i], name, INJ_NAME_MAXLEN);
     277                 :         117 :             old_wait_counts = pg_atomic_read_u32(&inj_state->wait_counts[i]);
     278                 :         117 :             break;
     279                 :             :         }
     280                 :             :     }
     281                 :         117 :     SpinLockRelease(&inj_state->lock);
     282                 :             : 
     283         [ -  + ]:         117 :     if (index < 0)
     284         [ #  # ]:           0 :         elog(ERROR, "could not find free slot for wait of injection point %s",
     285                 :             :              name);
     286                 :             : 
     287                 :             :     /*
     288                 :             :      * Wait until the counter is bumped by injection_points_wakeup().
     289                 :             :      *
     290                 :             :      * This loop starts with a short delay for responsiveness, enlarged to
     291                 :             :      * ease the CPU workload in slower environments.
     292                 :             :      */
     293                 :         117 :     delay_us = INJ_WAIT_INITIAL_US;
     294                 :             : 
     295                 :         117 :     pgstat_report_wait_start(injection_wait_event);
     296         [ +  + ]:         117 :     PG_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index));
     297                 :             :     {
     298         [ +  + ]:        1928 :         while (pg_atomic_read_u32(&inj_state->wait_counts[index]) == old_wait_counts)
     299                 :             :         {
     300         [ +  + ]:        1817 :             CHECK_FOR_INTERRUPTS();
     301                 :        1811 :             pg_usleep(delay_us);
     302         [ +  + ]:        1811 :             if (delay_us < INJ_WAIT_MAX_US)
     303                 :        1567 :                 delay_us = Min(delay_us * 2, INJ_WAIT_MAX_US);
     304                 :             :         }
     305                 :             :     }
     306         [ -  + ]:         114 :     PG_END_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index));
     307                 :         111 :     pgstat_report_wait_end();
     308                 :             : 
     309                 :             :     /* Remove this injection point from the waiters. */
     310                 :         111 :     injection_wait_cleanup(0, Int32GetDatum(index));
     311                 :             : }
     312                 :             : 
     313                 :             : /*
     314                 :             :  * SQL function for creating an injection point.
     315                 :             :  */
     316                 :         221 : PG_FUNCTION_INFO_V1(injection_points_attach);
     317                 :             : Datum
     318                 :         185 : injection_points_attach(PG_FUNCTION_ARGS)
     319                 :             : {
     320                 :             :     char       *name;
     321                 :             :     char       *action;
     322                 :             :     char       *str;
     323                 :             :     char       *function;
     324                 :         185 :     InjectionPointCondition condition = {0};
     325                 :             : 
     326         [ +  + ]:         185 :     if (PG_ARGISNULL(0))
     327         [ +  - ]:           1 :         elog(ERROR, "injection point name must not be null");
     328                 :             : 
     329         [ +  + ]:         184 :     if (PG_ARGISNULL(1))
     330         [ +  - ]:           1 :         elog(ERROR, "injection point action must not be null");
     331                 :             : 
     332                 :         183 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
     333                 :         183 :     action = text_to_cstring(PG_GETARG_TEXT_PP(1));
     334                 :         366 :     str = PG_ARGISNULL(2) ? NULL
     335         [ +  + ]:         183 :         : text_to_cstring(PG_GETARG_TEXT_PP(2));
     336                 :             : 
     337         [ +  + ]:         183 :     if (strcmp(action, "error") == 0)
     338                 :          26 :         function = "injection_error";
     339         [ +  + ]:         157 :     else if (strcmp(action, "notice") == 0)
     340                 :          33 :         function = "injection_notice";
     341         [ +  + ]:         124 :     else if (strcmp(action, "wait") == 0)
     342                 :         123 :         function = "injection_wait";
     343                 :             :     else
     344         [ +  - ]:           1 :         elog(ERROR, "incorrect action \"%s\" for injection point creation", action);
     345                 :             : 
     346         [ +  + ]:         182 :     if (injection_point_local)
     347                 :             :     {
     348                 :         127 :         condition.type |= INJ_CONDITION_PID;
     349                 :         127 :         condition.pid = MyProcPid;
     350                 :             :     }
     351                 :             : 
     352         [ +  + ]:         182 :     if (str)
     353                 :             :     {
     354         [ +  + ]:           6 :         if (str[0] == '\0')
     355         [ +  - ]:           1 :             elog(ERROR, "injection point condition string must not be empty");
     356         [ +  + ]:           5 :         if (strlen(str) >= INJ_DATA_MAXLEN)
     357         [ +  - ]:           1 :             elog(ERROR, "injection point condition string too long (maximum of %d characters)",
     358                 :             :                  INJ_DATA_MAXLEN - 1);
     359                 :           4 :         condition.type |= INJ_CONDITION_STRING;
     360                 :           4 :         strlcpy(condition.str, str, INJ_DATA_MAXLEN);
     361                 :             :     }
     362                 :             : 
     363                 :         180 :     InjectionPointAttach(name, "injection_points", function, &condition,
     364                 :             :                          sizeof(InjectionPointCondition));
     365                 :             : 
     366         [ +  + ]:         180 :     if (injection_point_local)
     367                 :             :     {
     368                 :             :         MemoryContext oldctx;
     369                 :             : 
     370                 :             :         /* Local injection point, so track it for automated cleanup */
     371                 :         127 :         oldctx = MemoryContextSwitchTo(TopMemoryContext);
     372                 :         127 :         inj_list_local = lappend(inj_list_local, makeString(pstrdup(name)));
     373                 :         127 :         MemoryContextSwitchTo(oldctx);
     374                 :             :     }
     375                 :             : 
     376                 :         180 :     PG_RETURN_VOID();
     377                 :             : }
     378                 :             : 
     379                 :             : /*
     380                 :             :  * SQL function for creating an injection point with library name, function
     381                 :             :  * name and private data.
     382                 :             :  */
     383                 :          80 : PG_FUNCTION_INFO_V1(injection_points_attach_func);
     384                 :             : Datum
     385                 :           8 : injection_points_attach_func(PG_FUNCTION_ARGS)
     386                 :             : {
     387                 :             :     char       *name;
     388                 :             :     char       *lib_name;
     389                 :             :     char       *function;
     390                 :           8 :     bytea      *private_data = NULL;
     391                 :           8 :     int         private_data_size = 0;
     392                 :             : 
     393         [ +  + ]:           8 :     if (PG_ARGISNULL(0))
     394         [ +  - ]:           1 :         elog(ERROR, "injection point name cannot be NULL");
     395         [ +  + ]:           7 :     if (PG_ARGISNULL(1))
     396         [ +  - ]:           1 :         elog(ERROR, "injection point library cannot be NULL");
     397         [ +  + ]:           6 :     if (PG_ARGISNULL(2))
     398         [ +  - ]:           1 :         elog(ERROR, "injection point function cannot be NULL");
     399                 :             : 
     400                 :           5 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
     401                 :           5 :     lib_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
     402                 :           5 :     function = text_to_cstring(PG_GETARG_TEXT_PP(2));
     403                 :             : 
     404         [ +  + ]:           5 :     if (!PG_ARGISNULL(3))
     405                 :             :     {
     406                 :           1 :         private_data = PG_GETARG_BYTEA_PP(3);
     407                 :           1 :         private_data_size = VARSIZE_ANY_EXHDR(private_data);
     408                 :             :     }
     409                 :             : 
     410         [ +  + ]:           5 :     if (private_data != NULL)
     411                 :           1 :         InjectionPointAttach(name, lib_name, function, VARDATA_ANY(private_data),
     412                 :             :                              private_data_size);
     413                 :             :     else
     414                 :           4 :         InjectionPointAttach(name, lib_name, function, NULL,
     415                 :             :                              0);
     416                 :           1 :     PG_RETURN_VOID();
     417                 :             : }
     418                 :             : 
     419                 :             : /*
     420                 :             :  * SQL function for loading an injection point.
     421                 :             :  */
     422                 :          80 : PG_FUNCTION_INFO_V1(injection_points_load);
     423                 :             : Datum
     424                 :           2 : injection_points_load(PG_FUNCTION_ARGS)
     425                 :             : {
     426                 :           2 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
     427                 :             : 
     428         [ +  + ]:           2 :     if (inj_state == NULL)
     429                 :           1 :         injection_init_shmem();
     430                 :             : 
     431                 :           2 :     INJECTION_POINT_LOAD(name);
     432                 :             : 
     433                 :           2 :     PG_RETURN_VOID();
     434                 :             : }
     435                 :             : 
     436                 :             : /*
     437                 :             :  * SQL function for triggering an injection point.
     438                 :             :  */
     439                 :          89 : PG_FUNCTION_INFO_V1(injection_points_run);
     440                 :             : Datum
     441                 :          35 : injection_points_run(PG_FUNCTION_ARGS)
     442                 :             : {
     443                 :             :     char       *name;
     444                 :          35 :     char       *arg = NULL;
     445                 :             : 
     446         [ +  + ]:          35 :     if (PG_ARGISNULL(0))
     447                 :           1 :         PG_RETURN_VOID();
     448                 :          34 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
     449                 :             : 
     450         [ +  + ]:          34 :     if (!PG_ARGISNULL(1))
     451                 :           4 :         arg = text_to_cstring(PG_GETARG_TEXT_PP(1));
     452                 :             : 
     453                 :          34 :     INJECTION_POINT(name, arg);
     454                 :             : 
     455                 :          26 :     PG_RETURN_VOID();
     456                 :             : }
     457                 :             : 
     458                 :             : /*
     459                 :             :  * SQL function for triggering an injection point from cache.
     460                 :             :  */
     461                 :          81 : PG_FUNCTION_INFO_V1(injection_points_cached);
     462                 :             : Datum
     463                 :           5 : injection_points_cached(PG_FUNCTION_ARGS)
     464                 :             : {
     465                 :             :     char       *name;
     466                 :           5 :     char       *arg = NULL;
     467                 :             : 
     468         [ +  + ]:           5 :     if (PG_ARGISNULL(0))
     469                 :           1 :         PG_RETURN_VOID();
     470                 :           4 :     name = text_to_cstring(PG_GETARG_TEXT_PP(0));
     471                 :             : 
     472         [ +  + ]:           4 :     if (!PG_ARGISNULL(1))
     473                 :           1 :         arg = text_to_cstring(PG_GETARG_TEXT_PP(1));
     474                 :             : 
     475                 :           4 :     INJECTION_POINT_CACHED(name, arg);
     476                 :             : 
     477                 :           4 :     PG_RETURN_VOID();
     478                 :             : }
     479                 :             : 
     480                 :             : /*
     481                 :             :  * SQL function for waking up an injection point waiting in injection_wait().
     482                 :             :  */
     483                 :         191 : PG_FUNCTION_INFO_V1(injection_points_wakeup);
     484                 :             : Datum
     485                 :         118 : injection_points_wakeup(PG_FUNCTION_ARGS)
     486                 :             : {
     487                 :         118 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
     488                 :         118 :     int         index = -1;
     489                 :             : 
     490         [ +  + ]:         118 :     if (inj_state == NULL)
     491                 :          87 :         injection_init_shmem();
     492                 :             : 
     493                 :             :     /* Find the injection point then bump its wait counter */
     494                 :         118 :     SpinLockAcquire(&inj_state->lock);
     495         [ +  + ]:         160 :     for (int i = 0; i < INJ_MAX_WAIT; i++)
     496                 :             :     {
     497         [ +  + ]:         159 :         if (strcmp(name, inj_state->name[i]) == 0)
     498                 :             :         {
     499                 :         117 :             index = i;
     500                 :         117 :             break;
     501                 :             :         }
     502                 :             :     }
     503         [ +  + ]:         118 :     if (index < 0)
     504                 :             :     {
     505                 :           1 :         SpinLockRelease(&inj_state->lock);
     506         [ +  - ]:           1 :         elog(ERROR, "could not find injection point %s to wake up", name);
     507                 :             :     }
     508                 :         117 :     SpinLockRelease(&inj_state->lock);
     509                 :             : 
     510                 :         117 :     pg_atomic_fetch_add_u32(&inj_state->wait_counts[index], 1);
     511                 :         117 :     PG_RETURN_VOID();
     512                 :             : }
     513                 :             : 
     514                 :             : /*
     515                 :             :  * injection_points_set_local
     516                 :             :  *
     517                 :             :  * Track if any injection point created in this process ought to run only
     518                 :             :  * in this process.  Such injection points are detached automatically when
     519                 :             :  * this process exits.  This is useful to make test suites concurrent-safe.
     520                 :             :  */
     521                 :         173 : PG_FUNCTION_INFO_V1(injection_points_set_local);
     522                 :             : Datum
     523                 :          94 : injection_points_set_local(PG_FUNCTION_ARGS)
     524                 :             : {
     525                 :             :     /* Enable flag to add a runtime condition based on this process ID */
     526                 :          94 :     injection_point_local = true;
     527                 :             : 
     528         [ +  + ]:          94 :     if (inj_state == NULL)
     529                 :          69 :         injection_init_shmem();
     530                 :             : 
     531                 :             :     /*
     532                 :             :      * Register a before_shmem_exit callback to remove any injection points
     533                 :             :      * linked to this process.
     534                 :             :      */
     535                 :          94 :     before_shmem_exit(injection_points_cleanup, (Datum) 0);
     536                 :             : 
     537                 :          94 :     PG_RETURN_VOID();
     538                 :             : }
     539                 :             : 
     540                 :             : /*
     541                 :             :  * SQL function for dropping an injection point.
     542                 :             :  */
     543                 :         202 : PG_FUNCTION_INFO_V1(injection_points_detach);
     544                 :             : Datum
     545                 :         157 : injection_points_detach(PG_FUNCTION_ARGS)
     546                 :             : {
     547                 :         157 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
     548                 :             : 
     549         [ +  + ]:         157 :     if (!InjectionPointDetach(name))
     550         [ +  - ]:           1 :         elog(ERROR, "could not detach injection point \"%s\"", name);
     551                 :             : 
     552                 :             :     /* Remove point from local list, if required */
     553         [ +  + ]:         156 :     if (inj_list_local != NIL)
     554                 :             :     {
     555                 :             :         MemoryContext oldctx;
     556                 :             : 
     557                 :          39 :         oldctx = MemoryContextSwitchTo(TopMemoryContext);
     558                 :          39 :         inj_list_local = list_delete(inj_list_local, makeString(name));
     559                 :          39 :         MemoryContextSwitchTo(oldctx);
     560                 :             :     }
     561                 :             : 
     562                 :         156 :     PG_RETURN_VOID();
     563                 :             : }
     564                 :             : 
     565                 :             : /*
     566                 :             :  * SQL function for listing all the injection points attached.
     567                 :             :  */
     568                 :          81 : PG_FUNCTION_INFO_V1(injection_points_list);
     569                 :             : Datum
     570                 :           3 : injection_points_list(PG_FUNCTION_ARGS)
     571                 :             : {
     572                 :             : #define NUM_INJECTION_POINTS_LIST 3
     573                 :           3 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     574                 :             :     List       *inj_points;
     575                 :             :     ListCell   *lc;
     576                 :             : 
     577                 :             :     /* Build a tuplestore to return our results in */
     578                 :           3 :     InitMaterializedSRF(fcinfo, 0);
     579                 :             : 
     580                 :           3 :     inj_points = InjectionPointList();
     581                 :             : 
     582   [ +  +  +  +  :           7 :     foreach(lc, inj_points)
                   +  + ]
     583                 :             :     {
     584                 :             :         Datum       values[NUM_INJECTION_POINTS_LIST];
     585                 :             :         bool        nulls[NUM_INJECTION_POINTS_LIST];
     586                 :           4 :         InjectionPointData *inj_point = lfirst(lc);
     587                 :             : 
     588                 :           4 :         memset(values, 0, sizeof(values));
     589                 :           4 :         memset(nulls, 0, sizeof(nulls));
     590                 :             : 
     591                 :           4 :         values[0] = PointerGetDatum(cstring_to_text(inj_point->name));
     592                 :           4 :         values[1] = PointerGetDatum(cstring_to_text(inj_point->library));
     593                 :           4 :         values[2] = PointerGetDatum(cstring_to_text(inj_point->function));
     594                 :             : 
     595                 :             :         /* shove row into tuplestore */
     596                 :           4 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
     597                 :             :     }
     598                 :             : 
     599                 :           3 :     return (Datum) 0;
     600                 :             : #undef NUM_INJECTION_POINTS_LIST
     601                 :             : }
     602                 :             : 
     603                 :             : void
     604                 :         304 : _PG_init(void)
     605                 :             : {
     606         [ +  + ]:         304 :     if (!process_shared_preload_libraries_in_progress)
     607                 :         301 :         return;
     608                 :             : 
     609                 :           3 :     RegisterShmemCallbacks(&injection_shmem_callbacks);
     610                 :             : }
        

Generated by: LCOV version 2.0-1