PostgreSQL Source Code git master
injection_points.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "injection_stats.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "nodes/value.h"
#include "storage/condition_variable.h"
#include "storage/dsm_registry.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/wait_event.h"
Include dependency graph for injection_points.c:

Go to the source code of this file.

Data Structures

struct  InjectionPointCondition
 
struct  InjectionPointSharedState
 

Macros

#define INJ_MAX_WAIT   8
 
#define INJ_NAME_MAXLEN   64
 

Typedefs

typedef enum InjectionPointConditionType InjectionPointConditionType
 
typedef struct InjectionPointCondition InjectionPointCondition
 
typedef struct InjectionPointSharedState InjectionPointSharedState
 

Enumerations

enum  InjectionPointConditionType { INJ_CONDITION_ALWAYS = 0 , INJ_CONDITION_PID }
 

Functions

PGDLLEXPORT void injection_error (const char *name, const void *private_data)
 
PGDLLEXPORT void injection_notice (const char *name, const void *private_data)
 
PGDLLEXPORT void injection_wait (const char *name, const void *private_data)
 
static void injection_point_init_state (void *ptr)
 
static void injection_shmem_request (void)
 
static void injection_shmem_startup (void)
 
static void injection_init_shmem (void)
 
static bool injection_point_allowed (InjectionPointCondition *condition)
 
static void injection_points_cleanup (int code, Datum arg)
 
 PG_FUNCTION_INFO_V1 (injection_points_attach)
 
Datum injection_points_attach (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_load)
 
Datum injection_points_load (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_run)
 
Datum injection_points_run (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_cached)
 
Datum injection_points_cached (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_wakeup)
 
Datum injection_points_wakeup (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_set_local)
 
Datum injection_points_set_local (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_detach)
 
Datum injection_points_detach (PG_FUNCTION_ARGS)
 
void _PG_init (void)
 

Variables

 PG_MODULE_MAGIC
 
static Listinj_list_local = NIL
 
static InjectionPointSharedStateinj_state = NULL
 
static bool injection_point_local = false
 
bool inj_stats_enabled = false
 
static shmem_request_hook_type prev_shmem_request_hook = NULL
 
static shmem_startup_hook_type prev_shmem_startup_hook = NULL
 

Macro Definition Documentation

◆ INJ_MAX_WAIT

#define INJ_MAX_WAIT   8

Definition at line 39 of file injection_points.c.

◆ INJ_NAME_MAXLEN

#define INJ_NAME_MAXLEN   64

Definition at line 40 of file injection_points.c.

Typedef Documentation

◆ InjectionPointCondition

◆ InjectionPointConditionType

◆ InjectionPointSharedState

Enumeration Type Documentation

◆ InjectionPointConditionType

Enumerator
INJ_CONDITION_ALWAYS 
INJ_CONDITION_PID 

Definition at line 51 of file injection_points.c.

52{
53 INJ_CONDITION_ALWAYS = 0, /* always run */
54 INJ_CONDITION_PID, /* PID restriction */
InjectionPointConditionType
@ INJ_CONDITION_PID
@ INJ_CONDITION_ALWAYS

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 521 of file injection_points.c.

522{
524 return;
525
526 DefineCustomBoolVariable("injection_points.stats",
527 "Enables statistics for injection points.",
528 NULL,
530 false,
532 0,
533 NULL,
534 NULL,
535 NULL);
536
537 MarkGUCPrefixReserved("injection_points");
538
539 /* Shared memory initialization */
544
547}
void DefineCustomBoolVariable(const char *name, const char *short_desc, const char *long_desc, bool *valueAddr, bool bootValue, GucContext context, int flags, GucBoolCheckHook check_hook, GucBoolAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:5132
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5279
@ PGC_POSTMASTER
Definition: guc.h:74
static void injection_shmem_request(void)
bool inj_stats_enabled
static shmem_startup_hook_type prev_shmem_startup_hook
static shmem_request_hook_type prev_shmem_request_hook
static void injection_shmem_startup(void)
void pgstat_register_inj(void)
void pgstat_register_inj_fixed(void)
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:58
shmem_request_hook_type shmem_request_hook
Definition: miscinit.c:1840
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1837

References DefineCustomBoolVariable(), inj_stats_enabled, injection_shmem_request(), injection_shmem_startup(), MarkGUCPrefixReserved(), PGC_POSTMASTER, pgstat_register_inj(), pgstat_register_inj_fixed(), prev_shmem_request_hook, prev_shmem_startup_hook, process_shared_preload_libraries_in_progress, shmem_request_hook, and shmem_startup_hook.

◆ injection_error()

void injection_error ( const char *  name,
const void *  private_data 
)

Definition at line 242 of file injection_points.c.

243{
244 InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
245
246 if (!injection_point_allowed(condition))
247 return;
248
250
251 elog(ERROR, "error triggered for injection point %s", name);
252}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
static bool injection_point_allowed(InjectionPointCondition *condition)
void pgstat_report_inj(const char *name)
const char * name

References elog, ERROR, injection_point_allowed(), name, and pgstat_report_inj().

◆ injection_init_shmem()

static void injection_init_shmem ( void  )
static

Definition at line 178 of file injection_points.c.

179{
180 bool found;
181
182 if (inj_state != NULL)
183 return;
184
185 inj_state = GetNamedDSMSegment("injection_points",
188 &found);
189}
void * GetNamedDSMSegment(const char *name, size_t size, void(*init_callback)(void *ptr), bool *found)
Definition: dsm_registry.c:131
static void injection_point_init_state(void *ptr)
static InjectionPointSharedState * inj_state

References GetNamedDSMSegment(), inj_state, and injection_point_init_state().

Referenced by injection_points_load(), injection_points_set_local(), injection_points_wakeup(), and injection_wait().

◆ injection_notice()

void injection_notice ( const char *  name,
const void *  private_data 
)

Definition at line 255 of file injection_points.c.

256{
257 InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
258
259 if (!injection_point_allowed(condition))
260 return;
261
263
264 elog(NOTICE, "notice triggered for injection point %s", name);
265}
#define NOTICE
Definition: elog.h:35

References elog, injection_point_allowed(), name, NOTICE, and pgstat_report_inj().

◆ injection_point_allowed()

static bool injection_point_allowed ( InjectionPointCondition condition)
static

Definition at line 198 of file injection_points.c.

199{
200 bool result = true;
201
202 switch (condition->type)
203 {
205 if (MyProcPid != condition->pid)
206 result = false;
207 break;
209 break;
210 }
211
212 return result;
213}
int MyProcPid
Definition: globals.c:46
InjectionPointConditionType type

References INJ_CONDITION_ALWAYS, INJ_CONDITION_PID, MyProcPid, InjectionPointCondition::pid, and InjectionPointCondition::type.

Referenced by injection_error(), injection_notice(), and injection_wait().

◆ injection_point_init_state()

static void injection_point_init_state ( void *  ptr)
static

Definition at line 124 of file injection_points.c.

125{
127
128 SpinLockInit(&state->lock);
129 memset(state->wait_counts, 0, sizeof(state->wait_counts));
130 memset(state->name, 0, sizeof(state->name));
131 ConditionVariableInit(&state->wait_point);
132}
void ConditionVariableInit(ConditionVariable *cv)
#define SpinLockInit(lock)
Definition: spin.h:57
Definition: regguts.h:323

References ConditionVariableInit(), and SpinLockInit.

Referenced by injection_init_shmem(), and injection_shmem_startup().

◆ injection_points_attach()

Datum injection_points_attach ( PG_FUNCTION_ARGS  )

Definition at line 338 of file injection_points.c.

339{
342 char *function;
343 InjectionPointCondition condition = {0};
344
345 if (strcmp(action, "error") == 0)
346 function = "injection_error";
347 else if (strcmp(action, "notice") == 0)
348 function = "injection_notice";
349 else if (strcmp(action, "wait") == 0)
350 function = "injection_wait";
351 else
352 elog(ERROR, "incorrect action \"%s\" for injection point creation", action);
353
355 {
356 condition.type = INJ_CONDITION_PID;
357 condition.pid = MyProcPid;
358 }
359
360 pgstat_report_inj_fixed(1, 0, 0, 0, 0);
361 InjectionPointAttach(name, "injection_points", function, &condition,
363
365 {
366 MemoryContext oldctx;
367
368 /* Local injection point, so track it for automated cleanup */
371 MemoryContextSwitchTo(oldctx);
372 }
373
374 /* Add entry for stats */
376
378}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void InjectionPointAttach(const char *name, const char *library, const char *function, const void *private_data, int private_data_size)
static bool injection_point_local
static List * inj_list_local
void pgstat_create_inj(const char *name)
void pgstat_report_inj_fixed(uint32 numattach, uint32 numdetach, uint32 numrun, uint32 numcached, uint32 numloaded)
List * lappend(List *list, void *datum)
Definition: list.c:339
char * pstrdup(const char *in)
Definition: mcxt.c:1699
MemoryContext TopMemoryContext
Definition: mcxt.c:149
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
on_exit_nicely_callback function
String * makeString(char *str)
Definition: value.c:63
char * text_to_cstring(const text *t)
Definition: varlena.c:225

References generate_unaccent_rules::action, elog, ERROR, function, INJ_CONDITION_PID, inj_list_local, injection_point_local, InjectionPointAttach(), lappend(), makeString(), MemoryContextSwitchTo(), MyProcPid, name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_create_inj(), pgstat_report_inj_fixed(), InjectionPointCondition::pid, pstrdup(), text_to_cstring(), TopMemoryContext, and InjectionPointCondition::type.

◆ injection_points_cached()

Datum injection_points_cached ( PG_FUNCTION_ARGS  )

Definition at line 418 of file injection_points.c.

419{
421
422 pgstat_report_inj_fixed(0, 0, 0, 1, 0);
424
426}
#define INJECTION_POINT_CACHED(name)

References INJECTION_POINT_CACHED, name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_report_inj_fixed(), and text_to_cstring().

◆ injection_points_cleanup()

static void injection_points_cleanup ( int  code,
Datum  arg 
)
static

Definition at line 220 of file injection_points.c.

221{
222 ListCell *lc;
223
224 /* Leave if nothing is tracked locally */
226 return;
227
228 /* Detach all the local points */
229 foreach(lc, inj_list_local)
230 {
231 char *name = strVal(lfirst(lc));
232
234
235 /* Remove stats entry */
237 }
238}
bool InjectionPointDetach(const char *name)
void pgstat_drop_inj(const char *name)
#define lfirst(lc)
Definition: pg_list.h:172
#define strVal(v)
Definition: value.h:82

References inj_list_local, injection_point_local, InjectionPointDetach(), lfirst, name, pgstat_drop_inj(), and strVal.

Referenced by injection_points_set_local().

◆ injection_points_detach()

Datum injection_points_detach ( PG_FUNCTION_ARGS  )

Definition at line 495 of file injection_points.c.

496{
498
499 pgstat_report_inj_fixed(0, 1, 0, 0, 0);
501 elog(ERROR, "could not detach injection point \"%s\"", name);
502
503 /* Remove point from local list, if required */
504 if (inj_list_local != NIL)
505 {
506 MemoryContext oldctx;
507
510 MemoryContextSwitchTo(oldctx);
511 }
512
513 /* Remove stats entry */
515
517}
List * list_delete(List *list, void *datum)
Definition: list.c:853
#define NIL
Definition: pg_list.h:68

References elog, ERROR, inj_list_local, InjectionPointDetach(), list_delete(), makeString(), MemoryContextSwitchTo(), name, NIL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_drop_inj(), pgstat_report_inj_fixed(), text_to_cstring(), and TopMemoryContext.

◆ injection_points_load()

Datum injection_points_load ( PG_FUNCTION_ARGS  )

Definition at line 385 of file injection_points.c.

386{
388
389 if (inj_state == NULL)
391
392 pgstat_report_inj_fixed(0, 0, 0, 0, 1);
394
396}
#define INJECTION_POINT_LOAD(name)
static void injection_init_shmem(void)

References inj_state, injection_init_shmem(), INJECTION_POINT_LOAD, name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_report_inj_fixed(), and text_to_cstring().

◆ injection_points_run()

Datum injection_points_run ( PG_FUNCTION_ARGS  )

Definition at line 403 of file injection_points.c.

404{
406
407 pgstat_report_inj_fixed(0, 0, 1, 0, 0);
409
411}
#define INJECTION_POINT(name)

References INJECTION_POINT, name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgstat_report_inj_fixed(), and text_to_cstring().

◆ injection_points_set_local()

Datum injection_points_set_local ( PG_FUNCTION_ARGS  )

Definition at line 473 of file injection_points.c.

474{
475 /* Enable flag to add a runtime condition based on this process ID */
477
478 if (inj_state == NULL)
480
481 /*
482 * Register a before_shmem_exit callback to remove any injection points
483 * linked to this process.
484 */
486
488}
static void injection_points_cleanup(int code, Datum arg)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
uintptr_t Datum
Definition: postgres.h:69

References before_shmem_exit(), inj_state, injection_init_shmem(), injection_point_local, injection_points_cleanup(), and PG_RETURN_VOID.

◆ injection_points_wakeup()

Datum injection_points_wakeup ( PG_FUNCTION_ARGS  )

Definition at line 433 of file injection_points.c.

434{
436 int index = -1;
437
438 if (inj_state == NULL)
440
441 /* First bump the wait counter for the injection point to wake up */
443 for (int i = 0; i < INJ_MAX_WAIT; i++)
444 {
445 if (strcmp(name, inj_state->name[i]) == 0)
446 {
447 index = i;
448 break;
449 }
450 }
451 if (index < 0)
452 {
454 elog(ERROR, "could not find injection point %s to wake up", name);
455 }
458
459 /* And broadcast the change to the waiters */
462}
void ConditionVariableBroadcast(ConditionVariable *cv)
#define INJ_MAX_WAIT
int i
Definition: isn.c:74
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
uint32 wait_counts[INJ_MAX_WAIT]
char name[INJ_MAX_WAIT][INJ_NAME_MAXLEN]
ConditionVariable wait_point
Definition: type.h:96

References ConditionVariableBroadcast(), elog, ERROR, i, INJ_MAX_WAIT, inj_state, injection_init_shmem(), InjectionPointSharedState::lock, name, InjectionPointSharedState::name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, SpinLockAcquire, SpinLockRelease, text_to_cstring(), InjectionPointSharedState::wait_counts, and InjectionPointSharedState::wait_point.

◆ injection_shmem_request()

static void injection_shmem_request ( void  )
static

Definition at line 136 of file injection_points.c.

137{
138 Size size;
139
142
143 size = MAXALIGN(sizeof(InjectionPointSharedState));
145}
#define MAXALIGN(LEN)
Definition: c.h:782
size_t Size
Definition: c.h:576
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:74

References MAXALIGN, prev_shmem_request_hook, and RequestAddinShmemSpace().

Referenced by _PG_init().

◆ injection_shmem_startup()

static void injection_shmem_startup ( void  )
static

Definition at line 148 of file injection_points.c.

149{
150 bool found;
151
154
155 /* Create or attach to the shared memory state */
156 LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
157
158 inj_state = ShmemInitStruct("injection_points",
160 &found);
161
162 if (!found)
163 {
164 /*
165 * First time through, so initialize. This is shared with the dynamic
166 * initialization using a DSM.
167 */
169 }
170
171 LWLockRelease(AddinShmemInitLock);
172}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1179
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1899
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382

References inj_state, injection_point_init_state(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), prev_shmem_startup_hook, and ShmemInitStruct().

Referenced by _PG_init().

◆ injection_wait()

void injection_wait ( const char *  name,
const void *  private_data 
)

Definition at line 269 of file injection_points.c.

270{
271 uint32 old_wait_counts = 0;
272 int index = -1;
273 uint32 injection_wait_event = 0;
274 InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
275
276 if (inj_state == NULL)
278
279 if (!injection_point_allowed(condition))
280 return;
281
283
284 /*
285 * Use the injection point name for this custom wait event. Note that
286 * this custom wait event name is not released, but we don't care much for
287 * testing as this should be short-lived.
288 */
289 injection_wait_event = WaitEventInjectionPointNew(name);
290
291 /*
292 * Find a free slot to wait for, and register this injection point's name.
293 */
295 for (int i = 0; i < INJ_MAX_WAIT; i++)
296 {
297 if (inj_state->name[i][0] == '\0')
298 {
299 index = i;
301 old_wait_counts = inj_state->wait_counts[i];
302 break;
303 }
304 }
306
307 if (index < 0)
308 elog(ERROR, "could not find free slot for wait of injection point %s ",
309 name);
310
311 /* And sleep.. */
313 for (;;)
314 {
315 uint32 new_wait_counts;
316
318 new_wait_counts = inj_state->wait_counts[index];
320
321 if (old_wait_counts != new_wait_counts)
322 break;
323 ConditionVariableSleep(&inj_state->wait_point, injection_wait_event);
324 }
326
327 /* Remove this injection point from the waiters. */
329 inj_state->name[index][0] = '\0';
331}
uint32_t uint32
Definition: c.h:502
bool ConditionVariableCancelSleep(void)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define INJ_NAME_MAXLEN
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
uint32 WaitEventInjectionPointNew(const char *wait_event_name)
Definition: wait_event.c:169

References ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), elog, ERROR, i, INJ_MAX_WAIT, INJ_NAME_MAXLEN, inj_state, injection_init_shmem(), injection_point_allowed(), InjectionPointSharedState::lock, name, InjectionPointSharedState::name, pgstat_report_inj(), SpinLockAcquire, SpinLockRelease, strlcpy(), InjectionPointSharedState::wait_counts, InjectionPointSharedState::wait_point, and WaitEventInjectionPointNew().

◆ PG_FUNCTION_INFO_V1() [1/7]

PG_FUNCTION_INFO_V1 ( injection_points_attach  )

◆ PG_FUNCTION_INFO_V1() [2/7]

PG_FUNCTION_INFO_V1 ( injection_points_cached  )

◆ PG_FUNCTION_INFO_V1() [3/7]

PG_FUNCTION_INFO_V1 ( injection_points_detach  )

◆ PG_FUNCTION_INFO_V1() [4/7]

PG_FUNCTION_INFO_V1 ( injection_points_load  )

◆ PG_FUNCTION_INFO_V1() [5/7]

PG_FUNCTION_INFO_V1 ( injection_points_run  )

◆ PG_FUNCTION_INFO_V1() [6/7]

PG_FUNCTION_INFO_V1 ( injection_points_set_local  )

◆ PG_FUNCTION_INFO_V1() [7/7]

PG_FUNCTION_INFO_V1 ( injection_points_wakeup  )

Variable Documentation

◆ inj_list_local

List* inj_list_local = NIL
static

◆ inj_state

◆ inj_stats_enabled

◆ injection_point_local

bool injection_point_local = false
static

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 36 of file injection_points.c.

◆ prev_shmem_request_hook

shmem_request_hook_type prev_shmem_request_hook = NULL
static

Definition at line 116 of file injection_points.c.

Referenced by _PG_init(), and injection_shmem_request().

◆ prev_shmem_startup_hook

shmem_startup_hook_type prev_shmem_startup_hook = NULL
static

Definition at line 117 of file injection_points.c.

Referenced by _PG_init(), and injection_shmem_startup().