1/*-------------------------------------------------------------------------
2 *
3 * parsenodes.h
4 * definitions for parse tree nodes
5 *
6 * Many of the node types used in parsetrees include a "location" field.
7 * This is a byte (not character) offset in the original source text, to be
8 * used for positioning an error cursor when there is an error related to
9 * the node. Access to the original source text is needed to make use of
10 * the location. At the topmost (statement) level, we also provide a
11 * statement length, likewise measured in bytes, for convenience in
12 * identifying statement boundaries in multi-statement source strings.
13 *
14 *
15 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development PGGroup
16 * Portions Copyright (c) 1994, Regents of the University of California
17 *
18 * src/include/nodes/parsenodes.h
19 *
20 *-------------------------------------------------------------------------
21 */
22#pragma once
23
24#include "nodes/bitmapset.hpp"
25#include "nodes/lockoptions.hpp"
26#include "nodes/primnodes.hpp"
27#include "nodes/value.hpp"
28
29typedef enum PGOverridingKind
30{
31 PG_OVERRIDING_NOT_SET = 0,
32 PG_OVERRIDING_USER_VALUE,
33 OVERRIDING_SYSTEM_VALUE
34} PGOverridingKind;
35
36/* Possible sources of a PGQuery */
37typedef enum PGQuerySource
38{
39 PG_QSRC_ORIGINAL, /* original parsetree (explicit query) */
40 PG_QSRC_PARSER, /* added by parse analysis (now unused) */
41 PG_QSRC_INSTEAD_RULE, /* added by unconditional INSTEAD rule */
42 PG_QSRC_QUAL_INSTEAD_RULE, /* added by conditional INSTEAD rule */
43 QSRC_NON_INSTEAD_RULE /* added by non-INSTEAD rule */
44} PGQuerySource;
45
46/* PGSort ordering options for ORDER BY and CREATE INDEX */
47typedef enum PGSortByDir
48{
49 PG_SORTBY_DEFAULT,
50 PG_SORTBY_ASC,
51 PG_SORTBY_DESC,
52 SORTBY_USING /* not allowed in CREATE INDEX ... */
53} PGSortByDir;
54
55typedef enum PGSortByNulls
56{
57 PG_SORTBY_NULLS_DEFAULT,
58 PG_SORTBY_NULLS_FIRST,
59 SORTBY_NULLS_LAST
60} PGSortByNulls;
61
62/*****************************************************************************
63 * PGQuery Tree
64 *****************************************************************************/
65
66/*
67 * PGQuery -
68 * Parse analysis turns all statements into a PGQuery tree
69 * for further processing by the rewriter and planner.
70 *
71 * Utility statements (i.e. non-optimizable statements) have the
72 * utilityStmt field set, and the rest of the PGQuery is mostly dummy.
73 *
74 * Planning converts a PGQuery tree into a PGPlan tree headed by a PGPlannedStmt
75 * node --- the PGQuery structure is not used by the executor.
76 */
77typedef struct PGQuery
78{
79 PGNodeTag type;
80
81 PGCmdType commandType; /* select|insert|update|delete|utility */
82
83 PGQuerySource querySource; /* where did I come from? */
84
85 uint32_t queryId; /* query identifier (can be set by plugins) */
86
87 bool canSetTag; /* do I set the command result tag? */
88
89 PGNode *utilityStmt; /* non-null if commandType == PG_CMD_UTILITY */
90
91 int resultRelation; /* rtable index of target relation for
92 * INSERT/UPDATE/DELETE; 0 for SELECT */
93
94 bool hasAggs; /* has aggregates in tlist or havingQual */
95 bool hasWindowFuncs; /* has window functions in tlist */
96 bool hasTargetSRFs; /* has set-returning functions in tlist */
97 bool hasSubLinks; /* has subquery PGSubLink */
98 bool hasDistinctOn; /* distinctClause is from DISTINCT ON */
99 bool hasRecursive; /* WITH RECURSIVE was specified */
100 bool hasModifyingCTE; /* has INSERT/UPDATE/DELETE in WITH */
101 bool hasForUpdate; /* FOR [KEY] UPDATE/SHARE was specified */
102 bool hasRowSecurity; /* rewriter has applied some RLS policy */
103
104 PGList *cteList; /* WITH list (of CommonTableExpr's) */
105
106 PGList *rtable; /* list of range table entries */
107 PGFromExpr *jointree; /* table join tree (FROM and WHERE clauses) */
108
109 PGList *targetList; /* target list (of PGTargetEntry) */
110
111 PGOverridingKind override; /* OVERRIDING clause */
112
113 PGOnConflictExpr *onConflict; /* ON CONFLICT DO [NOTHING | UPDATE] */
114
115 PGList *returningList; /* return-values list (of PGTargetEntry) */
116
117 PGList *groupClause; /* a list of SortGroupClause's */
118
119 PGList *groupingSets; /* a list of GroupingSet's if present */
120
121 PGNode *havingQual; /* qualifications applied to groups */
122
123 PGList *windowClause; /* a list of WindowClause's */
124
125 PGList *distinctClause; /* a list of SortGroupClause's */
126
127 PGList *sortClause; /* a list of SortGroupClause's */
128
129 PGNode *limitOffset; /* # of result tuples to skip (int8_t expr) */
130 PGNode *limitCount; /* # of result tuples to return (int8_t expr) */
131
132 PGList *rowMarks; /* a list of RowMarkClause's */
133
134 PGNode *setOperations; /* set-operation tree if this is top level of
135 * a UNION/INTERSECT/EXCEPT query */
136
137 PGList *constraintDeps; /* a list of pg_constraint OIDs that the query
138 * depends on to be semantically valid */
139
140 PGList *withCheckOptions; /* a list of WithCheckOption's, which are
141 * only added during rewrite and therefore
142 * are not written out as part of Query. */
143
144 /*
145 * The following two fields identify the portion of the source text string
146 * containing this query. They are typically only populated in top-level
147 * Queries, not in sub-queries. When not set, they might both be zero, or
148 * both be -1 meaning "unknown".
149 */
150 int stmt_location; /* start location, or -1 if unknown */
151 int stmt_len; /* length in bytes; 0 means "rest of string" */
152} PGQuery;
153
154
155/****************************************************************************
156 * Supporting data structures for Parse Trees
157 *
158 * Most of these node types appear in raw parsetrees output by the grammar,
159 * and get transformed to something else by the analyzer. A few of them
160 * are used as-is in transformed querytrees.
161 ****************************************************************************/
162
163/*
164 * PGTypeName - specifies a type in definitions
165 *
166 * For PGTypeName structures generated internally, it is often easier to
167 * specify the type by OID than by name. If "names" is NIL then the
168 * actual type OID is given by typeOid, otherwise typeOid is unused.
169 * Similarly, if "typmods" is NIL then the actual typmod is expected to
170 * be prespecified in typemod, otherwise typemod is unused.
171 *
172 * If pct_type is true, then names is actually a field name and we look up
173 * the type of that field. Otherwise (the normal case), names is a type
174 * name possibly qualified with schema and database name.
175 */
176typedef struct PGTypeName
177{
178 PGNodeTag type;
179 PGList *names; /* qualified name (list of PGValue strings) */
180 PGOid typeOid; /* type identified by OID */
181 bool setof; /* is a set? */
182 bool pct_type; /* %TYPE specified? */
183 PGList *typmods; /* type modifier expression(s) */
184 int32_t typemod; /* prespecified type modifier */
185 PGList *arrayBounds; /* array bounds */
186 int location; /* token location, or -1 if unknown */
187} PGTypeName;
188
189/*
190 * PGColumnRef - specifies a reference to a column, or possibly a whole tuple
191 *
192 * The "fields" list must be nonempty. It can contain string PGValue nodes
193 * (representing names) and PGAStar nodes (representing occurrence of a '*').
194 * Currently, PGAStar must appear only as the last list element --- the grammar
195 * is responsible for enforcing this!
196 *
197 * Note: any array subscripting or selection of fields from composite columns
198 * is represented by an PGAIndirection node above the ColumnRef. However,
199 * for simplicity in the normal case, initial field selection from a table
200 * name is represented within PGColumnRef and not by adding AIndirection.
201 */
202typedef struct PGColumnRef
203{
204 PGNodeTag type;
205 PGList *fields; /* field names (PGValue strings) or PGAStar */
206 int location; /* token location, or -1 if unknown */
207} PGColumnRef;
208
209/*
210 * PGParamRef - specifies a $n parameter reference
211 */
212typedef struct PGParamRef
213{
214 PGNodeTag type;
215 int number; /* the number of the parameter */
216 int location; /* token location, or -1 if unknown */
217} PGParamRef;
218
219/*
220 * PGAExpr - infix, prefix, and postfix expressions
221 */
222typedef enum PGAExpr_Kind
223{
224 PG_AEXPR_OP, /* normal operator */
225 PG_AEXPR_OP_ANY, /* scalar op ANY (array) */
226 PG_AEXPR_OP_ALL, /* scalar op ALL (array) */
227 PG_AEXPR_DISTINCT, /* IS DISTINCT FROM - name must be "=" */
228 PG_AEXPR_NOT_DISTINCT, /* IS NOT DISTINCT FROM - name must be "=" */
229 PG_AEXPR_NULLIF, /* NULLIF - name must be "=" */
230 PG_AEXPR_OF, /* IS [NOT] OF - name must be "=" or "<>" */
231 PG_AEXPR_IN, /* [NOT] IN - name must be "=" or "<>" */
232 PG_AEXPR_LIKE, /* [NOT] LIKE - name must be "~~" or "!~~" */
233 PG_AEXPR_ILIKE, /* [NOT] ILIKE - name must be "~~*" or "!~~*" */
234 PG_AEXPR_SIMILAR, /* [NOT] SIMILAR - name must be "~" or "!~" */
235 PG_AEXPR_BETWEEN, /* name must be "BETWEEN" */
236 PG_AEXPR_NOT_BETWEEN, /* name must be "NOT BETWEEN" */
237 PG_AEXPR_BETWEEN_SYM, /* name must be "BETWEEN SYMMETRIC" */
238 PG_AEXPR_NOT_BETWEEN_SYM, /* name must be "NOT BETWEEN SYMMETRIC" */
239 AEXPR_PAREN /* nameless dummy node for parentheses */
240} PGAExpr_Kind;
241
242typedef struct PGAExpr
243{
244 PGNodeTag type;
245 PGAExpr_Kind kind; /* see above */
246 PGList *name; /* possibly-qualified name of operator */
247 PGNode *lexpr; /* left argument, or NULL if none */
248 PGNode *rexpr; /* right argument, or NULL if none */
249 int location; /* token location, or -1 if unknown */
250} PGAExpr;
251
252/*
253 * PGAConst - a literal constant
254 */
255typedef struct PGAConst
256{
257 PGNodeTag type;
258 PGValue val; /* value (includes type info, see value.h) */
259 int location; /* token location, or -1 if unknown */
260} PGAConst;
261
262/*
263 * PGTypeCast - a CAST expression
264 */
265typedef struct PGTypeCast
266{
267 PGNodeTag type;
268 PGNode *arg; /* the expression being casted */
269 PGTypeName *typeName; /* the target type */
270 int location; /* token location, or -1 if unknown */
271} PGTypeCast;
272
273/*
274 * PGCollateClause - a COLLATE expression
275 */
276typedef struct PGCollateClause
277{
278 PGNodeTag type;
279 PGNode *arg; /* input expression */
280 PGList *collname; /* possibly-qualified collation name */
281 int location; /* token location, or -1 if unknown */
282} PGCollateClause;
283
284/*
285 * PGFuncCall - a function or aggregate invocation
286 *
287 * agg_order (if not NIL) indicates we saw 'foo(... ORDER BY ...)', or if
288 * agg_within_group is true, it was 'foo(...) WITHIN GROUP (ORDER BY ...)'.
289 * agg_star indicates we saw a 'foo(*)' construct, while agg_distinct
290 * indicates we saw 'foo(DISTINCT ...)'. In any of these cases, the
291 * construct *must* be an aggregate call. Otherwise, it might be either an
292 * aggregate or some other kind of function. However, if FILTER or OVER is
293 * present it had better be an aggregate or window function.
294 *
295 * Normally, you'd initialize this via makeFuncCall() and then only change the
296 * parts of the struct its defaults don't match afterwards, as needed.
297 */
298typedef struct PGFuncCall
299{
300 PGNodeTag type;
301 PGList *funcname; /* qualified name of function */
302 PGList *args; /* the arguments (list of exprs) */
303 PGList *agg_order; /* ORDER BY (list of PGSortBy) */
304 PGNode *agg_filter; /* FILTER clause, if any */
305 bool agg_within_group; /* ORDER BY appeared in WITHIN GROUP */
306 bool agg_star; /* argument was really '*' */
307 bool agg_distinct; /* arguments were labeled DISTINCT */
308 bool func_variadic; /* last argument was labeled VARIADIC */
309 struct PGWindowDef *over; /* OVER clause, if any */
310 int location; /* token location, or -1 if unknown */
311} PGFuncCall;
312
313/*
314 * PGAStar - '*' representing all columns of a table or compound field
315 *
316 * This can appear within ColumnRef.fields, AIndirection.indirection, and
317 * ResTarget.indirection lists.
318 */
319typedef struct PGAStar
320{
321 PGNodeTag type;
322} PGAStar;
323
324/*
325 * PGAIndices - array subscript or slice bounds ([idx] or [lidx:uidx])
326 *
327 * In slice case, either or both of lidx and uidx can be NULL (omitted).
328 * In non-slice case, uidx holds the single subscript and lidx is always NULL.
329 */
330typedef struct PGAIndices
331{
332 PGNodeTag type;
333 bool is_slice; /* true if slice (i.e., colon present) */
334 PGNode *lidx; /* slice lower bound, if any */
335 PGNode *uidx; /* subscript, or slice upper bound if any */
336} PGAIndices;
337
338/*
339 * PGAIndirection - select a field and/or array element from an expression
340 *
341 * The indirection list can contain PGAIndices nodes (representing
342 * subscripting), string PGValue nodes (representing field selection --- the
343 * string value is the name of the field to select), and PGAStar nodes
344 * (representing selection of all fields of a composite type).
345 * For example, a complex selection operation like
346 * (foo).field1[42][7].field2
347 * would be represented with a single PGAIndirection node having a 4-element
348 * indirection list.
349 *
350 * Currently, PGAStar must appear only as the last list element --- the grammar
351 * is responsible for enforcing this!
352 */
353typedef struct PGAIndirection
354{
355 PGNodeTag type;
356 PGNode *arg; /* the thing being selected from */
357 PGList *indirection; /* subscripts and/or field names and/or * */
358} PGAIndirection;
359
360/*
361 * PGAArrayExpr - an ARRAY[] construct
362 */
363typedef struct PGAArrayExpr
364{
365 PGNodeTag type;
366 PGList *elements; /* array element expressions */
367 int location; /* token location, or -1 if unknown */
368} PGAArrayExpr;
369
370/*
371 * PGResTarget -
372 * result target (used in target list of pre-transformed parse trees)
373 *
374 * In a SELECT target list, 'name' is the column label from an
375 * 'AS ColumnLabel' clause, or NULL if there was none, and 'val' is the
376 * value expression itself. The 'indirection' field is not used.
377 *
378 * INSERT uses PGResTarget in its target-column-names list. Here, 'name' is
379 * the name of the destination column, 'indirection' stores any subscripts
380 * attached to the destination, and 'val' is not used.
381 *
382 * In an UPDATE target list, 'name' is the name of the destination column,
383 * 'indirection' stores any subscripts attached to the destination, and
384 * 'val' is the expression to assign.
385 *
386 * See PGAIndirection for more info about what can appear in 'indirection'.
387 */
388typedef struct PGResTarget
389{
390 PGNodeTag type;
391 char *name; /* column name or NULL */
392 PGList *indirection; /* subscripts, field names, and '*', or NIL */
393 PGNode *val; /* the value expression to compute or assign */
394 int location; /* token location, or -1 if unknown */
395} PGResTarget;
396
397/*
398 * PGMultiAssignRef - element of a row source expression for UPDATE
399 *
400 * In an UPDATE target list, when we have SET (a,b,c) = row-valued-expression,
401 * we generate separate PGResTarget items for each of a,b,c. Their "val" trees
402 * are PGMultiAssignRef nodes numbered 1..n, linking to a common copy of the
403 * row-valued-expression (which parse analysis will process only once, when
404 * handling the PGMultiAssignRef with colno=1).
405 */
406typedef struct PGMultiAssignRef
407{
408 PGNodeTag type;
409 PGNode *source; /* the row-valued expression */
410 int colno; /* column number for this target (1..n) */
411 int ncolumns; /* number of targets in the construct */
412} PGMultiAssignRef;
413
414/*
415 * PGSortBy - for ORDER BY clause
416 */
417typedef struct PGSortBy
418{
419 PGNodeTag type;
420 PGNode *node; /* expression to sort on */
421 PGSortByDir sortby_dir; /* ASC/DESC/USING/default */
422 PGSortByNulls sortby_nulls; /* NULLS FIRST/LAST */
423 PGList *useOp; /* name of op to use, if SORTBY_USING */
424 int location; /* operator location, or -1 if none/unknown */
425} PGSortBy;
426
427/*
428 * PGWindowDef - raw representation of WINDOW and OVER clauses
429 *
430 * For entries in a WINDOW list, "name" is the window name being defined.
431 * For OVER clauses, we use "name" for the "OVER window" syntax, or "refname"
432 * for the "OVER (window)" syntax, which is subtly different --- the latter
433 * implies overriding the window frame clause.
434 */
435typedef struct PGWindowDef
436{
437 PGNodeTag type;
438 char *name; /* window's own name */
439 char *refname; /* referenced window name, if any */
440 PGList *partitionClause; /* PARTITION BY expression list */
441 PGList *orderClause; /* ORDER BY (list of PGSortBy) */
442 int frameOptions; /* frame_clause options, see below */
443 PGNode *startOffset; /* expression for starting bound, if any */
444 PGNode *endOffset; /* expression for ending bound, if any */
445 int location; /* parse location, or -1 if none/unknown */
446} PGWindowDef;
447
448/*
449 * frameOptions is an OR of these bits. The NONDEFAULT and BETWEEN bits are
450 * used so that ruleutils.c can tell which properties were specified and
451 * which were defaulted; the correct behavioral bits must be set either way.
452 * The START_foo and END_foo options must come in pairs of adjacent bits for
453 * the convenience of gram.y, even though some of them are useless/invalid.
454 * We will need more bits (and fields) to cover the full SQL:2008 option set.
455 */
456#define FRAMEOPTION_NONDEFAULT 0x00001 /* any specified? */
457#define FRAMEOPTION_RANGE 0x00002 /* RANGE behavior */
458#define FRAMEOPTION_ROWS 0x00004 /* ROWS behavior */
459#define FRAMEOPTION_BETWEEN 0x00008 /* BETWEEN given? */
460#define FRAMEOPTION_START_UNBOUNDED_PRECEDING 0x00010 /* start is U. P. */
461#define FRAMEOPTION_END_UNBOUNDED_PRECEDING 0x00020 /* (disallowed) */
462#define FRAMEOPTION_START_UNBOUNDED_FOLLOWING 0x00040 /* (disallowed) */
463#define FRAMEOPTION_END_UNBOUNDED_FOLLOWING 0x00080 /* end is U. F. */
464#define FRAMEOPTION_START_CURRENT_ROW 0x00100 /* start is C. R. */
465#define FRAMEOPTION_END_CURRENT_ROW 0x00200 /* end is C. R. */
466#define FRAMEOPTION_START_VALUE_PRECEDING 0x00400 /* start is V. P. */
467#define FRAMEOPTION_END_VALUE_PRECEDING 0x00800 /* end is V. P. */
468#define FRAMEOPTION_START_VALUE_FOLLOWING 0x01000 /* start is V. F. */
469#define FRAMEOPTION_END_VALUE_FOLLOWING 0x02000 /* end is V. F. */
470
471#define FRAMEOPTION_START_VALUE \
472 (FRAMEOPTION_START_VALUE_PRECEDING | FRAMEOPTION_START_VALUE_FOLLOWING)
473#define FRAMEOPTION_END_VALUE \
474 (FRAMEOPTION_END_VALUE_PRECEDING | FRAMEOPTION_END_VALUE_FOLLOWING)
475
476#define FRAMEOPTION_DEFAULTS \
477 (FRAMEOPTION_RANGE | FRAMEOPTION_START_UNBOUNDED_PRECEDING | \
478 FRAMEOPTION_END_CURRENT_ROW)
479
480/*
481 * PGRangeSubselect - subquery appearing in a FROM clause
482 */
483typedef struct PGRangeSubselect
484{
485 PGNodeTag type;
486 bool lateral; /* does it have LATERAL prefix? */
487 PGNode *subquery; /* the untransformed sub-select clause */
488 PGAlias *alias; /* table alias & optional column aliases */
489} PGRangeSubselect;
490
491/*
492 * PGRangeFunction - function call appearing in a FROM clause
493 *
494 * functions is a PGList because we use this to represent the construct
495 * ROWS FROM(func1(...), func2(...), ...). Each element of this list is a
496 * two-element sublist, the first element being the untransformed function
497 * call tree, and the second element being a possibly-empty list of PGColumnDef
498 * nodes representing any columndef list attached to that function within the
499 * ROWS FROM() syntax.
500 *
501 * alias and coldeflist represent any alias and/or columndef list attached
502 * at the top level. (We disallow coldeflist appearing both here and
503 * per-function, but that's checked in parse analysis, not by the grammar.)
504 */
505typedef struct PGRangeFunction
506{
507 PGNodeTag type;
508 bool lateral; /* does it have LATERAL prefix? */
509 bool ordinality; /* does it have WITH ORDINALITY suffix? */
510 bool is_rowsfrom; /* is result of ROWS FROM() syntax? */
511 PGList *functions; /* per-function information, see above */
512 PGAlias *alias; /* table alias & optional column aliases */
513 PGList *coldeflist; /* list of PGColumnDef nodes to describe result
514 * of function returning RECORD */
515} PGRangeFunction;
516
517/*
518 * PGRangeTableSample - TABLESAMPLE appearing in a raw FROM clause
519 *
520 * This node, appearing only in raw parse trees, represents
521 * <relation> TABLESAMPLE <method> (<params>) REPEATABLE (<num>)
522 * Currently, the <relation> can only be a PGRangeVar, but we might in future
523 * allow PGRangeSubselect and other options. Note that the PGRangeTableSample
524 * is wrapped around the node representing the <relation>, rather than being
525 * a subfield of it.
526 */
527typedef struct PGRangeTableSample
528{
529 PGNodeTag type;
530 PGNode *relation; /* relation to be sampled */
531 PGList *method; /* sampling method name (possibly qualified) */
532 PGList *args; /* argument(s) for sampling method */
533 PGNode *repeatable; /* REPEATABLE expression, or NULL if none */
534 int location; /* method name location, or -1 if unknown */
535} PGRangeTableSample;
536
537/*
538 * PGColumnDef - column definition (used in various creates)
539 *
540 * If the column has a default value, we may have the value expression
541 * in either "raw" form (an untransformed parse tree) or "cooked" form
542 * (a post-parse-analysis, executable expression tree), depending on
543 * how this PGColumnDef node was created (by parsing, or by inheritance
544 * from an existing relation). We should never have both in the same node!
545 *
546 * Similarly, we may have a COLLATE specification in either raw form
547 * (represented as a PGCollateClause with arg==NULL) or cooked form
548 * (the collation's OID).
549 *
550 * The constraints list may contain a PG_CONSTR_DEFAULT item in a raw
551 * parsetree produced by gram.y, but transformCreateStmt will remove
552 * the item and set raw_default instead. PG_CONSTR_DEFAULT items
553 * should not appear in any subsequent processing.
554 */
555typedef struct PGColumnDef
556{
557 PGNodeTag type;
558 char *colname; /* name of column */
559 PGTypeName *typeName; /* type of column */
560 int inhcount; /* number of times column is inherited */
561 bool is_local; /* column has local (non-inherited) def'n */
562 bool is_not_null; /* NOT NULL constraint specified? */
563 bool is_from_type; /* column definition came from table type */
564 bool is_from_parent; /* column def came from partition parent */
565 char storage; /* attstorage setting, or 0 for default */
566 PGNode *raw_default; /* default value (untransformed parse tree) */
567 PGNode *cooked_default; /* default value (transformed expr tree) */
568 char identity; /* attidentity setting */
569 PGRangeVar *identitySequence; /* to store identity sequence name for ALTER
570 * TABLE ... ADD COLUMN */
571 PGCollateClause *collClause; /* untransformed COLLATE spec, if any */
572 PGOid collOid; /* collation OID (InvalidOid if not set) */
573 PGList *constraints; /* other constraints on column */
574 PGList *fdwoptions; /* per-column FDW options */
575 int location; /* parse location, or -1 if none/unknown */
576} PGColumnDef;
577
578/*
579 * PGTableLikeClause - CREATE TABLE ( ... LIKE ... ) clause
580 */
581typedef struct PGTableLikeClause
582{
583 PGNodeTag type;
584 PGRangeVar *relation;
585 uint32_t options; /* OR of PGTableLikeOption flags */
586} PGTableLikeClause;
587
588typedef enum PGTableLikeOption
589{
590 PG_CREATE_TABLE_LIKE_DEFAULTS = 1 << 0,
591 PG_CREATE_TABLE_LIKE_CONSTRAINTS = 1 << 1,
592 PG_CREATE_TABLE_LIKE_IDENTITY = 1 << 2,
593 PG_CREATE_TABLE_LIKE_INDEXES = 1 << 3,
594 PG_CREATE_TABLE_LIKE_STORAGE = 1 << 4,
595 PG_CREATE_TABLE_LIKE_COMMENTS = 1 << 5,
596 PG_CREATE_TABLE_LIKE_STATISTICS = 1 << 6,
597 PG_CREATE_TABLE_LIKE_ALL = INT_MAX
598} PGTableLikeOption;
599
600/*
601 * PGIndexElem - index parameters (used in CREATE INDEX, and in ON CONFLICT)
602 *
603 * For a plain index attribute, 'name' is the name of the table column to
604 * index, and 'expr' is NULL. For an index expression, 'name' is NULL and
605 * 'expr' is the expression tree.
606 */
607typedef struct PGIndexElem
608{
609 PGNodeTag type;
610 char *name; /* name of attribute to index, or NULL */
611 PGNode *expr; /* expression to index, or NULL */
612 char *indexcolname; /* name for index column; NULL = default */
613 PGList *collation; /* name of collation; NIL = default */
614 PGList *opclass; /* name of desired opclass; NIL = default */
615 PGSortByDir ordering; /* ASC/DESC/default */
616 PGSortByNulls nulls_ordering; /* FIRST/LAST/default */
617} PGIndexElem;
618
619/*
620 * PGDefElem - a generic "name = value" option definition
621 *
622 * In some contexts the name can be qualified. Also, certain SQL commands
623 * allow a SET/ADD/DROP action to be attached to option settings, so it's
624 * convenient to carry a field for that too. (Note: currently, it is our
625 * practice that the grammar allows namespace and action only in statements
626 * where they are relevant; C code can just ignore those fields in other
627 * statements.)
628 */
629typedef enum PGDefElemAction
630{
631 PG_DEFELEM_UNSPEC, /* no action given */
632 PG_DEFELEM_SET,
633 PG_DEFELEM_ADD,
634 DEFELEM_DROP
635} PGDefElemAction;
636
637typedef struct PGDefElem
638{
639 PGNodeTag type;
640 char *defnamespace; /* NULL if unqualified name */
641 char *defname;
642 PGNode *arg; /* a (PGValue *) or a (PGTypeName *) */
643 PGDefElemAction defaction; /* unspecified action, or SET/ADD/DROP */
644 int location; /* token location, or -1 if unknown */
645} PGDefElem;
646
647/*
648 * PGLockingClause - raw representation of FOR [NO KEY] UPDATE/[KEY] SHARE
649 * options
650 *
651 * Note: lockedRels == NIL means "all relations in query". Otherwise it
652 * is a list of PGRangeVar nodes. (We use PGRangeVar mainly because it carries
653 * a location field --- currently, parse analysis insists on unqualified
654 * names in LockingClause.)
655 */
656typedef struct PGLockingClause
657{
658 PGNodeTag type;
659 PGList *lockedRels; /* FOR [KEY] UPDATE/SHARE relations */
660 PGLockClauseStrength strength;
661 PGLockWaitPolicy waitPolicy; /* NOWAIT and SKIP LOCKED */
662} PGLockingClause;
663
664/****************************************************************************
665 * Nodes for a PGQuery tree
666 ****************************************************************************/
667
668/*--------------------
669 * PGRangeTblEntry -
670 * A range table is a PGList of PGRangeTblEntry nodes.
671 *
672 * A range table entry may represent a plain relation, a sub-select in
673 * FROM, or the result of a JOIN clause. (Only explicit JOIN syntax
674 * produces an RTE, not the implicit join resulting from multiple FROM
675 * items. This is because we only need the RTE to deal with SQL features
676 * like outer joins and join-output-column aliasing.) Other special
677 * RTE types also exist, as indicated by RTEKind.
678 *
679 * Note that we consider PG_RTE_RELATION to cover anything that has a pg_class
680 * entry. relkind distinguishes the sub-cases.
681 *
682 * alias is an PGAlias node representing the AS alias-clause attached to the
683 * FROM expression, or NULL if no clause.
684 *
685 * eref is the table reference name and column reference names (either
686 * real or aliases). Note that system columns (OID etc) are not included
687 * in the column list.
688 * eref->aliasname is required to be present, and should generally be used
689 * to identify the RTE for error messages etc.
690 *
691 * In RELATION RTEs, the colnames in both alias and eref are indexed by
692 * physical attribute number; this means there must be colname entries for
693 * dropped columns. When building an RTE we insert empty strings ("") for
694 * dropped columns. Note however that a stored rule may have nonempty
695 * colnames for columns dropped since the rule was created (and for that
696 * matter the colnames might be out of date due to column renamings).
697 * The same comments apply to FUNCTION RTEs when a function's return type
698 * is a named composite type.
699 *
700 * In JOIN RTEs, the colnames in both alias and eref are one-to-one with
701 * joinaliasvars entries. A JOIN RTE will omit columns of its inputs when
702 * those columns are known to be dropped at parse time. Again, however,
703 * a stored rule might contain entries for columns dropped since the rule
704 * was created. (This is only possible for columns not actually referenced
705 * in the rule.) When loading a stored rule, we replace the joinaliasvars
706 * items for any such columns with null pointers. (We can't simply delete
707 * them from the joinaliasvars list, because that would affect the attnums
708 * of Vars referencing the rest of the list.)
709 *
710 * inh is true for relation references that should be expanded to include
711 * inheritance children, if the rel has any. This *must* be false for
712 * RTEs other than PG_RTE_RELATION entries.
713 *
714 * inFromCl marks those range variables that are listed in the FROM clause.
715 * It's false for RTEs that are added to a query behind the scenes, such
716 * as the NEW and OLD variables for a rule, or the subqueries of a UNION.
717 * This flag is not used anymore during parsing, since the parser now uses
718 * a separate "namespace" data structure to control visibility, but it is
719 * needed by ruleutils.c to determine whether RTEs should be shown in
720 * decompiled queries.
721 *--------------------
722 */
723typedef enum PGRTEKind
724{
725 PG_RTE_RELATION, /* ordinary relation reference */
726 PG_RTE_SUBQUERY, /* subquery in FROM */
727 PG_RTE_JOIN, /* join */
728 PG_RTE_FUNCTION, /* function in FROM */
729 PG_RTE_TABLEFUNC, /* TableFunc(.., column list) */
730 PG_RTE_VALUES, /* VALUES (<exprlist>), (<exprlist>), ... */
731 PG_RTE_CTE, /* common table expr (WITH list element) */
732 RTE_NAMEDTUPLESTORE /* tuplestore, e.g. for AFTER triggers */
733} PGRTEKind;
734
735typedef struct PGRangeTblEntry
736{
737 PGNodeTag type;
738
739 PGRTEKind rtekind; /* see above */
740
741 /*
742 * XXX the fields applicable to only some rte kinds should be merged into
743 * a union. I didn't do this yet because the diffs would impact a lot of
744 * code that is being actively worked on. FIXME someday.
745 */
746
747 /*
748 * Fields valid for a plain relation RTE (else zero):
749 *
750 * As a special case, RTE_NAMEDTUPLESTORE can also set relid to indicate
751 * that the tuple format of the tuplestore is the same as the referenced
752 * relation. This allows plans referencing AFTER trigger transition
753 * tables to be invalidated if the underlying table is altered.
754 */
755 PGOid relid; /* OID of the relation */
756 char relkind; /* relation kind (see pg_class.relkind) */
757 struct PGTableSampleClause *tablesample; /* sampling info, or NULL */
758
759 /*
760 * Fields valid for a subquery RTE (else NULL):
761 */
762 PGQuery *subquery; /* the sub-query */
763
764 /*
765 * Fields valid for a join RTE (else NULL/zero):
766 *
767 * joinaliasvars is a list of (usually) Vars corresponding to the columns
768 * of the join result. An alias PGVar referencing column K of the join
769 * result can be replaced by the K'th element of joinaliasvars --- but to
770 * simplify the task of reverse-listing aliases correctly, we do not do
771 * that until planning time. In detail: an element of joinaliasvars can
772 * be a PGVar of one of the join's input relations, or such a PGVar with an
773 * implicit coercion to the join's output column type, or a COALESCE
774 * expression containing the two input column Vars (possibly coerced).
775 * Within a PGQuery loaded from a stored rule, it is also possible for
776 * joinaliasvars items to be null pointers, which are placeholders for
777 * (necessarily unreferenced) columns dropped since the rule was made.
778 * Also, once planning begins, joinaliasvars items can be almost anything,
779 * as a result of subquery-flattening substitutions.
780 */
781 PGJoinType jointype; /* type of join */
782 PGList *joinaliasvars; /* list of alias-var expansions */
783
784 /*
785 * Fields valid for a function RTE (else NIL/zero):
786 *
787 * When funcordinality is true, the eref->colnames list includes an alias
788 * for the ordinality column. The ordinality column is otherwise
789 * implicit, and must be accounted for "by hand" in places such as
790 * expandRTE().
791 */
792 PGList *functions; /* list of PGRangeTblFunction nodes */
793 bool funcordinality; /* is this called WITH ORDINALITY? */
794
795 /*
796 * Fields valid for a PGTableFunc RTE (else NULL):
797 */
798 PGTableFunc *tablefunc;
799
800 /*
801 * Fields valid for a values RTE (else NIL):
802 */
803 PGList *values_lists; /* list of expression lists */
804
805 /*
806 * Fields valid for a CTE RTE (else NULL/zero):
807 */
808 char *ctename; /* name of the WITH list item */
809 PGIndex ctelevelsup; /* number of query levels up */
810 bool self_reference; /* is this a recursive self-reference? */
811
812 /*
813 * Fields valid for table functions, values, CTE and ENR RTEs (else NIL):
814 *
815 * We need these for CTE RTEs so that the types of self-referential
816 * columns are well-defined. For VALUES RTEs, storing these explicitly
817 * saves having to re-determine the info by scanning the values_lists. For
818 * ENRs, we store the types explicitly here (we could get the information
819 * from the catalogs if 'relid' was supplied, but we'd still need these
820 * for TupleDesc-based ENRs, so we might as well always store the type
821 * info here).
822 *
823 * For ENRs only, we have to consider the possibility of dropped columns.
824 * A dropped column is included in these lists, but it will have zeroes in
825 * all three lists (as well as an empty-string entry in eref). Testing
826 * for zero coltype is the standard way to detect a dropped column.
827 */
828 PGList *coltypes; /* OID list of column type OIDs */
829 PGList *coltypmods; /* integer list of column typmods */
830 PGList *colcollations; /* OID list of column collation OIDs */
831
832 /*
833 * Fields valid for ENR RTEs (else NULL/zero):
834 */
835 char *enrname; /* name of ephemeral named relation */
836 double enrtuples; /* estimated or actual from caller */
837
838 /*
839 * Fields valid in all RTEs:
840 */
841 PGAlias *alias; /* user-written alias clause, if any */
842 PGAlias *eref; /* expanded reference names */
843 bool lateral; /* subquery, function, or values is LATERAL? */
844 bool inh; /* inheritance requested? */
845 bool inFromCl; /* present in FROM clause? */
846} PGRangeTblEntry;
847
848/*
849 * PGRangeTblFunction -
850 * PGRangeTblEntry subsidiary data for one function in a FUNCTION RTE.
851 *
852 * If the function had a column definition list (required for an
853 * otherwise-unspecified RECORD result), funccolnames lists the names given
854 * in the definition list, funccoltypes lists their declared column types,
855 * funccoltypmods lists their typmods, funccolcollations their collations.
856 * Otherwise, those fields are NIL.
857 *
858 * Notice we don't attempt to store info about the results of functions
859 * returning named composite types, because those can change from time to
860 * time. We do however remember how many columns we thought the type had
861 * (including dropped columns!), so that we can successfully ignore any
862 * columns added after the query was parsed.
863 */
864typedef struct PGRangeTblFunction
865{
866 PGNodeTag type;
867
868 PGNode *funcexpr; /* expression tree for func call */
869 int funccolcount; /* number of columns it contributes to RTE */
870 /* These fields record the contents of a column definition list, if any: */
871 PGList *funccolnames; /* column names (list of String) */
872 PGList *funccoltypes; /* OID list of column type OIDs */
873 PGList *funccoltypmods; /* integer list of column typmods */
874 PGList *funccolcollations; /* OID list of column collation OIDs */
875 /* This is set during planning for use by the executor: */
876 PGBitmapset *funcparams; /* PG_PARAM_EXEC PGParam IDs affecting this func */
877} PGRangeTblFunction;
878
879/*
880 * PGTableSampleClause - TABLESAMPLE appearing in a transformed FROM clause
881 *
882 * Unlike PGRangeTableSample, this is a subnode of the relevant RangeTblEntry.
883 */
884typedef struct PGTableSampleClause
885{
886 PGNodeTag type;
887 PGOid tsmhandler; /* OID of the tablesample handler function */
888 PGList *args; /* tablesample argument expression(s) */
889 PGExpr *repeatable; /* REPEATABLE expression, or NULL if none */
890} PGTableSampleClause;
891
892/*
893 * PGSortGroupClause -
894 * representation of ORDER BY, GROUP BY, PARTITION BY,
895 * DISTINCT, DISTINCT ON items
896 *
897 * You might think that ORDER BY is only interested in defining ordering,
898 * and GROUP/DISTINCT are only interested in defining equality. However,
899 * one way to implement grouping is to sort and then apply a "uniq"-like
900 * filter. So it's also interesting to keep track of possible sort operators
901 * for GROUP/DISTINCT, and in particular to try to sort for the grouping
902 * in a way that will also yield a requested ORDER BY ordering. So we need
903 * to be able to compare ORDER BY and GROUP/DISTINCT lists, which motivates
904 * the decision to give them the same representation.
905 *
906 * tleSortGroupRef must match ressortgroupref of exactly one entry of the
907 * query's targetlist; that is the expression to be sorted or grouped by.
908 * eqop is the OID of the equality operator.
909 * sortop is the OID of the ordering operator (a "<" or ">" operator),
910 * or InvalidOid if not available.
911 * nulls_first means about what you'd expect. If sortop is InvalidOid
912 * then nulls_first is meaningless and should be set to false.
913 * hashable is true if eqop is hashable (note this condition also depends
914 * on the datatype of the input expression).
915 *
916 * In an ORDER BY item, all fields must be valid. (The eqop isn't essential
917 * here, but it's cheap to get it along with the sortop, and requiring it
918 * to be valid eases comparisons to grouping items.) Note that this isn't
919 * actually enough information to determine an ordering: if the sortop is
920 * collation-sensitive, a collation OID is needed too. We don't store the
921 * collation in PGSortGroupClause because it's not available at the time the
922 * parser builds the PGSortGroupClause; instead, consult the exposed collation
923 * of the referenced targetlist expression to find out what it is.
924 *
925 * In a grouping item, eqop must be valid. If the eqop is a btree equality
926 * operator, then sortop should be set to a compatible ordering operator.
927 * We prefer to set eqop/sortop/nulls_first to match any ORDER BY item that
928 * the query presents for the same tlist item. If there is none, we just
929 * use the default ordering op for the datatype.
930 *
931 * If the tlist item's type has a hash opclass but no btree opclass, then
932 * we will set eqop to the hash equality operator, sortop to InvalidOid,
933 * and nulls_first to false. A grouping item of this kind can only be
934 * implemented by hashing, and of course it'll never match an ORDER BY item.
935 *
936 * The hashable flag is provided since we generally have the requisite
937 * information readily available when the PGSortGroupClause is constructed,
938 * and it's relatively expensive to get it again later. Note there is no
939 * need for a "sortable" flag since OidIsValid(sortop) serves the purpose.
940 *
941 * A query might have both ORDER BY and DISTINCT (or DISTINCT ON) clauses.
942 * In SELECT DISTINCT, the distinctClause list is as long or longer than the
943 * sortClause list, while in SELECT DISTINCT ON it's typically shorter.
944 * The two lists must match up to the end of the shorter one --- the parser
945 * rearranges the distinctClause if necessary to make this true. (This
946 * restriction ensures that only one sort step is needed to both satisfy the
947 * ORDER BY and set up for the PGUnique step. This is semantically necessary
948 * for DISTINCT ON, and presents no real drawback for DISTINCT.)
949 */
950typedef struct PGSortGroupClause
951{
952 PGNodeTag type;
953 PGIndex tleSortGroupRef; /* reference into targetlist */
954 PGOid eqop; /* the equality operator ('=' op) */
955 PGOid sortop; /* the ordering operator ('<' op), or 0 */
956 bool nulls_first; /* do NULLs come before normal values? */
957 bool hashable; /* can eqop be implemented by hashing? */
958} PGSortGroupClause;
959
960/*
961 * PGGroupingSet -
962 * representation of CUBE, ROLLUP and GROUPING SETS clauses
963 *
964 * In a PGQuery with grouping sets, the groupClause contains a flat list of
965 * PGSortGroupClause nodes for each distinct expression used. The actual
966 * structure of the GROUP BY clause is given by the groupingSets tree.
967 *
968 * In the raw parser output, PGGroupingSet nodes (of all types except SIMPLE
969 * which is not used) are potentially mixed in with the expressions in the
970 * groupClause of the SelectStmt. (An expression can't contain a PGGroupingSet,
971 * but a list may mix PGGroupingSet and expression nodes.) At this stage, the
972 * content of each node is a list of expressions, some of which may be RowExprs
973 * which represent sublists rather than actual row constructors, and nested
974 * PGGroupingSet nodes where legal in the grammar. The structure directly
975 * reflects the query syntax.
976 *
977 * In parse analysis, the transformed expressions are used to build the tlist
978 * and groupClause list (of PGSortGroupClause nodes), and the groupingSets tree
979 * is eventually reduced to a fixed format:
980 *
981 * EMPTY nodes represent (), and obviously have no content
982 *
983 * SIMPLE nodes represent a list of one or more expressions to be treated as an
984 * atom by the enclosing structure; the content is an integer list of
985 * ressortgroupref values (see PGSortGroupClause)
986 *
987 * CUBE and ROLLUP nodes contain a list of one or more SIMPLE nodes.
988 *
989 * SETS nodes contain a list of EMPTY, SIMPLE, CUBE or ROLLUP nodes, but after
990 * parse analysis they cannot contain more SETS nodes; enough of the syntactic
991 * transforms of the spec have been applied that we no longer have arbitrarily
992 * deep nesting (though we still preserve the use of cube/rollup).
993 *
994 * Note that if the groupingSets tree contains no SIMPLE nodes (only EMPTY
995 * nodes at the leaves), then the groupClause will be empty, but this is still
996 * an aggregation query (similar to using aggs or HAVING without GROUP BY).
997 *
998 * As an example, the following clause:
999 *
1000 * GROUP BY GROUPING SETS ((a,b), CUBE(c,(d,e)))
1001 *
1002 * looks like this after raw parsing:
1003 *
1004 * SETS( RowExpr(a,b) , CUBE( c, RowExpr(d,e) ) )
1005 *
1006 * and parse analysis converts it to:
1007 *
1008 * SETS( SIMPLE(1,2), CUBE( SIMPLE(3), SIMPLE(4,5) ) )
1009 */
1010typedef enum
1011{
1012 GROUPING_SET_EMPTY,
1013 GROUPING_SET_SIMPLE,
1014 GROUPING_SET_ROLLUP,
1015 GROUPING_SET_CUBE,
1016 GROUPING_SET_SETS
1017} GroupingSetKind;
1018
1019typedef struct PGGroupingSet
1020{
1021 PGNodeTag type;
1022 GroupingSetKind kind;
1023 PGList *content;
1024 int location;
1025} PGGroupingSet;
1026
1027/*
1028 * PGWindowClause -
1029 * transformed representation of WINDOW and OVER clauses
1030 *
1031 * A parsed Query's windowClause list contains these structs. "name" is set
1032 * if the clause originally came from WINDOW, and is NULL if it originally
1033 * was an OVER clause (but note that we collapse out duplicate OVERs).
1034 * partitionClause and orderClause are lists of PGSortGroupClause structs.
1035 * winref is an ID number referenced by PGWindowFunc nodes; it must be unique
1036 * among the members of a Query's windowClause list.
1037 * When refname isn't null, the partitionClause is always copied from there;
1038 * the orderClause might or might not be copied (see copiedOrder); the framing
1039 * options are never copied, per spec.
1040 */
1041typedef struct PGWindowClause
1042{
1043 PGNodeTag type;
1044 char *name; /* window name (NULL in an OVER clause) */
1045 char *refname; /* referenced window name, if any */
1046 PGList *partitionClause; /* PARTITION BY list */
1047 PGList *orderClause; /* ORDER BY list */
1048 int frameOptions; /* frame_clause options, see PGWindowDef */
1049 PGNode *startOffset; /* expression for starting bound, if any */
1050 PGNode *endOffset; /* expression for ending bound, if any */
1051 PGIndex winref; /* ID referenced by window functions */
1052 bool copiedOrder; /* did we copy orderClause from refname? */
1053} PGWindowClause;
1054
1055/*
1056 * RowMarkClause -
1057 * parser output representation of FOR [KEY] UPDATE/SHARE clauses
1058 *
1059 * Query.rowMarks contains a separate RowMarkClause node for each relation
1060 * identified as a FOR [KEY] UPDATE/SHARE target. If one of these clauses
1061 * is applied to a subquery, we generate RowMarkClauses for all normal and
1062 * subquery rels in the subquery, but they are marked pushedDown = true to
1063 * distinguish them from clauses that were explicitly written at this query
1064 * level. Also, Query.hasForUpdate tells whether there were explicit FOR
1065 * UPDATE/SHARE/KEY SHARE clauses in the current query level.
1066 */
1067
1068
1069/*
1070 * PGWithClause -
1071 * representation of WITH clause
1072 *
1073 * Note: PGWithClause does not propagate into the PGQuery representation;
1074 * but PGCommonTableExpr does.
1075 */
1076typedef struct PGWithClause
1077{
1078 PGNodeTag type;
1079 PGList *ctes; /* list of CommonTableExprs */
1080 bool recursive; /* true = WITH RECURSIVE */
1081 int location; /* token location, or -1 if unknown */
1082} PGWithClause;
1083
1084/*
1085 * PGInferClause -
1086 * ON CONFLICT unique index inference clause
1087 *
1088 * Note: PGInferClause does not propagate into the PGQuery representation.
1089 */
1090typedef struct PGInferClause
1091{
1092 PGNodeTag type;
1093 PGList *indexElems; /* IndexElems to infer unique index */
1094 PGNode *whereClause; /* qualification (partial-index predicate) */
1095 char *conname; /* PGConstraint name, or NULL if unnamed */
1096 int location; /* token location, or -1 if unknown */
1097} PGInferClause;
1098
1099/*
1100 * PGOnConflictClause -
1101 * representation of ON CONFLICT clause
1102 *
1103 * Note: PGOnConflictClause does not propagate into the PGQuery representation.
1104 */
1105typedef struct PGOnConflictClause
1106{
1107 PGNodeTag type;
1108 PGOnConflictAction action; /* DO NOTHING or UPDATE? */
1109 PGInferClause *infer; /* Optional index inference clause */
1110 PGList *targetList; /* the target list (of PGResTarget) */
1111 PGNode *whereClause; /* qualifications */
1112 int location; /* token location, or -1 if unknown */
1113} PGOnConflictClause;
1114
1115/*
1116 * PGCommonTableExpr -
1117 * representation of WITH list element
1118 *
1119 * We don't currently support the SEARCH or CYCLE clause.
1120 */
1121typedef struct PGCommonTableExpr
1122{
1123 PGNodeTag type;
1124 char *ctename; /* query name (never qualified) */
1125 PGList *aliascolnames; /* optional list of column names */
1126 /* SelectStmt/InsertStmt/etc before parse analysis, PGQuery afterwards: */
1127 PGNode *ctequery; /* the CTE's subquery */
1128 int location; /* token location, or -1 if unknown */
1129 /* These fields are set during parse analysis: */
1130 bool cterecursive; /* is this CTE actually recursive? */
1131 int cterefcount; /* number of RTEs referencing this CTE
1132 * (excluding internal self-references) */
1133 PGList *ctecolnames; /* list of output column names */
1134 PGList *ctecoltypes; /* OID list of output column type OIDs */
1135 PGList *ctecoltypmods; /* integer list of output column typmods */
1136 PGList *ctecolcollations; /* OID list of column collation OIDs */
1137} PGCommonTableExpr;
1138
1139/* Convenience macro to get the output tlist of a CTE's query */
1140#define GetCTETargetList(cte) \
1141 (AssertMacro(IsA((cte)->ctequery, PGQuery)), \
1142 ((PGQuery *) (cte)->ctequery)->commandType == PG_CMD_SELECT ? \
1143 ((PGQuery *) (cte)->ctequery)->targetList : \
1144 ((PGQuery *) (cte)->ctequery)->returningList)
1145
1146/*
1147 * TriggerTransition -
1148 * representation of transition row or table naming clause
1149 *
1150 * Only transition tables are initially supported in the syntax, and only for
1151 * AFTER triggers, but other permutations are accepted by the parser so we can
1152 * give a meaningful message from C code.
1153 */
1154
1155
1156/*****************************************************************************
1157 * Raw Grammar Output Statements
1158 *****************************************************************************/
1159
1160/*
1161 * PGRawStmt --- container for any one statement's raw parse tree
1162 *
1163 * Parse analysis converts a raw parse tree headed by a PGRawStmt node into
1164 * an analyzed statement headed by a PGQuery node. For optimizable statements,
1165 * the conversion is complex. For utility statements, the parser usually just
1166 * transfers the raw parse tree (sans PGRawStmt) into the utilityStmt field of
1167 * the PGQuery node, and all the useful work happens at execution time.
1168 *
1169 * stmt_location/stmt_len identify the portion of the source text string
1170 * containing this raw statement (useful for multi-statement strings).
1171 */
1172typedef struct PGRawStmt
1173{
1174 PGNodeTag type;
1175 PGNode *stmt; /* raw parse tree */
1176 int stmt_location; /* start location, or -1 if unknown */
1177 int stmt_len; /* length in bytes; 0 means "rest of string" */
1178} PGRawStmt;
1179
1180/*****************************************************************************
1181 * Optimizable Statements
1182 *****************************************************************************/
1183
1184/* ----------------------
1185 * Insert Statement
1186 *
1187 * The source expression is represented by PGSelectStmt for both the
1188 * SELECT and VALUES cases. If selectStmt is NULL, then the query
1189 * is INSERT ... DEFAULT VALUES.
1190 * ----------------------
1191 */
1192typedef struct PGInsertStmt
1193{
1194 PGNodeTag type;
1195 PGRangeVar *relation; /* relation to insert into */
1196 PGList *cols; /* optional: names of the target columns */
1197 PGNode *selectStmt; /* the source SELECT/VALUES, or NULL */
1198 PGOnConflictClause *onConflictClause; /* ON CONFLICT clause */
1199 PGList *returningList; /* list of expressions to return */
1200 PGWithClause *withClause; /* WITH clause */
1201 PGOverridingKind override; /* OVERRIDING clause */
1202} PGInsertStmt;
1203
1204/* ----------------------
1205 * Delete Statement
1206 * ----------------------
1207 */
1208typedef struct PGDeleteStmt
1209{
1210 PGNodeTag type;
1211 PGRangeVar *relation; /* relation to delete from */
1212 PGList *usingClause; /* optional using clause for more tables */
1213 PGNode *whereClause; /* qualifications */
1214 PGList *returningList; /* list of expressions to return */
1215 PGWithClause *withClause; /* WITH clause */
1216} PGDeleteStmt;
1217
1218/* ----------------------
1219 * Update Statement
1220 * ----------------------
1221 */
1222typedef struct PGUpdateStmt
1223{
1224 PGNodeTag type;
1225 PGRangeVar *relation; /* relation to update */
1226 PGList *targetList; /* the target list (of PGResTarget) */
1227 PGNode *whereClause; /* qualifications */
1228 PGList *fromClause; /* optional from clause for more tables */
1229 PGList *returningList; /* list of expressions to return */
1230 PGWithClause *withClause; /* WITH clause */
1231} PGUpdateStmt;
1232
1233/* ----------------------
1234 * Select Statement
1235 *
1236 * A "simple" SELECT is represented in the output of gram.y by a single
1237 * PGSelectStmt node; so is a VALUES construct. A query containing set
1238 * operators (UNION, INTERSECT, EXCEPT) is represented by a tree of PGSelectStmt
1239 * nodes, in which the leaf nodes are component SELECTs and the internal nodes
1240 * represent UNION, INTERSECT, or EXCEPT operators. Using the same node
1241 * type for both leaf and internal nodes allows gram.y to stick ORDER BY,
1242 * LIMIT, etc, clause values into a SELECT statement without worrying
1243 * whether it is a simple or compound SELECT.
1244 * ----------------------
1245 */
1246typedef enum PGSetOperation
1247{
1248 PG_SETOP_NONE = 0,
1249 PG_SETOP_UNION,
1250 PG_SETOP_INTERSECT,
1251 PG_SETOP_EXCEPT
1252} PGSetOperation;
1253
1254typedef struct PGSelectStmt
1255{
1256 PGNodeTag type;
1257
1258 /*
1259 * These fields are used only in "leaf" SelectStmts.
1260 */
1261 PGList *distinctClause; /* NULL, list of DISTINCT ON exprs, or
1262 * lcons(NIL,NIL) for all (SELECT DISTINCT) */
1263 PGIntoClause *intoClause; /* target for SELECT INTO */
1264 PGList *targetList; /* the target list (of PGResTarget) */
1265 PGList *fromClause; /* the FROM clause */
1266 PGNode *whereClause; /* WHERE qualification */
1267 PGList *groupClause; /* GROUP BY clauses */
1268 PGNode *havingClause; /* HAVING conditional-expression */
1269 PGList *windowClause; /* WINDOW window_name AS (...), ... */
1270
1271 /*
1272 * In a "leaf" node representing a VALUES list, the above fields are all
1273 * null, and instead this field is set. Note that the elements of the
1274 * sublists are just expressions, without PGResTarget decoration. Also note
1275 * that a list element can be DEFAULT (represented as a PGSetToDefault
1276 * node), regardless of the context of the VALUES list. It's up to parse
1277 * analysis to reject that where not valid.
1278 */
1279 PGList *valuesLists; /* untransformed list of expression lists */
1280
1281 /*
1282 * These fields are used in both "leaf" SelectStmts and upper-level
1283 * SelectStmts.
1284 */
1285 PGList *sortClause; /* sort clause (a list of SortBy's) */
1286 PGNode *limitOffset; /* # of result tuples to skip */
1287 PGNode *limitCount; /* # of result tuples to return */
1288 PGList *lockingClause; /* FOR UPDATE (list of LockingClause's) */
1289 PGWithClause *withClause; /* WITH clause */
1290
1291 /*
1292 * These fields are used only in upper-level SelectStmts.
1293 */
1294 PGSetOperation op; /* type of set op */
1295 bool all; /* ALL specified? */
1296 struct PGSelectStmt *larg; /* left child */
1297 struct PGSelectStmt *rarg; /* right child */
1298 /* Eventually add fields for CORRESPONDING spec here */
1299} PGSelectStmt;
1300
1301
1302/* ----------------------
1303 * Set Operation node for post-analysis query trees
1304 *
1305 * After parse analysis, a SELECT with set operations is represented by a
1306 * top-level PGQuery node containing the leaf SELECTs as subqueries in its
1307 * range table. Its setOperations field shows the tree of set operations,
1308 * with leaf PGSelectStmt nodes replaced by PGRangeTblRef nodes, and internal
1309 * nodes replaced by SetOperationStmt nodes. Information about the output
1310 * column types is added, too. (Note that the child nodes do not necessarily
1311 * produce these types directly, but we've checked that their output types
1312 * can be coerced to the output column type.) Also, if it's not UNION ALL,
1313 * information about the types' sort/group semantics is provided in the form
1314 * of a PGSortGroupClause list (same representation as, eg, DISTINCT).
1315 * The resolved common column collations are provided too; but note that if
1316 * it's not UNION ALL, it's okay for a column to not have a common collation,
1317 * so a member of the colCollations list could be InvalidOid even though the
1318 * column has a collatable type.
1319 * ----------------------
1320 */
1321
1322
1323
1324/*****************************************************************************
1325 * Other Statements (no optimizations required)
1326 *
1327 * These are not touched by parser/analyze.c except to put them into
1328 * the utilityStmt field of a Query. This is eventually passed to
1329 * ProcessUtility (by-passing rewriting and planning). Some of the
1330 * statements do need attention from parse analysis, and this is
1331 * done by routines in parser/parse_utilcmd.c after ProcessUtility
1332 * receives the command for execution.
1333 * DECLARE CURSOR, EXPLAIN, and CREATE TABLE AS are special cases:
1334 * they contain optimizable statements, which get processed normally
1335 * by parser/analyze.c.
1336 *****************************************************************************/
1337
1338/*
1339 * When a command can act on several kinds of objects with only one
1340 * parse structure required, use these constants to designate the
1341 * object type. Note that commands typically don't support all the types.
1342 */
1343
1344typedef enum PGObjectType
1345{
1346 PG_OBJECT_ACCESS_METHOD,
1347 PG_OBJECT_AGGREGATE,
1348 PG_OBJECT_AMOP,
1349 PG_OBJECT_AMPROC,
1350 PG_OBJECT_ATTRIBUTE, /* type's attribute, when distinct from column */
1351 PG_OBJECT_CAST,
1352 PG_OBJECT_COLUMN,
1353 PG_OBJECT_COLLATION,
1354 PG_OBJECT_CONVERSION,
1355 PG_OBJECT_DATABASE,
1356 PG_OBJECT_DEFAULT,
1357 PG_OBJECT_DEFACL,
1358 PG_OBJECT_DOMAIN,
1359 PG_OBJECT_DOMCONSTRAINT,
1360 PG_OBJECT_EVENT_TRIGGER,
1361 PG_OBJECT_EXTENSION,
1362 PG_OBJECT_FDW,
1363 PG_OBJECT_FOREIGN_SERVER,
1364 PG_OBJECT_FOREIGN_TABLE,
1365 PG_OBJECT_FUNCTION,
1366 PG_OBJECT_INDEX,
1367 PG_OBJECT_LANGUAGE,
1368 PG_OBJECT_LARGEOBJECT,
1369 PG_OBJECT_MATVIEW,
1370 PG_OBJECT_OPCLASS,
1371 PG_OBJECT_OPERATOR,
1372 PG_OBJECT_OPFAMILY,
1373 PG_OBJECT_POLICY,
1374 PG_OBJECT_PUBLICATION,
1375 PG_OBJECT_PUBLICATION_REL,
1376 PG_OBJECT_ROLE,
1377 PG_OBJECT_RULE,
1378 PG_OBJECT_SCHEMA,
1379 PG_OBJECT_SEQUENCE,
1380 PG_OBJECT_SUBSCRIPTION,
1381 PG_OBJECT_STATISTIC_EXT,
1382 PG_OBJECT_TABCONSTRAINT,
1383 PG_OBJECT_TABLE,
1384 PG_OBJECT_TABLESPACE,
1385 PG_OBJECT_TRANSFORM,
1386 PG_OBJECT_TRIGGER,
1387 PG_OBJECT_TSCONFIGURATION,
1388 PG_OBJECT_TSDICTIONARY,
1389 PG_OBJECT_TSPARSER,
1390 PG_OBJECT_TSTEMPLATE,
1391 PG_OBJECT_TYPE,
1392 PG_OBJECT_USER_MAPPING,
1393 PG_OBJECT_VIEW
1394} PGObjectType;
1395
1396/* ----------------------
1397 * Create Schema Statement
1398 *
1399 * NOTE: the schemaElts list contains raw parsetrees for component statements
1400 * of the schema, such as CREATE TABLE, GRANT, etc. These are analyzed and
1401 * executed after the schema itself is created.
1402 * ----------------------
1403 */
1404typedef struct PGCreateSchemaStmt
1405{
1406 PGNodeTag type;
1407 char *schemaname; /* the name of the schema to create */
1408 PGList *schemaElts; /* schema components (list of parsenodes) */
1409 bool if_not_exists; /* just do nothing if schema already exists? */
1410} PGCreateSchemaStmt;
1411
1412typedef enum PGDropBehavior
1413{
1414 PG_DROP_RESTRICT, /* drop fails if any dependent objects */
1415 PG_DROP_CASCADE /* remove dependent objects too */
1416} PGDropBehavior;
1417
1418/* ----------------------
1419 * Alter Table
1420 * ----------------------
1421 */
1422typedef struct PGAlterTableStmt
1423{
1424 PGNodeTag type;
1425 PGRangeVar *relation; /* table to work on */
1426 PGList *cmds; /* list of subcommands */
1427 PGObjectType relkind; /* type of object */
1428 bool missing_ok; /* skip error if table missing */
1429} PGAlterTableStmt;
1430
1431typedef enum PGAlterTableType
1432{
1433 PG_AT_AddColumn, /* add column */
1434 PG_AT_AddColumnRecurse, /* internal to commands/tablecmds.c */
1435 PG_AT_AddColumnToView, /* implicitly via CREATE OR REPLACE VIEW */
1436 PG_AT_ColumnDefault, /* alter column default */
1437 PG_AT_DropNotNull, /* alter column drop not null */
1438 PG_AT_SetNotNull, /* alter column set not null */
1439 PG_AT_SetStatistics, /* alter column set statistics */
1440 PG_AT_SetOptions, /* alter column set ( options ) */
1441 PG_AT_ResetOptions, /* alter column reset ( options ) */
1442 PG_AT_SetStorage, /* alter column set storage */
1443 PG_AT_DropColumn, /* drop column */
1444 PG_AT_DropColumnRecurse, /* internal to commands/tablecmds.c */
1445 PG_AT_AddIndex, /* add index */
1446 PG_AT_ReAddIndex, /* internal to commands/tablecmds.c */
1447 PG_AT_AddConstraint, /* add constraint */
1448 PG_AT_AddConstraintRecurse, /* internal to commands/tablecmds.c */
1449 PG_AT_ReAddConstraint, /* internal to commands/tablecmds.c */
1450 PG_AT_AlterConstraint, /* alter constraint */
1451 PG_AT_ValidateConstraint, /* validate constraint */
1452 PG_AT_ValidateConstraintRecurse, /* internal to commands/tablecmds.c */
1453 PG_AT_ProcessedConstraint, /* pre-processed add constraint (local in
1454 * parser/parse_utilcmd.c) */
1455 PG_AT_AddIndexConstraint, /* add constraint using existing index */
1456 PG_AT_DropConstraint, /* drop constraint */
1457 PG_AT_DropConstraintRecurse, /* internal to commands/tablecmds.c */
1458 PG_AT_ReAddComment, /* internal to commands/tablecmds.c */
1459 PG_AT_AlterColumnType, /* alter column type */
1460 PG_AT_AlterColumnGenericOptions, /* alter column OPTIONS (...) */
1461 PG_AT_ChangeOwner, /* change owner */
1462 PG_AT_ClusterOn, /* CLUSTER ON */
1463 PG_AT_DropCluster, /* SET WITHOUT CLUSTER */
1464 PG_AT_SetLogged, /* SET LOGGED */
1465 PG_AT_SetUnLogged, /* SET UNLOGGED */
1466 PG_AT_AddOids, /* SET WITH OIDS */
1467 PG_AT_AddOidsRecurse, /* internal to commands/tablecmds.c */
1468 PG_AT_DropOids, /* SET WITHOUT OIDS */
1469 PG_AT_SetTableSpace, /* SET TABLESPACE */
1470 PG_AT_SetRelOptions, /* SET (...) -- AM specific parameters */
1471 PG_AT_ResetRelOptions, /* RESET (...) -- AM specific parameters */
1472 PG_AT_ReplaceRelOptions, /* replace reloption list in its entirety */
1473 PG_AT_EnableTrig, /* ENABLE TRIGGER name */
1474 PG_AT_EnableAlwaysTrig, /* ENABLE ALWAYS TRIGGER name */
1475 PG_AT_EnableReplicaTrig, /* ENABLE REPLICA TRIGGER name */
1476 PG_AT_DisableTrig, /* DISABLE TRIGGER name */
1477 PG_AT_EnableTrigAll, /* ENABLE TRIGGER ALL */
1478 PG_AT_DisableTrigAll, /* DISABLE TRIGGER ALL */
1479 PG_AT_EnableTrigUser, /* ENABLE TRIGGER USER */
1480 PG_AT_DisableTrigUser, /* DISABLE TRIGGER USER */
1481 PG_AT_EnableRule, /* ENABLE RULE name */
1482 PG_AT_EnableAlwaysRule, /* ENABLE ALWAYS RULE name */
1483 PG_AT_EnableReplicaRule, /* ENABLE REPLICA RULE name */
1484 PG_AT_DisableRule, /* DISABLE RULE name */
1485 PG_AT_AddInherit, /* INHERIT parent */
1486 PG_AT_DropInherit, /* NO INHERIT parent */
1487 PG_AT_AddOf, /* OF <type_name> */
1488 PG_AT_DropOf, /* NOT OF */
1489 PG_AT_ReplicaIdentity, /* REPLICA IDENTITY */
1490 PG_AT_EnableRowSecurity, /* ENABLE ROW SECURITY */
1491 PG_AT_DisableRowSecurity, /* DISABLE ROW SECURITY */
1492 PG_AT_ForceRowSecurity, /* FORCE ROW SECURITY */
1493 PG_AT_NoForceRowSecurity, /* NO FORCE ROW SECURITY */
1494 PG_AT_GenericOptions, /* OPTIONS (...) */
1495 PG_AT_AttachPartition, /* ATTACH PARTITION */
1496 PG_AT_DetachPartition, /* DETACH PARTITION */
1497 PG_AT_AddIdentity, /* ADD IDENTITY */
1498 PG_AT_SetIdentity, /* SET identity column options */
1499 AT_DropIdentity /* DROP IDENTITY */
1500} PGAlterTableType;
1501
1502
1503
1504typedef struct PGAlterTableCmd /* one subcommand of an ALTER TABLE */
1505{
1506 PGNodeTag type;
1507 PGAlterTableType subtype; /* Type of table alteration to apply */
1508 char *name; /* column, constraint, or trigger to act on,
1509 * or tablespace */
1510 PGNode *def; /* definition of new column, index,
1511 * constraint, or parent table */
1512 PGDropBehavior behavior; /* RESTRICT or CASCADE for DROP cases */
1513 bool missing_ok; /* skip error if missing? */
1514} PGAlterTableCmd;
1515
1516/*
1517 * Note: PGObjectWithArgs carries only the types of the input parameters of the
1518 * function. So it is sufficient to identify an existing function, but it
1519 * is not enough info to define a function nor to call it.
1520 */
1521typedef struct PGObjectWithArgs
1522{
1523 PGNodeTag type;
1524 PGList *objname; /* qualified name of function/operator */
1525 PGList *objargs; /* list of Typename nodes */
1526 bool args_unspecified; /* argument list was omitted, so name must
1527 * be unique (note that objargs == NIL
1528 * means zero args) */
1529} PGObjectWithArgs;
1530
1531/* ----------------------
1532 * Copy Statement
1533 *
1534 * We support "COPY relation FROM file", "COPY relation TO file", and
1535 * "COPY (query) TO file". In any given PGCopyStmt, exactly one of "relation"
1536 * and "query" must be non-NULL.
1537 * ----------------------
1538 */
1539typedef struct PGCopyStmt
1540{
1541 PGNodeTag type;
1542 PGRangeVar *relation; /* the relation to copy */
1543 PGNode *query; /* the query (SELECT or DML statement with
1544 * RETURNING) to copy, as a raw parse tree */
1545 PGList *attlist; /* PGList of column names (as Strings), or NIL
1546 * for all columns */
1547 bool is_from; /* TO or FROM */
1548 bool is_program; /* is 'filename' a program to popen? */
1549 char *filename; /* filename, or NULL for STDIN/STDOUT */
1550 PGList *options; /* PGList of PGDefElem nodes */
1551} PGCopyStmt;
1552
1553/* ----------------------
1554 * SET Statement (includes RESET)
1555 *
1556 * "SET var TO DEFAULT" and "RESET var" are semantically equivalent, but we
1557 * preserve the distinction in VariableSetKind for CreateCommandTag().
1558 * ----------------------
1559 */
1560typedef enum
1561{
1562 VAR_SET_VALUE, /* SET var = value */
1563 VAR_SET_DEFAULT, /* SET var TO DEFAULT */
1564 VAR_SET_CURRENT, /* SET var FROM CURRENT */
1565 VAR_SET_MULTI, /* special case for SET TRANSACTION ... */
1566 VAR_RESET, /* RESET var */
1567 VAR_RESET_ALL /* RESET ALL */
1568} VariableSetKind;
1569
1570typedef struct PGVariableSetStmt
1571{
1572 PGNodeTag type;
1573 VariableSetKind kind;
1574 char *name; /* variable to be set */
1575 PGList *args; /* PGList of PGAConst nodes */
1576 bool is_local; /* SET LOCAL? */
1577} PGVariableSetStmt;
1578
1579/* ----------------------
1580 * Show Statement
1581 * ----------------------
1582 */
1583typedef struct PGVariableShowStmt
1584{
1585 PGNodeTag type;
1586 char *name;
1587} PGVariableShowStmt;
1588
1589/* ----------------------
1590 * Create Table Statement
1591 *
1592 * NOTE: in the raw gram.y output, PGColumnDef and PGConstraint nodes are
1593 * intermixed in tableElts, and constraints is NIL. After parse analysis,
1594 * tableElts contains just ColumnDefs, and constraints contains just
1595 * PGConstraint nodes (in fact, only PG_CONSTR_CHECK nodes, in the present
1596 * implementation).
1597 * ----------------------
1598 */
1599
1600typedef struct PGCreateStmt
1601{
1602 PGNodeTag type;
1603 PGRangeVar *relation; /* relation to create */
1604 PGList *tableElts; /* column definitions (list of PGColumnDef) */
1605 PGList *inhRelations; /* relations to inherit from (list of
1606 * inhRelation) */
1607 PGTypeName *ofTypename; /* OF typename */
1608 PGList *constraints; /* constraints (list of PGConstraint nodes) */
1609 PGList *options; /* options from WITH clause */
1610 PGOnCommitAction oncommit; /* what do we do at COMMIT? */
1611 char *tablespacename; /* table space to use, or NULL */
1612 bool if_not_exists; /* just do nothing if it already exists? */
1613} PGCreateStmt;
1614
1615/* ----------
1616 * Definitions for constraints in PGCreateStmt
1617 *
1618 * Note that column defaults are treated as a type of constraint,
1619 * even though that's a bit odd semantically.
1620 *
1621 * For constraints that use expressions (CONSTR_CHECK, PG_CONSTR_DEFAULT)
1622 * we may have the expression in either "raw" form (an untransformed
1623 * parse tree) or "cooked" form (the nodeToString representation of
1624 * an executable expression tree), depending on how this PGConstraint
1625 * node was created (by parsing, or by inheritance from an existing
1626 * relation). We should never have both in the same node!
1627 *
1628 * PG_FKCONSTR_ACTION_xxx values are stored into pg_constraint.confupdtype
1629 * and pg_constraint.confdeltype columns; PG_FKCONSTR_MATCH_xxx values are
1630 * stored into pg_constraint.confmatchtype. Changing the code values may
1631 * require an initdb!
1632 *
1633 * If skip_validation is true then we skip checking that the existing rows
1634 * in the table satisfy the constraint, and just install the catalog entries
1635 * for the constraint. A new FK constraint is marked as valid iff
1636 * initially_valid is true. (Usually skip_validation and initially_valid
1637 * are inverses, but we can set both true if the table is known empty.)
1638 *
1639 * PGConstraint attributes (DEFERRABLE etc) are initially represented as
1640 * separate PGConstraint nodes for simplicity of parsing. parse_utilcmd.c makes
1641 * a pass through the constraints list to insert the info into the appropriate
1642 * PGConstraint node.
1643 * ----------
1644 */
1645
1646typedef enum PGConstrType /* types of constraints */
1647{
1648 PG_CONSTR_NULL, /* not standard SQL, but a lot of people
1649 * expect it */
1650 PG_CONSTR_NOTNULL,
1651 PG_CONSTR_DEFAULT,
1652 PG_CONSTR_IDENTITY,
1653 PG_CONSTR_CHECK,
1654 PG_CONSTR_PRIMARY,
1655 PG_CONSTR_UNIQUE,
1656 PG_CONSTR_EXCLUSION,
1657 PG_CONSTR_FOREIGN,
1658 PG_CONSTR_ATTR_DEFERRABLE, /* attributes for previous constraint node */
1659 PG_CONSTR_ATTR_NOT_DEFERRABLE,
1660 PG_CONSTR_ATTR_DEFERRED,
1661 PG_CONSTR_ATTR_IMMEDIATE
1662} PGConstrType;
1663
1664/* Foreign key action codes */
1665#define PG_FKCONSTR_ACTION_NOACTION 'a'
1666#define PG_FKCONSTR_ACTION_RESTRICT 'r'
1667#define PG_FKCONSTR_ACTION_CASCADE 'c'
1668#define PG_FKCONSTR_ACTION_SETNULL 'n'
1669#define PG_FKCONSTR_ACTION_SETDEFAULT 'd'
1670
1671/* Foreign key matchtype codes */
1672#define PG_FKCONSTR_MATCH_FULL 'f'
1673#define PG_FKCONSTR_MATCH_PARTIAL 'p'
1674#define PG_FKCONSTR_MATCH_SIMPLE 's'
1675
1676typedef struct PGConstraint
1677{
1678 PGNodeTag type;
1679 PGConstrType contype; /* see above */
1680
1681 /* Fields used for most/all constraint types: */
1682 char *conname; /* PGConstraint name, or NULL if unnamed */
1683 bool deferrable; /* DEFERRABLE? */
1684 bool initdeferred; /* INITIALLY DEFERRED? */
1685 int location; /* token location, or -1 if unknown */
1686
1687 /* Fields used for constraints with expressions (CHECK and DEFAULT): */
1688 bool is_no_inherit; /* is constraint non-inheritable? */
1689 PGNode *raw_expr; /* expr, as untransformed parse tree */
1690 char *cooked_expr; /* expr, as nodeToString representation */
1691 char generated_when;
1692
1693 /* Fields used for unique constraints (UNIQUE and PRIMARY KEY): */
1694 PGList *keys; /* String nodes naming referenced column(s) */
1695
1696 /* Fields used for EXCLUSION constraints: */
1697 PGList *exclusions; /* list of (PGIndexElem, operator name) pairs */
1698
1699 /* Fields used for index constraints (UNIQUE, PRIMARY KEY, EXCLUSION): */
1700 PGList *options; /* options from WITH clause */
1701 char *indexname; /* existing index to use; otherwise NULL */
1702 char *indexspace; /* index tablespace; NULL for default */
1703 /* These could be, but currently are not, used for UNIQUE/PKEY: */
1704 char *access_method; /* index access method; NULL for default */
1705 PGNode *where_clause; /* partial index predicate */
1706
1707 /* Fields used for FOREIGN KEY constraints: */
1708 PGRangeVar *pktable; /* Primary key table */
1709 PGList *fk_attrs; /* Attributes of foreign key */
1710 PGList *pk_attrs; /* Corresponding attrs in PK table */
1711 char fk_matchtype; /* FULL, PARTIAL, SIMPLE */
1712 char fk_upd_action; /* ON UPDATE action */
1713 char fk_del_action; /* ON DELETE action */
1714 PGList *old_conpfeqop; /* pg_constraint.conpfeqop of my former self */
1715 PGOid old_pktable_oid; /* pg_constraint.confrelid of my former
1716 * self */
1717
1718 /* Fields used for constraints that allow a NOT VALID specification */
1719 bool skip_validation; /* skip validation of existing rows? */
1720 bool initially_valid; /* mark the new constraint as valid? */
1721} PGConstraint;
1722
1723/* ----------------------
1724 * {Create|Alter} SEQUENCE Statement
1725 * ----------------------
1726 */
1727
1728typedef struct PGCreateSeqStmt
1729{
1730 PGNodeTag type;
1731 PGRangeVar *sequence; /* the sequence to create */
1732 PGList *options;
1733 PGOid ownerId; /* ID of owner, or InvalidOid for default */
1734 bool for_identity;
1735 bool if_not_exists; /* just do nothing if it already exists? */
1736} PGCreateSeqStmt;
1737
1738typedef struct PGAlterSeqStmt
1739{
1740 PGNodeTag type;
1741 PGRangeVar *sequence; /* the sequence to alter */
1742 PGList *options;
1743 bool for_identity;
1744 bool missing_ok; /* skip error if a role is missing? */
1745} PGAlterSeqStmt;
1746
1747/* ----------------------
1748 * Drop Table|Sequence|View|Index|Type|Domain|Conversion|Schema Statement
1749 * ----------------------
1750 */
1751
1752typedef struct PGDropStmt
1753{
1754 PGNodeTag type;
1755 PGList *objects; /* list of names */
1756 PGObjectType removeType; /* object type */
1757 PGDropBehavior behavior; /* RESTRICT or CASCADE behavior */
1758 bool missing_ok; /* skip error if object is missing? */
1759 bool concurrent; /* drop index concurrently? */
1760} PGDropStmt;
1761
1762/* ----------------------
1763 * Create PGIndex Statement
1764 *
1765 * This represents creation of an index and/or an associated constraint.
1766 * If isconstraint is true, we should create a pg_constraint entry along
1767 * with the index. But if indexOid isn't InvalidOid, we are not creating an
1768 * index, just a UNIQUE/PKEY constraint using an existing index. isconstraint
1769 * must always be true in this case, and the fields describing the index
1770 * properties are empty.
1771 * ----------------------
1772 */
1773typedef struct PGIndexStmt
1774{
1775 PGNodeTag type;
1776 char *idxname; /* name of new index, or NULL for default */
1777 PGRangeVar *relation; /* relation to build index on */
1778 char *accessMethod; /* name of access method (eg. btree) */
1779 char *tableSpace; /* tablespace, or NULL for default */
1780 PGList *indexParams; /* columns to index: a list of PGIndexElem */
1781 PGList *options; /* WITH clause options: a list of PGDefElem */
1782 PGNode *whereClause; /* qualification (partial-index predicate) */
1783 PGList *excludeOpNames; /* exclusion operator names, or NIL if none */
1784 char *idxcomment; /* comment to apply to index, or NULL */
1785 PGOid indexOid; /* OID of an existing index, if any */
1786 PGOid oldNode; /* relfilenode of existing storage, if any */
1787 bool unique; /* is index unique? */
1788 bool primary; /* is index a primary key? */
1789 bool isconstraint; /* is it for a pkey/unique constraint? */
1790 bool deferrable; /* is the constraint DEFERRABLE? */
1791 bool initdeferred; /* is the constraint INITIALLY DEFERRED? */
1792 bool transformed; /* true when transformIndexStmt is finished */
1793 bool concurrent; /* should this be a concurrent index build? */
1794 bool if_not_exists; /* just do nothing if index already exists? */
1795} PGIndexStmt;
1796
1797
1798/* ----------------------
1799 * Alter Object Rename Statement
1800 * ----------------------
1801 */
1802typedef struct PGRenameStmt
1803{
1804 PGNodeTag type;
1805 PGObjectType renameType; /* PG_OBJECT_TABLE, PG_OBJECT_COLUMN, etc */
1806 PGObjectType relationType; /* if column name, associated relation type */
1807 PGRangeVar *relation; /* in case it's a table */
1808 PGNode *object; /* in case it's some other object */
1809 char *subname; /* name of contained object (column, rule,
1810 * trigger, etc) */
1811 char *newname; /* the new name */
1812 PGDropBehavior behavior; /* RESTRICT or CASCADE behavior */
1813 bool missing_ok; /* skip error if missing? */
1814} PGRenameStmt;
1815
1816
1817/* ----------------------
1818 * ALTER object SET SCHEMA Statement
1819 * ----------------------
1820 */
1821typedef struct PGAlterObjectSchemaStmt
1822{
1823 PGNodeTag type;
1824 PGObjectType objectType; /* PG_OBJECT_TABLE, PG_OBJECT_TYPE, etc */
1825 PGRangeVar *relation; /* in case it's a table */
1826 PGNode *object; /* in case it's some other object */
1827 char *newschema; /* the new schema */
1828 bool missing_ok; /* skip error if missing? */
1829} PGAlterObjectSchemaStmt;
1830
1831/* ----------------------
1832 * {Begin|Commit|Rollback} Transaction Statement
1833 * ----------------------
1834 */
1835typedef enum PGTransactionStmtKind
1836{
1837 PG_TRANS_STMT_BEGIN,
1838 PG_TRANS_STMT_START, /* semantically identical to BEGIN */
1839 PG_TRANS_STMT_COMMIT,
1840 PG_TRANS_STMT_ROLLBACK,
1841 PG_TRANS_STMT_SAVEPOINT,
1842 PG_TRANS_STMT_RELEASE,
1843 PG_TRANS_STMT_ROLLBACK_TO,
1844 PG_TRANS_STMT_PREPARE,
1845 PG_TRANS_STMT_COMMIT_PREPARED,
1846 TRANS_STMT_ROLLBACK_PREPARED
1847} PGTransactionStmtKind;
1848
1849typedef struct PGTransactionStmt
1850{
1851 PGNodeTag type;
1852 PGTransactionStmtKind kind; /* see above */
1853 PGList *options; /* for BEGIN/START and savepoint commands */
1854 char *gid; /* for two-phase-commit related commands */
1855} PGTransactionStmt;
1856
1857/* ----------------------
1858 * Create View Statement
1859 * ----------------------
1860 */
1861typedef enum PGViewCheckOption
1862{
1863 PG_NO_CHECK_OPTION,
1864 PG_LOCAL_CHECK_OPTION,
1865 CASCADED_CHECK_OPTION
1866} PGViewCheckOption;
1867
1868typedef struct PGViewStmt
1869{
1870 PGNodeTag type;
1871 PGRangeVar *view; /* the view to be created */
1872 PGList *aliases; /* target column names */
1873 PGNode *query; /* the SELECT query (as a raw parse tree) */
1874 bool replace; /* replace an existing view? */
1875 PGList *options; /* options from WITH clause */
1876 PGViewCheckOption withCheckOption; /* WITH CHECK OPTION */
1877} PGViewStmt;
1878
1879/* ----------------------
1880 * Load Statement
1881 * ----------------------
1882 */
1883typedef struct PGLoadStmt
1884{
1885 PGNodeTag type;
1886 char *filename; /* file to load */
1887} PGLoadStmt;
1888
1889/* ----------------------
1890 * Vacuum and Analyze Statements
1891 *
1892 * Even though these are nominally two statements, it's convenient to use
1893 * just one node type for both. Note that at least one of PG_VACOPT_VACUUM
1894 * and PG_VACOPT_ANALYZE must be set in options.
1895 * ----------------------
1896 */
1897typedef enum PGVacuumOption
1898{
1899 PG_VACOPT_VACUUM = 1 << 0, /* do VACUUM */
1900 PG_VACOPT_ANALYZE = 1 << 1, /* do ANALYZE */
1901 PG_VACOPT_VERBOSE = 1 << 2, /* print progress info */
1902 PG_VACOPT_FREEZE = 1 << 3, /* FREEZE option */
1903 PG_VACOPT_FULL = 1 << 4, /* FULL (non-concurrent) vacuum */
1904 PG_VACOPT_NOWAIT = 1 << 5, /* don't wait to get lock (autovacuum only) */
1905 PG_VACOPT_SKIPTOAST = 1 << 6, /* don't process the TOAST table, if any */
1906 PG_VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7 /* don't skip any pages */
1907} PGVacuumOption;
1908
1909typedef struct PGVacuumStmt
1910{
1911 PGNodeTag type;
1912 int options; /* OR of PGVacuumOption flags */
1913 PGRangeVar *relation; /* single table to process, or NULL */
1914 PGList *va_cols; /* list of column names, or NIL for all */
1915} PGVacuumStmt;
1916
1917/* ----------------------
1918 * Explain Statement
1919 *
1920 * The "query" field is initially a raw parse tree, and is converted to a
1921 * PGQuery node during parse analysis. Note that rewriting and planning
1922 * of the query are always postponed until execution.
1923 * ----------------------
1924 */
1925typedef struct PGExplainStmt
1926{
1927 PGNodeTag type;
1928 PGNode *query; /* the query (see comments above) */
1929 PGList *options; /* list of PGDefElem nodes */
1930} PGExplainStmt;
1931
1932/* ----------------------
1933 * CREATE TABLE AS Statement (a/k/a SELECT INTO)
1934 *
1935 * A query written as CREATE TABLE AS will produce this node type natively.
1936 * A query written as SELECT ... INTO will be transformed to this form during
1937 * parse analysis.
1938 * A query written as CREATE MATERIALIZED view will produce this node type,
1939 * during parse analysis, since it needs all the same data.
1940 *
1941 * The "query" field is handled similarly to EXPLAIN, though note that it
1942 * can be a SELECT or an EXECUTE, but not other DML statements.
1943 * ----------------------
1944 */
1945typedef struct PGCreateTableAsStmt
1946{
1947 PGNodeTag type;
1948 PGNode *query; /* the query (see comments above) */
1949 PGIntoClause *into; /* destination table */
1950 PGObjectType relkind; /* PG_OBJECT_TABLE or PG_OBJECT_MATVIEW */
1951 bool is_select_into; /* it was written as SELECT INTO */
1952 bool if_not_exists; /* just do nothing if it already exists? */
1953} PGCreateTableAsStmt;
1954
1955/* ----------------------
1956 * Checkpoint Statement
1957 * ----------------------
1958 */
1959typedef struct PGCheckPointStmt
1960{
1961 PGNodeTag type;
1962} PGCheckPointStmt;
1963
1964/* ----------------------
1965 * PREPARE Statement
1966 * ----------------------
1967 */
1968typedef struct PGPrepareStmt
1969{
1970 PGNodeTag type;
1971 char *name; /* Name of plan, arbitrary */
1972 PGList *argtypes; /* Types of parameters (PGList of PGTypeName) */
1973 PGNode *query; /* The query itself (as a raw parsetree) */
1974} PGPrepareStmt;
1975
1976
1977/* ----------------------
1978 * EXECUTE Statement
1979 * ----------------------
1980 */
1981
1982typedef struct PGExecuteStmt
1983{
1984 PGNodeTag type;
1985 char *name; /* The name of the plan to execute */
1986 PGList *params; /* Values to assign to parameters */
1987} PGExecuteStmt;
1988
1989
1990/* ----------------------
1991 * DEALLOCATE Statement
1992 * ----------------------
1993 */
1994typedef struct PGDeallocateStmt
1995{
1996 PGNodeTag type;
1997 char *name; /* The name of the plan to remove */
1998 /* NULL means DEALLOCATE ALL */
1999} PGDeallocateStmt;
2000
2001
2002/* ----------------------
2003 * PRAGMA statements
2004 * Three types of pragma statements:
2005 * PRAGMA pragma_name; (NOTHING)
2006 * PRAGMA pragma_name='param'; (ASSIGNMENT)
2007 * PRAGMA pragma_name('param'); (CALL)
2008 * ----------------------
2009 */
2010typedef enum
2011 {
2012 PG_PRAGMA_TYPE_NOTHING,
2013 PG_PRAGMA_TYPE_ASSIGNMENT,
2014 PG_PRAGMA_TYPE_CALL
2015} PGPragmaKind;
2016
2017typedef struct PGPragmaStmt
2018{
2019 PGNodeTag type;
2020 PGPragmaKind kind;
2021 char *name; /* variable to be set */
2022 PGList *args; /* PGList of PGAConst nodes */
2023} PGPragmaStmt;
2024