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