PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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, void *arg)
 
PGDLLEXPORT void injection_notice (const char *name, const void *private_data, void *arg)
 
PGDLLEXPORT void injection_wait (const char *name, const void *private_data, void *arg)
 
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 550 of file injection_points.c.

551{
553 return;
554
555 DefineCustomBoolVariable("injection_points.stats",
556 "Enables statistics for injection points.",
557 NULL,
559 false,
561 0,
562 NULL,
563 NULL,
564 NULL);
565
566 MarkGUCPrefixReserved("injection_points");
567
568 /* Shared memory initialization */
573
576}
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:5133
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5280
@ 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:59
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,
void *  arg 
)

Definition at line 245 of file injection_points.c.

246{
247 InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
248 char *argstr = (char *) arg;
249
250 if (!injection_point_allowed(condition))
251 return;
252
254
255 if (argstr)
256 elog(ERROR, "error triggered for injection point %s (%s)",
257 name, argstr);
258 else
259 elog(ERROR, "error triggered for injection point %s", name);
260}
#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)
void * arg
const char * name

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

◆ injection_init_shmem()

static void injection_init_shmem ( void  )
static

Definition at line 181 of file injection_points.c.

182{
183 bool found;
184
185 if (inj_state != NULL)
186 return;
187
188 inj_state = GetNamedDSMSegment("injection_points",
191 &found);
192}
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,
void *  arg 
)

Definition at line 263 of file injection_points.c.

264{
265 InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
266 char *argstr = (char *) arg;
267
268 if (!injection_point_allowed(condition))
269 return;
270
272
273 if (argstr)
274 elog(NOTICE, "notice triggered for injection point %s (%s)",
275 name, argstr);
276 else
277 elog(NOTICE, "notice triggered for injection point %s", name);
278}
#define NOTICE
Definition: elog.h:35

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

◆ injection_point_allowed()

static bool injection_point_allowed ( InjectionPointCondition condition)
static

Definition at line 201 of file injection_points.c.

202{
203 bool result = true;
204
205 switch (condition->type)
206 {
208 if (MyProcPid != condition->pid)
209 result = false;
210 break;
212 break;
213 }
214
215 return result;
216}
int MyProcPid
Definition: globals.c:48
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 127 of file injection_points.c.

128{
130
131 SpinLockInit(&state->lock);
132 memset(state->wait_counts, 0, sizeof(state->wait_counts));
133 memset(state->name, 0, sizeof(state->name));
134 ConditionVariableInit(&state->wait_point);
135}
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 351 of file injection_points.c.

352{
355 char *function;
356 InjectionPointCondition condition = {0};
357
358 if (strcmp(action, "error") == 0)
359 function = "injection_error";
360 else if (strcmp(action, "notice") == 0)
361 function = "injection_notice";
362 else if (strcmp(action, "wait") == 0)
363 function = "injection_wait";
364 else
365 elog(ERROR, "incorrect action \"%s\" for injection point creation", action);
366
368 {
369 condition.type = INJ_CONDITION_PID;
370 condition.pid = MyProcPid;
371 }
372
373 pgstat_report_inj_fixed(1, 0, 0, 0, 0);
374 InjectionPointAttach(name, "injection_points", function, &condition,
376
378 {
379 MemoryContext oldctx;
380
381 /* Local injection point, so track it for automated cleanup */
384 MemoryContextSwitchTo(oldctx);
385 }
386
387 /* Add entry for stats */
389
391}
#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:2327
MemoryContext TopMemoryContext
Definition: mcxt.c:165
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 439 of file injection_points.c.

440{
441 char *name;
442 char *arg = NULL;
443
444 if (PG_ARGISNULL(0))
447
448 if (!PG_ARGISNULL(1))
450
451 pgstat_report_inj_fixed(0, 0, 0, 1, 0);
453
455}
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define INJECTION_POINT_CACHED(name, arg)

References arg, INJECTION_POINT_CACHED, name, PG_ARGISNULL, 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 223 of file injection_points.c.

224{
225 ListCell *lc;
226
227 /* Leave if nothing is tracked locally */
229 return;
230
231 /* Detach all the local points */
232 foreach(lc, inj_list_local)
233 {
234 char *name = strVal(lfirst(lc));
235
237
238 /* Remove stats entry */
240 }
241}
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 524 of file injection_points.c.

525{
527
528 pgstat_report_inj_fixed(0, 1, 0, 0, 0);
530 elog(ERROR, "could not detach injection point \"%s\"", name);
531
532 /* Remove point from local list, if required */
533 if (inj_list_local != NIL)
534 {
535 MemoryContext oldctx;
536
539 MemoryContextSwitchTo(oldctx);
540 }
541
542 /* Remove stats entry */
544
546}
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 398 of file injection_points.c.

399{
401
402 if (inj_state == NULL)
404
405 pgstat_report_inj_fixed(0, 0, 0, 0, 1);
407
409}
#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 416 of file injection_points.c.

417{
418 char *name;
419 char *arg = NULL;
420
421 if (PG_ARGISNULL(0))
424
425 if (!PG_ARGISNULL(1))
427
428 pgstat_report_inj_fixed(0, 0, 1, 0, 0);
430
432}
#define INJECTION_POINT(name, arg)

References arg, INJECTION_POINT, name, PG_ARGISNULL, 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 502 of file injection_points.c.

503{
504 /* Enable flag to add a runtime condition based on this process ID */
506
507 if (inj_state == NULL)
509
510 /*
511 * Register a before_shmem_exit callback to remove any injection points
512 * linked to this process.
513 */
515
517}
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 462 of file injection_points.c.

463{
465 int index = -1;
466
467 if (inj_state == NULL)
469
470 /* First bump the wait counter for the injection point to wake up */
472 for (int i = 0; i < INJ_MAX_WAIT; i++)
473 {
474 if (strcmp(name, inj_state->name[i]) == 0)
475 {
476 index = i;
477 break;
478 }
479 }
480 if (index < 0)
481 {
483 elog(ERROR, "could not find injection point %s to wake up", name);
484 }
487
488 /* And broadcast the change to the waiters */
491}
void ConditionVariableBroadcast(ConditionVariable *cv)
#define INJ_MAX_WAIT
int i
Definition: isn.c:77
#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 139 of file injection_points.c.

140{
141 Size size;
142
145
146 size = MAXALIGN(sizeof(InjectionPointSharedState));
148}
#define MAXALIGN(LEN)
Definition: c.h:782
size_t Size
Definition: c.h:576
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:75

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 151 of file injection_points.c.

152{
153 bool found;
154
157
158 /* Create or attach to the shared memory state */
159 LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
160
161 inj_state = ShmemInitStruct("injection_points",
163 &found);
164
165 if (!found)
166 {
167 /*
168 * First time through, so initialize. This is shared with the dynamic
169 * initialization using a DSM.
170 */
172 }
173
174 LWLockRelease(AddinShmemInitLock);
175}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387

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,
void *  arg 
)

Definition at line 282 of file injection_points.c.

283{
284 uint32 old_wait_counts = 0;
285 int index = -1;
286 uint32 injection_wait_event = 0;
287 InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
288
289 if (inj_state == NULL)
291
292 if (!injection_point_allowed(condition))
293 return;
294
296
297 /*
298 * Use the injection point name for this custom wait event. Note that
299 * this custom wait event name is not released, but we don't care much for
300 * testing as this should be short-lived.
301 */
302 injection_wait_event = WaitEventInjectionPointNew(name);
303
304 /*
305 * Find a free slot to wait for, and register this injection point's name.
306 */
308 for (int i = 0; i < INJ_MAX_WAIT; i++)
309 {
310 if (inj_state->name[i][0] == '\0')
311 {
312 index = i;
314 old_wait_counts = inj_state->wait_counts[i];
315 break;
316 }
317 }
319
320 if (index < 0)
321 elog(ERROR, "could not find free slot for wait of injection point %s ",
322 name);
323
324 /* And sleep.. */
326 for (;;)
327 {
328 uint32 new_wait_counts;
329
331 new_wait_counts = inj_state->wait_counts[index];
333
334 if (old_wait_counts != new_wait_counts)
335 break;
336 ConditionVariableSleep(&inj_state->wait_point, injection_wait_event);
337 }
339
340 /* Remove this injection point from the waiters. */
342 inj_state->name[index][0] = '\0';
344}
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 119 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 120 of file injection_points.c.

Referenced by _PG_init(), and injection_shmem_startup().