-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsafeupdate.c
70 lines (62 loc) · 1.59 KB
/
safeupdate.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
#include "postgres.h"
#include "fmgr.h"
#include "nodes/nodeFuncs.h"
#include "parser/analyze.h"
#include "utils/guc.h"
PG_MODULE_MAGIC;
void _PG_init(void);
static bool safeupdate_enabled;
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
static void
delete_needs_where_check(ParseState *pstate, Query *query, JumbleState *jstate)
{
ListCell *l;
Query *ctequery;
if (!safeupdate_enabled)
return;
if (query->hasModifyingCTE)
{
foreach (l, query->cteList)
{
CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
ctequery = castNode(Query, cte->ctequery);
delete_needs_where_check(pstate, ctequery, jstate);
}
}
switch (query->commandType)
{
case CMD_DELETE:
Assert(query->jointree != NULL);
if (query->jointree->quals == NULL)
ereport(ERROR,
(errcode(ERRCODE_CARDINALITY_VIOLATION),
errmsg("DELETE requires a WHERE clause")));
break;
case CMD_UPDATE:
Assert(query->jointree != NULL);
if (query->jointree->quals == NULL)
ereport(ERROR,
(errcode(ERRCODE_CARDINALITY_VIOLATION),
errmsg("UPDATE requires a WHERE clause")));
default:
break;
}
if (prev_post_parse_analyze_hook != NULL)
(*prev_post_parse_analyze_hook)(pstate, query, jstate);
}
void
_PG_init(void)
{
DefineCustomBoolVariable("safeupdate.enabled",
"Enforce qualified updates",
"Prevent DML without a WHERE clause",
&safeupdate_enabled,
1,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = delete_needs_where_check;
}