PostgreSQL Source Code  git master
delay_execution.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * delay_execution.c
4  * Test module to allow delay between parsing and execution of a query.
5  *
6  * The delay is implemented by taking and immediately releasing a specified
7  * advisory lock. If another process has previously taken that lock, the
8  * current process will be blocked until the lock is released; otherwise,
9  * there's no effect. This allows an isolationtester script to reliably
10  * test behaviors where some specified action happens in another backend
11  * between parsing and execution of any desired query.
12  *
13  * Copyright (c) 2020-2022, PostgreSQL Global Development Group
14  *
15  * IDENTIFICATION
16  * src/test/modules/delay_execution/delay_execution.c
17  *
18  *-------------------------------------------------------------------------
19  */
20 
21 #include "postgres.h"
22 
23 #include <limits.h>
24 
25 #include "optimizer/planner.h"
26 #include "utils/builtins.h"
27 #include "utils/guc.h"
28 #include "utils/inval.h"
29 
30 
32 
33 /* GUC: advisory lock ID to use. Zero disables the feature. */
34 static int post_planning_lock_id = 0;
35 
36 /* Save previous planner hook user to be a good citizen */
38 
39 /* Module load function */
40 void _PG_init(void);
41 
42 
43 /* planner_hook function to provide the desired delay */
44 static PlannedStmt *
45 delay_execution_planner(Query *parse, const char *query_string,
46  int cursorOptions, ParamListInfo boundParams)
47 {
48  PlannedStmt *result;
49 
50  /* Invoke the planner, possibly via a previous hook user */
52  result = prev_planner_hook(parse, query_string, cursorOptions,
53  boundParams);
54  else
55  result = standard_planner(parse, query_string, cursorOptions,
56  boundParams);
57 
58  /* If enabled, delay by taking and releasing the specified lock */
59  if (post_planning_lock_id != 0)
60  {
65 
66  /*
67  * Ensure that we notice any pending invalidations, since the advisory
68  * lock functions don't do this.
69  */
71  }
72 
73  return result;
74 }
75 
76 /* Module load function */
77 void
78 _PG_init(void)
79 {
80  /* Set up the GUC to control which lock is used */
81  DefineCustomIntVariable("delay_execution.post_planning_lock_id",
82  "Sets the advisory lock ID to be locked/unlocked after planning.",
83  "Zero disables the delay.",
85  0,
86  0, INT_MAX,
88  0,
89  NULL,
90  NULL,
91  NULL);
92 
93  MarkGUCPrefixReserved("delay_execution");
94 
95  /* Install our hook */
98 }
static PlannedStmt * delay_execution_planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
static planner_hook_type prev_planner_hook
void _PG_init(void)
PG_MODULE_MAGIC
static int post_planning_lock_id
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:9623
void DefineCustomIntVariable(const char *name, const char *short_desc, const char *long_desc, int *valueAddr, int bootValue, int minValue, int maxValue, GucContext context, int flags, GucIntCheckHook check_hook, GucIntAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:9502
@ PGC_USERSET
Definition: guc.h:76
void AcceptInvalidationMessages(void)
Definition: inval.c:746
Datum pg_advisory_unlock_int8(PG_FUNCTION_ARGS)
Definition: lockfuncs.c:836
Datum pg_advisory_lock_int8(PG_FUNCTION_ARGS)
Definition: lockfuncs.c:690
planner_hook_type planner_hook
Definition: planner.c:74
PlannedStmt * standard_planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: planner.c:282
PlannedStmt *(* planner_hook_type)(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: planner.h:26
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673