-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathdeclarative.c
382 lines (318 loc) · 11.1 KB
/
declarative.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "pathman.h"
#include "declarative.h"
#include "utils.h"
#include "partition_creation.h"
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/planner.h"
#include "parser/parse_coerce.h"
#include "parser/parse_func.h"
#include "utils/builtins.h"
#include "utils/int8.h"
#include "utils/int8.h"
#include "utils/lsyscache.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/varbit.h"
/*
* Modifies query of declarative partitioning commands,
* There is a little hack here, ATTACH PARTITION command
* expects relation with REL_PARTITIONED_TABLE relkind.
* To avoid this check we negate subtype, and then after the checks
* we set it back (look `is_pathman_related_partitioning_cmd`)
*/
void
modify_declarative_partitioning_query(Query *query)
{
if (query->commandType != CMD_UTILITY)
return;
if (IsA(query->utilityStmt, AlterTableStmt))
{
PartRelationInfo *prel;
ListCell *lcmd;
Oid relid;
AlterTableStmt *stmt = (AlterTableStmt *) query->utilityStmt;
relid = RangeVarGetRelid(stmt->relation, NoLock, true);
if ((prel = get_pathman_relation_info(relid)) != NULL)
{
close_pathman_relation_info(prel);
foreach(lcmd, stmt->cmds)
{
AlterTableCmd *cmd = (AlterTableCmd *) lfirst(lcmd);
switch (cmd->subtype)
{
case AT_AttachPartition:
case AT_DetachPartition:
cmd->subtype = -cmd->subtype;
break;
default:
break;
}
}
}
}
}
/* is it one of declarative partitioning commands? */
bool
is_pathman_related_partitioning_cmd(Node *parsetree, Oid *parent_relid)
{
PartRelationInfo *prel;
if (IsA(parsetree, AlterTableStmt))
{
ListCell *lc;
AlterTableStmt *stmt = (AlterTableStmt *) parsetree;
int cnt = 0;
*parent_relid = RangeVarGetRelid(stmt->relation, NoLock, stmt->missing_ok);
if (stmt->missing_ok && *parent_relid == InvalidOid)
return false;
if ((prel = get_pathman_relation_info(*parent_relid)) == NULL)
return false;
close_pathman_relation_info(prel);
/*
* Since cmds can contain multiple commmands but we can handle only
* two of them here, so we need to check that there are only commands
* we can handle. In case if cmds contain other commands we skip all
* commands in this statement.
*/
foreach(lc, stmt->cmds)
{
AlterTableCmd *cmd = (AlterTableCmd *) lfirst(lc);
switch (abs(cmd->subtype))
{
case AT_AttachPartition:
case AT_DetachPartition:
/*
* We need to fix all subtypes,
* possibly we're not going to handle this
*/
cmd->subtype = abs(cmd->subtype);
continue;
default:
cnt++;
}
}
return (cnt == 0);
}
else if (IsA(parsetree, CreateStmt))
{
/* inhRelations != NULL, partbound != NULL, tableElts == NULL */
CreateStmt *stmt = (CreateStmt *) parsetree;
if (stmt->inhRelations && stmt->partbound != NULL)
{
RangeVar *rv = castNode(RangeVar, linitial(stmt->inhRelations));
*parent_relid = RangeVarGetRelid(rv, NoLock, false);
if ((prel = get_pathman_relation_info(*parent_relid)) == NULL)
return false;
close_pathman_relation_info(prel);
if (stmt->tableElts != NIL)
elog(ERROR, "pg_pathman doesn't support column definitions "
"in declarative syntax yet");
return true;
}
}
return false;
}
static FuncExpr *
make_fn_expr(Oid funcOid, List *args)
{
FuncExpr *fn_expr;
HeapTuple procTup;
Form_pg_proc procStruct;
procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
if (!HeapTupleIsValid(procTup))
elog(ERROR, "cache lookup failed for function %u", funcOid);
procStruct = (Form_pg_proc) GETSTRUCT(procTup);
fn_expr = makeFuncExpr(funcOid, procStruct->prorettype, args,
InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
ReleaseSysCache(procTup);
return fn_expr;
}
/*
* Transform one constant in a partition bound spec
*/
static Const *
transform_bound_value(ParseState *pstate, A_Const *con,
Oid colType, int32 colTypmod)
{
Node *value;
/* Make it into a Const */
value = (Node *) make_const(pstate, &con->val, con->location);
/* Coerce to correct type */
value = coerce_to_target_type(pstate,
value, exprType(value),
colType,
colTypmod,
COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST,
-1);
if (value == NULL)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("specified value cannot be cast to type %s",
format_type_be(colType)),
parser_errposition(pstate, con->location)));
/* Simplify the expression, in case we had a coercion */
if (!IsA(value, Const))
value = (Node *) expression_planner((Expr *) value);
/* Fail if we don't have a constant (i.e., non-immutable coercion) */
if (!IsA(value, Const))
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("specified value cannot be cast to type %s",
format_type_be(colType)),
errdetail("The cast requires a non-immutable conversion."),
errhint("Try putting the literal value in single quotes."),
parser_errposition(pstate, con->location)));
return (Const *) value;
}
/* handle ALTER TABLE .. ATTACH PARTITION command */
void
handle_attach_partition(Oid parent_relid, AlterTableCmd *cmd)
{
Oid partition_relid,
proc_args[] = { REGCLASSOID, REGCLASSOID,
ANYELEMENTOID, ANYELEMENTOID };
List *proc_name;
FmgrInfo proc_flinfo;
FunctionCallInfoData proc_fcinfo;
char *pathman_schema;
PartitionRangeDatum *ldatum,
*rdatum;
Const *lval,
*rval;
A_Const *con;
List *fn_args;
ParseState *pstate = make_parsestate(NULL);
PartRelationInfo *prel;
PartitionCmd *pcmd = (PartitionCmd *) cmd->def;
/* in 10beta1, PartitionCmd->bound is (Node *) */
PartitionBoundSpec *bound = (PartitionBoundSpec *) pcmd->bound;
Assert(cmd->subtype == AT_AttachPartition);
if (bound->strategy != PARTITION_STRATEGY_RANGE)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_pathman only supports queries for range partitions")));
if ((prel = get_pathman_relation_info(parent_relid)) == NULL)
elog(ERROR, "relation is not partitioned");
partition_relid = RangeVarGetRelid(pcmd->name, NoLock, false);
/* Fetch pg_pathman's schema */
pathman_schema = get_namespace_name(get_pathman_schema());
if (pathman_schema == NULL)
elog(ERROR, "pg_pathman schema not initialized");
/* Build function's name */
proc_name = list_make2(makeString(pathman_schema),
makeString(CppAsString(attach_range_partition)));
if ((!list_length(bound->lowerdatums)) ||
(!list_length(bound->upperdatums)))
elog(ERROR, "provide start and end value for range partition");
ldatum = (PartitionRangeDatum *) linitial(bound->lowerdatums);
con = castNode(A_Const, ldatum->value);
lval = transform_bound_value(pstate, con, prel->ev_type, prel->ev_typmod);
rdatum = (PartitionRangeDatum *) linitial(bound->upperdatums);
con = castNode(A_Const, rdatum->value);
rval = transform_bound_value(pstate, con, prel->ev_type, prel->ev_typmod);
close_pathman_relation_info(prel);
/* Lookup function's Oid and get FmgrInfo */
fmgr_info(LookupFuncName(proc_name, 4, proc_args, false), &proc_flinfo);
InitFunctionCallInfoData(proc_fcinfo, &proc_flinfo,
4, InvalidOid, NULL, NULL);
proc_fcinfo.arg[0] = ObjectIdGetDatum(parent_relid);
proc_fcinfo.argnull[0] = false;
proc_fcinfo.arg[1] = ObjectIdGetDatum(partition_relid);
proc_fcinfo.argnull[1] = false;
/* Make function expression, we will need it to determine argument types */
fn_args = list_make4(NULL, NULL, lval, rval);
proc_fcinfo.flinfo->fn_expr =
(Node *) make_fn_expr(proc_fcinfo.flinfo->fn_oid, fn_args);
proc_fcinfo.arg[2] = lval->constvalue;
proc_fcinfo.argnull[2] = lval->constisnull;
proc_fcinfo.arg[3] = rval->constvalue;
proc_fcinfo.argnull[3] = rval->constisnull;
/* Invoke the callback */
FunctionCallInvoke(&proc_fcinfo);
}
/* handle ALTER TABLE .. DETACH PARTITION command */
void
handle_detach_partition(AlterTableCmd *cmd)
{
List *proc_name;
FmgrInfo proc_flinfo;
FunctionCallInfoData proc_fcinfo;
char *pathman_schema;
Oid partition_relid,
args = REGCLASSOID;
PartitionCmd *pcmd = (PartitionCmd *) cmd->def;
Assert(cmd->subtype == AT_DetachPartition);
partition_relid = RangeVarGetRelid(pcmd->name, NoLock, false);
/* Fetch pg_pathman's schema */
pathman_schema = get_namespace_name(get_pathman_schema());
if (pathman_schema == NULL)
elog(ERROR, "pg_pathman schema not initialized");
/* Build function's name */
proc_name = list_make2(makeString(pathman_schema),
makeString(CppAsString(detach_range_partition)));
/* Lookup function's Oid and get FmgrInfo */
fmgr_info(LookupFuncName(proc_name, 1, &args, false), &proc_flinfo);
InitFunctionCallInfoData(proc_fcinfo, &proc_flinfo,
4, InvalidOid, NULL, NULL);
proc_fcinfo.arg[0] = ObjectIdGetDatum(partition_relid);
proc_fcinfo.argnull[0] = false;
/* Invoke the callback */
FunctionCallInvoke(&proc_fcinfo);
}
/* handle CREATE TABLE .. PARTITION OF <parent> FOR VALUES FROM .. TO .. */
void
handle_create_partition_of(Oid parent_relid, CreateStmt *stmt)
{
Bound start,
end;
PartRelationInfo *prel;
ParseState *pstate = make_parsestate(NULL);
PartitionRangeDatum *ldatum,
*rdatum;
Const *lval,
*rval;
A_Const *con;
/* in 10beta1, PartitionCmd->bound is (Node *) */
PartitionBoundSpec *bound = (PartitionBoundSpec *) stmt->partbound;
/* we show errors earlier for these asserts */
Assert(stmt->inhRelations != NULL);
Assert(stmt->tableElts == NIL);
if (bound->strategy != PARTITION_STRATEGY_RANGE)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("pg_pathman only supports queries for range partitions")));
if ((prel = get_pathman_relation_info(parent_relid)) == NULL)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("table \"%s\" is not partitioned",
get_rel_name_or_relid(parent_relid))));
if (prel->parttype != PT_RANGE)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("table \"%s\" is not partitioned by RANGE",
get_rel_name_or_relid(parent_relid))));
ldatum = (PartitionRangeDatum *) linitial(bound->lowerdatums);
con = castNode(A_Const, ldatum->value);
lval = transform_bound_value(pstate, con, prel->ev_type, prel->ev_typmod);
rdatum = (PartitionRangeDatum *) linitial(bound->upperdatums);
con = castNode(A_Const, rdatum->value);
rval = transform_bound_value(pstate, con, prel->ev_type, prel->ev_typmod);
close_pathman_relation_info(prel);
start = lval->constisnull?
MakeBoundInf(MINUS_INFINITY) :
MakeBound(lval->constvalue);
end = rval->constisnull?
MakeBoundInf(PLUS_INFINITY) :
MakeBound(rval->constvalue);
/* more checks */
check_range_available(parent_relid, &start, &end, lval->consttype, true);
/* Create a new RANGE partition and return its Oid */
create_single_range_partition_internal(parent_relid,
&start,
&end,
lval->consttype,
stmt->relation,
stmt->tablespacename);
}