Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * analyze.c
4 : : * transform the raw parse tree into a query tree
5 : : *
6 : : * For optimizable statements, we are careful to obtain a suitable lock on
7 : : * each referenced table, and other modules of the backend preserve or
8 : : * re-obtain these locks before depending on the results. It is therefore
9 : : * okay to do significant semantic analysis of these statements. For
10 : : * utility commands, no locks are obtained here (and if they were, we could
11 : : * not be sure we'd still have them at execution). Hence the general rule
12 : : * for utility commands is to just dump them into a Query node untransformed.
13 : : * DECLARE CURSOR, EXPLAIN, and CREATE TABLE AS are exceptions because they
14 : : * contain optimizable statements, which we should transform.
15 : : *
16 : : *
17 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
18 : : * Portions Copyright (c) 1994, Regents of the University of California
19 : : *
20 : : * src/backend/parser/analyze.c
21 : : *
22 : : *-------------------------------------------------------------------------
23 : : */
24 : :
25 : : #include "postgres.h"
26 : :
27 : : #include "access/stratnum.h"
28 : : #include "access/sysattr.h"
29 : : #include "catalog/dependency.h"
30 : : #include "catalog/pg_am.h"
31 : : #include "catalog/pg_operator.h"
32 : : #include "catalog/pg_proc.h"
33 : : #include "catalog/pg_type.h"
34 : : #include "commands/defrem.h"
35 : : #include "miscadmin.h"
36 : : #include "nodes/makefuncs.h"
37 : : #include "nodes/nodeFuncs.h"
38 : : #include "nodes/queryjumble.h"
39 : : #include "optimizer/optimizer.h"
40 : : #include "parser/analyze.h"
41 : : #include "parser/parse_agg.h"
42 : : #include "parser/parse_clause.h"
43 : : #include "parser/parse_coerce.h"
44 : : #include "parser/parse_collate.h"
45 : : #include "parser/parse_cte.h"
46 : : #include "parser/parse_expr.h"
47 : : #include "parser/parse_func.h"
48 : : #include "parser/parse_merge.h"
49 : : #include "parser/parse_oper.h"
50 : : #include "parser/parse_param.h"
51 : : #include "parser/parse_relation.h"
52 : : #include "parser/parse_target.h"
53 : : #include "parser/parse_type.h"
54 : : #include "parser/parsetree.h"
55 : : #include "utils/backend_status.h"
56 : : #include "utils/builtins.h"
57 : : #include "utils/fmgroids.h"
58 : : #include "utils/guc.h"
59 : : #include "utils/lsyscache.h"
60 : : #include "utils/rangetypes.h"
61 : : #include "utils/rel.h"
62 : : #include "utils/syscache.h"
63 : :
64 : :
65 : : /* Passthrough data for transformPLAssignStmtTarget */
66 : : typedef struct SelectStmtPassthrough
67 : : {
68 : : PLAssignStmt *stmt; /* the assignment statement */
69 : : Node *target; /* node representing the target variable */
70 : : List *indirection; /* indirection yet to be applied to target */
71 : : } SelectStmtPassthrough;
72 : :
73 : : /* Hook for plugins to get control at end of parse analysis */
74 : : post_parse_analyze_hook_type post_parse_analyze_hook = NULL;
75 : :
76 : : static Query *transformOptionalSelectInto(ParseState *pstate, Node *parseTree);
77 : : static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
78 : : static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
79 : : static OnConflictExpr *transformOnConflictClause(ParseState *pstate,
80 : : OnConflictClause *onConflictClause);
81 : : static ForPortionOfExpr *transformForPortionOfClause(ParseState *pstate,
82 : : int rtindex,
83 : : const ForPortionOfClause *forPortionOf,
84 : : const Node *whereClause,
85 : : bool isUpdate);
86 : : static int count_rowexpr_columns(ParseState *pstate, Node *expr);
87 : : static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt,
88 : : SelectStmtPassthrough *passthru);
89 : : static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
90 : : static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
91 : : static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
92 : : bool isTopLevel, List **targetlist);
93 : : static void determineRecursiveColTypes(ParseState *pstate,
94 : : Node *larg, List *nrtargetlist);
95 : : static Query *transformReturnStmt(ParseState *pstate, ReturnStmt *stmt);
96 : : static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
97 : : static Query *transformPLAssignStmt(ParseState *pstate,
98 : : PLAssignStmt *stmt);
99 : : static List *transformPLAssignStmtTarget(ParseState *pstate, List *tlist,
100 : : SelectStmtPassthrough *passthru);
101 : : static Query *transformDeclareCursorStmt(ParseState *pstate,
102 : : DeclareCursorStmt *stmt);
103 : : static Query *transformExplainStmt(ParseState *pstate,
104 : : ExplainStmt *stmt);
105 : : static Query *transformCreateTableAsStmt(ParseState *pstate,
106 : : CreateTableAsStmt *stmt);
107 : : static Query *transformCallStmt(ParseState *pstate,
108 : : CallStmt *stmt);
109 : : static void transformLockingClause(ParseState *pstate, Query *qry,
110 : : LockingClause *lc, bool pushedDown);
111 : : #ifdef DEBUG_NODE_TESTS_ENABLED
112 : : static bool test_raw_expression_coverage(Node *node, void *context);
113 : : #endif
114 : :
115 : :
116 : : /*
117 : : * parse_analyze_fixedparams
118 : : * Analyze a raw parse tree and transform it to Query form.
119 : : *
120 : : * Optionally, information about $n parameter types can be supplied.
121 : : * References to $n indexes not defined by paramTypes[] are disallowed.
122 : : *
123 : : * The result is a Query node. Optimizable statements require considerable
124 : : * transformation, while utility-type statements are simply hung off
125 : : * a dummy CMD_UTILITY Query node.
126 : : */
127 : : Query *
1579 peter@eisentraut.org 128 :CBC 486501 : parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
129 : : const Oid *paramTypes, int numParams,
130 : : QueryEnvironment *queryEnv)
131 : : {
8463 tgl@sss.pgh.pa.us 132 : 486501 : ParseState *pstate = make_parsestate(NULL);
133 : : Query *query;
1910 bruce@momjian.us 134 : 486501 : JumbleState *jstate = NULL;
135 : :
6228 136 [ - + ]: 486501 : Assert(sourceText != NULL); /* required as of 8.4 */
137 : :
7413 tgl@sss.pgh.pa.us 138 : 486501 : pstate->p_sourcetext = sourceText;
139 : :
6086 140 [ + + ]: 486501 : if (numParams > 0)
1579 peter@eisentraut.org 141 : 1630 : setup_parse_fixed_parameters(pstate, paramTypes, numParams);
142 : :
3378 kgrittn@postgresql.o 143 : 486501 : pstate->p_queryEnv = queryEnv;
144 : :
5216 tgl@sss.pgh.pa.us 145 : 486501 : query = transformTopLevelStmt(pstate, parseTree);
146 : :
1872 alvherre@alvh.no-ip. 147 [ + + ]: 480698 : if (IsQueryIdEnabled())
1098 michael@paquier.xyz 148 : 75925 : jstate = JumbleQuery(query);
149 : :
5208 tgl@sss.pgh.pa.us 150 [ + + ]: 480698 : if (post_parse_analyze_hook)
1910 bruce@momjian.us 151 : 75752 : (*post_parse_analyze_hook) (pstate, query, jstate);
152 : :
6947 tgl@sss.pgh.pa.us 153 : 480698 : free_parsestate(pstate);
154 : :
1897 bruce@momjian.us 155 : 480698 : pgstat_report_query_id(query->queryId, false);
156 : :
6947 tgl@sss.pgh.pa.us 157 : 480698 : return query;
158 : : }
159 : :
160 : : /*
161 : : * parse_analyze_varparams
162 : : *
163 : : * This variant is used when it's okay to deduce information about $n
164 : : * symbol datatypes from context. The passed-in paramTypes[] array can
165 : : * be modified or enlarged (via repalloc).
166 : : */
167 : : Query *
3454 168 : 5654 : parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
169 : : Oid **paramTypes, int *numParams,
170 : : QueryEnvironment *queryEnv)
171 : : {
8463 172 : 5654 : ParseState *pstate = make_parsestate(NULL);
173 : : Query *query;
1910 bruce@momjian.us 174 : 5654 : JumbleState *jstate = NULL;
175 : :
6228 176 [ - + ]: 5654 : Assert(sourceText != NULL); /* required as of 8.4 */
177 : :
7413 tgl@sss.pgh.pa.us 178 : 5654 : pstate->p_sourcetext = sourceText;
179 : :
1579 peter@eisentraut.org 180 : 5654 : setup_parse_variable_parameters(pstate, paramTypes, numParams);
181 : :
182 : 5654 : pstate->p_queryEnv = queryEnv;
183 : :
5216 tgl@sss.pgh.pa.us 184 : 5654 : query = transformTopLevelStmt(pstate, parseTree);
185 : :
186 : : /* make sure all is well with parameter types */
6086 187 : 5645 : check_variable_parameters(pstate, query);
188 : :
1872 alvherre@alvh.no-ip. 189 [ + + ]: 5645 : if (IsQueryIdEnabled())
1098 michael@paquier.xyz 190 : 288 : jstate = JumbleQuery(query);
191 : :
5208 tgl@sss.pgh.pa.us 192 [ + + ]: 5645 : if (post_parse_analyze_hook)
1910 bruce@momjian.us 193 : 288 : (*post_parse_analyze_hook) (pstate, query, jstate);
194 : :
6947 tgl@sss.pgh.pa.us 195 : 5645 : free_parsestate(pstate);
196 : :
1897 bruce@momjian.us 197 : 5645 : pgstat_report_query_id(query->queryId, false);
198 : :
6947 tgl@sss.pgh.pa.us 199 : 5645 : return query;
200 : : }
201 : :
202 : : /*
203 : : * parse_analyze_withcb
204 : : *
205 : : * This variant is used when the caller supplies their own parser callback to
206 : : * resolve parameters and possibly other things.
207 : : */
208 : : Query *
1574 peter@eisentraut.org 209 : 24927 : parse_analyze_withcb(RawStmt *parseTree, const char *sourceText,
210 : : ParserSetupHook parserSetup,
211 : : void *parserSetupArg,
212 : : QueryEnvironment *queryEnv)
213 : : {
214 : 24927 : ParseState *pstate = make_parsestate(NULL);
215 : : Query *query;
216 : 24927 : JumbleState *jstate = NULL;
217 : :
218 [ - + ]: 24927 : Assert(sourceText != NULL); /* required as of 8.4 */
219 : :
220 : 24927 : pstate->p_sourcetext = sourceText;
221 : 24927 : pstate->p_queryEnv = queryEnv;
222 : 24927 : (*parserSetup) (pstate, parserSetupArg);
223 : :
224 : 24927 : query = transformTopLevelStmt(pstate, parseTree);
225 : :
226 [ + + ]: 24852 : if (IsQueryIdEnabled())
1098 michael@paquier.xyz 227 : 4114 : jstate = JumbleQuery(query);
228 : :
1574 peter@eisentraut.org 229 [ + + ]: 24852 : if (post_parse_analyze_hook)
230 : 4111 : (*post_parse_analyze_hook) (pstate, query, jstate);
231 : :
232 : 24852 : free_parsestate(pstate);
233 : :
234 : 24852 : pgstat_report_query_id(query->queryId, false);
235 : :
236 : 24852 : return query;
237 : : }
238 : :
239 : :
240 : : /*
241 : : * parse_sub_analyze
242 : : * Entry point for recursively analyzing a sub-statement.
243 : : */
244 : : Query *
6138 tgl@sss.pgh.pa.us 245 : 69816 : parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
246 : : CommonTableExpr *parentCTE,
247 : : bool locked_from_parent,
248 : : bool resolve_unknowns)
249 : : {
9397 250 : 69816 : ParseState *pstate = make_parsestate(parentParseState);
251 : : Query *query;
252 : :
6138 253 : 69816 : pstate->p_parent_cte = parentCTE;
6090 254 : 69816 : pstate->p_locked_from_parent = locked_from_parent;
3443 255 : 69816 : pstate->p_resolve_unknowns = resolve_unknowns;
256 : :
6947 257 : 69816 : query = transformStmt(pstate, parseTree);
258 : :
259 : 69675 : free_parsestate(pstate);
260 : :
261 : 69675 : return query;
262 : : }
263 : :
264 : : /*
265 : : * transformTopLevelStmt -
266 : : * transform a Parse tree into a Query tree.
267 : : *
268 : : * This function is just responsible for transferring statement location data
269 : : * from the RawStmt into the finished Query.
270 : : */
271 : : Query *
3454 272 : 519477 : transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
273 : : {
274 : : Query *result;
275 : :
276 : : /* We're at top level, so allow SELECT INTO */
277 : 519477 : result = transformOptionalSelectInto(pstate, parseTree->stmt);
278 : :
383 michael@paquier.xyz 279 : 513586 : result->stmt_location = parseTree->stmt_location;
280 : 513586 : result->stmt_len = parseTree->stmt_len;
281 : :
3454 tgl@sss.pgh.pa.us 282 : 513586 : return result;
283 : : }
284 : :
285 : : /*
286 : : * transformOptionalSelectInto -
287 : : * If SELECT has INTO, convert it to CREATE TABLE AS.
288 : : *
289 : : * The only thing we do here that we don't do in transformStmt() is to
290 : : * convert SELECT ... INTO into CREATE TABLE AS. Since utility statements
291 : : * aren't allowed within larger statements, this is only allowed at the top
292 : : * of the parse tree, and so we only try it before entering the recursive
293 : : * transformStmt() processing.
294 : : */
295 : : static Query *
296 : 536009 : transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
297 : : {
5216 298 [ + + ]: 536009 : if (IsA(parseTree, SelectStmt))
299 : : {
300 : 233961 : SelectStmt *stmt = (SelectStmt *) parseTree;
301 : :
302 : : /* If it's a set-operation tree, drill down to leftmost SelectStmt */
303 [ + - + + ]: 241003 : while (stmt && stmt->op != SETOP_NONE)
304 : 7042 : stmt = stmt->larg;
2236 305 [ + - + - : 233961 : Assert(stmt && IsA(stmt, SelectStmt) && stmt->larg == NULL);
- + ]
306 : :
5216 307 [ + + ]: 233961 : if (stmt->intoClause)
308 : : {
309 : 70 : CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
310 : :
311 : 70 : ctas->query = parseTree;
312 : 70 : ctas->into = stmt->intoClause;
2180 michael@paquier.xyz 313 : 70 : ctas->objtype = OBJECT_TABLE;
5216 tgl@sss.pgh.pa.us 314 : 70 : ctas->is_select_into = true;
315 : :
316 : : /*
317 : : * Remove the intoClause from the SelectStmt. This makes it safe
318 : : * for transformSelectStmt to complain if it finds intoClause set
319 : : * (implying that the INTO appeared in a disallowed place).
320 : : */
321 : 70 : stmt->intoClause = NULL;
322 : :
323 : 70 : parseTree = (Node *) ctas;
324 : : }
325 : : }
326 : :
327 : 536009 : return transformStmt(pstate, parseTree);
328 : : }
329 : :
330 : : /*
331 : : * transformStmt -
332 : : * recursively transform a Parse tree into a Query tree.
333 : : */
334 : : Query *
6947 335 : 618331 : transformStmt(ParseState *pstate, Node *parseTree)
336 : : {
337 : : Query *result;
338 : :
339 : : #ifdef DEBUG_NODE_TESTS_ENABLED
340 : :
341 : : /*
342 : : * We apply debug_raw_expression_coverage_test testing to basic DML
343 : : * statements; we can't just run it on everything because
344 : : * raw_expression_tree_walker() doesn't claim to handle utility
345 : : * statements.
346 : : */
698 peter@eisentraut.org 347 [ - + ]: 618331 : if (Debug_raw_expression_coverage_test)
348 : : {
698 peter@eisentraut.org 349 [ # # ]:UBC 0 : switch (nodeTag(parseTree))
350 : : {
351 : 0 : case T_SelectStmt:
352 : : case T_InsertStmt:
353 : : case T_UpdateStmt:
354 : : case T_DeleteStmt:
355 : : case T_MergeStmt:
356 : 0 : (void) test_raw_expression_coverage(parseTree, NULL);
357 : 0 : break;
358 : 0 : default:
359 : 0 : break;
360 : : }
361 : : }
362 : : #endif /* DEBUG_NODE_TESTS_ENABLED */
363 : :
364 : : /*
365 : : * Caution: when changing the set of statement types that have non-default
366 : : * processing here, see also stmt_requires_parse_analysis() and
367 : : * analyze_requires_snapshot().
368 : : */
10523 bruce@momjian.us 369 [ + + + + :CBC 618331 : switch (nodeTag(parseTree))
+ + + + +
+ + + ]
370 : : {
371 : : /*
372 : : * Optimizable statements
373 : : */
10399 374 : 43795 : case T_InsertStmt:
6947 tgl@sss.pgh.pa.us 375 : 43795 : result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
10522 bruce@momjian.us 376 : 42800 : break;
377 : :
378 : 3360 : case T_DeleteStmt:
379 : 3360 : result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
380 : 3268 : break;
381 : :
10399 382 : 9288 : case T_UpdateStmt:
383 : 9288 : result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
10522 384 : 9151 : break;
385 : :
1555 alvherre@alvh.no-ip. 386 : 1383 : case T_MergeStmt:
387 : 1383 : result = transformMergeStmt(pstate, (MergeStmt *) parseTree);
388 : 1339 : break;
389 : :
10399 bruce@momjian.us 390 : 312527 : case T_SelectStmt:
391 : : {
7272 mail@joeconway.com 392 : 312527 : SelectStmt *n = (SelectStmt *) parseTree;
393 : :
394 [ + + ]: 312527 : if (n->valuesLists)
395 : 5806 : result = transformValuesClause(pstate, n);
396 [ + + ]: 306721 : else if (n->op == SETOP_NONE)
276 tgl@sss.pgh.pa.us 397 :GNC 298286 : result = transformSelectStmt(pstate, n, NULL);
398 : : else
7272 mail@joeconway.com 399 :CBC 8435 : result = transformSetOperationStmt(pstate, n);
400 : : }
10522 bruce@momjian.us 401 : 307793 : break;
402 : :
1910 peter@eisentraut.org 403 : 2728 : case T_ReturnStmt:
404 : 2728 : result = transformReturnStmt(pstate, (ReturnStmt *) parseTree);
405 : 2724 : break;
406 : :
2003 tgl@sss.pgh.pa.us 407 : 3375 : case T_PLAssignStmt:
408 : 3375 : result = transformPLAssignStmt(pstate,
409 : : (PLAssignStmt *) parseTree);
410 : 3362 : break;
411 : :
412 : : /*
413 : : * Special cases
414 : : */
7004 415 : 2711 : case T_DeclareCursorStmt:
416 : 2711 : result = transformDeclareCursorStmt(pstate,
417 : : (DeclareCursorStmt *) parseTree);
418 : 2698 : break;
419 : :
420 : 16532 : case T_ExplainStmt:
421 : 16532 : result = transformExplainStmt(pstate,
422 : : (ExplainStmt *) parseTree);
423 : 16523 : break;
424 : :
5216 425 : 1316 : case T_CreateTableAsStmt:
426 : 1316 : result = transformCreateTableAsStmt(pstate,
427 : : (CreateTableAsStmt *) parseTree);
428 : 1307 : break;
429 : :
3052 peter_e@gmx.net 430 : 313 : case T_CallStmt:
431 : 313 : result = transformCallStmt(pstate,
432 : : (CallStmt *) parseTree);
3025 433 : 292 : break;
434 : :
10522 bruce@momjian.us 435 : 221003 : default:
436 : :
437 : : /*
438 : : * other statements don't require any transformation; just return
439 : : * the original parsetree with a Query node plastered on top.
440 : : */
441 : 221003 : result = makeNode(Query);
442 : 221003 : result->commandType = CMD_UTILITY;
210 peter@eisentraut.org 443 :GNC 221003 : result->utilityStmt = parseTree;
10522 bruce@momjian.us 444 :CBC 221003 : break;
445 : : }
446 : :
447 : : /* Mark as original query until we learn differently */
8460 tgl@sss.pgh.pa.us 448 : 612260 : result->querySource = QSRC_ORIGINAL;
449 : 612260 : result->canSetTag = true;
450 : :
10523 bruce@momjian.us 451 : 612260 : return result;
452 : : }
453 : :
454 : : /*
455 : : * stmt_requires_parse_analysis
456 : : * Returns true if parse analysis will do anything non-trivial
457 : : * with the given raw parse tree.
458 : : *
459 : : * Generally, this should return true for any statement type for which
460 : : * transformStmt() does more than wrap a CMD_UTILITY Query around it.
461 : : * When it returns false, the caller can assume that there is no situation
462 : : * in which parse analysis of the raw statement could need to be re-done.
463 : : *
464 : : * Currently, since the rewriter and planner do nothing for CMD_UTILITY
465 : : * Queries, a false result means that the entire parse analysis/rewrite/plan
466 : : * pipeline will never need to be re-done. If that ever changes, callers
467 : : * will likely need adjustment.
468 : : */
469 : : bool
1041 tgl@sss.pgh.pa.us 470 : 21032377 : stmt_requires_parse_analysis(RawStmt *parseTree)
471 : : {
472 : : bool result;
473 : :
3454 474 [ + + + ]: 21032377 : switch (nodeTag(parseTree->stmt))
475 : : {
476 : : /*
477 : : * Optimizable statements
478 : : */
6408 479 : 20482086 : case T_InsertStmt:
480 : : case T_DeleteStmt:
481 : : case T_UpdateStmt:
482 : : case T_MergeStmt:
483 : : case T_SelectStmt:
484 : : case T_ReturnStmt:
485 : : case T_PLAssignStmt:
486 : 20482086 : result = true;
487 : 20482086 : break;
488 : :
489 : : /*
490 : : * Special cases
491 : : */
492 : 33147 : case T_DeclareCursorStmt:
493 : : case T_ExplainStmt:
494 : : case T_CreateTableAsStmt:
495 : : case T_CallStmt:
496 : 33147 : result = true;
497 : 33147 : break;
498 : :
499 : 517144 : default:
500 : : /* all other statements just get wrapped in a CMD_UTILITY Query */
501 : 517144 : result = false;
502 : 517144 : break;
503 : : }
504 : :
505 : 21032377 : return result;
506 : : }
507 : :
508 : : /*
509 : : * analyze_requires_snapshot
510 : : * Returns true if a snapshot must be set before doing parse analysis
511 : : * on the given raw parse tree.
512 : : */
513 : : bool
1041 514 : 454763 : analyze_requires_snapshot(RawStmt *parseTree)
515 : : {
516 : : /*
517 : : * Currently, this should return true in exactly the same cases that
518 : : * stmt_requires_parse_analysis() does, so we just invoke that function
519 : : * rather than duplicating it. We keep the two entry points separate for
520 : : * clarity of callers, since from the callers' standpoint these are
521 : : * different conditions.
522 : : *
523 : : * While there may someday be a statement type for which transformStmt()
524 : : * does something nontrivial and yet no snapshot is needed for that
525 : : * processing, it seems likely that making such a choice would be fragile.
526 : : * If you want to install an exception, document the reasoning for it in a
527 : : * comment.
528 : : */
529 : 454763 : return stmt_requires_parse_analysis(parseTree);
530 : : }
531 : :
532 : : /*
533 : : * query_requires_rewrite_plan()
534 : : * Returns true if rewriting or planning is non-trivial for this Query.
535 : : *
536 : : * This is much like stmt_requires_parse_analysis(), but applies one step
537 : : * further down the pipeline.
538 : : *
539 : : * We do not provide an equivalent of analyze_requires_snapshot(): callers
540 : : * can assume that any rewriting or planning activity needs a snapshot.
541 : : */
542 : : bool
454 543 : 376334 : query_requires_rewrite_plan(Query *query)
544 : : {
545 : : bool result;
546 : :
547 [ + - ]: 376334 : if (query->commandType != CMD_UTILITY)
548 : : {
549 : : /* All optimizable statements require rewriting/planning */
550 : 376334 : result = true;
551 : : }
552 : : else
553 : : {
554 : : /* This list should match stmt_requires_parse_analysis() */
454 tgl@sss.pgh.pa.us 555 [ # # ]:UBC 0 : switch (nodeTag(query->utilityStmt))
556 : : {
557 : 0 : case T_DeclareCursorStmt:
558 : : case T_ExplainStmt:
559 : : case T_CreateTableAsStmt:
560 : : case T_CallStmt:
561 : 0 : result = true;
562 : 0 : break;
563 : 0 : default:
564 : 0 : result = false;
565 : 0 : break;
566 : : }
567 : : }
454 tgl@sss.pgh.pa.us 568 :CBC 376334 : return result;
569 : : }
570 : :
571 : : /*
572 : : * transformDeleteStmt -
573 : : * transforms a Delete Statement
574 : : */
575 : : static Query *
10522 bruce@momjian.us 576 : 3360 : transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
577 : : {
578 : 3360 : Query *qry = makeNode(Query);
579 : : ParseNamespaceItem *nsitem;
580 : : Node *qual;
581 : :
10523 582 : 3360 : qry->commandType = CMD_DELETE;
583 : :
584 : : /* process the WITH clause independently of all else */
5737 tgl@sss.pgh.pa.us 585 [ + + ]: 3360 : if (stmt->withClause)
586 : : {
587 : 20 : qry->hasRecursive = stmt->withClause->recursive;
588 : 20 : qry->cteList = transformWithClause(pstate, stmt->withClause);
5604 589 : 20 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
590 : : }
591 : :
592 : : /* set up range table with just the result rel */
8866 593 : 6716 : qry->resultRelation = setTargetTable(pstate, stmt->relation,
3476 594 : 3360 : stmt->relation->inh,
595 : : true,
596 : : ACL_DELETE);
2371 597 : 3356 : nsitem = pstate->p_target_nsitem;
598 : :
599 : : /* disallow DELETE ... WHERE CURRENT OF on a view */
69 dean.a.rasheed@gmail 600 [ + + ]: 3356 : if (stmt->whereClause &&
601 [ + + ]: 2274 : IsA(stmt->whereClause, CurrentOfExpr) &&
602 [ + + ]: 80 : pstate->p_target_relation->rd_rel->relkind == RELKIND_VIEW)
603 [ + - ]: 4 : ereport(ERROR,
604 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
605 : : errmsg("WHERE CURRENT OF on a view is not implemented"));
606 : :
607 : : /* there's no DISTINCT in DELETE */
9651 tgl@sss.pgh.pa.us 608 : 3352 : qry->distinctClause = NIL;
609 : :
610 : : /* subqueries in USING cannot access the result relation */
4557 611 : 3352 : nsitem->p_lateral_only = true;
4553 612 : 3352 : nsitem->p_lateral_ok = false;
613 : :
614 : : /*
615 : : * The USING clause is non-standard SQL syntax, and is equivalent in
616 : : * functionality to the FROM list that can be specified for UPDATE. The
617 : : * USING keyword is used rather than FROM because FROM is already a
618 : : * keyword in the DELETE syntax.
619 : : */
7754 neilc@samurai.com 620 : 3352 : transformFromClause(pstate, stmt->usingClause);
621 : :
622 : : /* remaining clauses can reference the result relation normally */
4557 tgl@sss.pgh.pa.us 623 : 3340 : nsitem->p_lateral_only = false;
4553 624 : 3340 : nsitem->p_lateral_ok = true;
625 : :
90 peter@eisentraut.org 626 [ + + ]:GNC 3340 : if (stmt->forPortionOf)
627 : 399 : qry->forPortionOf = transformForPortionOfClause(pstate,
628 : : qry->resultRelation,
629 : 451 : stmt->forPortionOf,
1 630 : 451 : stmt->whereClause,
631 : : false);
632 : :
5072 tgl@sss.pgh.pa.us 633 :CBC 3288 : qual = transformWhereClause(pstate, stmt->whereClause,
634 : : EXPR_KIND_WHERE, "WHERE");
635 : :
530 dean.a.rasheed@gmail 636 : 3272 : transformReturningClause(pstate, qry, stmt->returningClause,
637 : : EXPR_KIND_RETURNING);
638 : :
639 : : /* done building the range table and jointree */
10523 bruce@momjian.us 640 : 3268 : qry->rtable = pstate->p_rtable;
1302 alvherre@alvh.no-ip. 641 : 3268 : qry->rteperminfos = pstate->p_rteperminfos;
9405 tgl@sss.pgh.pa.us 642 : 3268 : qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
643 : :
9724 644 : 3268 : qry->hasSubLinks = pstate->p_hasSubLinks;
6393 645 : 3268 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
3577 646 : 3268 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
5256 647 : 3268 : qry->hasAggs = pstate->p_hasAggs;
648 : :
5582 649 : 3268 : assign_query_collations(pstate, qry);
650 : :
651 : : /* this must be done after collations, for reliable comparison of exprs */
2721 rhodiumtoad@postgres 652 [ - + ]: 3268 : if (pstate->p_hasAggs)
2721 rhodiumtoad@postgres 653 :UBC 0 : parseCheckAggregates(pstate, qry);
654 : :
9399 tgl@sss.pgh.pa.us 655 :CBC 3268 : return qry;
656 : : }
657 : :
658 : : /*
659 : : * transformInsertStmt -
660 : : * transform an Insert Statement
661 : : */
662 : : static Query *
6947 663 : 43795 : transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
664 : : {
9843 665 : 43795 : Query *qry = makeNode(Query);
7272 mail@joeconway.com 666 : 43795 : SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
667 : 43795 : List *exprList = NIL;
668 : : bool isGeneralSelect;
669 : : List *sub_rtable;
670 : : List *sub_rteperminfos;
671 : : List *sub_namespace;
672 : : List *icolumns;
673 : : List *attrnos;
674 : : ParseNamespaceItem *nsitem;
675 : : RTEPermissionInfo *perminfo;
676 : : ListCell *icols;
677 : : ListCell *attnos;
678 : : ListCell *lc;
679 : : bool requiresUpdatePerm;
680 : : AclMode targetPerms;
681 : :
682 : : /* There can't be any outer WITH to worry about */
5737 tgl@sss.pgh.pa.us 683 [ - + ]: 43795 : Assert(pstate->p_ctenamespace == NIL);
684 : :
10523 bruce@momjian.us 685 : 43795 : qry->commandType = CMD_INSERT;
686 : :
687 : : /* process the WITH clause independently of all else */
5737 tgl@sss.pgh.pa.us 688 [ + + ]: 43795 : if (stmt->withClause)
689 : : {
690 : 189 : qry->hasRecursive = stmt->withClause->recursive;
691 : 189 : qry->cteList = transformWithClause(pstate, stmt->withClause);
5604 692 : 189 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
693 : : }
694 : :
3372 peter_e@gmx.net 695 : 43795 : qry->override = stmt->override;
696 : :
697 : : /*
698 : : * ON CONFLICT DO UPDATE and ON CONFLICT DO SELECT FOR UPDATE/SHARE
699 : : * require UPDATE permission on the target relation.
700 : : */
138 dean.a.rasheed@gmail 701 [ + + ]:GNC 45356 : requiresUpdatePerm = (stmt->onConflictClause &&
702 [ + + ]: 1561 : (stmt->onConflictClause->action == ONCONFLICT_UPDATE ||
703 [ + + ]: 626 : (stmt->onConflictClause->action == ONCONFLICT_SELECT &&
704 [ + + ]: 240 : stmt->onConflictClause->lockStrength != LCS_NONE)));
705 : :
706 : : /*
707 : : * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
708 : : * VALUES list, or general SELECT input. We special-case VALUES, both for
709 : : * efficiency and so we can handle DEFAULT specifications.
710 : : *
711 : : * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
712 : : * VALUES clause. If we have any of those, treat it as a general SELECT;
713 : : * so it will work, but you can't use DEFAULT items together with those.
714 : : */
5750 tgl@sss.pgh.pa.us 715 [ + + + + ]:CBC 77578 : isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
716 [ + - ]: 33783 : selectStmt->sortClause != NIL ||
717 [ + - ]: 33783 : selectStmt->limitOffset != NULL ||
718 [ + - ]: 33783 : selectStmt->limitCount != NULL ||
719 [ + - ]: 33783 : selectStmt->lockingClause != NIL ||
720 [ - + ]: 33783 : selectStmt->withClause != NULL));
721 : :
722 : : /*
723 : : * If a non-nil rangetable/namespace was passed in, and we are doing
724 : : * INSERT/SELECT, arrange to pass the rangetable/rteperminfos/namespace
725 : : * down to the SELECT. This can only happen if we are inside a CREATE
726 : : * RULE, and in that case we want the rule's OLD and NEW rtable entries to
727 : : * appear as part of the SELECT's rtable, not as outer references for it.
728 : : * (Kluge!) The SELECT's joinlist is not affected however. We must do
729 : : * this before adding the target table to the INSERT's rtable.
730 : : */
7272 mail@joeconway.com 731 [ + + ]: 43795 : if (isGeneralSelect)
732 : : {
9267 tgl@sss.pgh.pa.us 733 : 4482 : sub_rtable = pstate->p_rtable;
734 : 4482 : pstate->p_rtable = NIL;
1302 alvherre@alvh.no-ip. 735 : 4482 : sub_rteperminfos = pstate->p_rteperminfos;
736 : 4482 : pstate->p_rteperminfos = NIL;
5074 tgl@sss.pgh.pa.us 737 : 4482 : sub_namespace = pstate->p_namespace;
738 : 4482 : pstate->p_namespace = NIL;
739 : : }
740 : : else
741 : : {
9267 742 : 39313 : sub_rtable = NIL; /* not used, but keep compiler quiet */
1281 743 : 39313 : sub_rteperminfos = NIL;
5074 744 : 39313 : sub_namespace = NIL;
745 : : }
746 : :
747 : : /*
748 : : * Must get write lock on INSERT target table before scanning SELECT, else
749 : : * we will grab the wrong kind of initial lock if the target table is also
750 : : * mentioned in the SELECT part. Note that the target table is not added
751 : : * to the joinlist or namespace.
752 : : */
4071 andres@anarazel.de 753 : 43795 : targetPerms = ACL_INSERT;
138 dean.a.rasheed@gmail 754 [ + + ]:GNC 43795 : if (requiresUpdatePerm)
4071 andres@anarazel.de 755 :CBC 1013 : targetPerms |= ACL_UPDATE;
8866 tgl@sss.pgh.pa.us 756 : 43795 : qry->resultRelation = setTargetTable(pstate, stmt->relation,
757 : : false, false, targetPerms);
758 : :
759 : : /* Validate stmt->cols list, or build default list if no list given */
7272 mail@joeconway.com 760 : 43779 : icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
761 [ - + ]: 43747 : Assert(list_length(icolumns) == list_length(attrnos));
762 : :
763 : : /*
764 : : * Determine which variant of INSERT we have.
765 : : */
766 [ + + ]: 43747 : if (selectStmt == NULL)
767 : : {
768 : : /*
769 : : * We have INSERT ... DEFAULT VALUES. We can handle this case by
770 : : * emitting an empty targetlist --- all columns will be defaulted when
771 : : * the planner expands the targetlist.
772 : : */
773 : 5530 : exprList = NIL;
774 : : }
775 [ + + ]: 38217 : else if (isGeneralSelect)
776 : : {
777 : : /*
778 : : * We make the sub-pstate a child of the outer pstate so that it can
779 : : * see any Param definitions supplied from above. Since the outer
780 : : * pstate's rtable and namespace are presently empty, there are no
781 : : * side-effects of exposing names the sub-SELECT shouldn't be able to
782 : : * see.
783 : : */
8463 tgl@sss.pgh.pa.us 784 : 4482 : ParseState *sub_pstate = make_parsestate(pstate);
785 : : Query *selectQuery;
786 : :
787 : : /*
788 : : * Process the source SELECT.
789 : : *
790 : : * It is important that this be handled just like a standalone SELECT;
791 : : * otherwise the behavior of SELECT within INSERT might be different
792 : : * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
793 : : * bugs of just that nature...)
794 : : *
795 : : * The sole exception is that we prevent resolving unknown-type
796 : : * outputs as TEXT. This does not change the semantics since if the
797 : : * column type matters semantically, it would have been resolved to
798 : : * something else anyway. Doing this lets us resolve such outputs as
799 : : * the target column's type, which we handle below.
800 : : */
9267 801 : 4482 : sub_pstate->p_rtable = sub_rtable;
1302 alvherre@alvh.no-ip. 802 : 4482 : sub_pstate->p_rteperminfos = sub_rteperminfos;
6228 bruce@momjian.us 803 : 4482 : sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
1247 tgl@sss.pgh.pa.us 804 : 4482 : sub_pstate->p_nullingrels = NIL;
5074 805 : 4482 : sub_pstate->p_namespace = sub_namespace;
3443 806 : 4482 : sub_pstate->p_resolve_unknowns = false;
807 : :
6947 808 : 4482 : selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
809 : :
810 : 4478 : free_parsestate(sub_pstate);
811 : :
812 : : /* The grammar should have produced a SELECT */
6511 813 [ + - ]: 4478 : if (!IsA(selectQuery, Query) ||
3454 814 [ - + ]: 4478 : selectQuery->commandType != CMD_SELECT)
6511 tgl@sss.pgh.pa.us 815 [ # # ]:UBC 0 : elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
816 : :
817 : : /*
818 : : * Make the source be a subquery in the INSERT's rangetable, and add
819 : : * it to the INSERT's joinlist (but not the namespace).
820 : : */
2371 tgl@sss.pgh.pa.us 821 :CBC 4478 : nsitem = addRangeTableEntryForSubquery(pstate,
822 : : selectQuery,
823 : : NULL,
824 : : false,
825 : : false);
826 : 4478 : addNSItemToQuery(pstate, nsitem, true, false, false);
827 : :
828 : : /*----------
829 : : * Generate an expression list for the INSERT that selects all the
830 : : * non-resjunk columns from the subquery. (INSERT's tlist must be
831 : : * separate from the subquery's tlist because we may add columns,
832 : : * insert datatype coercions, etc.)
833 : : *
834 : : * HACK: unknown-type constants and params in the SELECT's targetlist
835 : : * are copied up as-is rather than being referenced as subquery
836 : : * outputs. This is to ensure that when we try to coerce them to
837 : : * the target column's datatype, the right things happen (see
838 : : * special cases in coerce_type). Otherwise, this fails:
839 : : * INSERT INTO foo SELECT 'bar', ... FROM baz
840 : : *----------
841 : : */
7272 mail@joeconway.com 842 : 4478 : exprList = NIL;
843 [ + + + + : 15666 : foreach(lc, selectQuery->targetList)
+ + ]
844 : : {
845 : 11188 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
846 : : Expr *expr;
847 : :
7755 tgl@sss.pgh.pa.us 848 [ + + ]: 11188 : if (tle->resjunk)
9399 849 : 64 : continue;
8463 850 [ + - ]: 11124 : if (tle->expr &&
2236 851 [ + + + + : 13787 : (IsA(tle->expr, Const) || IsA(tle->expr, Param)) &&
+ + ]
8463 852 : 2663 : exprType((Node *) tle->expr) == UNKNOWNOID)
9399 853 : 827 : expr = tle->expr;
854 : : else
855 : : {
2371 856 : 10297 : Var *var = makeVarFromTargetEntry(nsitem->p_rtindex, tle);
857 : :
6475 858 : 10297 : var->location = exprLocation((Node *) tle->expr);
859 : 10297 : expr = (Expr *) var;
860 : : }
7272 mail@joeconway.com 861 : 11124 : exprList = lappend(exprList, expr);
862 : : }
863 : :
864 : : /* Prepare row for assignment to target table */
865 : 4478 : exprList = transformInsertRow(pstate, exprList,
866 : : stmt->cols,
867 : : icolumns, attrnos,
868 : : false);
869 : : }
870 [ + + ]: 33735 : else if (list_length(selectStmt->valuesLists) > 1)
871 : : {
872 : : /*
873 : : * Process INSERT ... VALUES with multiple VALUES sublists. We
874 : : * generate a VALUES RTE holding the transformed expression lists, and
875 : : * build up a targetlist containing Vars that reference the VALUES
876 : : * RTE.
877 : : */
878 : 3348 : List *exprsLists = NIL;
3491 tgl@sss.pgh.pa.us 879 : 3348 : List *coltypes = NIL;
880 : 3348 : List *coltypmods = NIL;
881 : 3348 : List *colcollations = NIL;
7272 mail@joeconway.com 882 : 3348 : int sublist_length = -1;
5063 tgl@sss.pgh.pa.us 883 : 3348 : bool lateral = false;
884 : :
5216 885 [ - + ]: 3348 : Assert(selectStmt->intoClause == NULL);
886 : :
7272 mail@joeconway.com 887 [ + - + + : 14735 : foreach(lc, selectStmt->valuesLists)
+ + ]
888 : : {
7209 bruce@momjian.us 889 : 11387 : List *sublist = (List *) lfirst(lc);
890 : :
891 : : /*
892 : : * Do basic expression transformation (same as a ROW() expr, but
893 : : * allow SetToDefault at top level)
894 : : */
3507 tgl@sss.pgh.pa.us 895 : 11387 : sublist = transformExpressionList(pstate, sublist,
896 : : EXPR_KIND_VALUES, true);
897 : :
898 : : /*
899 : : * All the sublists must be the same length, *after*
900 : : * transformation (which might expand '*' into multiple items).
901 : : * The VALUES RTE can't handle anything different.
902 : : */
7272 mail@joeconway.com 903 [ + + ]: 11387 : if (sublist_length < 0)
904 : : {
905 : : /* Remember post-transformation length of first sublist */
906 : 3348 : sublist_length = list_length(sublist);
907 : : }
908 [ - + ]: 8039 : else if (sublist_length != list_length(sublist))
909 : : {
7272 mail@joeconway.com 910 [ # # ]:UBC 0 : ereport(ERROR,
911 : : (errcode(ERRCODE_SYNTAX_ERROR),
912 : : errmsg("VALUES lists must all be the same length"),
913 : : parser_errposition(pstate,
914 : : exprLocation((Node *) sublist))));
915 : : }
916 : :
917 : : /*
918 : : * Prepare row for assignment to target table. We process any
919 : : * indirection on the target column specs normally but then strip
920 : : * off the resulting field/array assignment nodes, since we don't
921 : : * want the parsed statement to contain copies of those in each
922 : : * VALUES row. (It's annoying to have to transform the
923 : : * indirection specs over and over like this, but avoiding it
924 : : * would take some really messy refactoring of
925 : : * transformAssignmentIndirection.)
926 : : */
7272 mail@joeconway.com 927 :CBC 11387 : sublist = transformInsertRow(pstate, sublist,
928 : : stmt->cols,
929 : : icolumns, attrnos,
930 : : true);
931 : :
932 : : /*
933 : : * We must assign collations now because assign_query_collations
934 : : * doesn't process rangetable entries. We just assign all the
935 : : * collations independently in each row, and don't worry about
936 : : * whether they are consistent vertically. The outer INSERT query
937 : : * isn't going to care about the collations of the VALUES columns,
938 : : * so it's not worth the effort to identify a common collation for
939 : : * each one here. (But note this does have one user-visible
940 : : * consequence: INSERT ... VALUES won't complain about conflicting
941 : : * explicit COLLATEs in a column, whereas the same VALUES
942 : : * construct in another context would complain.)
943 : : */
5582 tgl@sss.pgh.pa.us 944 : 11387 : assign_list_collations(pstate, sublist);
945 : :
7272 mail@joeconway.com 946 : 11387 : exprsLists = lappend(exprsLists, sublist);
947 : : }
948 : :
949 : : /*
950 : : * Construct column type/typmod/collation lists for the VALUES RTE.
951 : : * Every expression in each column has been coerced to the type/typmod
952 : : * of the corresponding target column or subfield, so it's sufficient
953 : : * to look at the exprType/exprTypmod of the first row. We don't care
954 : : * about the collation labeling, so just fill in InvalidOid for that.
955 : : */
3491 tgl@sss.pgh.pa.us 956 [ + - + + : 9707 : foreach(lc, (List *) linitial(exprsLists))
+ + ]
957 : : {
958 : 6359 : Node *val = (Node *) lfirst(lc);
959 : :
960 : 6359 : coltypes = lappend_oid(coltypes, exprType(val));
961 : 6359 : coltypmods = lappend_int(coltypmods, exprTypmod(val));
962 : 6359 : colcollations = lappend_oid(colcollations, InvalidOid);
963 : : }
964 : :
965 : : /*
966 : : * Ordinarily there can't be any current-level Vars in the expression
967 : : * lists, because the namespace was empty ... but if we're inside
968 : : * CREATE RULE, then NEW/OLD references might appear. In that case we
969 : : * have to mark the VALUES RTE as LATERAL.
970 : : */
7272 971 [ + + + - ]: 3366 : if (list_length(pstate->p_rtable) != 1 &&
972 : 18 : contain_vars_of_level((Node *) exprsLists, 0))
5063 973 : 18 : lateral = true;
974 : :
975 : : /*
976 : : * Generate the VALUES RTE
977 : : */
2371 978 : 3348 : nsitem = addRangeTableEntryForValues(pstate, exprsLists,
979 : : coltypes, coltypmods, colcollations,
980 : : NULL, lateral, true);
981 : 3348 : addNSItemToQuery(pstate, nsitem, true, false, false);
982 : :
983 : : /*
984 : : * Generate list of Vars referencing the RTE
985 : : */
1247 986 : 3348 : exprList = expandNSItemVars(pstate, nsitem, 0, -1, NULL);
987 : :
988 : : /*
989 : : * Re-apply any indirection on the target column specs to the Vars
990 : : */
3618 991 : 3348 : exprList = transformInsertRow(pstate, exprList,
992 : : stmt->cols,
993 : : icolumns, attrnos,
994 : : false);
995 : : }
996 : : else
997 : : {
998 : : /*
999 : : * Process INSERT ... VALUES with a single VALUES sublist. We treat
1000 : : * this case separately for efficiency. The sublist is just computed
1001 : : * directly as the Query's targetlist, with no VALUES RTE. So it
1002 : : * works just like a SELECT without any FROM.
1003 : : */
7272 mail@joeconway.com 1004 : 30387 : List *valuesLists = selectStmt->valuesLists;
1005 : :
1006 [ - + ]: 30387 : Assert(list_length(valuesLists) == 1);
5216 tgl@sss.pgh.pa.us 1007 [ - + ]: 30387 : Assert(selectStmt->intoClause == NULL);
1008 : :
1009 : : /*
1010 : : * Do basic expression transformation (same as a ROW() expr, but allow
1011 : : * SetToDefault at top level)
1012 : : */
7272 mail@joeconway.com 1013 : 30387 : exprList = transformExpressionList(pstate,
5072 tgl@sss.pgh.pa.us 1014 : 30387 : (List *) linitial(valuesLists),
1015 : : EXPR_KIND_VALUES_SINGLE,
1016 : : true);
1017 : :
1018 : : /* Prepare row for assignment to target table */
7272 mail@joeconway.com 1019 : 30371 : exprList = transformInsertRow(pstate, exprList,
1020 : : stmt->cols,
1021 : : icolumns, attrnos,
1022 : : false);
1023 : : }
1024 : :
1025 : : /*
1026 : : * Generate query's target list using the computed list of expressions.
1027 : : * Also, mark all the target columns as needing insert permissions.
1028 : : */
1302 alvherre@alvh.no-ip. 1029 : 42876 : perminfo = pstate->p_target_nsitem->p_perminfo;
7272 mail@joeconway.com 1030 : 42876 : qry->targetList = NIL;
2679 tgl@sss.pgh.pa.us 1031 [ - + ]: 42876 : Assert(list_length(exprList) <= list_length(icolumns));
1032 [ + + + + : 127062 : forthree(lc, exprList, icols, icolumns, attnos, attrnos)
+ + + + +
+ + + + +
+ - + - +
+ ]
1033 : : {
7209 bruce@momjian.us 1034 : 84186 : Expr *expr = (Expr *) lfirst(lc);
2679 tgl@sss.pgh.pa.us 1035 : 84186 : ResTarget *col = lfirst_node(ResTarget, icols);
1036 : 84186 : AttrNumber attr_num = (AttrNumber) lfirst_int(attnos);
1037 : : TargetEntry *tle;
1038 : :
7272 mail@joeconway.com 1039 : 84186 : tle = makeTargetEntry(expr,
1040 : : attr_num,
1041 : : col->name,
1042 : : false);
1043 : 84186 : qry->targetList = lappend(qry->targetList, tle);
1044 : :
1302 alvherre@alvh.no-ip. 1045 : 84186 : perminfo->insertedCols = bms_add_member(perminfo->insertedCols,
1046 : : attr_num - FirstLowInvalidHeapAttributeNumber);
1047 : : }
1048 : :
1049 : : /*
1050 : : * If we have any clauses yet to process, set the query namespace to
1051 : : * contain only the target relation, removing any entries added in a
1052 : : * sub-SELECT or VALUES list.
1053 : : */
530 dean.a.rasheed@gmail 1054 [ + + + + ]: 42876 : if (stmt->onConflictClause || stmt->returningClause)
1055 : : {
5074 tgl@sss.pgh.pa.us 1056 : 2207 : pstate->p_namespace = NIL;
2371 1057 : 2207 : addNSItemToQuery(pstate, pstate->p_target_nsitem,
1058 : : false, true, true);
1059 : : }
1060 : :
1061 : : /* ON CONFLICT DO SELECT requires a RETURNING clause */
138 dean.a.rasheed@gmail 1062 [ + + ]:GNC 42876 : if (stmt->onConflictClause &&
1063 [ + + ]: 1561 : stmt->onConflictClause->action == ONCONFLICT_SELECT &&
1064 [ + + ]: 240 : !stmt->returningClause)
1065 [ + - ]: 4 : ereport(ERROR,
1066 : : errcode(ERRCODE_SYNTAX_ERROR),
1067 : : errmsg("ON CONFLICT DO SELECT requires a RETURNING clause"),
1068 : : parser_errposition(pstate, stmt->onConflictClause->location));
1069 : :
1070 : : /* Process ON CONFLICT, if any. */
1904 tgl@sss.pgh.pa.us 1071 [ + + ]:CBC 42872 : if (stmt->onConflictClause)
1072 : 1557 : qry->onConflict = transformOnConflictClause(pstate,
1073 : : stmt->onConflictClause);
1074 : :
1075 : : /* Process RETURNING, if any. */
530 dean.a.rasheed@gmail 1076 [ + + ]: 42832 : if (stmt->returningClause)
1077 : 1098 : transformReturningClause(pstate, qry, stmt->returningClause,
1078 : : EXPR_KIND_RETURNING);
1079 : :
1080 : : /* done building the range table and jointree */
9399 tgl@sss.pgh.pa.us 1081 : 42800 : qry->rtable = pstate->p_rtable;
1302 alvherre@alvh.no-ip. 1082 : 42800 : qry->rteperminfos = pstate->p_rteperminfos;
6947 tgl@sss.pgh.pa.us 1083 : 42800 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1084 : :
3577 1085 : 42800 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
6947 1086 : 42800 : qry->hasSubLinks = pstate->p_hasSubLinks;
1087 : :
5582 1088 : 42800 : assign_query_collations(pstate, qry);
1089 : :
6947 1090 : 42800 : return qry;
1091 : : }
1092 : :
1093 : : /*
1094 : : * Prepare an INSERT row for assignment to the target table.
1095 : : *
1096 : : * exprlist: transformed expressions for source values; these might come from
1097 : : * a VALUES row, or be Vars referencing a sub-SELECT or VALUES RTE output.
1098 : : * stmtcols: original target-columns spec for INSERT (we just test for NIL)
1099 : : * icolumns: effective target-columns spec (list of ResTarget)
1100 : : * attrnos: integer column numbers (must be same length as icolumns)
1101 : : * strip_indirection: if true, remove any field/array assignment nodes
1102 : : */
1103 : : List *
1104 : 50240 : transformInsertRow(ParseState *pstate, List *exprlist,
1105 : : List *stmtcols, List *icolumns, List *attrnos,
1106 : : bool strip_indirection)
1107 : : {
1108 : : List *result;
1109 : : ListCell *lc;
1110 : : ListCell *icols;
1111 : : ListCell *attnos;
1112 : :
1113 : : /*
1114 : : * Check length of expr list. It must not have more expressions than
1115 : : * there are target columns. We allow fewer, but only if no explicit
1116 : : * columns list was given (the remaining columns are implicitly
1117 : : * defaulted). Note we must check this *after* transformation because
1118 : : * that could expand '*' into multiple items.
1119 : : */
1120 [ + + ]: 50240 : if (list_length(exprlist) > list_length(icolumns))
1121 [ + - ]: 17 : ereport(ERROR,
1122 : : (errcode(ERRCODE_SYNTAX_ERROR),
1123 : : errmsg("INSERT has more expressions than target columns"),
1124 : : parser_errposition(pstate,
1125 : : exprLocation(list_nth(exprlist,
1126 : : list_length(icolumns))))));
1127 [ + + + + ]: 60684 : if (stmtcols != NIL &&
1128 : 10461 : list_length(exprlist) < list_length(icolumns))
1129 : : {
1130 : : /*
1131 : : * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1132 : : * where the user accidentally created a RowExpr instead of separate
1133 : : * columns. Add a suitable hint if that seems to be the problem,
1134 : : * because the main error message is quite misleading for this case.
1135 : : * (If there's no stmtcols, you'll get something about data type
1136 : : * mismatch, which is less misleading so we don't worry about giving a
1137 : : * hint in that case.)
1138 : : */
1139 [ + - - + : 8 : ereport(ERROR,
- - ]
1140 : : (errcode(ERRCODE_SYNTAX_ERROR),
1141 : : errmsg("INSERT has more target columns than expressions"),
1142 : : ((list_length(exprlist) == 1 &&
1143 : : count_rowexpr_columns(pstate, linitial(exprlist)) ==
1144 : : list_length(icolumns)) ?
1145 : : errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
1146 : : parser_errposition(pstate,
1147 : : exprLocation(list_nth(icolumns,
1148 : : list_length(exprlist))))));
1149 : : }
1150 : :
1151 : : /*
1152 : : * Prepare columns for assignment to target table.
1153 : : */
1154 : 50215 : result = NIL;
2679 1155 [ + + + + : 160787 : forthree(lc, exprlist, icols, icolumns, attnos, attrnos)
+ + + + +
+ + + + +
+ - + - +
+ ]
1156 : : {
6947 1157 : 111398 : Expr *expr = (Expr *) lfirst(lc);
2679 1158 : 111398 : ResTarget *col = lfirst_node(ResTarget, icols);
1159 : 111398 : int attno = lfirst_int(attnos);
1160 : :
6947 1161 : 111398 : expr = transformAssignedExpr(pstate, expr,
1162 : : EXPR_KIND_INSERT_TARGET,
1163 : 111398 : col->name,
1164 : : attno,
1165 : : col->indirection,
1166 : : col->location);
1167 : :
3618 1168 [ + + ]: 110572 : if (strip_indirection)
1169 : : {
1170 : : /*
1171 : : * We need to remove top-level FieldStores and SubscriptingRefs,
1172 : : * as well as any CoerceToDomain appearing above one of those ---
1173 : : * but not a CoerceToDomain that isn't above one of those.
1174 : : */
1175 [ + - ]: 25242 : while (expr)
1176 : : {
838 1177 : 25242 : Expr *subexpr = expr;
1178 : :
1179 [ + + ]: 25418 : while (IsA(subexpr, CoerceToDomain))
1180 : : {
1181 : 176 : subexpr = ((CoerceToDomain *) subexpr)->arg;
1182 : : }
1183 [ + + ]: 25242 : if (IsA(subexpr, FieldStore))
1184 : : {
1185 : 144 : FieldStore *fstore = (FieldStore *) subexpr;
1186 : :
3618 1187 : 144 : expr = (Expr *) linitial(fstore->newvals);
1188 : : }
838 1189 [ + + ]: 25098 : else if (IsA(subexpr, SubscriptingRef))
1190 : : {
1191 : 232 : SubscriptingRef *sbsref = (SubscriptingRef *) subexpr;
1192 : :
2706 alvherre@alvh.no-ip. 1193 [ - + ]: 232 : if (sbsref->refassgnexpr == NULL)
3618 tgl@sss.pgh.pa.us 1194 :UBC 0 : break;
1195 : :
2706 alvherre@alvh.no-ip. 1196 :CBC 232 : expr = sbsref->refassgnexpr;
1197 : : }
1198 : : else
3618 tgl@sss.pgh.pa.us 1199 : 24866 : break;
1200 : : }
1201 : : }
1202 : :
6947 1203 : 110572 : result = lappend(result, expr);
1204 : : }
1205 : :
1206 : 49389 : return result;
1207 : : }
1208 : :
1209 : : /*
1210 : : * transformOnConflictClause -
1211 : : * transforms an OnConflictClause in an INSERT
1212 : : */
1213 : : static OnConflictExpr *
4071 andres@anarazel.de 1214 : 1557 : transformOnConflictClause(ParseState *pstate,
1215 : : OnConflictClause *onConflictClause)
1216 : : {
1904 tgl@sss.pgh.pa.us 1217 : 1557 : ParseNamespaceItem *exclNSItem = NULL;
1218 : : List *arbiterElems;
1219 : : Node *arbiterWhere;
1220 : : Oid arbiterConstraint;
4071 andres@anarazel.de 1221 : 1557 : List *onConflictSet = NIL;
1222 : 1557 : Node *onConflictWhere = NULL;
1223 : 1557 : int exclRelIndex = 0;
1224 : 1557 : List *exclRelTlist = NIL;
1225 : : OnConflictExpr *result;
1226 : :
1227 : : /*
1228 : : * If this is ON CONFLICT DO SELECT/UPDATE, first create the range table
1229 : : * entry for the EXCLUDED pseudo relation, so that that will be present
1230 : : * while processing arbiter expressions. (You can't actually reference it
1231 : : * from there, but this provides a useful error message if you try.)
1232 : : */
138 dean.a.rasheed@gmail 1233 [ + + ]:GNC 1557 : if (onConflictClause->action == ONCONFLICT_UPDATE ||
1234 [ + + ]: 622 : onConflictClause->action == ONCONFLICT_SELECT)
1235 : : {
3923 andres@anarazel.de 1236 :CBC 1171 : Relation targetrel = pstate->p_target_relation;
1237 : : RangeTblEntry *exclRte;
1238 : :
2371 tgl@sss.pgh.pa.us 1239 : 1171 : exclNSItem = addRangeTableEntryForRelation(pstate,
1240 : : targetrel,
1241 : : RowExclusiveLock,
1242 : : makeAlias("excluded", NIL),
1243 : : false, false);
1244 : 1171 : exclRte = exclNSItem->p_rte;
1245 : 1171 : exclRelIndex = exclNSItem->p_rtindex;
1246 : :
1247 : : /*
1248 : : * relkind is set to composite to signal that we're not dealing with
1249 : : * an actual relation, and no permission checks are required on it.
1250 : : * (We'll check the actual target relation, instead.)
1251 : : */
3923 andres@anarazel.de 1252 : 1171 : exclRte->relkind = RELKIND_COMPOSITE_TYPE;
1253 : :
1254 : : /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
2887 tgl@sss.pgh.pa.us 1255 : 1171 : exclRelTlist = BuildOnConflictExcludedTargetlist(targetrel,
1256 : : exclRelIndex);
1257 : : }
1258 : :
1259 : : /* Process the arbiter clause, ON CONFLICT ON (...) */
1904 1260 : 1557 : transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1261 : : &arbiterWhere, &arbiterConstraint);
1262 : :
1263 : : /* Process DO SELECT/UPDATE */
138 dean.a.rasheed@gmail 1264 [ + + ]:GNC 1537 : if (onConflictClause->action == ONCONFLICT_UPDATE ||
1265 [ + + ]: 610 : onConflictClause->action == ONCONFLICT_SELECT)
1266 : : {
1267 : : /*
1268 : : * Add the EXCLUDED pseudo relation to the query namespace, making it
1269 : : * available in SET and WHERE subexpressions.
1270 : : */
2371 tgl@sss.pgh.pa.us 1271 :CBC 1163 : addNSItemToQuery(pstate, exclNSItem, false, true, true);
1272 : :
1273 : : /* Process the UPDATE SET clause */
138 dean.a.rasheed@gmail 1274 [ + + ]:GNC 1163 : if (onConflictClause->action == ONCONFLICT_UPDATE)
1275 : : onConflictSet =
90 peter@eisentraut.org 1276 : 927 : transformUpdateTargetList(pstate, onConflictClause->targetList, NULL);
1277 : :
1278 : : /* Process the SELECT/UPDATE WHERE clause */
4071 andres@anarazel.de 1279 :CBC 1143 : onConflictWhere = transformWhereClause(pstate,
1280 : : onConflictClause->whereClause,
1281 : : EXPR_KIND_WHERE, "WHERE");
1282 : :
1283 : : /*
1284 : : * Remove the EXCLUDED pseudo relation from the query namespace, since
1285 : : * it's not supposed to be available in RETURNING. (Maybe someday we
1286 : : * could allow that, and drop this step.)
1287 : : */
1904 tgl@sss.pgh.pa.us 1288 [ - + ]: 1143 : Assert((ParseNamespaceItem *) llast(pstate->p_namespace) == exclNSItem);
1289 : 1143 : pstate->p_namespace = list_delete_last(pstate->p_namespace);
1290 : : }
1291 : :
1292 : : /* Finally, build ON CONFLICT DO [NOTHING | SELECT | UPDATE] expression */
4071 andres@anarazel.de 1293 : 1517 : result = makeNode(OnConflictExpr);
1294 : :
1295 : 1517 : result->action = onConflictClause->action;
1296 : 1517 : result->arbiterElems = arbiterElems;
1297 : 1517 : result->arbiterWhere = arbiterWhere;
1298 : 1517 : result->constraint = arbiterConstraint;
138 dean.a.rasheed@gmail 1299 :GNC 1517 : result->lockStrength = onConflictClause->lockStrength;
4071 andres@anarazel.de 1300 :CBC 1517 : result->onConflictSet = onConflictSet;
1301 : 1517 : result->onConflictWhere = onConflictWhere;
1302 : 1517 : result->exclRelIndex = exclRelIndex;
1303 : 1517 : result->exclRelTlist = exclRelTlist;
1304 : :
1305 : 1517 : return result;
1306 : : }
1307 : :
1308 : : /*
1309 : : * transformForPortionOfClause
1310 : : *
1311 : : * Transforms a ForPortionOfClause in an UPDATE/DELETE statement.
1312 : : *
1313 : : * - Look up the range/period requested.
1314 : : * - Build a compatible range value from the FROM and TO expressions.
1315 : : * - Build an "overlaps" expression for filtering, used later by the
1316 : : * rewriter.
1317 : : * - For UPDATEs, build an "intersects" expression the rewriter can add
1318 : : * to the targetList to change the temporal bounds.
1319 : : */
1320 : : static ForPortionOfExpr *
90 peter@eisentraut.org 1321 :GNC 1041 : transformForPortionOfClause(ParseState *pstate,
1322 : : int rtindex,
1323 : : const ForPortionOfClause *forPortionOf,
1324 : : const Node *whereClause,
1325 : : bool isUpdate)
1326 : : {
1327 : 1041 : Relation targetrel = pstate->p_target_relation;
1328 : 1041 : int range_attno = InvalidAttrNumber;
1329 : : Form_pg_attribute attr;
1330 : : Oid attbasetype;
1331 : : Oid opclass;
1332 : : Oid opfamily;
1333 : : Oid opcintype;
1334 : 1041 : Oid funcid = InvalidOid;
1335 : : StrategyNumber strat;
1336 : : Oid opid;
1337 : : OpExpr *op;
1338 : : ForPortionOfExpr *result;
1339 : : Var *rangeVar;
1340 : :
1341 : : /* disallow FOR PORTION OF ... WHERE CURRENT OF */
1 1342 [ + + + + ]: 1041 : if (whereClause && IsA(whereClause, CurrentOfExpr))
1343 [ + - ]: 8 : ereport(ERROR,
1344 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1345 : : errmsg("WHERE CURRENT OF with FOR PORTION OF is not implemented"));
1346 : :
90 1347 : 1033 : result = makeNode(ForPortionOfExpr);
1348 : :
1349 : : /* Look up the FOR PORTION OF name requested. */
1350 : 1033 : range_attno = attnameAttNum(targetrel, forPortionOf->range_name, false);
1351 [ + + ]: 1033 : if (range_attno == InvalidAttrNumber)
1352 [ + - ]: 8 : ereport(ERROR,
1353 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
1354 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
1355 : : forPortionOf->range_name,
1356 : : RelationGetRelationName(targetrel)),
1357 : : parser_errposition(pstate, forPortionOf->location)));
1358 : 1025 : attr = TupleDescAttr(targetrel->rd_att, range_attno - 1);
1359 : :
1360 : 1025 : attbasetype = getBaseType(attr->atttypid);
1361 : :
1362 : 1025 : rangeVar = makeVar(rtindex,
1363 : : range_attno,
1364 : : attr->atttypid,
1365 : : attr->atttypmod,
1366 : : attr->attcollation,
1367 : : 0);
1368 : 1025 : rangeVar->location = forPortionOf->location;
1369 : 1025 : result->rangeVar = rangeVar;
1370 : :
1371 : : /* Require SELECT privilege on the application-time column. */
1372 : 1025 : markVarForSelectPriv(pstate, rangeVar);
1373 : :
1374 : : /*
1375 : : * Use the basetype for the target, which shouldn't be required to follow
1376 : : * domain rules. The table's column type is in the Var if we need it.
1377 : : */
1378 : 1025 : result->rangeType = attbasetype;
1379 : 1025 : result->isDomain = attbasetype != attr->atttypid;
1380 : :
1381 [ + + ]: 1025 : if (forPortionOf->target)
1382 : : {
1383 : 200 : Oid declared_target_type = attbasetype;
1384 : : Oid actual_target_type;
1385 : :
1386 : : /*
1387 : : * We were already given an expression for the target, so we don't
1388 : : * have to build anything. We still have to make sure we got the right
1389 : : * type. NULL will be caught be the executor.
1390 : : */
1391 : :
1392 : 400 : result->targetRange = transformExpr(pstate,
1393 : 200 : forPortionOf->target,
1394 : : EXPR_KIND_FOR_PORTION);
1395 : :
1396 : 200 : actual_target_type = exprType(result->targetRange);
1397 : :
1398 [ + + ]: 200 : if (!can_coerce_type(1, &actual_target_type, &declared_target_type, COERCION_IMPLICIT))
1399 [ + - ]: 32 : ereport(ERROR,
1400 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
1401 : : errmsg("could not coerce FOR PORTION OF target from %s to %s",
1402 : : format_type_be(actual_target_type),
1403 : : format_type_be(declared_target_type)),
1404 : : parser_errposition(pstate, exprLocation(forPortionOf->target))));
1405 : :
1406 : 168 : result->targetRange = coerce_type(pstate,
1407 : : result->targetRange,
1408 : : actual_target_type,
1409 : : declared_target_type,
1410 : : -1,
1411 : : COERCION_IMPLICIT,
1412 : : COERCE_IMPLICIT_CAST,
1413 : 168 : exprLocation(forPortionOf->target));
1414 : :
1415 : : /*
1416 : : * XXX: For now we only support ranges and multiranges, so we fail on
1417 : : * anything else.
1418 : : */
1419 [ + + + + ]: 168 : if (!type_is_range(attbasetype) && !type_is_multirange(attbasetype))
1420 [ + - ]: 24 : ereport(ERROR,
1421 : : (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1422 : : errmsg("column \"%s\" of relation \"%s\" is not a range or multirange type",
1423 : : forPortionOf->range_name,
1424 : : RelationGetRelationName(targetrel)),
1425 : : parser_errposition(pstate, forPortionOf->location)));
1426 : :
1427 : : }
1428 : : else
1429 : : {
1430 : : Oid rngsubtype;
1431 : : Oid declared_arg_types[2];
1432 : : Oid actual_arg_types[2];
1433 : : List *args;
1434 : :
1435 : : /*
1436 : : * Make sure it's a range column. XXX: We could support this syntax on
1437 : : * multirange columns too, if we just built a one-range multirange
1438 : : * from the FROM/TO phrases.
1439 : : */
1440 [ + + ]: 825 : if (!type_is_range(attbasetype))
1441 [ + - ]: 8 : ereport(ERROR,
1442 : : (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1443 : : errmsg("column \"%s\" of relation \"%s\" is not a range type",
1444 : : forPortionOf->range_name,
1445 : : RelationGetRelationName(targetrel)),
1446 : : parser_errposition(pstate, forPortionOf->location)));
1447 : :
1448 : 817 : rngsubtype = get_range_subtype(attbasetype);
1449 : 817 : declared_arg_types[0] = rngsubtype;
1450 : 817 : declared_arg_types[1] = rngsubtype;
1451 : :
1452 : : /*
1453 : : * Build a range from the FROM ... TO ... bounds. This should give a
1454 : : * constant result, so we accept functions like NOW() but not column
1455 : : * references, subqueries, etc.
1456 : : */
1457 : 1618 : result->targetFrom = transformExpr(pstate,
1458 : 817 : forPortionOf->target_start,
1459 : : EXPR_KIND_FOR_PORTION);
1460 : 1602 : result->targetTo = transformExpr(pstate,
1461 : 801 : forPortionOf->target_end,
1462 : : EXPR_KIND_FOR_PORTION);
1463 : 801 : actual_arg_types[0] = exprType(result->targetFrom);
1464 : 801 : actual_arg_types[1] = exprType(result->targetTo);
1465 : 801 : args = list_make2(copyObject(result->targetFrom),
1466 : : copyObject(result->targetTo));
1467 : :
1468 : : /*
1469 : : * Check the bound types separately, for better error message and
1470 : : * location
1471 : : */
1472 [ + + ]: 801 : if (!can_coerce_type(1, actual_arg_types, declared_arg_types, COERCION_IMPLICIT))
1473 [ + - ]: 8 : ereport(ERROR,
1474 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
1475 : : errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1476 : : "FROM",
1477 : : format_type_be(actual_arg_types[0]),
1478 : : format_type_be(declared_arg_types[0])),
1479 : : parser_errposition(pstate, exprLocation(forPortionOf->target_start))));
1480 [ + + ]: 793 : if (!can_coerce_type(1, &actual_arg_types[1], &declared_arg_types[1], COERCION_IMPLICIT))
1481 [ + - ]: 8 : ereport(ERROR,
1482 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
1483 : : errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1484 : : "TO",
1485 : : format_type_be(actual_arg_types[1]),
1486 : : format_type_be(declared_arg_types[1])),
1487 : : parser_errposition(pstate, exprLocation(forPortionOf->target_end))));
1488 : :
1489 : 785 : make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types);
1490 : 785 : result->targetRange = (Node *) makeFuncExpr(get_range_constructor2(attbasetype),
1491 : : attbasetype,
1492 : : args,
1493 : : InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
1494 : : }
1495 : :
1496 : : /*
1497 : : * Build overlapsExpr to use as an extra qual. This means we only hit rows
1498 : : * matching the FROM & TO bounds. We must look up the overlaps operator
1499 : : * (usually "&&").
1500 : : */
1501 : 929 : opclass = GetDefaultOpClass(attr->atttypid, GIST_AM_OID);
1502 [ - + ]: 929 : if (!OidIsValid(opclass))
90 peter@eisentraut.org 1503 [ # # ]:UNC 0 : ereport(ERROR,
1504 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1505 : : errmsg("data type %s has no default operator class for access method \"%s\"",
1506 : : format_type_be(attr->atttypid), "gist"),
1507 : : errhint("You must define a default operator class for the data type.")));
1508 : :
1509 : : /* Look up the operators and functions we need. */
90 peter@eisentraut.org 1510 :GNC 929 : GetOperatorFromCompareType(opclass, InvalidOid, COMPARE_OVERLAP, &opid, &strat);
1511 : 929 : op = makeNode(OpExpr);
1512 : 929 : op->opno = opid;
1513 : 929 : op->opfuncid = get_opcode(opid);
1514 : 929 : op->opresulttype = BOOLOID;
1515 : 929 : op->args = list_make2(copyObject(rangeVar), copyObject(result->targetRange));
1516 : 929 : result->overlapsExpr = (Node *) op;
1517 : :
1518 : : /*
1519 : : * Look up the without_portion func. This computes the bounds of temporal
1520 : : * leftovers.
1521 : : *
1522 : : * XXX: Find a more extensible way to look up the function, permitting
1523 : : * user-defined types. An opclass support function doesn't make sense,
1524 : : * since there is no index involved. Perhaps a type support function.
1525 : : */
1526 [ + - ]: 929 : if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
1527 [ + + - ]: 929 : switch (opcintype)
1528 : : {
1529 : 841 : case ANYRANGEOID:
1530 : 841 : result->withoutPortionProc = F_RANGE_MINUS_MULTI;
1531 : 841 : break;
1532 : 88 : case ANYMULTIRANGEOID:
1533 : 88 : result->withoutPortionProc = F_MULTIRANGE_MINUS_MULTI;
1534 : 88 : break;
90 peter@eisentraut.org 1535 :UNC 0 : default:
1536 [ # # ]: 0 : elog(ERROR, "unexpected opcintype: %u", opcintype);
1537 : : }
1538 : : else
1539 [ # # ]: 0 : elog(ERROR, "unexpected opclass: %u", opclass);
1540 : :
90 peter@eisentraut.org 1541 [ + + ]:GNC 929 : if (isUpdate)
1542 : : {
1543 : : /*
1544 : : * Now make sure we update the start/end time of the record. For a
1545 : : * range col (r) this is `r = r * targetRange` (where * is the
1546 : : * intersect operator).
1547 : : */
1548 : : Oid intersectoperoid;
1549 : : List *funcArgs;
1550 : : Node *rangeTLEExpr;
1551 : : TargetEntry *tle;
26 1552 : 530 : RTEPermissionInfo *target_perminfo = pstate->p_target_nsitem->p_perminfo;
1553 : :
1554 : : /*
1555 : : * Whatever operator is used for intersect by temporal foreign keys,
1556 : : * we can use its backing procedure for intersects in FOR PORTION OF.
1557 : : * XXX: Share code with FindFKPeriodOpers?
1558 : : */
90 1559 [ + + - ]: 530 : switch (opcintype)
1560 : : {
1561 : 482 : case ANYRANGEOID:
1562 : 482 : intersectoperoid = OID_RANGE_INTERSECT_RANGE_OP;
1563 : 482 : break;
1564 : 48 : case ANYMULTIRANGEOID:
1565 : 48 : intersectoperoid = OID_MULTIRANGE_INTERSECT_MULTIRANGE_OP;
1566 : 48 : break;
90 peter@eisentraut.org 1567 :UNC 0 : default:
1568 [ # # ]: 0 : elog(ERROR, "unexpected opcintype: %u", opcintype);
1569 : : }
90 peter@eisentraut.org 1570 :GNC 530 : funcid = get_opcode(intersectoperoid);
1571 [ - + ]: 530 : if (!OidIsValid(funcid))
90 peter@eisentraut.org 1572 [ # # ]:UNC 0 : ereport(ERROR,
1573 : : errcode(ERRCODE_UNDEFINED_OBJECT),
1574 : : errmsg("could not identify an intersect function for type %s",
1575 : : format_type_be(opcintype)));
1576 : :
90 peter@eisentraut.org 1577 :GNC 530 : funcArgs = list_make2(copyObject(rangeVar),
1578 : : copyObject(result->targetRange));
1579 : 530 : rangeTLEExpr = (Node *) makeFuncExpr(funcid, attbasetype, funcArgs,
1580 : : InvalidOid, InvalidOid,
1581 : : COERCE_EXPLICIT_CALL);
1582 : :
1583 : : /*
1584 : : * Coerce to domain if necessary. If we skip this, we will allow
1585 : : * updating to forbidden values.
1586 : : */
1587 : 530 : rangeTLEExpr = coerce_type(pstate,
1588 : : rangeTLEExpr,
1589 : : attbasetype,
1590 : : attr->atttypid,
1591 : : -1,
1592 : : COERCION_IMPLICIT,
1593 : : COERCE_IMPLICIT_CAST,
1594 : 530 : exprLocation(forPortionOf->target));
1595 : :
1596 : : /* Make a TLE to set the range column */
1597 : 530 : result->rangeTargetList = NIL;
1598 : 530 : tle = makeTargetEntry((Expr *) rangeTLEExpr, range_attno,
1599 : 530 : forPortionOf->range_name, false);
1600 : 530 : result->rangeTargetList = lappend(result->rangeTargetList, tle);
1601 : :
1602 : : /* Mark the range column as requiring update permissions */
26 1603 : 530 : target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
1604 : : range_attno - FirstLowInvalidHeapAttributeNumber);
1605 : : }
1606 : : else
90 1607 : 399 : result->rangeTargetList = NIL;
1608 : :
1609 : 929 : result->range_name = forPortionOf->range_name;
1610 : 929 : result->location = forPortionOf->location;
1611 : 929 : result->targetLocation = forPortionOf->target_location;
1612 : :
1613 : 929 : return result;
1614 : : }
1615 : :
1616 : : /*
1617 : : * BuildOnConflictExcludedTargetlist
1618 : : * Create target list for the EXCLUDED pseudo-relation of ON CONFLICT,
1619 : : * representing the columns of targetrel with varno exclRelIndex.
1620 : : *
1621 : : * Note: Exported for use in the rewriter.
1622 : : */
1623 : : List *
2887 tgl@sss.pgh.pa.us 1624 :CBC 1319 : BuildOnConflictExcludedTargetlist(Relation targetrel,
1625 : : Index exclRelIndex)
1626 : : {
1627 : 1319 : List *result = NIL;
1628 : : int attno;
1629 : : Var *var;
1630 : : TargetEntry *te;
1631 : :
1632 : : /*
1633 : : * Note that resnos of the tlist must correspond to attnos of the
1634 : : * underlying relation, hence we need entries for dropped columns too.
1635 : : */
1636 [ + + ]: 4727 : for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1637 : : {
1638 : 3408 : Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1639 : : char *name;
1640 : :
1641 [ + + ]: 3408 : if (attr->attisdropped)
1642 : : {
1643 : : /*
1644 : : * can't use atttypid here, but it doesn't really matter what type
1645 : : * the Const claims to be.
1646 : : */
1647 : 74 : var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
2842 1648 : 74 : name = NULL;
1649 : : }
1650 : : else
1651 : : {
2887 1652 : 3334 : var = makeVar(exclRelIndex, attno + 1,
1653 : : attr->atttypid, attr->atttypmod,
1654 : : attr->attcollation,
1655 : : 0);
1656 : 3334 : name = pstrdup(NameStr(attr->attname));
1657 : : }
1658 : :
1659 : 3408 : te = makeTargetEntry((Expr *) var,
1660 : 3408 : attno + 1,
1661 : : name,
1662 : : false);
1663 : :
1664 : 3408 : result = lappend(result, te);
1665 : : }
1666 : :
1667 : : /*
1668 : : * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1669 : : * the other entries in the EXCLUDED tlist, its resno must match the Var's
1670 : : * varattno, else the wrong things happen while resolving references in
1671 : : * setrefs.c. This is against normal conventions for targetlists, but
1672 : : * it's okay since we don't use this as a real tlist.
1673 : : */
1674 : 1319 : var = makeVar(exclRelIndex, InvalidAttrNumber,
1675 : 1319 : targetrel->rd_rel->reltype,
1676 : : -1, InvalidOid, 0);
1677 : 1319 : te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1678 : 1319 : result = lappend(result, te);
1679 : :
1680 : 1319 : return result;
1681 : : }
1682 : :
1683 : :
1684 : : /*
1685 : : * count_rowexpr_columns -
1686 : : * get number of columns contained in a ROW() expression;
1687 : : * return -1 if expression isn't a RowExpr or a Var referencing one.
1688 : : *
1689 : : * This is currently used only for hint purposes, so we aren't terribly
1690 : : * tense about recognizing all possible cases. The Var case is interesting
1691 : : * because that's what we'll get in the INSERT ... SELECT (...) case.
1692 : : */
1693 : : static int
5764 tgl@sss.pgh.pa.us 1694 :UBC 0 : count_rowexpr_columns(ParseState *pstate, Node *expr)
1695 : : {
1696 [ # # ]: 0 : if (expr == NULL)
1697 : 0 : return -1;
1698 [ # # ]: 0 : if (IsA(expr, RowExpr))
1699 : 0 : return list_length(((RowExpr *) expr)->args);
1700 [ # # ]: 0 : if (IsA(expr, Var))
1701 : : {
1702 : 0 : Var *var = (Var *) expr;
1703 : 0 : AttrNumber attnum = var->varattno;
1704 : :
1705 [ # # # # ]: 0 : if (attnum > 0 && var->vartype == RECORDOID)
1706 : : {
1707 : : RangeTblEntry *rte;
1708 : :
1709 : 0 : rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1710 [ # # ]: 0 : if (rte->rtekind == RTE_SUBQUERY)
1711 : : {
1712 : : /* Subselect-in-FROM: examine sub-select's output expr */
1713 : 0 : TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1714 : : attnum);
1715 : :
1716 [ # # # # ]: 0 : if (ste == NULL || ste->resjunk)
1717 : 0 : return -1;
1718 : 0 : expr = (Node *) ste->expr;
1719 [ # # ]: 0 : if (IsA(expr, RowExpr))
1720 : 0 : return list_length(((RowExpr *) expr)->args);
1721 : : }
1722 : : }
1723 : : }
1724 : 0 : return -1;
1725 : : }
1726 : :
1727 : :
1728 : : /*
1729 : : * transformSelectStmt -
1730 : : * transforms a Select Statement
1731 : : *
1732 : : * This function is also used to transform the source expression of a
1733 : : * PLAssignStmt. In that usage, passthru is non-NULL and we need to
1734 : : * call transformPLAssignStmtTarget after the initial transformation of the
1735 : : * SELECT's targetlist. (We could generalize this into an arbitrary callback
1736 : : * function, but for now that would just be more notation with no benefit.)
1737 : : * All the rest is the same as a regular SelectStmt.
1738 : : *
1739 : : * Note: this covers only cases with no set operations and no VALUES lists;
1740 : : * see below for the other cases.
1741 : : */
1742 : : static Query *
276 tgl@sss.pgh.pa.us 1743 :GNC 301655 : transformSelectStmt(ParseState *pstate, SelectStmt *stmt,
1744 : : SelectStmtPassthrough *passthru)
1745 : : {
10522 bruce@momjian.us 1746 :CBC 301655 : Query *qry = makeNode(Query);
1747 : : Node *qual;
1748 : : ListCell *l;
1749 : :
10523 1750 : 301655 : qry->commandType = CMD_SELECT;
1751 : :
1752 : : /* process the WITH clause independently of all else */
6478 tgl@sss.pgh.pa.us 1753 [ + + ]: 301655 : if (stmt->withClause)
1754 : : {
1755 : 1652 : qry->hasRecursive = stmt->withClause->recursive;
1756 : 1652 : qry->cteList = transformWithClause(pstate, stmt->withClause);
5604 1757 : 1455 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1758 : : }
1759 : :
1760 : : /* Complain if we get called from someplace where INTO is not allowed */
5216 1761 [ + + ]: 301458 : if (stmt->intoClause)
1762 [ + - ]: 12 : ereport(ERROR,
1763 : : (errcode(ERRCODE_SYNTAX_ERROR),
1764 : : errmsg("SELECT ... INTO is not allowed here"),
1765 : : parser_errposition(pstate,
1766 : : exprLocation((Node *) stmt->intoClause))));
1767 : :
1768 : : /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
5982 1769 : 301446 : pstate->p_locking_clause = stmt->lockingClause;
1770 : :
1771 : : /* make WINDOW info available for window functions, too */
1772 : 301446 : pstate->p_windowdefs = stmt->windowClause;
1773 : :
1774 : : /* process the FROM clause */
9267 1775 : 301446 : transformFromClause(pstate, stmt->fromClause);
1776 : :
1777 : : /* transform targetlist */
5072 1778 : 300958 : qry->targetList = transformTargetList(pstate, stmt->targetList,
1779 : : EXPR_KIND_SELECT_TARGET);
1780 : :
1781 : : /*
1782 : : * If we're within a PLAssignStmt, do further transformation of the
1783 : : * targetlist; that has to happen before we consider sorting or grouping.
1784 : : * Otherwise, mark column origins (which are useless in a PLAssignStmt).
1785 : : */
276 tgl@sss.pgh.pa.us 1786 [ + + ]:GNC 297289 : if (passthru)
1787 : 3369 : qry->targetList = transformPLAssignStmtTarget(pstate, qry->targetList,
1788 : : passthru);
1789 : : else
1790 : 293920 : markTargetListOrigins(pstate, qry->targetList);
1791 : :
1792 : : /* transform WHERE */
5072 tgl@sss.pgh.pa.us 1793 :CBC 297282 : qual = transformWhereClause(pstate, stmt->whereClause,
1794 : : EXPR_KIND_WHERE, "WHERE");
1795 : :
1796 : : /* initial processing of HAVING clause is much like WHERE clause */
8398 1797 : 297211 : qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1798 : : EXPR_KIND_HAVING, "HAVING");
1799 : :
1800 : : /*
1801 : : * Transform sorting/grouping stuff. Do ORDER BY first because both
1802 : : * transformGroupClause and transformDistinctClause need the results. Note
1803 : : * that these functions can also change the targetList, so it's passed to
1804 : : * them by reference.
1805 : : */
10523 bruce@momjian.us 1806 : 297207 : qry->sortClause = transformSortClause(pstate,
1807 : : stmt->sortClause,
1808 : : &qry->targetList,
1809 : : EXPR_KIND_ORDER_BY,
1810 : : false /* allow SQL92 rules */ );
1811 : :
8416 tgl@sss.pgh.pa.us 1812 : 594358 : qry->groupClause = transformGroupClause(pstate,
1813 : : stmt->groupClause,
274 tgl@sss.pgh.pa.us 1814 :GNC 297187 : stmt->groupByAll,
1815 : : &qry->groupingSets,
1816 : : &qry->targetList,
1817 : : qry->sortClause,
1818 : : EXPR_KIND_GROUP_BY,
1819 : : false /* allow SQL92 rules */ );
1930 tomas.vondra@postgre 1820 :CBC 297171 : qry->groupDistinct = stmt->groupDistinct;
274 tgl@sss.pgh.pa.us 1821 :GNC 297171 : qry->groupByAll = stmt->groupByAll;
1822 : :
6541 tgl@sss.pgh.pa.us 1823 [ + + ]:CBC 297171 : if (stmt->distinctClause == NIL)
1824 : : {
1825 : 294734 : qry->distinctClause = NIL;
1826 : 294734 : qry->hasDistinctOn = false;
1827 : : }
1828 [ + + ]: 2437 : else if (linitial(stmt->distinctClause) == NULL)
1829 : : {
1830 : : /* We had SELECT DISTINCT */
1831 : 2269 : qry->distinctClause = transformDistinctClause(pstate,
1832 : : &qry->targetList,
1833 : : qry->sortClause,
1834 : : false);
1835 : 2269 : qry->hasDistinctOn = false;
1836 : : }
1837 : : else
1838 : : {
1839 : : /* We had SELECT DISTINCT ON */
1840 : 168 : qry->distinctClause = transformDistinctOnClause(pstate,
1841 : : stmt->distinctClause,
1842 : : &qry->targetList,
1843 : : qry->sortClause);
1844 : 160 : qry->hasDistinctOn = true;
1845 : : }
1846 : :
1847 : : /* transform LIMIT */
8398 1848 : 297163 : qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1849 : : EXPR_KIND_OFFSET, "OFFSET",
1850 : : stmt->limitOption);
1851 : 297163 : qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1852 : : EXPR_KIND_LIMIT, "LIMIT",
1853 : : stmt->limitOption);
2275 alvherre@alvh.no-ip. 1854 : 297155 : qry->limitOption = stmt->limitOption;
1855 : :
1856 : : /* transform window clauses after we have seen all window functions */
6393 tgl@sss.pgh.pa.us 1857 : 297155 : qry->windowClause = transformWindowDefinitions(pstate,
1858 : : pstate->p_windowdefs,
1859 : : &qry->targetList);
1860 : :
1861 : : /* resolve any still-unresolved output columns as being type text */
3443 1862 [ + + ]: 297111 : if (pstate->p_resolve_unknowns)
1863 : 270338 : resolveTargetListUnknowns(pstate, qry->targetList);
1864 : :
8565 1865 : 297111 : qry->rtable = pstate->p_rtable;
1302 alvherre@alvh.no-ip. 1866 : 297111 : qry->rteperminfos = pstate->p_rteperminfos;
8565 tgl@sss.pgh.pa.us 1867 : 297111 : qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1868 : :
9843 1869 : 297111 : qry->hasSubLinks = pstate->p_hasSubLinks;
6393 1870 : 297111 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
3577 1871 : 297111 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
5256 1872 : 297111 : qry->hasAggs = pstate->p_hasAggs;
1873 : :
7366 1874 [ + + + + : 302090 : foreach(l, stmt->lockingClause)
+ + ]
1875 : : {
6089 1876 : 5007 : transformLockingClause(pstate, qry,
1877 : 5007 : (LockingClause *) lfirst(l), false);
1878 : : }
1879 : :
5582 1880 : 297083 : assign_query_collations(pstate, qry);
1881 : :
1882 : : /* this must be done after collations, for reliable comparison of exprs */
2721 rhodiumtoad@postgres 1883 [ + + + + : 297055 : if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
+ + + + ]
1884 : 27596 : parseCheckAggregates(pstate, qry);
1885 : :
9399 tgl@sss.pgh.pa.us 1886 : 296979 : return qry;
1887 : : }
1888 : :
1889 : : /*
1890 : : * transformValuesClause -
1891 : : * transforms a VALUES clause that's being used as a standalone SELECT
1892 : : *
1893 : : * We build a Query containing a VALUES RTE, rather as if one had written
1894 : : * SELECT * FROM (VALUES ...) AS "*VALUES*"
1895 : : */
1896 : : static Query *
7272 mail@joeconway.com 1897 : 5806 : transformValuesClause(ParseState *pstate, SelectStmt *stmt)
1898 : : {
1899 : 5806 : Query *qry = makeNode(Query);
1513 tgl@sss.pgh.pa.us 1900 : 5806 : List *exprsLists = NIL;
3491 1901 : 5806 : List *coltypes = NIL;
1902 : 5806 : List *coltypmods = NIL;
1903 : 5806 : List *colcollations = NIL;
6515 1904 : 5806 : List **colexprs = NULL;
7272 mail@joeconway.com 1905 : 5806 : int sublist_length = -1;
5063 tgl@sss.pgh.pa.us 1906 : 5806 : bool lateral = false;
1907 : : ParseNamespaceItem *nsitem;
1908 : : ListCell *lc;
1909 : : ListCell *lc2;
1910 : : int i;
1911 : :
7272 mail@joeconway.com 1912 : 5806 : qry->commandType = CMD_SELECT;
1913 : :
1914 : : /* Most SELECT stuff doesn't apply in a VALUES clause */
1915 [ - + ]: 5806 : Assert(stmt->distinctClause == NIL);
5216 tgl@sss.pgh.pa.us 1916 [ - + ]: 5806 : Assert(stmt->intoClause == NULL);
7272 mail@joeconway.com 1917 [ - + ]: 5806 : Assert(stmt->targetList == NIL);
1918 [ - + ]: 5806 : Assert(stmt->fromClause == NIL);
1919 [ - + ]: 5806 : Assert(stmt->whereClause == NULL);
1920 [ - + ]: 5806 : Assert(stmt->groupClause == NIL);
1921 [ - + ]: 5806 : Assert(stmt->havingClause == NULL);
6393 tgl@sss.pgh.pa.us 1922 [ - + ]: 5806 : Assert(stmt->windowClause == NIL);
7272 mail@joeconway.com 1923 [ - + ]: 5806 : Assert(stmt->op == SETOP_NONE);
1924 : :
1925 : : /* process the WITH clause independently of all else */
6478 tgl@sss.pgh.pa.us 1926 [ + + ]: 5806 : if (stmt->withClause)
1927 : : {
1928 : 40 : qry->hasRecursive = stmt->withClause->recursive;
1929 : 40 : qry->cteList = transformWithClause(pstate, stmt->withClause);
5604 1930 : 36 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1931 : : }
1932 : :
1933 : : /*
1934 : : * For each row of VALUES, transform the raw expressions.
1935 : : *
1936 : : * Note that the intermediate representation we build is column-organized
1937 : : * not row-organized. That simplifies the type and collation processing
1938 : : * below.
1939 : : */
7272 mail@joeconway.com 1940 [ + - + + : 21588 : foreach(lc, stmt->valuesLists)
+ + ]
1941 : : {
7209 bruce@momjian.us 1942 : 15791 : List *sublist = (List *) lfirst(lc);
1943 : :
1944 : : /*
1945 : : * Do basic expression transformation (same as a ROW() expr, but here
1946 : : * we disallow SetToDefault)
1947 : : */
3507 tgl@sss.pgh.pa.us 1948 : 15791 : sublist = transformExpressionList(pstate, sublist,
1949 : : EXPR_KIND_VALUES, false);
1950 : :
1951 : : /*
1952 : : * All the sublists must be the same length, *after* transformation
1953 : : * (which might expand '*' into multiple items). The VALUES RTE can't
1954 : : * handle anything different.
1955 : : */
7272 mail@joeconway.com 1956 [ + + ]: 15786 : if (sublist_length < 0)
1957 : : {
1958 : : /* Remember post-transformation length of first sublist */
1959 : 5797 : sublist_length = list_length(sublist);
1960 : : /* and allocate array for per-column lists */
6515 tgl@sss.pgh.pa.us 1961 : 5797 : colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1962 : : }
7272 mail@joeconway.com 1963 [ - + ]: 9989 : else if (sublist_length != list_length(sublist))
1964 : : {
7272 mail@joeconway.com 1965 [ # # ]:UBC 0 : ereport(ERROR,
1966 : : (errcode(ERRCODE_SYNTAX_ERROR),
1967 : : errmsg("VALUES lists must all be the same length"),
1968 : : parser_errposition(pstate,
1969 : : exprLocation((Node *) sublist))));
1970 : : }
1971 : :
1972 : : /* Build per-column expression lists */
7272 mail@joeconway.com 1973 :CBC 15786 : i = 0;
1974 [ + + + + : 37701 : foreach(lc2, sublist)
+ + ]
1975 : : {
7209 bruce@momjian.us 1976 : 21915 : Node *col = (Node *) lfirst(lc2);
1977 : :
6515 tgl@sss.pgh.pa.us 1978 : 21915 : colexprs[i] = lappend(colexprs[i], col);
7272 mail@joeconway.com 1979 : 21915 : i++;
1980 : : }
1981 : :
1982 : : /* Release sub-list's cells to save memory */
5552 tgl@sss.pgh.pa.us 1983 : 15786 : list_free(sublist);
1984 : :
1985 : : /* Prepare an exprsLists element for this row */
1513 1986 : 15786 : exprsLists = lappend(exprsLists, NIL);
1987 : : }
1988 : :
1989 : : /*
1990 : : * Now resolve the common types of the columns, and coerce everything to
1991 : : * those types. Then identify the common typmod and common collation, if
1992 : : * any, of each column.
1993 : : *
1994 : : * We must do collation processing now because (1) assign_query_collations
1995 : : * doesn't process rangetable entries, and (2) we need to label the VALUES
1996 : : * RTE with column collations for use in the outer query. We don't
1997 : : * consider conflict of implicit collations to be an error here; instead
1998 : : * the column will just show InvalidOid as its collation, and you'll get a
1999 : : * failure later if that results in failure to resolve a collation.
2000 : : *
2001 : : * Note we modify the per-column expression lists in-place.
2002 : : */
7272 mail@joeconway.com 2003 [ + + ]: 13362 : for (i = 0; i < sublist_length; i++)
2004 : : {
2005 : : Oid coltype;
2006 : : int32 coltypmod;
2007 : : Oid colcoll;
2008 : :
5552 tgl@sss.pgh.pa.us 2009 : 7565 : coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
2010 : :
2011 [ + - + + : 29480 : foreach(lc, colexprs[i])
+ + ]
2012 : : {
2013 : 21915 : Node *col = (Node *) lfirst(lc);
2014 : :
2015 : 21915 : col = coerce_to_common_type(pstate, col, coltype, "VALUES");
579 peter@eisentraut.org 2016 : 21915 : lfirst(lc) = col;
2017 : : }
2018 : :
2072 2019 : 7565 : coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
5552 tgl@sss.pgh.pa.us 2020 : 7565 : colcoll = select_common_collation(pstate, colexprs[i], true);
2021 : :
3491 2022 : 7565 : coltypes = lappend_oid(coltypes, coltype);
2023 : 7565 : coltypmods = lappend_int(coltypmods, coltypmod);
2024 : 7565 : colcollations = lappend_oid(colcollations, colcoll);
2025 : : }
2026 : :
2027 : : /*
2028 : : * Finally, rearrange the coerced expressions into row-organized lists.
2029 : : */
1513 2030 [ + + ]: 13362 : for (i = 0; i < sublist_length; i++)
2031 : : {
5552 2032 [ + - + + : 29480 : forboth(lc, colexprs[i], lc2, exprsLists)
+ - + + +
+ + - +
+ ]
2033 : : {
2034 : 21915 : Node *col = (Node *) lfirst(lc);
2035 : 21915 : List *sublist = lfirst(lc2);
2036 : :
2082 peter@eisentraut.org 2037 : 21915 : sublist = lappend(sublist, col);
1513 tgl@sss.pgh.pa.us 2038 : 21915 : lfirst(lc2) = sublist;
2039 : : }
5552 2040 : 7565 : list_free(colexprs[i]);
2041 : : }
2042 : :
2043 : : /*
2044 : : * Ordinarily there can't be any current-level Vars in the expression
2045 : : * lists, because the namespace was empty ... but if we're inside CREATE
2046 : : * RULE, then NEW/OLD references might appear. In that case we have to
2047 : : * mark the VALUES RTE as LATERAL.
2048 : : */
5063 2049 [ + + + - ]: 5802 : if (pstate->p_rtable != NIL &&
2050 : 5 : contain_vars_of_level((Node *) exprsLists, 0))
2051 : 5 : lateral = true;
2052 : :
2053 : : /*
2054 : : * Generate the VALUES RTE
2055 : : */
2371 2056 : 5797 : nsitem = addRangeTableEntryForValues(pstate, exprsLists,
2057 : : coltypes, coltypmods, colcollations,
2058 : : NULL, lateral, true);
2059 : 5797 : addNSItemToQuery(pstate, nsitem, true, true, true);
2060 : :
2061 : : /*
2062 : : * Generate a targetlist as though expanding "*"
2063 : : */
7272 mail@joeconway.com 2064 [ - + ]: 5797 : Assert(pstate->p_next_resno == 1);
1555 alvherre@alvh.no-ip. 2065 : 5797 : qry->targetList = expandNSItemAttrs(pstate, nsitem, 0, true, -1);
2066 : :
2067 : : /*
2068 : : * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
2069 : : * VALUES, so cope.
2070 : : */
7272 mail@joeconway.com 2071 : 5797 : qry->sortClause = transformSortClause(pstate,
2072 : : stmt->sortClause,
2073 : : &qry->targetList,
2074 : : EXPR_KIND_ORDER_BY,
2075 : : false /* allow SQL92 rules */ );
2076 : :
2077 : 5797 : qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
2078 : : EXPR_KIND_OFFSET, "OFFSET",
2079 : : stmt->limitOption);
2080 : 5797 : qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
2081 : : EXPR_KIND_LIMIT, "LIMIT",
2082 : : stmt->limitOption);
2275 alvherre@alvh.no-ip. 2083 : 5797 : qry->limitOption = stmt->limitOption;
2084 : :
7272 mail@joeconway.com 2085 [ - + ]: 5797 : if (stmt->lockingClause)
7272 mail@joeconway.com 2086 [ # # ]:UBC 0 : ereport(ERROR,
2087 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2088 : : /*------
2089 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
2090 : : errmsg("%s cannot be applied to VALUES",
2091 : : LCS_asString(((LockingClause *)
2092 : : linitial(stmt->lockingClause))->strength))));
2093 : :
7272 mail@joeconway.com 2094 :CBC 5797 : qry->rtable = pstate->p_rtable;
1302 alvherre@alvh.no-ip. 2095 : 5797 : qry->rteperminfos = pstate->p_rteperminfos;
7272 mail@joeconway.com 2096 : 5797 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2097 : :
2098 : 5797 : qry->hasSubLinks = pstate->p_hasSubLinks;
2099 : :
5582 tgl@sss.pgh.pa.us 2100 : 5797 : assign_query_collations(pstate, qry);
2101 : :
7272 mail@joeconway.com 2102 : 5797 : return qry;
2103 : : }
2104 : :
2105 : : /*
2106 : : * transformSetOperationStmt -
2107 : : * transforms a set-operations tree
2108 : : *
2109 : : * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
2110 : : * structure to it. We must transform each leaf SELECT and build up a top-
2111 : : * level Query that contains the leaf SELECTs as subqueries in its rangetable.
2112 : : * The tree of set operations is converted into the setOperations field of
2113 : : * the top-level Query.
2114 : : */
2115 : : static Query *
9014 bruce@momjian.us 2116 : 8435 : transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
2117 : : {
9399 tgl@sss.pgh.pa.us 2118 : 8435 : Query *qry = makeNode(Query);
2119 : : SelectStmt *leftmostSelect;
2120 : : int leftmostRTI;
2121 : : Query *leftmostQuery;
2122 : : SetOperationStmt *sostmt;
2123 : : List *sortClause;
2124 : : Node *limitOffset;
2125 : : Node *limitCount;
2126 : : List *lockingClause;
2127 : : WithClause *withClause;
2128 : : Node *node;
2129 : : ListCell *left_tlist,
2130 : : *lct,
2131 : : *lcm,
2132 : : *lcc,
2133 : : *l;
2134 : : List *targetvars,
2135 : : *targetnames,
2136 : : *sv_namespace;
2137 : : int sv_rtable_length;
2138 : : ParseNamespaceItem *jnsitem;
2139 : : ParseNamespaceColumn *sortnscolumns;
2140 : : int sortcolindex;
2141 : : int tllen;
2142 : :
2143 : 8435 : qry->commandType = CMD_SELECT;
2144 : :
2145 : : /*
2146 : : * Find leftmost leaf SelectStmt. We currently only need to do this in
2147 : : * order to deliver a suitable error message if there's an INTO clause
2148 : : * there, implying the set-op tree is in a context that doesn't allow
2149 : : * INTO. (transformSetOperationTree would throw error anyway, but it
2150 : : * seems worth the trouble to throw a different error for non-leftmost
2151 : : * INTO, so we produce that error in transformSetOperationTree.)
2152 : : */
9368 2153 : 8435 : leftmostSelect = stmt->larg;
2154 [ + - + + ]: 12707 : while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
2155 : 4272 : leftmostSelect = leftmostSelect->larg;
2156 [ + - + - : 8435 : Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
- + ]
2157 : : leftmostSelect->larg == NULL);
5216 2158 [ - + ]: 8435 : if (leftmostSelect->intoClause)
5216 tgl@sss.pgh.pa.us 2159 [ # # ]:UBC 0 : ereport(ERROR,
2160 : : (errcode(ERRCODE_SYNTAX_ERROR),
2161 : : errmsg("SELECT ... INTO is not allowed here"),
2162 : : parser_errposition(pstate,
2163 : : exprLocation((Node *) leftmostSelect->intoClause))));
2164 : :
2165 : : /*
2166 : : * We need to extract ORDER BY and other top-level clauses here and not
2167 : : * let transformSetOperationTree() see them --- else it'll just recurse
2168 : : * right back here!
2169 : : */
9368 tgl@sss.pgh.pa.us 2170 :CBC 8435 : sortClause = stmt->sortClause;
2171 : 8435 : limitOffset = stmt->limitOffset;
2172 : 8435 : limitCount = stmt->limitCount;
7638 2173 : 8435 : lockingClause = stmt->lockingClause;
5082 2174 : 8435 : withClause = stmt->withClause;
2175 : :
9368 2176 : 8435 : stmt->sortClause = NIL;
2177 : 8435 : stmt->limitOffset = NULL;
2178 : 8435 : stmt->limitCount = NULL;
7366 2179 : 8435 : stmt->lockingClause = NIL;
5082 2180 : 8435 : stmt->withClause = NULL;
2181 : :
2182 : : /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
7638 2183 [ + + ]: 8435 : if (lockingClause)
8382 2184 [ + - ]: 4 : ereport(ERROR,
2185 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2186 : : /*------
2187 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
2188 : : errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2189 : : LCS_asString(((LockingClause *)
2190 : : linitial(lockingClause))->strength))));
2191 : :
2192 : : /* Process the WITH clause independently of all else */
5082 2193 [ + + ]: 8431 : if (withClause)
2194 : : {
2195 : 170 : qry->hasRecursive = withClause->recursive;
2196 : 170 : qry->cteList = transformWithClause(pstate, withClause);
2197 : 170 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2198 : : }
2199 : :
2200 : : /*
2201 : : * Recursively transform the components of the tree.
2202 : : */
3416 peter_e@gmx.net 2203 : 8431 : sostmt = castNode(SetOperationStmt,
2204 : : transformSetOperationTree(pstate, stmt, true, NULL));
2205 [ - + ]: 8383 : Assert(sostmt);
9368 tgl@sss.pgh.pa.us 2206 : 8383 : qry->setOperations = (Node *) sostmt;
2207 : :
2208 : : /*
2209 : : * Re-find leftmost SELECT (now it's a sub-query in rangetable)
2210 : : */
2211 : 8383 : node = sostmt->larg;
9399 2212 [ + - + + ]: 12643 : while (node && IsA(node, SetOperationStmt))
2213 : 4260 : node = ((SetOperationStmt *) node)->larg;
2214 [ + - - + ]: 8383 : Assert(node && IsA(node, RangeTblRef));
9368 2215 : 8383 : leftmostRTI = ((RangeTblRef *) node)->rtindex;
2216 : 8383 : leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
9399 2217 [ - + ]: 8383 : Assert(leftmostQuery != NULL);
2218 : :
2219 : : /*
2220 : : * Generate dummy targetlist for outer query using column names of
2221 : : * leftmost select and common datatypes/collations of topmost set
2222 : : * operation. Also make lists of the dummy vars and their names for use
2223 : : * in parsing ORDER BY.
2224 : : *
2225 : : * Note: we use leftmostRTI as the varno of the dummy variables. It
2226 : : * shouldn't matter too much which RT index they have, as long as they
2227 : : * have one that corresponds to a real RT entry; else funny things may
2228 : : * happen when the tree is mashed by rule rewriting.
2229 : : */
2230 : 8383 : qry->targetList = NIL;
8829 2231 : 8383 : targetvars = NIL;
9266 2232 : 8383 : targetnames = NIL;
2233 : : sortnscolumns = (ParseNamespaceColumn *)
2371 2234 : 8383 : palloc0(list_length(sostmt->colTypes) * sizeof(ParseNamespaceColumn));
2235 : 8383 : sortcolindex = 0;
2236 : :
2679 2237 [ + + + + : 28699 : forfour(lct, sostmt->colTypes,
+ + + + +
+ + + + +
+ + + + +
- + - + -
+ + ]
2238 : : lcm, sostmt->colTypmods,
2239 : : lcc, sostmt->colCollations,
2240 : : left_tlist, leftmostQuery->targetList)
2241 : : {
7264 2242 : 20316 : Oid colType = lfirst_oid(lct);
2243 : 20316 : int32 colTypmod = lfirst_int(lcm);
5621 peter_e@gmx.net 2244 : 20316 : Oid colCollation = lfirst_oid(lcc);
7755 tgl@sss.pgh.pa.us 2245 : 20316 : TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
2246 : : char *colName;
2247 : : TargetEntry *tle;
2248 : : Var *var;
2249 : :
2250 [ - + ]: 20316 : Assert(!lefttle->resjunk);
2251 : 20316 : colName = pstrdup(lefttle->resname);
6478 2252 : 20316 : var = makeVar(leftmostRTI,
2253 : 20316 : lefttle->resno,
2254 : : colType,
2255 : : colTypmod,
2256 : : colCollation,
2257 : : 0);
2258 : 20316 : var->location = exprLocation((Node *) lefttle->expr);
2259 : 20316 : tle = makeTargetEntry((Expr *) var,
7755 2260 : 20316 : (AttrNumber) pstate->p_next_resno++,
2261 : : colName,
2262 : : false);
2263 : 20316 : qry->targetList = lappend(qry->targetList, tle);
6478 2264 : 20316 : targetvars = lappend(targetvars, var);
9266 2265 : 20316 : targetnames = lappend(targetnames, makeString(colName));
2371 2266 : 20316 : sortnscolumns[sortcolindex].p_varno = leftmostRTI;
2267 : 20316 : sortnscolumns[sortcolindex].p_varattno = lefttle->resno;
2268 : 20316 : sortnscolumns[sortcolindex].p_vartype = colType;
2269 : 20316 : sortnscolumns[sortcolindex].p_vartypmod = colTypmod;
2270 : 20316 : sortnscolumns[sortcolindex].p_varcollid = colCollation;
2271 : 20316 : sortnscolumns[sortcolindex].p_varnosyn = leftmostRTI;
2272 : 20316 : sortnscolumns[sortcolindex].p_varattnosyn = lefttle->resno;
2273 : 20316 : sortcolindex++;
2274 : : }
2275 : :
2276 : : /*
2277 : : * As a first step towards supporting sort clauses that are expressions
2278 : : * using the output columns, generate a namespace entry that makes the
2279 : : * output columns visible. A Join RTE node is handy for this, since we
2280 : : * can easily control the Vars generated upon matches.
2281 : : *
2282 : : * Note: we don't yet do anything useful with such cases, but at least
2283 : : * "ORDER BY upper(foo)" will draw the right error message rather than
2284 : : * "foo not found".
2285 : : */
6368 2286 : 8383 : sv_rtable_length = list_length(pstate->p_rtable);
2287 : :
2371 2288 : 8383 : jnsitem = addRangeTableEntryForJoin(pstate,
2289 : : targetnames,
2290 : : sortnscolumns,
2291 : : JOIN_INNER,
2292 : : 0,
2293 : : targetvars,
2294 : : NIL,
2295 : : NIL,
2296 : : NULL,
2297 : : NULL,
2298 : : false);
2299 : :
5074 2300 : 8383 : sv_namespace = pstate->p_namespace;
2301 : 8383 : pstate->p_namespace = NIL;
2302 : :
2303 : : /* add jnsitem to column namespace only */
2371 2304 : 8383 : addNSItemToQuery(pstate, jnsitem, false, false, true);
2305 : :
2306 : : /*
2307 : : * For now, we don't support resjunk sort clauses on the output of a
2308 : : * setOperation tree --- you can only use the SQL92-spec options of
2309 : : * selecting an output column by name or number. Enforce by checking that
2310 : : * transformSortClause doesn't add any items to tlist. Note, if changing
2311 : : * this, add_setop_child_rel_equivalences() will need to be updated.
2312 : : */
8066 neilc@samurai.com 2313 : 8383 : tllen = list_length(qry->targetList);
2314 : :
9399 tgl@sss.pgh.pa.us 2315 : 8383 : qry->sortClause = transformSortClause(pstate,
2316 : : sortClause,
2317 : : &qry->targetList,
2318 : : EXPR_KIND_ORDER_BY,
2319 : : false /* allow SQL92 rules */ );
2320 : :
2321 : : /* restore namespace, remove join RTE from rtable */
5074 2322 : 8379 : pstate->p_namespace = sv_namespace;
6368 2323 : 8379 : pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
2324 : :
8066 neilc@samurai.com 2325 [ - + ]: 8379 : if (tllen != list_length(qry->targetList))
8382 tgl@sss.pgh.pa.us 2326 [ # # ]:UBC 0 : ereport(ERROR,
2327 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2328 : : errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
2329 : : errdetail("Only result column names can be used, not expressions or functions."),
2330 : : errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
2331 : : parser_errposition(pstate,
2332 : : exprLocation(list_nth(qry->targetList, tllen)))));
2333 : :
8398 tgl@sss.pgh.pa.us 2334 :CBC 8379 : qry->limitOffset = transformLimitClause(pstate, limitOffset,
2335 : : EXPR_KIND_OFFSET, "OFFSET",
2336 : : stmt->limitOption);
2337 : 8379 : qry->limitCount = transformLimitClause(pstate, limitCount,
2338 : : EXPR_KIND_LIMIT, "LIMIT",
2339 : : stmt->limitOption);
2275 alvherre@alvh.no-ip. 2340 : 8379 : qry->limitOption = stmt->limitOption;
2341 : :
8565 tgl@sss.pgh.pa.us 2342 : 8379 : qry->rtable = pstate->p_rtable;
1302 alvherre@alvh.no-ip. 2343 : 8379 : qry->rteperminfos = pstate->p_rteperminfos;
8565 tgl@sss.pgh.pa.us 2344 : 8379 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2345 : :
9399 2346 : 8379 : qry->hasSubLinks = pstate->p_hasSubLinks;
6393 2347 : 8379 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
3577 2348 : 8379 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
5256 2349 : 8379 : qry->hasAggs = pstate->p_hasAggs;
2350 : :
7366 2351 [ - + - - : 8379 : foreach(l, lockingClause)
- + ]
2352 : : {
6089 tgl@sss.pgh.pa.us 2353 :UBC 0 : transformLockingClause(pstate, qry,
2354 : 0 : (LockingClause *) lfirst(l), false);
2355 : : }
2356 : :
5582 tgl@sss.pgh.pa.us 2357 :CBC 8379 : assign_query_collations(pstate, qry);
2358 : :
2359 : : /* this must be done after collations, for reliable comparison of exprs */
2721 rhodiumtoad@postgres 2360 [ + - + - : 8379 : if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
+ - - + ]
2721 rhodiumtoad@postgres 2361 :UBC 0 : parseCheckAggregates(pstate, qry);
2362 : :
9399 tgl@sss.pgh.pa.us 2363 :CBC 8379 : return qry;
2364 : : }
2365 : :
2366 : : /*
2367 : : * Make a SortGroupClause node for a SetOperationStmt's groupClauses
2368 : : *
2369 : : * If require_hash is true, the caller is indicating that they need hash
2370 : : * support or they will fail. So look extra hard for hash support.
2371 : : */
2372 : : SortGroupClause *
1756 peter@eisentraut.org 2373 : 17374 : makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
2374 : : {
1975 2375 : 17374 : SortGroupClause *grpcl = makeNode(SortGroupClause);
2376 : : Oid sortop;
2377 : : Oid eqop;
2378 : : bool hashable;
2379 : :
2380 : : /* determine the eqop and optional sortop */
2381 : 17374 : get_sort_group_operators(rescoltype,
2382 : : false, true, false,
2383 : : &sortop, &eqop, NULL,
2384 : : &hashable);
2385 : :
2386 : : /*
2387 : : * The type cache doesn't believe that record is hashable (see
2388 : : * cache_record_field_properties()), but if the caller really needs hash
2389 : : * support, we can assume it does. Worst case, if any components of the
2390 : : * record don't support hashing, we will fail at execution.
2391 : : */
1756 2392 [ + + + + : 17374 : if (require_hash && (rescoltype == RECORDOID || rescoltype == RECORDARRAYOID))
+ + ]
2393 : 16 : hashable = true;
2394 : :
2395 : : /* we don't have a tlist yet, so can't assign sortgrouprefs */
1975 2396 : 17374 : grpcl->tleSortGroupRef = 0;
2397 : 17374 : grpcl->eqop = eqop;
2398 : 17374 : grpcl->sortop = sortop;
624 2399 : 17374 : grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
1975 2400 : 17374 : grpcl->nulls_first = false; /* OK with or without sortop */
2401 : 17374 : grpcl->hashable = hashable;
2402 : :
2403 : 17374 : return grpcl;
2404 : : }
2405 : :
2406 : : /*
2407 : : * transformSetOperationTree
2408 : : * Recursively transform leaves and internal nodes of a set-op tree
2409 : : *
2410 : : * In addition to returning the transformed node, if targetlist isn't NULL
2411 : : * then we return a list of its non-resjunk TargetEntry nodes. For a leaf
2412 : : * set-op node these are the actual targetlist entries; otherwise they are
2413 : : * dummy entries created to carry the type, typmod, collation, and location
2414 : : * (for error messages) of each output column of the set-op node. This info
2415 : : * is needed only during the internal recursion of this function, so outside
2416 : : * callers pass NULL for targetlist. Note: the reason for passing the
2417 : : * actual targetlist entries of a leaf node is so that upper levels can
2418 : : * replace UNKNOWN Consts with properly-coerced constants.
2419 : : */
2420 : : static Node *
6515 tgl@sss.pgh.pa.us 2421 : 33913 : transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
2422 : : bool isTopLevel, List **targetlist)
2423 : : {
2424 : : bool isLeaf;
2425 : :
9368 2426 [ + - - + ]: 33913 : Assert(stmt && IsA(stmt, SelectStmt));
2427 : :
2428 : : /* Guard against stack overflow due to overly complex set-expressions */
4979 2429 : 33913 : check_stack_depth();
2430 : :
2431 : : /*
2432 : : * Validity-check both leaf and internal SELECTs for disallowed ops.
2433 : : */
7004 2434 [ - + ]: 33913 : if (stmt->intoClause)
8382 tgl@sss.pgh.pa.us 2435 [ # # ]:UBC 0 : ereport(ERROR,
2436 : : (errcode(ERRCODE_SYNTAX_ERROR),
2437 : : errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
2438 : : parser_errposition(pstate,
2439 : : exprLocation((Node *) stmt->intoClause))));
2440 : :
2441 : : /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
7638 tgl@sss.pgh.pa.us 2442 [ - + ]:CBC 33913 : if (stmt->lockingClause)
8382 tgl@sss.pgh.pa.us 2443 [ # # ]:UBC 0 : ereport(ERROR,
2444 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2445 : : /*------
2446 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
2447 : : errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2448 : : LCS_asString(((LockingClause *)
2449 : : linitial(stmt->lockingClause))->strength))));
2450 : :
2451 : : /*
2452 : : * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
2453 : : * or WITH clauses attached, we need to treat it like a leaf node to
2454 : : * generate an independent sub-Query tree. Otherwise, it can be
2455 : : * represented by a SetOperationStmt node underneath the parent Query.
2456 : : */
9368 tgl@sss.pgh.pa.us 2457 [ + + ]:CBC 33913 : if (stmt->op == SETOP_NONE)
2458 : : {
2459 [ + - - + ]: 21130 : Assert(stmt->larg == NULL && stmt->rarg == NULL);
2460 : 21130 : isLeaf = true;
2461 : : }
2462 : : else
2463 : : {
2464 [ + - - + ]: 12783 : Assert(stmt->larg != NULL && stmt->rarg != NULL);
2465 [ + + + - : 12783 : if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
+ - ]
5082 2466 [ + - + + ]: 12767 : stmt->lockingClause || stmt->withClause)
9368 2467 : 40 : isLeaf = true;
2468 : : else
2469 : 12743 : isLeaf = false;
2470 : : }
2471 : :
2472 [ + + ]: 33913 : if (isLeaf)
2473 : : {
2474 : : /* Process leaf SELECT */
2475 : : Query *selectQuery;
2476 : : ParseNamespaceItem *nsitem;
2477 : : RangeTblRef *rtr;
2478 : :
2479 : : /*
2480 : : * Transform SelectStmt into a Query.
2481 : : *
2482 : : * This works the same as SELECT transformation normally would, except
2483 : : * that we prevent resolving unknown-type outputs as TEXT. This does
2484 : : * not change the subquery's semantics since if the column type
2485 : : * matters semantically, it would have been resolved to something else
2486 : : * anyway. Doing this lets us resolve such outputs using
2487 : : * select_common_type(), below.
2488 : : *
2489 : : * Note: previously transformed sub-queries don't affect the parsing
2490 : : * of this sub-query, because they are not in the toplevel pstate's
2491 : : * namespace list.
2492 : : */
3443 2493 : 21170 : selectQuery = parse_sub_analyze((Node *) stmt, pstate,
2494 : : NULL, false, false);
2495 : :
2496 : : /*
2497 : : * Check for bogus references to Vars on the current query level (but
2498 : : * upper-level references are okay). Normally this can't happen
2499 : : * because the namespace will be empty, but it could happen if we are
2500 : : * inside a rule.
2501 : : */
5074 2502 [ - + ]: 21150 : if (pstate->p_namespace)
2503 : : {
8538 tgl@sss.pgh.pa.us 2504 [ # # ]:UBC 0 : if (contain_vars_of_level((Node *) selectQuery, 1))
8382 2505 [ # # ]: 0 : ereport(ERROR,
2506 : : (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2507 : : errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
2508 : : parser_errposition(pstate,
2509 : : locate_var_of_level((Node *) selectQuery, 1))));
2510 : : }
2511 : :
2512 : : /*
2513 : : * Extract a list of the non-junk TLEs for upper-level processing.
2514 : : */
5586 tgl@sss.pgh.pa.us 2515 [ + - ]:CBC 21150 : if (targetlist)
2516 : : {
2517 : : ListCell *tl;
2518 : :
2519 : 21150 : *targetlist = NIL;
2520 [ + + + + : 80384 : foreach(tl, selectQuery->targetList)
+ + ]
2521 : : {
2522 : 59234 : TargetEntry *tle = (TargetEntry *) lfirst(tl);
2523 : :
2524 [ + + ]: 59234 : if (!tle->resjunk)
2525 : 59226 : *targetlist = lappend(*targetlist, tle);
2526 : : }
2527 : : }
2528 : :
2529 : : /*
2530 : : * Make the leaf query be a subquery in the top-level rangetable.
2531 : : */
2371 2532 : 21150 : nsitem = addRangeTableEntryForSubquery(pstate,
2533 : : selectQuery,
2534 : : NULL,
2535 : : false,
2536 : : false);
2537 : :
2538 : : /*
2539 : : * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
2540 : : */
9399 2541 : 21150 : rtr = makeNode(RangeTblRef);
2371 2542 : 21150 : rtr->rtindex = nsitem->p_rtindex;
9399 2543 : 21150 : return (Node *) rtr;
2544 : : }
2545 : : else
2546 : : {
2547 : : /* Process an internal node (set operation node) */
9368 2548 : 12743 : SetOperationStmt *op = makeNode(SetOperationStmt);
2549 : : List *ltargetlist;
2550 : : List *rtargetlist;
2551 : : const char *context;
1510 2552 [ + + ]: 13479 : bool recursive = (pstate->p_parent_cte &&
2553 [ + + ]: 736 : pstate->p_parent_cte->cterecursive);
2554 : :
9368 2555 [ + + ]: 13239 : context = (stmt->op == SETOP_UNION ? "UNION" :
2556 [ + + ]: 496 : (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
2557 : : "EXCEPT"));
2558 : :
2559 : 12743 : op->op = stmt->op;
2560 : 12743 : op->all = stmt->all;
2561 : :
2562 : : /*
2563 : : * Recursively transform the left child node.
2564 : : */
6515 2565 : 12743 : op->larg = transformSetOperationTree(pstate, stmt->larg,
2566 : : false,
2567 : : <argetlist);
2568 : :
2569 : : /*
2570 : : * If we are processing a recursive union query, now is the time to
2571 : : * examine the non-recursive term's output columns and mark the
2572 : : * containing CTE as having those result columns. We should do this
2573 : : * only at the topmost setop of the CTE, of course.
2574 : : */
1756 peter@eisentraut.org 2575 [ + + + + ]: 12739 : if (isTopLevel && recursive)
5586 tgl@sss.pgh.pa.us 2576 : 640 : determineRecursiveColTypes(pstate, op->larg, ltargetlist);
2577 : :
2578 : : /*
2579 : : * Recursively transform the right child node.
2580 : : */
6515 2581 : 12739 : op->rarg = transformSetOperationTree(pstate, stmt->rarg,
2582 : : false,
2583 : : &rtargetlist);
2584 : :
109 peter@eisentraut.org 2585 :GNC 12723 : constructSetOpTargetlist(pstate, op, ltargetlist, rtargetlist, targetlist,
2586 : : context, recursive);
2587 : :
2588 : 12695 : return (Node *) op;
2589 : : }
2590 : : }
2591 : :
2592 : : /*
2593 : : * constructSetOpTargetlist
2594 : : * Compute the types, typmods and collations of the columns in the target
2595 : : * list of the given set operation.
2596 : : *
2597 : : * For every pair of columns in the targetlists of the children, compute the
2598 : : * common type, typmod, and collation representing the output (UNION) column.
2599 : : * If targetlist is not NULL, also build the dummy output targetlist
2600 : : * containing non-resjunk output columns. The values are stored into the
2601 : : * given SetOperationStmt node. context is a string for error messages
2602 : : * ("UNION" etc.). recursive is true if it is a recursive union.
2603 : : */
2604 : : void
2605 : 13091 : constructSetOpTargetlist(ParseState *pstate, SetOperationStmt *op,
2606 : : const List *ltargetlist, const List *rtargetlist,
2607 : : List **targetlist, const char *context, bool recursive)
2608 : : {
2609 : : ListCell *ltl;
2610 : : ListCell *rtl;
2611 : :
2612 : : /*
2613 : : * Verify that the two children have the same number of non-junk columns,
2614 : : * and determine the types of the merged output columns.
2615 : : */
2616 [ - + ]: 13091 : if (list_length(ltargetlist) != list_length(rtargetlist))
109 peter@eisentraut.org 2617 [ # # ]:UNC 0 : ereport(ERROR,
2618 : : (errcode(ERRCODE_SYNTAX_ERROR),
2619 : : errmsg("each %s query must have the same number of columns",
2620 : : context),
2621 : : parser_errposition(pstate,
2622 : : exprLocation((const Node *) rtargetlist))));
2623 : :
109 peter@eisentraut.org 2624 [ + + ]:GNC 13091 : if (targetlist)
2625 : 4550 : *targetlist = NIL;
2626 : 13091 : op->colTypes = NIL;
2627 : 13091 : op->colTypmods = NIL;
2628 : 13091 : op->colCollations = NIL;
2629 : 13091 : op->groupClauses = NIL;
2630 : :
2631 [ + + + + : 52864 : forboth(ltl, ltargetlist, rtl, rtargetlist)
+ + + + +
+ + - +
+ ]
2632 : : {
2633 : 39801 : TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
2634 : 39801 : TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
2635 : 39801 : Node *lcolnode = (Node *) ltle->expr;
2636 : 39801 : Node *rcolnode = (Node *) rtle->expr;
2637 : 39801 : Oid lcoltype = exprType(lcolnode);
2638 : 39801 : Oid rcoltype = exprType(rcolnode);
2639 : : Node *bestexpr;
2640 : : int bestlocation;
2641 : : Oid rescoltype;
2642 : : int32 rescoltypmod;
2643 : : Oid rescolcoll;
2644 : :
2645 : : /* select common type, same as CASE et al */
2646 : 39801 : rescoltype = select_common_type(pstate,
2647 : : list_make2(lcolnode, rcolnode),
2648 : : context,
2649 : : &bestexpr);
2650 : 39801 : bestlocation = exprLocation(bestexpr);
2651 : :
2652 : : /*
2653 : : * Verify the coercions are actually possible. If not, we'd fail
2654 : : * later anyway, but we want to fail now while we have sufficient
2655 : : * context to produce an error cursor position.
2656 : : *
2657 : : * For all non-UNKNOWN-type cases, we verify coercibility but we don't
2658 : : * modify the child's expression, for fear of changing the child
2659 : : * query's semantics.
2660 : : *
2661 : : * If a child expression is an UNKNOWN-type Const or Param, we want to
2662 : : * replace it with the coerced expression. This can only happen when
2663 : : * the child is a leaf set-op node. It's safe to replace the
2664 : : * expression because if the child query's semantics depended on the
2665 : : * type of this output column, it'd have already coerced the UNKNOWN
2666 : : * to something else. We want to do this because (a) we want to
2667 : : * verify that a Const is valid for the target type, or resolve the
2668 : : * actual type of an UNKNOWN Param, and (b) we want to avoid
2669 : : * unnecessary discrepancies between the output type of the child
2670 : : * query and the resolved target type. Such a discrepancy would
2671 : : * disable optimization in the planner.
2672 : : *
2673 : : * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2674 : : * (knowing that coerce_to_common_type would fail). The planner is
2675 : : * sometimes able to fold an UNKNOWN Var to a constant before it has
2676 : : * to coerce the type, so failing now would just break cases that
2677 : : * might work.
2678 : : */
2679 [ + + ]: 39801 : if (lcoltype != UNKNOWNOID)
2680 : 35494 : lcolnode = coerce_to_common_type(pstate, lcolnode,
2681 : : rescoltype, context);
2682 [ - + ]: 4307 : else if (IsA(lcolnode, Const) ||
109 peter@eisentraut.org 2683 [ # # ]:UNC 0 : IsA(lcolnode, Param))
2684 : : {
109 peter@eisentraut.org 2685 :GNC 4307 : lcolnode = coerce_to_common_type(pstate, lcolnode,
2686 : : rescoltype, context);
2687 : 4307 : ltle->expr = (Expr *) lcolnode;
2688 : : }
2689 : :
2690 [ + + ]: 39801 : if (rcoltype != UNKNOWNOID)
2691 : 34958 : rcolnode = coerce_to_common_type(pstate, rcolnode,
2692 : : rescoltype, context);
2693 [ - + ]: 4843 : else if (IsA(rcolnode, Const) ||
109 peter@eisentraut.org 2694 [ # # ]:UNC 0 : IsA(rcolnode, Param))
2695 : : {
109 peter@eisentraut.org 2696 :GNC 4843 : rcolnode = coerce_to_common_type(pstate, rcolnode,
2697 : : rescoltype, context);
2698 : 4839 : rtle->expr = (Expr *) rcolnode;
2699 : : }
2700 : :
2701 : 39797 : rescoltypmod = select_common_typmod(pstate,
2702 : : list_make2(lcolnode, rcolnode),
2703 : : rescoltype);
2704 : :
2705 : : /*
2706 : : * Select common collation. A common collation is required for all
2707 : : * set operators except UNION ALL; see SQL:2008 7.13 <query
2708 : : * expression> Syntax Rule 15c. (If we fail to identify a common
2709 : : * collation for a UNION ALL column, the colCollations element will be
2710 : : * set to InvalidOid, which may result in a runtime error if something
2711 : : * at a higher query level wants to use the column's collation.)
2712 : : */
2713 : 39797 : rescolcoll = select_common_collation(pstate,
2714 : : list_make2(lcolnode, rcolnode),
2715 [ + + + + ]: 39797 : (op->op == SETOP_UNION && op->all));
2716 : :
2717 : : /* emit results */
2718 : 39773 : op->colTypes = lappend_oid(op->colTypes, rescoltype);
2719 : 39773 : op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2720 : 39773 : op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2721 : :
2722 : : /*
2723 : : * For all cases except UNION ALL, identify the grouping operators
2724 : : * (and, if available, sorting operators) that will be used to
2725 : : * eliminate duplicates.
2726 : : */
2727 [ + + + + ]: 39773 : if (op->op != SETOP_UNION || !op->all)
2728 : : {
2729 : : ParseCallbackState pcbstate;
2730 : :
2731 : 17358 : setup_parser_errposition_callback(&pcbstate, pstate,
2732 : : bestlocation);
2733 : :
2734 : : /* If it's a recursive union, we need to require hashing support. */
2735 : 17358 : op->groupClauses = lappend(op->groupClauses,
2736 : 17358 : makeSortGroupClauseForSetOp(rescoltype, recursive));
2737 : :
2738 : 17358 : cancel_parser_errposition_callback(&pcbstate);
2739 : : }
2740 : :
2741 : : /*
2742 : : * Construct a dummy tlist entry to return. We use a SetToDefault
2743 : : * node for the expression, since it carries exactly the fields
2744 : : * needed, but any other expression node type would do as well.
2745 : : */
2746 [ + + ]: 39773 : if (targetlist)
2747 : : {
2748 : 19086 : SetToDefault *rescolnode = makeNode(SetToDefault);
2749 : : TargetEntry *restle;
2750 : :
2751 : 19086 : rescolnode->typeId = rescoltype;
2752 : 19086 : rescolnode->typeMod = rescoltypmod;
2753 : 19086 : rescolnode->collation = rescolcoll;
2754 : 19086 : rescolnode->location = bestlocation;
2755 : 19086 : restle = makeTargetEntry((Expr *) rescolnode,
2756 : : 0, /* no need to set resno */
2757 : : NULL,
2758 : : false);
2759 : 19086 : *targetlist = lappend(*targetlist, restle);
2760 : : }
2761 : : }
10948 scrappy@hub.org 2762 :GIC 13063 : }
2763 : :
2764 : : /*
2765 : : * Process the outputs of the non-recursive term of a recursive union
2766 : : * to set up the parent CTE's columns
2767 : : */
2768 : : static void
5586 tgl@sss.pgh.pa.us 2769 :CBC 640 : determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
2770 : : {
2771 : : Node *node;
2772 : : int leftmostRTI;
2773 : : Query *leftmostQuery;
2774 : : List *targetList;
2775 : : ListCell *left_tlist;
2776 : : ListCell *nrtl;
2777 : : int next_resno;
2778 : :
2779 : : /*
2780 : : * Find leftmost leaf SELECT
2781 : : */
6138 2782 : 640 : node = larg;
2783 [ + - + + ]: 644 : while (node && IsA(node, SetOperationStmt))
2784 : 4 : node = ((SetOperationStmt *) node)->larg;
2785 [ + - - + ]: 640 : Assert(node && IsA(node, RangeTblRef));
2786 : 640 : leftmostRTI = ((RangeTblRef *) node)->rtindex;
2787 : 640 : leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2788 [ - + ]: 640 : Assert(leftmostQuery != NULL);
2789 : :
2790 : : /*
2791 : : * Generate dummy targetlist using column names of leftmost select and
2792 : : * dummy result expressions of the non-recursive term.
2793 : : */
2794 : 640 : targetList = NIL;
2795 : 640 : next_resno = 1;
2796 : :
2679 2797 [ + - + + : 1980 : forboth(nrtl, nrtargetlist, left_tlist, leftmostQuery->targetList)
+ - + + +
+ + - +
+ ]
2798 : : {
5586 2799 : 1340 : TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
6138 2800 : 1340 : TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
2801 : : char *colName;
2802 : : TargetEntry *tle;
2803 : :
2804 [ - + ]: 1340 : Assert(!lefttle->resjunk);
2805 : 1340 : colName = pstrdup(lefttle->resname);
5586 2806 : 1340 : tle = makeTargetEntry(nrtle->expr,
6138 2807 : 1340 : next_resno++,
2808 : : colName,
2809 : : false);
2810 : 1340 : targetList = lappend(targetList, tle);
2811 : : }
2812 : :
2813 : : /* Now build CTE's output column info using dummy targetlist */
2814 : 640 : analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2815 : 640 : }
2816 : :
2817 : :
2818 : : /*
2819 : : * transformReturnStmt -
2820 : : * transforms a return statement
2821 : : */
2822 : : static Query *
1910 peter@eisentraut.org 2823 : 2728 : transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
2824 : : {
2825 : 2728 : Query *qry = makeNode(Query);
2826 : :
2827 : 2728 : qry->commandType = CMD_SELECT;
2828 : 2728 : qry->isReturn = true;
2829 : :
2830 : 2728 : qry->targetList = list_make1(makeTargetEntry((Expr *) transformExpr(pstate, stmt->returnval, EXPR_KIND_SELECT_TARGET),
2831 : : 1, NULL, false));
2832 : :
2833 [ + - ]: 2724 : if (pstate->p_resolve_unknowns)
2834 : 2724 : resolveTargetListUnknowns(pstate, qry->targetList);
2835 : 2724 : qry->rtable = pstate->p_rtable;
1302 alvherre@alvh.no-ip. 2836 : 2724 : qry->rteperminfos = pstate->p_rteperminfos;
1910 peter@eisentraut.org 2837 : 2724 : qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2838 : 2724 : qry->hasSubLinks = pstate->p_hasSubLinks;
2839 : 2724 : qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2840 : 2724 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2841 : 2724 : qry->hasAggs = pstate->p_hasAggs;
2842 : :
2843 : 2724 : assign_query_collations(pstate, qry);
2844 : :
2845 : 2724 : return qry;
2846 : : }
2847 : :
2848 : :
2849 : : /*
2850 : : * transformUpdateStmt -
2851 : : * transforms an update statement
2852 : : */
2853 : : static Query *
10399 bruce@momjian.us 2854 : 9288 : transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
2855 : : {
10522 2856 : 9288 : Query *qry = makeNode(Query);
2857 : : ParseNamespaceItem *nsitem;
2858 : : Node *qual;
2859 : :
10523 2860 : 9288 : qry->commandType = CMD_UPDATE;
2861 : :
2862 : : /* process the WITH clause independently of all else */
5737 tgl@sss.pgh.pa.us 2863 [ + + ]: 9288 : if (stmt->withClause)
2864 : : {
2865 : 55 : qry->hasRecursive = stmt->withClause->recursive;
2866 : 55 : qry->cteList = transformWithClause(pstate, stmt->withClause);
5604 2867 : 55 : qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2868 : : }
2869 : :
8866 2870 : 18575 : qry->resultRelation = setTargetTable(pstate, stmt->relation,
3476 2871 : 9288 : stmt->relation->inh,
2872 : : true,
2873 : : ACL_UPDATE);
2874 : :
2875 : : /* disallow UPDATE ... WHERE CURRENT OF on a view */
69 dean.a.rasheed@gmail 2876 [ + + ]: 9287 : if (stmt->whereClause &&
2877 [ + + ]: 6969 : IsA(stmt->whereClause, CurrentOfExpr) &&
2878 [ + + ]: 108 : pstate->p_target_relation->rd_rel->relkind == RELKIND_VIEW)
2879 [ + - ]: 4 : ereport(ERROR,
2880 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2881 : : errmsg("WHERE CURRENT OF on a view is not implemented"));
2882 : :
90 peter@eisentraut.org 2883 [ + + ]:GNC 9283 : if (stmt->forPortionOf)
2884 : 530 : qry->forPortionOf = transformForPortionOfClause(pstate,
2885 : : qry->resultRelation,
2886 : 590 : stmt->forPortionOf,
1 2887 : 590 : stmt->whereClause,
2888 : : true);
2889 : :
2371 tgl@sss.pgh.pa.us 2890 : 9223 : nsitem = pstate->p_target_nsitem;
2891 : :
2892 : : /* subqueries in FROM cannot access the result relation */
4557 tgl@sss.pgh.pa.us 2893 :CBC 9223 : nsitem->p_lateral_only = true;
4553 2894 : 9223 : nsitem->p_lateral_ok = false;
2895 : :
2896 : : /*
2897 : : * the FROM clause is non-standard SQL syntax. We used to be able to do
2898 : : * this with REPLACE in POSTQUEL so we keep the feature.
2899 : : */
9267 2900 : 9223 : transformFromClause(pstate, stmt->fromClause);
2901 : :
2902 : : /* remaining clauses can reference the result relation normally */
4557 2903 : 9207 : nsitem->p_lateral_only = false;
4553 2904 : 9207 : nsitem->p_lateral_ok = true;
2905 : :
5072 2906 : 9207 : qual = transformWhereClause(pstate, stmt->whereClause,
2907 : : EXPR_KIND_WHERE, "WHERE");
2908 : :
530 dean.a.rasheed@gmail 2909 : 9199 : transformReturningClause(pstate, qry, stmt->returningClause,
2910 : : EXPR_KIND_RETURNING);
2911 : :
2912 : : /*
2913 : : * Now we are done with SELECT-like processing, and can get on with
2914 : : * transforming the target list to match the UPDATE target columns.
2915 : : */
90 peter@eisentraut.org 2916 :GNC 9187 : qry->targetList = transformUpdateTargetList(pstate, stmt->targetList,
2917 : : qry->forPortionOf);
2918 : :
10523 bruce@momjian.us 2919 :CBC 9151 : qry->rtable = pstate->p_rtable;
1302 alvherre@alvh.no-ip. 2920 : 9151 : qry->rteperminfos = pstate->p_rteperminfos;
9405 tgl@sss.pgh.pa.us 2921 : 9151 : qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2922 : :
3577 2923 : 9151 : qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
9422 2924 : 9151 : qry->hasSubLinks = pstate->p_hasSubLinks;
2925 : :
4071 andres@anarazel.de 2926 : 9151 : assign_query_collations(pstate, qry);
2927 : :
2928 : 9151 : return qry;
2929 : : }
2930 : :
2931 : : /*
2932 : : * transformUpdateTargetList -
2933 : : * handle SET clause in UPDATE/MERGE/INSERT ... ON CONFLICT UPDATE
2934 : : */
2935 : : List *
90 peter@eisentraut.org 2936 :GNC 11092 : transformUpdateTargetList(ParseState *pstate, List *origTlist, ForPortionOfExpr *forPortionOf)
2937 : : {
4056 bruce@momjian.us 2938 :CBC 11092 : List *tlist = NIL;
2939 : : RTEPermissionInfo *target_perminfo;
2940 : : ListCell *orig_tl;
2941 : : ListCell *tl;
2942 : :
4071 andres@anarazel.de 2943 : 11092 : tlist = transformTargetList(pstate, origTlist,
2944 : : EXPR_KIND_UPDATE_SOURCE);
2945 : :
2946 : : /* Prepare to assign non-conflicting resnos to resjunk attributes */
3006 teodor@sigaev.ru 2947 [ + + ]: 11060 : if (pstate->p_next_resno <= RelationGetNumberOfAttributes(pstate->p_target_relation))
2948 : 9403 : pstate->p_next_resno = RelationGetNumberOfAttributes(pstate->p_target_relation) + 1;
2949 : :
2950 : : /* Prepare non-junk columns for assignment to target table */
1302 alvherre@alvh.no-ip. 2951 : 11060 : target_perminfo = pstate->p_target_nsitem->p_perminfo;
4071 andres@anarazel.de 2952 : 11060 : orig_tl = list_head(origTlist);
2953 : :
2954 [ + - + + : 24745 : foreach(tl, tlist)
+ + ]
2955 : : {
9843 tgl@sss.pgh.pa.us 2956 : 13713 : TargetEntry *tle = (TargetEntry *) lfirst(tl);
2957 : : ResTarget *origTarget;
2958 : : int attrno;
2959 : :
7755 2960 [ + + ]: 13713 : if (tle->resjunk)
2961 : : {
2962 : : /*
2963 : : * Resjunk nodes need no additional processing, but be sure they
2964 : : * have resnos that do not match any target columns; else rewriter
2965 : : * or planner might get confused. They don't need a resname
2966 : : * either.
2967 : : */
2968 : 91 : tle->resno = (AttrNumber) pstate->p_next_resno++;
2969 : 91 : tle->resname = NULL;
9843 2970 : 91 : continue;
2971 : : }
4071 andres@anarazel.de 2972 [ - + ]: 13622 : if (orig_tl == NULL)
9843 tgl@sss.pgh.pa.us 2973 [ # # ]:UBC 0 : elog(ERROR, "UPDATE target count mismatch --- internal error");
3368 tgl@sss.pgh.pa.us 2974 :CBC 13622 : origTarget = lfirst_node(ResTarget, orig_tl);
2975 : :
7404 2976 : 13622 : attrno = attnameAttNum(pstate->p_target_relation,
2977 : 13622 : origTarget->name, true);
2978 [ + + ]: 13622 : if (attrno == InvalidAttrNumber)
2979 [ + - + + : 16 : ereport(ERROR,
+ - ]
2980 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2981 : : errmsg("column \"%s\" of relation \"%s\" does not exist",
2982 : : origTarget->name,
2983 : : RelationGetRelationName(pstate->p_target_relation)),
2984 : : (origTarget->indirection != NIL &&
2985 : : strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2986 : : errhint("SET target columns cannot be qualified with the relation name.") : 0,
2987 : : parser_errposition(pstate, origTarget->location)));
2988 : :
2989 : : /*
2990 : : * If this is a FOR PORTION OF update, forbid directly setting the
2991 : : * range column, since that would conflict with the implicit updates.
2992 : : */
90 peter@eisentraut.org 2993 [ + + ]:GNC 13606 : if (forPortionOf != NULL)
2994 : : {
2995 [ + + ]: 543 : if (attrno == forPortionOf->rangeVar->varattno)
2996 [ + - ]: 4 : ereport(ERROR,
2997 : : (errcode(ERRCODE_SYNTAX_ERROR),
2998 : : errmsg("cannot update column \"%s\" because it is used in FOR PORTION OF",
2999 : : origTarget->name),
3000 : : parser_errposition(pstate, origTarget->location)));
3001 : : }
3002 : :
9738 tgl@sss.pgh.pa.us 3003 :CBC 13602 : updateTargetListEntry(pstate, tle, origTarget->name,
3004 : : attrno,
3005 : : origTarget->indirection,
3006 : : origTarget->location);
3007 : :
3008 : : /* Mark the target column as requiring update permissions */
1302 alvherre@alvh.no-ip. 3009 : 13594 : target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
3010 : : attrno - FirstLowInvalidHeapAttributeNumber);
3011 : :
2542 tgl@sss.pgh.pa.us 3012 : 13594 : orig_tl = lnext(origTlist, orig_tl);
3013 : : }
4071 andres@anarazel.de 3014 [ - + ]: 11032 : if (orig_tl != NULL)
9843 tgl@sss.pgh.pa.us 3015 [ # # ]:UBC 0 : elog(ERROR, "UPDATE target count mismatch --- internal error");
3016 : :
2325 peter@eisentraut.org 3017 :CBC 11032 : return tlist;
3018 : : }
3019 : :
3020 : : /*
3021 : : * addNSItemForReturning -
3022 : : * add a ParseNamespaceItem for the OLD or NEW alias in RETURNING.
3023 : : */
3024 : : static void
530 dean.a.rasheed@gmail 3025 : 4602 : addNSItemForReturning(ParseState *pstate, const char *aliasname,
3026 : : VarReturningType returning_type)
3027 : : {
3028 : : List *colnames;
3029 : : int numattrs;
3030 : : ParseNamespaceColumn *nscolumns;
3031 : : ParseNamespaceItem *nsitem;
3032 : :
3033 : : /* copy per-column data from the target relation */
3034 : 4602 : colnames = pstate->p_target_nsitem->p_rte->eref->colnames;
3035 : 4602 : numattrs = list_length(colnames);
3036 : :
202 michael@paquier.xyz 3037 :GNC 4602 : nscolumns = palloc_array(ParseNamespaceColumn, numattrs);
3038 : :
530 dean.a.rasheed@gmail 3039 :CBC 4602 : memcpy(nscolumns, pstate->p_target_nsitem->p_nscolumns,
3040 : : numattrs * sizeof(ParseNamespaceColumn));
3041 : :
3042 : : /* mark all columns as returning OLD/NEW */
3043 [ + + ]: 18082 : for (int i = 0; i < numattrs; i++)
3044 : 13480 : nscolumns[i].p_varreturningtype = returning_type;
3045 : :
3046 : : /* build the nsitem, copying most fields from the target relation */
202 michael@paquier.xyz 3047 :GNC 4602 : nsitem = palloc_object(ParseNamespaceItem);
530 dean.a.rasheed@gmail 3048 :CBC 4602 : nsitem->p_names = makeAlias(aliasname, colnames);
3049 : 4602 : nsitem->p_rte = pstate->p_target_nsitem->p_rte;
3050 : 4602 : nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex;
3051 : 4602 : nsitem->p_perminfo = pstate->p_target_nsitem->p_perminfo;
3052 : 4602 : nsitem->p_nscolumns = nscolumns;
3053 : 4602 : nsitem->p_returning_type = returning_type;
3054 : :
3055 : : /* add it to the query namespace as a table-only item */
3056 : 4602 : addNSItemToQuery(pstate, nsitem, false, true, false);
3057 : 4602 : }
3058 : :
3059 : : /*
3060 : : * transformReturningClause -
3061 : : * handle a RETURNING clause in INSERT/UPDATE/DELETE/MERGE
3062 : : */
3063 : : void
3064 : 14932 : transformReturningClause(ParseState *pstate, Query *qry,
3065 : : ReturningClause *returningClause,
3066 : : ParseExprKind exprKind)
3067 : : {
3068 : 14932 : int save_nslen = list_length(pstate->p_namespace);
3069 : : int save_next_resno;
3070 : :
3071 [ + + ]: 14932 : if (returningClause == NULL)
3072 : 12585 : return; /* nothing to do */
3073 : :
3074 : : /*
3075 : : * Scan RETURNING WITH(...) options for OLD/NEW alias names. Complain if
3076 : : * there is any conflict with existing relations.
3077 : : */
3078 [ + + + + : 4742 : foreach_node(ReturningOption, option, returningClause->options)
+ + ]
3079 : : {
3080 [ + + - ]: 80 : switch (option->option)
3081 : : {
3082 : 36 : case RETURNING_OPTION_OLD:
3083 [ + + ]: 36 : if (qry->returningOldAlias != NULL)
3084 [ + - ]: 4 : ereport(ERROR,
3085 : : errcode(ERRCODE_SYNTAX_ERROR),
3086 : : /* translator: %s is OLD or NEW */
3087 : : errmsg("%s cannot be specified multiple times", "OLD"),
3088 : : parser_errposition(pstate, option->location));
3089 : 32 : qry->returningOldAlias = option->value;
3090 : 32 : break;
3091 : :
3092 : 44 : case RETURNING_OPTION_NEW:
3093 [ + + ]: 44 : if (qry->returningNewAlias != NULL)
3094 [ + - ]: 4 : ereport(ERROR,
3095 : : errcode(ERRCODE_SYNTAX_ERROR),
3096 : : /* translator: %s is OLD or NEW */
3097 : : errmsg("%s cannot be specified multiple times", "NEW"),
3098 : : parser_errposition(pstate, option->location));
3099 : 40 : qry->returningNewAlias = option->value;
3100 : 40 : break;
3101 : :
530 dean.a.rasheed@gmail 3102 :UBC 0 : default:
3103 [ # # ]: 0 : elog(ERROR, "unrecognized returning option: %d", option->option);
3104 : : }
3105 : :
530 dean.a.rasheed@gmail 3106 [ + + ]:CBC 72 : if (refnameNamespaceItem(pstate, NULL, option->value, -1, NULL) != NULL)
3107 [ + - ]: 8 : ereport(ERROR,
3108 : : errcode(ERRCODE_DUPLICATE_ALIAS),
3109 : : errmsg("table name \"%s\" specified more than once",
3110 : : option->value),
3111 : : parser_errposition(pstate, option->location));
3112 : :
3113 : 64 : addNSItemForReturning(pstate, option->value,
3114 [ + + ]: 64 : option->option == RETURNING_OPTION_OLD ?
3115 : : VAR_RETURNING_OLD : VAR_RETURNING_NEW);
3116 : : }
3117 : :
3118 : : /*
3119 : : * If OLD/NEW alias names weren't explicitly specified, use "old"/"new"
3120 : : * unless masked by existing relations.
3121 : : */
3122 [ + + + + ]: 4642 : if (qry->returningOldAlias == NULL &&
3123 : 2311 : refnameNamespaceItem(pstate, NULL, "old", -1, NULL) == NULL)
3124 : : {
3125 : 2271 : qry->returningOldAlias = "old";
3126 : 2271 : addNSItemForReturning(pstate, "old", VAR_RETURNING_OLD);
3127 : : }
3128 [ + + + + ]: 4638 : if (qry->returningNewAlias == NULL &&
3129 : 2307 : refnameNamespaceItem(pstate, NULL, "new", -1, NULL) == NULL)
3130 : : {
3131 : 2267 : qry->returningNewAlias = "new";
3132 : 2267 : addNSItemForReturning(pstate, "new", VAR_RETURNING_NEW);
3133 : : }
3134 : :
3135 : : /*
3136 : : * We need to assign resnos starting at one in the RETURNING list. Save
3137 : : * and restore the main tlist's value of p_next_resno, just in case
3138 : : * someone looks at it later (probably won't happen).
3139 : : */
7262 tgl@sss.pgh.pa.us 3140 : 2331 : save_next_resno = pstate->p_next_resno;
3141 : 2331 : pstate->p_next_resno = 1;
3142 : :
3143 : : /* transform RETURNING expressions identically to a SELECT targetlist */
530 dean.a.rasheed@gmail 3144 : 2331 : qry->returningList = transformTargetList(pstate,
3145 : : returningClause->exprs,
3146 : : exprKind);
3147 : :
3148 : : /*
3149 : : * Complain if the nonempty tlist expanded to nothing (which is possible
3150 : : * if it contains only a star-expansion of a zero-column table). If we
3151 : : * allow this, the parsed Query will look like it didn't have RETURNING,
3152 : : * with results that would probably surprise the user.
3153 : : */
3154 [ + + ]: 2303 : if (qry->returningList == NIL)
4581 tgl@sss.pgh.pa.us 3155 [ + - ]: 4 : ereport(ERROR,
3156 : : (errcode(ERRCODE_SYNTAX_ERROR),
3157 : : errmsg("RETURNING must have at least one column"),
3158 : : parser_errposition(pstate,
3159 : : exprLocation(linitial(returningClause->exprs)))));
3160 : :
3161 : : /* mark column origins */
530 dean.a.rasheed@gmail 3162 : 2299 : markTargetListOrigins(pstate, qry->returningList);
3163 : :
3164 : : /* resolve any still-unresolved output columns as being type text */
3443 tgl@sss.pgh.pa.us 3165 [ + - ]: 2299 : if (pstate->p_resolve_unknowns)
530 dean.a.rasheed@gmail 3166 : 2299 : resolveTargetListUnknowns(pstate, qry->returningList);
3167 : :
3168 : : /* restore state */
3169 : 2299 : pstate->p_namespace = list_truncate(pstate->p_namespace, save_nslen);
7262 tgl@sss.pgh.pa.us 3170 : 2299 : pstate->p_next_resno = save_next_resno;
3171 : : }
3172 : :
3173 : :
3174 : : /*
3175 : : * transformPLAssignStmt -
3176 : : * transform a PL/pgSQL assignment statement
3177 : : *
3178 : : * If there is no opt_indirection, the transformed statement looks like
3179 : : * "SELECT a_expr ...", except the expression has been cast to the type of
3180 : : * the target. With indirection, it's still a SELECT, but the expression will
3181 : : * incorporate FieldStore and/or assignment SubscriptingRef nodes to compute a
3182 : : * new value for a container-type variable represented by the target. The
3183 : : * expression references the target as the container source.
3184 : : */
3185 : : static Query *
2003 3186 : 3375 : transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
3187 : : {
3188 : : Query *qry;
3189 : 3375 : ColumnRef *cref = makeNode(ColumnRef);
3190 : 3375 : List *indirection = stmt->indirection;
3191 : 3375 : int nnames = stmt->nnames;
3192 : : Node *target;
3193 : : SelectStmtPassthrough passthru;
3194 : : bool save_resolve_unknowns;
3195 : :
3196 : : /*
3197 : : * First, construct a ColumnRef for the target variable. If the target
3198 : : * has more than one dotted name, we have to pull the extra names out of
3199 : : * the indirection list.
3200 : : */
3201 : 3375 : cref->fields = list_make1(makeString(stmt->name));
3202 : 3375 : cref->location = stmt->location;
3203 [ + + ]: 3375 : if (nnames > 1)
3204 : : {
3205 : : /* avoid munging the raw parsetree */
3206 : 250 : indirection = list_copy(indirection);
3207 [ + + + - ]: 507 : while (--nnames > 0 && indirection != NIL)
3208 : : {
3209 : 257 : Node *ind = (Node *) linitial(indirection);
3210 : :
3211 [ - + ]: 257 : if (!IsA(ind, String))
2003 tgl@sss.pgh.pa.us 3212 [ # # ]:UBC 0 : elog(ERROR, "invalid name count in PLAssignStmt");
2003 tgl@sss.pgh.pa.us 3213 :CBC 257 : cref->fields = lappend(cref->fields, ind);
3214 : 257 : indirection = list_delete_first(indirection);
3215 : : }
3216 : : }
3217 : :
3218 : : /*
3219 : : * Transform the target reference. Typically we will get back a Param
3220 : : * node, but there's no reason to be too picky about its type. (Note that
3221 : : * we must do this before calling transformSelectStmt. It's tempting to
3222 : : * do it inside transformPLAssignStmtTarget, but we need to do it before
3223 : : * adding any FROM tables to the pstate's namespace, else we might wrongly
3224 : : * resolve the target as a table column.)
3225 : : */
3226 : 3375 : target = transformExpr(pstate, (Node *) cref,
3227 : : EXPR_KIND_UPDATE_TARGET);
3228 : :
3229 : : /* Set up passthrough data for transformPLAssignStmtTarget */
276 tgl@sss.pgh.pa.us 3230 :GNC 3369 : passthru.stmt = stmt;
3231 : 3369 : passthru.target = target;
3232 : 3369 : passthru.indirection = indirection;
3233 : :
3234 : : /*
3235 : : * To avoid duplicating a lot of code, we use transformSelectStmt to do
3236 : : * almost all of the work. However, we need to do additional processing
3237 : : * on the SELECT's targetlist after it's been transformed, but before
3238 : : * possible addition of targetlist items for ORDER BY or GROUP BY.
3239 : : * transformSelectStmt knows it should call transformPLAssignStmtTarget if
3240 : : * it's passed a passthru argument.
3241 : : *
3242 : : * Also, disable resolution of unknown-type tlist items; PL/pgSQL wants to
3243 : : * deal with that itself.
3244 : : */
3245 : 3369 : save_resolve_unknowns = pstate->p_resolve_unknowns;
3246 : 3369 : pstate->p_resolve_unknowns = false;
3247 : 3369 : qry = transformSelectStmt(pstate, stmt->val, &passthru);
3248 : 3362 : pstate->p_resolve_unknowns = save_resolve_unknowns;
3249 : :
3250 : 3362 : return qry;
3251 : : }
3252 : :
3253 : : /*
3254 : : * Callback function to adjust a SELECT's tlist to make the output suitable
3255 : : * for assignment to a PLAssignStmt's target variable.
3256 : : *
3257 : : * Note: we actually modify the tle->expr in-place, but the function's API
3258 : : * is set up to not presume that.
3259 : : */
3260 : : static List *
3261 : 3369 : transformPLAssignStmtTarget(ParseState *pstate, List *tlist,
3262 : : SelectStmtPassthrough *passthru)
3263 : : {
3264 : 3369 : PLAssignStmt *stmt = passthru->stmt;
3265 : 3369 : Node *target = passthru->target;
3266 : 3369 : List *indirection = passthru->indirection;
3267 : : Oid targettype;
3268 : : int32 targettypmod;
3269 : : Oid targetcollation;
3270 : : TargetEntry *tle;
3271 : : Oid type_id;
3272 : :
3273 : 3369 : targettype = exprType(target);
3274 : 3369 : targettypmod = exprTypmod(target);
3275 : 3369 : targetcollation = exprCollation(target);
3276 : :
3277 : : /* we should have exactly one targetlist item */
2003 tgl@sss.pgh.pa.us 3278 [ + + ]:CBC 3369 : if (list_length(tlist) != 1)
3279 [ + - ]: 2 : ereport(ERROR,
3280 : : (errcode(ERRCODE_SYNTAX_ERROR),
3281 : : errmsg_plural("assignment source returned %d column",
3282 : : "assignment source returned %d columns",
3283 : : list_length(tlist),
3284 : : list_length(tlist))));
3285 : :
3286 : 3367 : tle = linitial_node(TargetEntry, tlist);
3287 : :
3288 : : /*
3289 : : * This next bit is similar to transformAssignedExpr; the key difference
3290 : : * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
3291 : : */
3292 : 3367 : type_id = exprType((Node *) tle->expr);
3293 : :
3294 : 3367 : pstate->p_expr_kind = EXPR_KIND_UPDATE_TARGET;
3295 : :
3296 [ + + ]: 3367 : if (indirection)
3297 : : {
3298 : 60 : tle->expr = (Expr *)
3299 : 65 : transformAssignmentIndirection(pstate,
3300 : : target,
3301 : 65 : stmt->name,
3302 : : false,
3303 : : targettype,
3304 : : targettypmod,
3305 : : targetcollation,
3306 : : indirection,
3307 : : list_head(indirection),
3308 : 65 : (Node *) tle->expr,
3309 : : COERCION_PLPGSQL,
3310 : : exprLocation(target));
3311 : : }
3312 [ + + + + ]: 3302 : else if (targettype != type_id &&
3313 [ + + + + ]: 913 : (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
3314 [ + + ]: 226 : (type_id == RECORDOID || ISCOMPLEX(type_id)))
3315 : : {
3316 : : /*
3317 : : * Hack: do not let coerce_to_target_type() deal with inconsistent
3318 : : * composite types. Just pass the expression result through as-is,
3319 : : * and let the PL/pgSQL executor do the conversion its way. This is
3320 : : * rather bogus, but it's needed for backwards compatibility.
3321 : : */
3322 : : }
3323 : : else
3324 : : {
3325 : : /*
3326 : : * For normal non-qualified target column, do type checking and
3327 : : * coercion.
3328 : : */
3329 : 3118 : Node *orig_expr = (Node *) tle->expr;
3330 : :
3331 : 3118 : tle->expr = (Expr *)
3332 : 3118 : coerce_to_target_type(pstate,
3333 : : orig_expr, type_id,
3334 : : targettype, targettypmod,
3335 : : COERCION_PLPGSQL,
3336 : : COERCE_IMPLICIT_CAST,
3337 : : -1);
3338 : : /* With COERCION_PLPGSQL, this error is probably unreachable */
3339 [ - + ]: 3118 : if (tle->expr == NULL)
2003 tgl@sss.pgh.pa.us 3340 [ # # ]:UBC 0 : ereport(ERROR,
3341 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
3342 : : errmsg("variable \"%s\" is of type %s"
3343 : : " but expression is of type %s",
3344 : : stmt->name,
3345 : : format_type_be(targettype),
3346 : : format_type_be(type_id)),
3347 : : errhint("You will need to rewrite or cast the expression."),
3348 : : parser_errposition(pstate, exprLocation(orig_expr))));
3349 : : }
3350 : :
2003 tgl@sss.pgh.pa.us 3351 :CBC 3362 : pstate->p_expr_kind = EXPR_KIND_NONE;
3352 : :
276 tgl@sss.pgh.pa.us 3353 :GNC 3362 : return list_make1(tle);
3354 : : }
3355 : :
3356 : :
3357 : : /*
3358 : : * transformDeclareCursorStmt -
3359 : : * transform a DECLARE CURSOR Statement
3360 : : *
3361 : : * DECLARE CURSOR is like other utility statements in that we emit it as a
3362 : : * CMD_UTILITY Query node; however, we must first transform the contained
3363 : : * query. We used to postpone that until execution, but it's really necessary
3364 : : * to do it during the normal parse analysis phase to ensure that side effects
3365 : : * of parser hooks happen at the expected time.
3366 : : */
3367 : : static Query *
7004 tgl@sss.pgh.pa.us 3368 :CBC 2711 : transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
3369 : : {
3370 : : Query *result;
3371 : : Query *query;
3372 : :
3373 [ + + ]: 2711 : if ((stmt->options & CURSOR_OPT_SCROLL) &&
3374 [ - + ]: 160 : (stmt->options & CURSOR_OPT_NO_SCROLL))
7004 tgl@sss.pgh.pa.us 3375 [ # # ]:UBC 0 : ereport(ERROR,
3376 : : (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3377 : : /* translator: %s is a SQL keyword */
3378 : : errmsg("cannot specify both %s and %s",
3379 : : "SCROLL", "NO SCROLL")));
3380 : :
1910 peter@eisentraut.org 3381 [ - + ]:CBC 2711 : if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
1910 peter@eisentraut.org 3382 [ # # ]:UBC 0 : (stmt->options & CURSOR_OPT_INSENSITIVE))
3383 [ # # ]: 0 : ereport(ERROR,
3384 : : (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3385 : : /* translator: %s is a SQL keyword */
3386 : : errmsg("cannot specify both %s and %s",
3387 : : "ASENSITIVE", "INSENSITIVE")));
3388 : :
3389 : : /* Transform contained query, not allowing SELECT INTO */
3454 tgl@sss.pgh.pa.us 3390 :CBC 2711 : query = transformStmt(pstate, stmt->query);
3391 : 2698 : stmt->query = (Node *) query;
3392 : :
3393 : : /* Grammar should not have allowed anything but SELECT */
3394 [ + - ]: 2698 : if (!IsA(query, Query) ||
3395 [ - + ]: 2698 : query->commandType != CMD_SELECT)
6511 tgl@sss.pgh.pa.us 3396 [ # # ]:UBC 0 : elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
3397 : :
3398 : : /*
3399 : : * We also disallow data-modifying WITH in a cursor. (This could be
3400 : : * allowed, but the semantics of when the updates occur might be
3401 : : * surprising.)
3402 : : */
3454 tgl@sss.pgh.pa.us 3403 [ - + ]:CBC 2698 : if (query->hasModifyingCTE)
5604 tgl@sss.pgh.pa.us 3404 [ # # ]:UBC 0 : ereport(ERROR,
3405 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3406 : : errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
3407 : :
3408 : : /* FOR UPDATE and WITH HOLD are not compatible */
3454 tgl@sss.pgh.pa.us 3409 [ + + - + ]:CBC 2698 : if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
7004 tgl@sss.pgh.pa.us 3410 [ # # ]:UBC 0 : ereport(ERROR,
3411 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3412 : : /*------
3413 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3414 : : errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
3415 : : LCS_asString(((RowMarkClause *)
3416 : : linitial(query->rowMarks))->strength)),
3417 : : errdetail("Holdable cursors must be READ ONLY.")));
3418 : :
3419 : : /* FOR UPDATE and SCROLL are not compatible */
3454 tgl@sss.pgh.pa.us 3420 [ + + - + ]:CBC 2698 : if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
6824 tgl@sss.pgh.pa.us 3421 [ # # ]:UBC 0 : ereport(ERROR,
3422 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3423 : : /*------
3424 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3425 : : errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
3426 : : LCS_asString(((RowMarkClause *)
3427 : : linitial(query->rowMarks))->strength)),
3428 : : errdetail("Scrollable cursors must be READ ONLY.")));
3429 : :
3430 : : /* FOR UPDATE and INSENSITIVE are not compatible */
3454 tgl@sss.pgh.pa.us 3431 [ + + - + ]:CBC 2698 : if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
6824 tgl@sss.pgh.pa.us 3432 [ # # ]:UBC 0 : ereport(ERROR,
3433 : : (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
3434 : : /*------
3435 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3436 : : errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
3437 : : LCS_asString(((RowMarkClause *)
3438 : : linitial(query->rowMarks))->strength)),
3439 : : errdetail("Insensitive cursors must be READ ONLY.")));
3440 : :
3441 : : /* represent the command as a utility Query */
3454 tgl@sss.pgh.pa.us 3442 :CBC 2698 : result = makeNode(Query);
3443 : 2698 : result->commandType = CMD_UTILITY;
7004 3444 : 2698 : result->utilityStmt = (Node *) stmt;
3445 : :
3446 : 2698 : return result;
3447 : : }
3448 : :
3449 : :
3450 : : /*
3451 : : * transformExplainStmt -
3452 : : * transform an EXPLAIN Statement
3453 : : *
3454 : : * EXPLAIN is like other utility statements in that we emit it as a
3455 : : * CMD_UTILITY Query node; however, we must first transform the contained
3456 : : * query. We used to postpone that until execution, but it's really necessary
3457 : : * to do it during the normal parse analysis phase to ensure that side effects
3458 : : * of parser hooks happen at the expected time.
3459 : : */
3460 : : static Query *
3461 : 16532 : transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
3462 : : {
3463 : : Query *result;
1194 3464 : 16532 : bool generic_plan = false;
3465 : 16532 : Oid *paramTypes = NULL;
3466 : 16532 : int numParams = 0;
3467 : :
3468 : : /*
3469 : : * If we have no external source of parameter definitions, and the
3470 : : * GENERIC_PLAN option is specified, then accept variable parameter
3471 : : * definitions (similarly to PREPARE, for example).
3472 : : */
3473 [ + + ]: 16532 : if (pstate->p_paramref_hook == NULL)
3474 : : {
3475 : : ListCell *lc;
3476 : :
3477 [ + + + + : 32840 : foreach(lc, stmt->options)
+ + ]
3478 : : {
3479 : 16320 : DefElem *opt = (DefElem *) lfirst(lc);
3480 : :
3481 [ + + ]: 16320 : if (strcmp(opt->defname, "generic_plan") == 0)
3482 : 12 : generic_plan = defGetBoolean(opt);
3483 : : /* don't "break", as we want the last value */
3484 : : }
3485 [ + + ]: 16520 : if (generic_plan)
3486 : 12 : setup_parse_variable_parameters(pstate, ¶mTypes, &numParams);
3487 : : }
3488 : :
3489 : : /* transform contained query, allowing SELECT INTO */
3454 3490 : 16532 : stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
3491 : :
3492 : : /* make sure all is well with parameter types */
1194 3493 [ + + ]: 16523 : if (generic_plan)
3494 : 12 : check_variable_parameters(pstate, (Query *) stmt->query);
3495 : :
3496 : : /* represent the command as a utility Query */
5216 3497 : 16523 : result = makeNode(Query);
3498 : 16523 : result->commandType = CMD_UTILITY;
3499 : 16523 : result->utilityStmt = (Node *) stmt;
3500 : :
3501 : 16523 : return result;
3502 : : }
3503 : :
3504 : :
3505 : : /*
3506 : : * transformCreateTableAsStmt -
3507 : : * transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
3508 : : * Statement
3509 : : *
3510 : : * As with DECLARE CURSOR and EXPLAIN, transform the contained statement now.
3511 : : */
3512 : : static Query *
3513 : 1316 : transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
3514 : : {
3515 : : Query *result;
3516 : : Query *query;
3517 : :
3518 : : /* transform contained query, not allowing SELECT INTO */
4827 3519 : 1316 : query = transformStmt(pstate, stmt->query);
3520 : 1315 : stmt->query = (Node *) query;
3521 : :
3522 : : /* additional work needed for CREATE MATERIALIZED VIEW */
2180 michael@paquier.xyz 3523 [ + + ]: 1315 : if (stmt->objtype == OBJECT_MATVIEW)
3524 : : {
3525 : : ObjectAddress temp_object;
3526 : :
3527 : : /*
3528 : : * Prohibit a data-modifying CTE in the query used to create a
3529 : : * materialized view. It's not sufficiently clear what the user would
3530 : : * want to happen if the MV is refreshed or incrementally maintained.
3531 : : */
4827 tgl@sss.pgh.pa.us 3532 [ - + ]: 356 : if (query->hasModifyingCTE)
4827 tgl@sss.pgh.pa.us 3533 [ # # ]:UBC 0 : ereport(ERROR,
3534 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3535 : : errmsg("materialized views must not use data-modifying statements in WITH")));
3536 : :
3537 : : /*
3538 : : * Check whether any temporary database objects are used in the
3539 : : * creation query. It would be hard to refresh data or incrementally
3540 : : * maintain it if a source disappeared.
3541 : : */
218 tgl@sss.pgh.pa.us 3542 [ + + ]:GNC 356 : if (query_uses_temp_object(query, &temp_object))
4827 tgl@sss.pgh.pa.us 3543 [ + - ]:GBC 4 : ereport(ERROR,
3544 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3545 : : errmsg("materialized views must not use temporary objects"),
3546 : : errdetail("This view depends on temporary %s.",
3547 : : getObjectDescription(&temp_object, false))));
3548 : :
3549 : : /*
3550 : : * A materialized view would either need to save parameters for use in
3551 : : * maintaining/loading the data or prohibit them entirely. The latter
3552 : : * seems safer and more sane.
3553 : : */
4827 tgl@sss.pgh.pa.us 3554 [ - + ]:CBC 348 : if (query_contains_extern_params(query))
4827 tgl@sss.pgh.pa.us 3555 [ # # ]:UBC 0 : ereport(ERROR,
3556 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3557 : : errmsg("materialized views may not be defined using bound parameters")));
3558 : :
3559 : : /*
3560 : : * For now, we disallow unlogged materialized views, because it seems
3561 : : * like a bad idea for them to just go to empty after a crash. (If we
3562 : : * could mark them as unpopulated, that would be better, but that
3563 : : * requires catalog changes which crash recovery can't presently
3564 : : * handle.)
3565 : : */
4803 tgl@sss.pgh.pa.us 3566 [ - + ]:CBC 348 : if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
4803 tgl@sss.pgh.pa.us 3567 [ # # ]:UBC 0 : ereport(ERROR,
3568 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3569 : : errmsg("materialized views cannot be unlogged")));
3570 : :
3571 : : /*
3572 : : * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
3573 : : * for purposes of creating the view's ON SELECT rule. We stash that
3574 : : * in the IntoClause because that's where intorel_startup() can
3575 : : * conveniently get it from.
3576 : : */
621 peter@eisentraut.org 3577 :CBC 348 : stmt->into->viewQuery = copyObject(query);
3578 : : }
3579 : :
3580 : : /* represent the command as a utility Query */
7004 tgl@sss.pgh.pa.us 3581 : 1307 : result = makeNode(Query);
3582 : 1307 : result->commandType = CMD_UTILITY;
3583 : 1307 : result->utilityStmt = (Node *) stmt;
3584 : :
3585 : 1307 : return result;
3586 : : }
3587 : :
3588 : : /*
3589 : : * transform a CallStmt
3590 : : */
3591 : : static Query *
3052 peter_e@gmx.net 3592 : 313 : transformCallStmt(ParseState *pstate, CallStmt *stmt)
3593 : : {
3594 : : List *targs;
3595 : : ListCell *lc;
3596 : : Node *node;
3597 : : FuncExpr *fexpr;
3598 : : HeapTuple proctup;
3599 : : Datum proargmodes;
3600 : : bool isNull;
1846 tgl@sss.pgh.pa.us 3601 : 313 : List *outargs = NIL;
3602 : : Query *result;
3603 : :
3604 : : /*
3605 : : * First, do standard parse analysis on the procedure call and its
3606 : : * arguments, allowing us to identify the called procedure.
3607 : : */
3052 peter_e@gmx.net 3608 : 313 : targs = NIL;
3609 [ + + + + : 765 : foreach(lc, stmt->funccall->args)
+ + ]
3610 : : {
3611 : 452 : targs = lappend(targs, transformExpr(pstate,
3612 : 452 : (Node *) lfirst(lc),
3613 : : EXPR_KIND_CALL_ARGUMENT));
3614 : : }
3615 : :
3616 : 313 : node = ParseFuncOrColumn(pstate,
3617 : 313 : stmt->funccall->funcname,
3618 : : targs,
3619 : : pstate->p_last_srf,
3620 : : stmt->funccall,
3621 : : true,
3622 : 313 : stmt->funccall->location);
3623 : :
2702 peter@eisentraut.org 3624 : 292 : assign_expr_collations(pstate, node);
3625 : :
1846 tgl@sss.pgh.pa.us 3626 : 292 : fexpr = castNode(FuncExpr, node);
3627 : :
3628 : 292 : proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fexpr->funcid));
3629 [ - + ]: 292 : if (!HeapTupleIsValid(proctup))
1846 tgl@sss.pgh.pa.us 3630 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3631 : :
3632 : : /*
3633 : : * Expand the argument list to deal with named-argument notation and
3634 : : * default arguments. For ordinary FuncExprs this'd be done during
3635 : : * planning, but a CallStmt doesn't go through planning, and there seems
3636 : : * no good reason not to do it here.
3637 : : */
1846 tgl@sss.pgh.pa.us 3638 :CBC 292 : fexpr->args = expand_function_arguments(fexpr->args,
3639 : : true,
3640 : : fexpr->funcresulttype,
3641 : : proctup);
3642 : :
3643 : : /* Fetch proargmodes; if it's null, there are no output args */
3644 : 292 : proargmodes = SysCacheGetAttr(PROCOID, proctup,
3645 : : Anum_pg_proc_proargmodes,
3646 : : &isNull);
3647 [ + + ]: 292 : if (!isNull)
3648 : : {
3649 : : /*
3650 : : * Split the list into input arguments in fexpr->args and output
3651 : : * arguments in stmt->outargs. INOUT arguments appear in both lists.
3652 : : */
3653 : : ArrayType *arr;
3654 : : int numargs;
3655 : : char *argmodes;
3656 : : List *inargs;
3657 : : int i;
3658 : :
3659 : 119 : arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
3660 : 119 : numargs = list_length(fexpr->args);
3661 [ + - ]: 119 : if (ARR_NDIM(arr) != 1 ||
3662 [ + - ]: 119 : ARR_DIMS(arr)[0] != numargs ||
3663 [ + - ]: 119 : ARR_HASNULL(arr) ||
3664 [ - + ]: 119 : ARR_ELEMTYPE(arr) != CHAROID)
1846 tgl@sss.pgh.pa.us 3665 [ # # ]:UBC 0 : elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
3666 : : numargs);
1846 tgl@sss.pgh.pa.us 3667 [ - + ]:CBC 119 : argmodes = (char *) ARR_DATA_PTR(arr);
3668 : :
3669 : 119 : inargs = NIL;
3670 : 119 : i = 0;
3671 [ + - + + : 395 : foreach(lc, fexpr->args)
+ + ]
3672 : : {
3673 : 276 : Node *n = lfirst(lc);
3674 : :
3675 [ + + + - ]: 276 : switch (argmodes[i])
3676 : : {
3677 : 91 : case PROARGMODE_IN:
3678 : : case PROARGMODE_VARIADIC:
3679 : 91 : inargs = lappend(inargs, n);
3680 : 91 : break;
3681 : 72 : case PROARGMODE_OUT:
3682 : 72 : outargs = lappend(outargs, n);
3683 : 72 : break;
3684 : 113 : case PROARGMODE_INOUT:
3685 : 113 : inargs = lappend(inargs, n);
3686 : 113 : outargs = lappend(outargs, copyObject(n));
3687 : 113 : break;
1846 tgl@sss.pgh.pa.us 3688 :UBC 0 : default:
3689 : : /* note we don't support PROARGMODE_TABLE */
3690 [ # # ]: 0 : elog(ERROR, "invalid argmode %c for procedure",
3691 : : argmodes[i]);
3692 : : break;
3693 : : }
1846 tgl@sss.pgh.pa.us 3694 :CBC 276 : i++;
3695 : : }
3696 : 119 : fexpr->args = inargs;
3697 : : }
3698 : :
3699 : 292 : stmt->funcexpr = fexpr;
3700 : 292 : stmt->outargs = outargs;
3701 : :
3702 : 292 : ReleaseSysCache(proctup);
3703 : :
3704 : : /* represent the command as a utility Query */
3052 peter_e@gmx.net 3705 : 292 : result = makeNode(Query);
3706 : 292 : result->commandType = CMD_UTILITY;
3707 : 292 : result->utilityStmt = (Node *) stmt;
3708 : :
3709 : 292 : return result;
3710 : : }
3711 : :
3712 : : /*
3713 : : * Produce a string representation of a LockClauseStrength value.
3714 : : * This should only be applied to valid values (not LCS_NONE).
3715 : : */
3716 : : const char *
4725 alvherre@alvh.no-ip. 3717 : 32 : LCS_asString(LockClauseStrength strength)
3718 : : {
3719 [ - - - + : 32 : switch (strength)
+ - ]
3720 : : {
4125 tgl@sss.pgh.pa.us 3721 :UBC 0 : case LCS_NONE:
3722 : 0 : Assert(false);
3723 : : break;
4725 alvherre@alvh.no-ip. 3724 : 0 : case LCS_FORKEYSHARE:
3725 : 0 : return "FOR KEY SHARE";
3726 : 0 : case LCS_FORSHARE:
3727 : 0 : return "FOR SHARE";
4725 alvherre@alvh.no-ip. 3728 :CBC 4 : case LCS_FORNOKEYUPDATE:
3729 : 4 : return "FOR NO KEY UPDATE";
3730 : 28 : case LCS_FORUPDATE:
3731 : 28 : return "FOR UPDATE";
3732 : : }
4438 bruce@momjian.us 3733 :UBC 0 : return "FOR some"; /* shouldn't happen */
3734 : : }
3735 : :
3736 : : /*
3737 : : * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
3738 : : *
3739 : : * exported so planner can check again after rewriting, query pullup, etc
3740 : : */
3741 : : void
4715 alvherre@alvh.no-ip. 3742 :CBC 11590 : CheckSelectLocking(Query *qry, LockClauseStrength strength)
3743 : : {
3296 tgl@sss.pgh.pa.us 3744 [ - + ]: 11590 : Assert(strength != LCS_NONE); /* else caller error */
3745 : :
9399 3746 [ - + ]: 11590 : if (qry->setOperations)
8382 tgl@sss.pgh.pa.us 3747 [ # # ]:UBC 0 : ereport(ERROR,
3748 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3749 : : /*------
3750 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3751 : : errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3752 : : LCS_asString(strength))));
9651 tgl@sss.pgh.pa.us 3753 [ - + ]:CBC 11590 : if (qry->distinctClause != NIL)
8382 tgl@sss.pgh.pa.us 3754 [ # # ]:UBC 0 : ereport(ERROR,
3755 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3756 : : /*------
3757 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3758 : : errmsg("%s is not allowed with DISTINCT clause",
3759 : : LCS_asString(strength))));
1855 tgl@sss.pgh.pa.us 3760 [ + + + + ]:CBC 11590 : if (qry->groupClause != NIL || qry->groupingSets != NIL)
8382 3761 [ + - ]: 8 : ereport(ERROR,
3762 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3763 : : /*------
3764 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3765 : : errmsg("%s is not allowed with GROUP BY clause",
3766 : : LCS_asString(strength))));
7782 3767 [ - + ]: 11582 : if (qry->havingQual != NULL)
7782 tgl@sss.pgh.pa.us 3768 [ # # ]:UBC 0 : ereport(ERROR,
3769 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3770 : : /*------
3771 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3772 : : errmsg("%s is not allowed with HAVING clause",
3773 : : LCS_asString(strength))));
10018 vadim4o@yahoo.com 3774 [ + + ]:CBC 11582 : if (qry->hasAggs)
8382 tgl@sss.pgh.pa.us 3775 [ + - ]: 4 : ereport(ERROR,
3776 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3777 : : /*------
3778 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3779 : : errmsg("%s is not allowed with aggregate functions",
3780 : : LCS_asString(strength))));
6393 3781 [ - + ]: 11578 : if (qry->hasWindowFuncs)
6393 tgl@sss.pgh.pa.us 3782 [ # # ]:UBC 0 : ereport(ERROR,
3783 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3784 : : /*------
3785 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3786 : : errmsg("%s is not allowed with window functions",
3787 : : LCS_asString(strength))));
3577 tgl@sss.pgh.pa.us 3788 [ - + ]:CBC 11578 : if (qry->hasTargetSRFs)
6091 tgl@sss.pgh.pa.us 3789 [ # # ]:UBC 0 : ereport(ERROR,
3790 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3791 : : /*------
3792 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3793 : : errmsg("%s is not allowed with set-returning functions in the target list",
3794 : : LCS_asString(strength))));
10018 vadim4o@yahoo.com 3795 :CBC 11578 : }
3796 : :
3797 : : /*
3798 : : * Transform a FOR [KEY] UPDATE/SHARE clause
3799 : : *
3800 : : * This basically involves replacing names by integer relids.
3801 : : *
3802 : : * NB: if you need to change this, see also markQueryForLocking()
3803 : : * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
3804 : : */
3805 : : static void
6089 tgl@sss.pgh.pa.us 3806 : 5013 : transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
3807 : : bool pushedDown)
3808 : : {
7638 3809 : 5013 : List *lockedRels = lc->lockedRels;
3810 : : ListCell *l;
3811 : : ListCell *rt;
3812 : : Index i;
3813 : : LockingClause *allrels;
3814 : :
4715 alvherre@alvh.no-ip. 3815 : 5013 : CheckSelectLocking(qry, lc->strength);
3816 : :
3817 : : /* make a clause we can pass down to subqueries to select all rels */
7638 tgl@sss.pgh.pa.us 3818 : 5001 : allrels = makeNode(LockingClause);
7563 bruce@momjian.us 3819 : 5001 : allrels->lockedRels = NIL; /* indicates all rels */
4906 alvherre@alvh.no-ip. 3820 : 5001 : allrels->strength = lc->strength;
4284 3821 : 5001 : allrels->waitPolicy = lc->waitPolicy;
3822 : :
7638 tgl@sss.pgh.pa.us 3823 [ + + ]: 5001 : if (lockedRels == NIL)
3824 : : {
3825 : : /*
3826 : : * Lock all regular tables used in query and its subqueries. We
3827 : : * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
3828 : : * in rules. This is a bit of an abuse of a mostly-obsolete flag, but
3829 : : * it's convenient. We can't rely on the namespace mechanism that has
3830 : : * largely replaced inFromCl, since for example we need to lock
3831 : : * base-relation RTEs even if they are masked by upper joins.
3832 : : */
9405 3833 : 3799 : i = 0;
3834 [ + + + + : 7648 : foreach(rt, qry->rtable)
+ + ]
3835 : : {
3836 : 3849 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
3837 : :
3838 : 3849 : ++i;
1776 3839 [ + + ]: 3849 : if (!rte->inFromCl)
3840 : 8 : continue;
8273 3841 [ + - + ]: 3841 : switch (rte->rtekind)
3842 : : {
3843 : 3825 : case RTE_RELATION:
3844 : : {
3845 : : RTEPermissionInfo *perminfo;
3846 : :
1302 alvherre@alvh.no-ip. 3847 : 3825 : applyLockingClause(qry, i,
3848 : : lc->strength,
3849 : : lc->waitPolicy,
3850 : : pushedDown);
3851 : 3825 : perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3852 : 3825 : perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3853 : : }
8273 tgl@sss.pgh.pa.us 3854 : 3825 : break;
8273 tgl@sss.pgh.pa.us 3855 :UBC 0 : case RTE_SUBQUERY:
4284 alvherre@alvh.no-ip. 3856 : 0 : applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
3857 : : pushedDown);
3858 : :
3859 : : /*
3860 : : * FOR UPDATE/SHARE of subquery is propagated to all of
3861 : : * subquery's rels, too. We could do this later (based on
3862 : : * the marking of the subquery RTE) but it is convenient
3863 : : * to have local knowledge in each query level about which
3864 : : * rels need to be opened with RowShareLock.
3865 : : */
6089 tgl@sss.pgh.pa.us 3866 : 0 : transformLockingClause(pstate, rte->subquery,
3867 : : allrels, true);
8273 3868 : 0 : break;
8273 tgl@sss.pgh.pa.us 3869 :CBC 16 : default:
3870 : : /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
3871 : 16 : break;
3872 : : }
3873 : : }
3874 : : }
3875 : : else
3876 : : {
3877 : : /*
3878 : : * Lock just the named tables. As above, we allow locking any base
3879 : : * relation regardless of alias-visibility rules, so we need to
3880 : : * examine inFromCl to exclude OLD/NEW.
3881 : : */
7733 3882 [ + - + + : 2394 : foreach(l, lockedRels)
+ + ]
3883 : : {
6511 3884 : 1208 : RangeVar *thisrel = (RangeVar *) lfirst(l);
3885 : :
3886 : : /* For simplicity we insist on unqualified alias names here */
3887 [ + - - + ]: 1208 : if (thisrel->catalogname || thisrel->schemaname)
6511 tgl@sss.pgh.pa.us 3888 [ # # ]:UBC 0 : ereport(ERROR,
3889 : : (errcode(ERRCODE_SYNTAX_ERROR),
3890 : : /*------
3891 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3892 : : errmsg("%s must specify unqualified relation names",
3893 : : LCS_asString(lc->strength)),
3894 : : parser_errposition(pstate, thisrel->location)));
3895 : :
9405 tgl@sss.pgh.pa.us 3896 :CBC 1208 : i = 0;
3897 [ + - + + : 1402 : foreach(rt, qry->rtable)
+ + ]
3898 : : {
3899 : 1394 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
1441 dean.a.rasheed@gmail 3900 : 1394 : char *rtename = rte->eref->aliasname;
3901 : :
9405 tgl@sss.pgh.pa.us 3902 : 1394 : ++i;
1776 3903 [ + + ]: 1394 : if (!rte->inFromCl)
3904 : 16 : continue;
3905 : :
3906 : : /*
3907 : : * A join RTE without an alias is not visible as a relation
3908 : : * name and needs to be skipped (otherwise it might hide a
3909 : : * base relation with the same name), except if it has a USING
3910 : : * alias, which *is* visible.
3911 : : *
3912 : : * Subquery and values RTEs without aliases are never visible
3913 : : * as relation names and must always be skipped.
3914 : : */
1441 dean.a.rasheed@gmail 3915 [ + + ]: 1378 : if (rte->alias == NULL)
3916 : : {
3917 [ + + ]: 109 : if (rte->rtekind == RTE_JOIN)
3918 : : {
3919 [ + + ]: 40 : if (rte->join_using_alias == NULL)
3920 : 32 : continue;
3921 : 8 : rtename = rte->join_using_alias->aliasname;
3922 : : }
3923 [ + + ]: 69 : else if (rte->rtekind == RTE_SUBQUERY ||
3924 [ - + ]: 65 : rte->rtekind == RTE_VALUES)
1454 3925 : 4 : continue;
3926 : : }
3927 : :
3928 [ + + ]: 1342 : if (strcmp(rtename, thisrel->relname) == 0)
3929 : : {
8273 tgl@sss.pgh.pa.us 3930 [ + + + - : 1200 : switch (rte->rtekind)
- - - -
- ]
3931 : : {
3932 : 1186 : case RTE_RELATION:
3933 : : {
3934 : : RTEPermissionInfo *perminfo;
3935 : :
1302 alvherre@alvh.no-ip. 3936 : 1186 : applyLockingClause(qry, i,
3937 : : lc->strength,
3938 : : lc->waitPolicy,
3939 : : pushedDown);
3940 : 1186 : perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3941 : 1186 : perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3942 : : }
8273 tgl@sss.pgh.pa.us 3943 : 1186 : break;
3944 : 6 : case RTE_SUBQUERY:
4284 alvherre@alvh.no-ip. 3945 : 6 : applyLockingClause(qry, i, lc->strength,
3946 : : lc->waitPolicy, pushedDown);
3947 : : /* see comment above */
6089 tgl@sss.pgh.pa.us 3948 : 6 : transformLockingClause(pstate, rte->subquery,
3949 : : allrels, true);
8273 3950 : 6 : break;
3951 : 8 : case RTE_JOIN:
3952 [ + - ]: 8 : ereport(ERROR,
3953 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3954 : : /*------
3955 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3956 : : errmsg("%s cannot be applied to a join",
3957 : : LCS_asString(lc->strength)),
3958 : : parser_errposition(pstate, thisrel->location)));
3959 : : break;
8273 tgl@sss.pgh.pa.us 3960 :UBC 0 : case RTE_FUNCTION:
3961 [ # # ]: 0 : ereport(ERROR,
3962 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3963 : : /*------
3964 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3965 : : errmsg("%s cannot be applied to a function",
3966 : : LCS_asString(lc->strength)),
3967 : : parser_errposition(pstate, thisrel->location)));
3968 : : break;
3401 alvherre@alvh.no-ip. 3969 : 0 : case RTE_TABLEFUNC:
3970 [ # # ]: 0 : ereport(ERROR,
3971 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3972 : : /*------
3973 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3974 : : errmsg("%s cannot be applied to a table function",
3975 : : LCS_asString(lc->strength)),
3976 : : parser_errposition(pstate, thisrel->location)));
3977 : : break;
7272 mail@joeconway.com 3978 : 0 : case RTE_VALUES:
3979 [ # # ]: 0 : ereport(ERROR,
3980 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3981 : : /*------
3982 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3983 : : errmsg("%s cannot be applied to VALUES",
3984 : : LCS_asString(lc->strength)),
3985 : : parser_errposition(pstate, thisrel->location)));
3986 : : break;
6478 tgl@sss.pgh.pa.us 3987 : 0 : case RTE_CTE:
6090 3988 [ # # ]: 0 : ereport(ERROR,
3989 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3990 : : /*------
3991 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
3992 : : errmsg("%s cannot be applied to a WITH query",
3993 : : LCS_asString(lc->strength)),
3994 : : parser_errposition(pstate, thisrel->location)));
3995 : : break;
3378 kgrittn@postgresql.o 3996 : 0 : case RTE_NAMEDTUPLESTORE:
3997 [ # # ]: 0 : ereport(ERROR,
3998 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3999 : : /*------
4000 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
4001 : : errmsg("%s cannot be applied to a named tuplestore",
4002 : : LCS_asString(lc->strength)),
4003 : : parser_errposition(pstate, thisrel->location)));
4004 : : break;
4005 : :
4006 : : /* Shouldn't be possible to see RTE_RESULT here */
4007 : :
8273 tgl@sss.pgh.pa.us 4008 : 0 : default:
4009 [ # # ]: 0 : elog(ERROR, "unrecognized RTE type: %d",
4010 : : (int) rte->rtekind);
4011 : : break;
4012 : : }
8273 tgl@sss.pgh.pa.us 4013 :CBC 1192 : break; /* out of foreach loop */
4014 : : }
4015 : : }
8070 neilc@samurai.com 4016 [ + + ]: 1200 : if (rt == NULL)
8382 tgl@sss.pgh.pa.us 4017 [ + - ]: 8 : ereport(ERROR,
4018 : : (errcode(ERRCODE_UNDEFINED_TABLE),
4019 : : /*------
4020 : : translator: %s is a SQL row locking clause such as FOR UPDATE */
4021 : : errmsg("relation \"%s\" in %s clause not found in FROM clause",
4022 : : thisrel->relname,
4023 : : LCS_asString(lc->strength)),
4024 : : parser_errposition(pstate, thisrel->location)));
4025 : : }
4026 : : }
7366 4027 : 4985 : }
4028 : :
4029 : : /*
4030 : : * Record locking info for a single rangetable item
4031 : : */
4032 : : void
6089 4033 : 5081 : applyLockingClause(Query *qry, Index rtindex,
4034 : : LockClauseStrength strength, LockWaitPolicy waitPolicy,
4035 : : bool pushedDown)
4036 : : {
4037 : : RowMarkClause *rc;
4038 : :
3296 4039 [ - + ]: 5081 : Assert(strength != LCS_NONE); /* else caller error */
4040 : :
4041 : : /* If it's an explicit clause, make sure hasForUpdate gets set */
6089 4042 [ + + ]: 5081 : if (!pushedDown)
4043 : 5015 : qry->hasForUpdate = true;
4044 : :
4045 : : /* Check for pre-existing entry for same rtindex */
6091 4046 [ - + ]: 5081 : if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
4047 : : {
4048 : : /*
4049 : : * If the same RTE is specified with more than one locking strength,
4050 : : * use the strongest. (Reasonable, since you can't take both a shared
4051 : : * and exclusive lock at the same time; it'll end up being exclusive
4052 : : * anyway.)
4053 : : *
4054 : : * Similarly, if the same RTE is specified with more than one lock
4055 : : * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
4056 : : * turn wins over waiting for the lock (the default). This is a bit
4057 : : * more debatable but raising an error doesn't seem helpful. (Consider
4058 : : * for instance SELECT FOR UPDATE NOWAIT from a view that internally
4059 : : * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
4060 : : * LOCKED is reasonable since the former throws an error in case of
4061 : : * coming across a locked tuple, which may be undesirable in some
4062 : : * cases but it seems better than silently returning inconsistent
4063 : : * results.
4064 : : *
4065 : : * And of course pushedDown becomes false if any clause is explicit.
4066 : : */
4906 alvherre@alvh.no-ip. 4067 :UBC 0 : rc->strength = Max(rc->strength, strength);
4284 4068 : 0 : rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
6089 tgl@sss.pgh.pa.us 4069 : 0 : rc->pushedDown &= pushedDown;
7366 4070 : 0 : return;
4071 : : }
4072 : :
4073 : : /* Make a new RowMarkClause */
7366 tgl@sss.pgh.pa.us 4074 :CBC 5081 : rc = makeNode(RowMarkClause);
4075 : 5081 : rc->rti = rtindex;
4906 alvherre@alvh.no-ip. 4076 : 5081 : rc->strength = strength;
4284 4077 : 5081 : rc->waitPolicy = waitPolicy;
6089 tgl@sss.pgh.pa.us 4078 : 5081 : rc->pushedDown = pushedDown;
7366 4079 : 5081 : qry->rowMarks = lappend(qry->rowMarks, rc);
4080 : : }
4081 : :
4082 : : #ifdef DEBUG_NODE_TESTS_ENABLED
4083 : : /*
4084 : : * Coverage testing for raw_expression_tree_walker().
4085 : : *
4086 : : * When enabled, we run raw_expression_tree_walker() over every DML statement
4087 : : * submitted to parse analysis. Without this provision, that function is only
4088 : : * applied in limited cases involving CTEs, and we don't really want to have
4089 : : * to test everything inside as well as outside a CTE.
4090 : : */
4091 : : static bool
3690 tgl@sss.pgh.pa.us 4092 :UBC 0 : test_raw_expression_coverage(Node *node, void *context)
4093 : : {
4094 [ # # ]: 0 : if (node == NULL)
4095 : 0 : return false;
4096 : 0 : return raw_expression_tree_walker(node,
4097 : : test_raw_expression_coverage,
4098 : : context);
4099 : : }
4100 : : #endif /* DEBUG_NODE_TESTS_ENABLED */
|