-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplan_filter.c
152 lines (127 loc) · 3.53 KB
/
plan_filter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*-------------------------------------------------------------------------
*
* plan_filter.c
*
* Loadable PostgreSQL module to filter statements according to configured
* criteria and stop them before they start to run.
*
* The currently implemented criterion is the plan's estimated maximum cost.
*
* Copyright 2012-2015 PostgreSQL Experts, Inc.
*
* Distributed under The PostgreSQL License
* see License file for terms
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <float.h>
#include "optimizer/planner.h"
#include "utils/guc.h"
#define PG13_GTE (PG_VERSION_NUM >= 130000)
#if PG13_GTE
#define PLANNER_HOOK_PARAMS \
Query *parse, const char *queryString \
, int cursorOptions, ParamListInfo boundParams
#else
#define PLANNER_HOOK_PARAMS \
Query *parse \
, int cursorOptions, ParamListInfo boundParams
#endif
#if PG13_GTE
#define PLANNER_HOOK_ARGS \
parse, queryString \
, cursorOptions, boundParams
#else
#define PLANNER_HOOK_ARGS \
parse \
, cursorOptions, boundParams
#endif
PG_MODULE_MAGIC;
static double statement_cost_limit = 0.0;
static bool module_loaded = false;
static bool filter_select_only = false;
static planner_hook_type prev_planner_hook = NULL;
static PlannedStmt *limit_func(PLANNER_HOOK_PARAMS);
void _PG_init(void);
void _PG_fini(void);
/*
* Module load callback
*/
void
_PG_init(void)
{
/* Define custom GUC variable. */
DefineCustomRealVariable("plan_filter.statement_cost_limit",
"Sets the maximum allowed plan cost above which "
"a statement will not run.",
"Zero turns this feature off.",
&statement_cost_limit,
0.0,
0.0, DBL_MAX,
PGC_SUSET,
0, /* no flags required */
NULL,
NULL,
NULL);
/* Define custom GUC variable. */
DefineCustomBoolVariable("plan_filter.module_loaded",
"true if the module is loaded ",
NULL,
&module_loaded,
true,
PGC_BACKEND,
0, /* no flags required */
NULL,
NULL,
NULL);
/* Define custom GUC variable. */
DefineCustomBoolVariable("plan_filter.filter_select_only",
"Limit the filter to SELECT queries "
"only.",
"true turns this feature on (Default is false).",
&filter_select_only,
false,
PGC_SUSET,
0, /* no flags required */
NULL,
NULL,
NULL);
/* install the hook */
prev_planner_hook = planner_hook;
planner_hook = limit_func;
}
/*
* Module unload callback
*/
void
_PG_fini(void)
{
/* Uninstall hook. */
planner_hook = prev_planner_hook;
/* reset loaded var */
module_loaded = false;
}
static PlannedStmt *
limit_func(PLANNER_HOOK_PARAMS)
{
PlannedStmt *result;
/* this way we can daisy chain planner hooks if necessary */
if (prev_planner_hook != NULL)
result = (*prev_planner_hook) (PLANNER_HOOK_ARGS);
else
result = standard_planner(PLANNER_HOOK_ARGS);
if(filter_select_only && parse->commandType != CMD_SELECT)
return result;
if (statement_cost_limit > 0.0 &&
result->planTree->total_cost > statement_cost_limit)
ereport(ERROR,
(errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
errmsg("plan cost limit exceeded"),
errhint("The plan for your query shows that it would probably "
"have an excessive run time. This may be due to a "
"logic error in the SQL, or it maybe just a very "
"costly query. Rewrite your query or increase the "
"configuration parameter "
"\"plan_filter.statement_cost_limit\".")));
return result;
}