codekingpro/portable-devtools
114k
1/*-------------------------------------------------------------------------2 *3 * parsenodes.h4 * definitions for parse tree nodes5 *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 be8 * used for positioning an error cursor when there is an error related to9 * the node. Access to the original source text is needed to make use of10 * the location. At the topmost (statement) level, we also provide a11 * statement length, likewise measured in bytes, for convenience in12 * identifying statement boundaries in multi-statement source strings.13 *14 *15 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group16 * Portions Copyright (c) 1994, Regents of the University of California17 *18 * src/include/nodes/parsenodes.h19 *20 *-------------------------------------------------------------------------21 */22#ifndef PARSENODES_H23#define PARSENODES_H24 25#include "common/relpath.h"26#include "nodes/bitmapset.h"27#include "nodes/lockoptions.h"28#include "nodes/primnodes.h"29#include "nodes/value.h"30#include "partitioning/partdefs.h"31 32 33typedef enum OverridingKind34{35 OVERRIDING_NOT_SET = 0,36 OVERRIDING_USER_VALUE,37 OVERRIDING_SYSTEM_VALUE38} OverridingKind;39 40/* Possible sources of a Query */41typedef enum QuerySource42{43 QSRC_ORIGINAL, /* original parsetree (explicit query) */44 QSRC_PARSER, /* added by parse analysis (now unused) */45 QSRC_INSTEAD_RULE, /* added by unconditional INSTEAD rule */46 QSRC_QUAL_INSTEAD_RULE, /* added by conditional INSTEAD rule */47 QSRC_NON_INSTEAD_RULE /* added by non-INSTEAD rule */48} QuerySource;49 50/* Sort ordering options for ORDER BY and CREATE INDEX */51typedef enum SortByDir52{53 SORTBY_DEFAULT,54 SORTBY_ASC,55 SORTBY_DESC,56 SORTBY_USING /* not allowed in CREATE INDEX ... */57} SortByDir;58 59typedef enum SortByNulls60{61 SORTBY_NULLS_DEFAULT,62 SORTBY_NULLS_FIRST,63 SORTBY_NULLS_LAST64} SortByNulls;65 66/* Options for [ ALL | DISTINCT ] */67typedef enum SetQuantifier68{69 SET_QUANTIFIER_DEFAULT,70 SET_QUANTIFIER_ALL,71 SET_QUANTIFIER_DISTINCT72} SetQuantifier;73 74/*75 * Grantable rights are encoded so that we can OR them together in a bitmask.76 * The present representation of AclItem limits us to 32 distinct rights,77 * even though AclMode is defined as uint64. See utils/acl.h.78 *79 * Caution: changing these codes breaks stored ACLs, hence forces initdb.80 */81typedef uint64 AclMode; /* a bitmask of privilege bits */82 83#define ACL_INSERT (1<<0) /* for relations */84#define ACL_SELECT (1<<1)85#define ACL_UPDATE (1<<2)86#define ACL_DELETE (1<<3)87#define ACL_TRUNCATE (1<<4)88#define ACL_REFERENCES (1<<5)89#define ACL_TRIGGER (1<<6)90#define ACL_EXECUTE (1<<7) /* for functions */91#define ACL_USAGE (1<<8) /* for various object types */92#define ACL_CREATE (1<<9) /* for namespaces and databases */93#define ACL_CREATE_TEMP (1<<10) /* for databases */94#define ACL_CONNECT (1<<11) /* for databases */95#define ACL_SET (1<<12) /* for configuration parameters */96#define ACL_ALTER_SYSTEM (1<<13) /* for configuration parameters */97#define N_ACL_RIGHTS 14 /* 1 plus the last 1<<x */98#define ACL_NO_RIGHTS 099/* Currently, SELECT ... FOR [KEY] UPDATE/SHARE requires UPDATE privileges */100#define ACL_SELECT_FOR_UPDATE ACL_UPDATE101 102 103/*****************************************************************************104 * Query Tree105 *****************************************************************************/106 107/*108 * Query -109 * Parse analysis turns all statements into a Query tree110 * for further processing by the rewriter and planner.111 *112 * Utility statements (i.e. non-optimizable statements) have the113 * utilityStmt field set, and the rest of the Query is mostly dummy.114 *115 * Planning converts a Query tree into a Plan tree headed by a PlannedStmt116 * node --- the Query structure is not used by the executor.117 *118 * All the fields ignored for the query jumbling are not semantically119 * significant (such as alias names), as is ignored anything that can120 * be deduced from child nodes (else we'd just be double-hashing that121 * piece of information).122 */123typedef struct Query124{125 NodeTag type;126 127 CmdType commandType; /* select|insert|update|delete|merge|utility */128 129 /* where did I come from? */130 QuerySource querySource pg_node_attr(query_jumble_ignore);131 132 /*133 * query identifier (can be set by plugins); ignored for equal, as it134 * might not be set; also not stored. This is the result of the query135 * jumble, hence ignored.136 */137 uint64 queryId pg_node_attr(equal_ignore, query_jumble_ignore, read_write_ignore, read_as(0));138 139 /* do I set the command result tag? */140 bool canSetTag pg_node_attr(query_jumble_ignore);141 142 Node *utilityStmt; /* non-null if commandType == CMD_UTILITY */143 144 /*145 * rtable index of target relation for INSERT/UPDATE/DELETE/MERGE; 0 for146 * SELECT. This is ignored in the query jumble as unrelated to the147 * compilation of the query ID.148 */149 int resultRelation pg_node_attr(query_jumble_ignore);150 151 /* has aggregates in tlist or havingQual */152 bool hasAggs pg_node_attr(query_jumble_ignore);153 /* has window functions in tlist */154 bool hasWindowFuncs pg_node_attr(query_jumble_ignore);155 /* has set-returning functions in tlist */156 bool hasTargetSRFs pg_node_attr(query_jumble_ignore);157 /* has subquery SubLink */158 bool hasSubLinks pg_node_attr(query_jumble_ignore);159 /* distinctClause is from DISTINCT ON */160 bool hasDistinctOn pg_node_attr(query_jumble_ignore);161 /* WITH RECURSIVE was specified */162 bool hasRecursive pg_node_attr(query_jumble_ignore);163 /* has INSERT/UPDATE/DELETE in WITH */164 bool hasModifyingCTE pg_node_attr(query_jumble_ignore);165 /* FOR [KEY] UPDATE/SHARE was specified */166 bool hasForUpdate pg_node_attr(query_jumble_ignore);167 /* rewriter has applied some RLS policy */168 bool hasRowSecurity pg_node_attr(query_jumble_ignore);169 /* is a RETURN statement */170 bool isReturn pg_node_attr(query_jumble_ignore);171 172 List *cteList; /* WITH list (of CommonTableExpr's) */173 174 List *rtable; /* list of range table entries */175 176 /*177 * list of RTEPermissionInfo nodes for the rtable entries having178 * perminfoindex > 0179 */180 List *rteperminfos pg_node_attr(query_jumble_ignore);181 FromExpr *jointree; /* table join tree (FROM and WHERE clauses);182 * also USING clause for MERGE */183 184 List *mergeActionList; /* list of actions for MERGE (only) */185 /* whether to use outer join */186 bool mergeUseOuterJoin pg_node_attr(query_jumble_ignore);187 188 List *targetList; /* target list (of TargetEntry) */189 190 /* OVERRIDING clause */191 OverridingKind override pg_node_attr(query_jumble_ignore);192 193 OnConflictExpr *onConflict; /* ON CONFLICT DO [NOTHING | UPDATE] */194 195 List *returningList; /* return-values list (of TargetEntry) */196 197 List *groupClause; /* a list of SortGroupClause's */198 bool groupDistinct; /* is the group by clause distinct? */199 200 List *groupingSets; /* a list of GroupingSet's if present */201 202 Node *havingQual; /* qualifications applied to groups */203 204 List *windowClause; /* a list of WindowClause's */205 206 List *distinctClause; /* a list of SortGroupClause's */207 208 List *sortClause; /* a list of SortGroupClause's */209 210 Node *limitOffset; /* # of result tuples to skip (int8 expr) */211 Node *limitCount; /* # of result tuples to return (int8 expr) */212 LimitOption limitOption; /* limit type */213 214 List *rowMarks; /* a list of RowMarkClause's */215 216 Node *setOperations; /* set-operation tree if this is top level of217 * a UNION/INTERSECT/EXCEPT query */218 219 /*220 * A list of pg_constraint OIDs that the query depends on to be221 * semantically valid222 */223 List *constraintDeps pg_node_attr(query_jumble_ignore);224 225 /* a list of WithCheckOption's (added during rewrite) */226 List *withCheckOptions pg_node_attr(query_jumble_ignore);227 228 /*229 * The following two fields identify the portion of the source text string230 * containing this query. They are typically only populated in top-level231 * Queries, not in sub-queries. When not set, they might both be zero, or232 * both be -1 meaning "unknown".233 */234 /* start location, or -1 if unknown */235 int stmt_location;236 /* length in bytes; 0 means "rest of string" */237 int stmt_len pg_node_attr(query_jumble_ignore);238} Query;239 240 241/****************************************************************************242 * Supporting data structures for Parse Trees243 *244 * Most of these node types appear in raw parsetrees output by the grammar,245 * and get transformed to something else by the analyzer. A few of them246 * are used as-is in transformed querytrees.247 ****************************************************************************/248 249/*250 * TypeName - specifies a type in definitions251 *252 * For TypeName structures generated internally, it is often easier to253 * specify the type by OID than by name. If "names" is NIL then the254 * actual type OID is given by typeOid, otherwise typeOid is unused.255 * Similarly, if "typmods" is NIL then the actual typmod is expected to256 * be prespecified in typemod, otherwise typemod is unused.257 *258 * If pct_type is true, then names is actually a field name and we look up259 * the type of that field. Otherwise (the normal case), names is a type260 * name possibly qualified with schema and database name.261 */262typedef struct TypeName263{264 NodeTag type;265 List *names; /* qualified name (list of String nodes) */266 Oid typeOid; /* type identified by OID */267 bool setof; /* is a set? */268 bool pct_type; /* %TYPE specified? */269 List *typmods; /* type modifier expression(s) */270 int32 typemod; /* prespecified type modifier */271 List *arrayBounds; /* array bounds */272 int location; /* token location, or -1 if unknown */273} TypeName;274 275/*276 * ColumnRef - specifies a reference to a column, or possibly a whole tuple277 *278 * The "fields" list must be nonempty. It can contain String nodes279 * (representing names) and A_Star nodes (representing occurrence of a '*').280 * Currently, A_Star must appear only as the last list element --- the grammar281 * is responsible for enforcing this!282 *283 * Note: any container subscripting or selection of fields from composite columns284 * is represented by an A_Indirection node above the ColumnRef. However,285 * for simplicity in the normal case, initial field selection from a table286 * name is represented within ColumnRef and not by adding A_Indirection.287 */288typedef struct ColumnRef289{290 NodeTag type;291 List *fields; /* field names (String nodes) or A_Star */292 int location; /* token location, or -1 if unknown */293} ColumnRef;294 295/*296 * ParamRef - specifies a $n parameter reference297 */298typedef struct ParamRef299{300 NodeTag type;301 int number; /* the number of the parameter */302 int location; /* token location, or -1 if unknown */303} ParamRef;304 305/*306 * A_Expr - infix, prefix, and postfix expressions307 */308typedef enum A_Expr_Kind309{310 AEXPR_OP, /* normal operator */311 AEXPR_OP_ANY, /* scalar op ANY (array) */312 AEXPR_OP_ALL, /* scalar op ALL (array) */313 AEXPR_DISTINCT, /* IS DISTINCT FROM - name must be "=" */314 AEXPR_NOT_DISTINCT, /* IS NOT DISTINCT FROM - name must be "=" */315 AEXPR_NULLIF, /* NULLIF - name must be "=" */316 AEXPR_IN, /* [NOT] IN - name must be "=" or "<>" */317 AEXPR_LIKE, /* [NOT] LIKE - name must be "~~" or "!~~" */318 AEXPR_ILIKE, /* [NOT] ILIKE - name must be "~~*" or "!~~*" */319 AEXPR_SIMILAR, /* [NOT] SIMILAR - name must be "~" or "!~" */320 AEXPR_BETWEEN, /* name must be "BETWEEN" */321 AEXPR_NOT_BETWEEN, /* name must be "NOT BETWEEN" */322 AEXPR_BETWEEN_SYM, /* name must be "BETWEEN SYMMETRIC" */323 AEXPR_NOT_BETWEEN_SYM /* name must be "NOT BETWEEN SYMMETRIC" */324} A_Expr_Kind;325 326typedef struct A_Expr327{328 pg_node_attr(custom_read_write)329 330 NodeTag type;331 A_Expr_Kind kind; /* see above */332 List *name; /* possibly-qualified name of operator */333 Node *lexpr; /* left argument, or NULL if none */334 Node *rexpr; /* right argument, or NULL if none */335 int location; /* token location, or -1 if unknown */336} A_Expr;337 338/*339 * A_Const - a literal constant340 *341 * Value nodes are inline for performance. You can treat 'val' as a node,342 * as in IsA(&val, Integer). 'val' is not valid if isnull is true.343 */344union ValUnion345{346 Node node;347 Integer ival;348 Float fval;349 Boolean boolval;350 String sval;351 BitString bsval;352};353 354typedef struct A_Const355{356 pg_node_attr(custom_copy_equal, custom_read_write, custom_query_jumble)357 358 NodeTag type;359 union ValUnion val;360 bool isnull; /* SQL NULL constant */361 int location; /* token location, or -1 if unknown */362} A_Const;363 364/*365 * TypeCast - a CAST expression366 */367typedef struct TypeCast368{369 NodeTag type;370 Node *arg; /* the expression being casted */371 TypeName *typeName; /* the target type */372 int location; /* token location, or -1 if unknown */373} TypeCast;374 375/*376 * CollateClause - a COLLATE expression377 */378typedef struct CollateClause379{380 NodeTag type;381 Node *arg; /* input expression */382 List *collname; /* possibly-qualified collation name */383 int location; /* token location, or -1 if unknown */384} CollateClause;385 386/*387 * RoleSpec - a role name or one of a few special values.388 */389typedef enum RoleSpecType390{391 ROLESPEC_CSTRING, /* role name is stored as a C string */392 ROLESPEC_CURRENT_ROLE, /* role spec is CURRENT_ROLE */393 ROLESPEC_CURRENT_USER, /* role spec is CURRENT_USER */394 ROLESPEC_SESSION_USER, /* role spec is SESSION_USER */395 ROLESPEC_PUBLIC /* role name is "public" */396} RoleSpecType;397 398typedef struct RoleSpec399{400 NodeTag type;401 RoleSpecType roletype; /* Type of this rolespec */402 char *rolename; /* filled only for ROLESPEC_CSTRING */403 int location; /* token location, or -1 if unknown */404} RoleSpec;405 406/*407 * FuncCall - a function or aggregate invocation408 *409 * agg_order (if not NIL) indicates we saw 'foo(... ORDER BY ...)', or if410 * agg_within_group is true, it was 'foo(...) WITHIN GROUP (ORDER BY ...)'.411 * agg_star indicates we saw a 'foo(*)' construct, while agg_distinct412 * indicates we saw 'foo(DISTINCT ...)'. In any of these cases, the413 * construct *must* be an aggregate call. Otherwise, it might be either an414 * aggregate or some other kind of function. However, if FILTER or OVER is415 * present it had better be an aggregate or window function.416 *417 * Normally, you'd initialize this via makeFuncCall() and then only change the418 * parts of the struct its defaults don't match afterwards, as needed.419 */420typedef struct FuncCall421{422 NodeTag type;423 List *funcname; /* qualified name of function */424 List *args; /* the arguments (list of exprs) */425 List *agg_order; /* ORDER BY (list of SortBy) */426 Node *agg_filter; /* FILTER clause, if any */427 struct WindowDef *over; /* OVER clause, if any */428 bool agg_within_group; /* ORDER BY appeared in WITHIN GROUP */429 bool agg_star; /* argument was really '*' */430 bool agg_distinct; /* arguments were labeled DISTINCT */431 bool func_variadic; /* last argument was labeled VARIADIC */432 CoercionForm funcformat; /* how to display this node */433 int location; /* token location, or -1 if unknown */434} FuncCall;435 436/*437 * A_Star - '*' representing all columns of a table or compound field438 *439 * This can appear within ColumnRef.fields, A_Indirection.indirection, and440 * ResTarget.indirection lists.441 */442typedef struct A_Star443{444 NodeTag type;445} A_Star;446 447/*448 * A_Indices - array subscript or slice bounds ([idx] or [lidx:uidx])449 *450 * In slice case, either or both of lidx and uidx can be NULL (omitted).451 * In non-slice case, uidx holds the single subscript and lidx is always NULL.452 */453typedef struct A_Indices454{455 NodeTag type;456 bool is_slice; /* true if slice (i.e., colon present) */457 Node *lidx; /* slice lower bound, if any */458 Node *uidx; /* subscript, or slice upper bound if any */459} A_Indices;460 461/*462 * A_Indirection - select a field and/or array element from an expression463 *464 * The indirection list can contain A_Indices nodes (representing465 * subscripting), String nodes (representing field selection --- the466 * string value is the name of the field to select), and A_Star nodes467 * (representing selection of all fields of a composite type).468 * For example, a complex selection operation like469 * (foo).field1[42][7].field2470 * would be represented with a single A_Indirection node having a 4-element471 * indirection list.472 *473 * Currently, A_Star must appear only as the last list element --- the grammar474 * is responsible for enforcing this!475 */476typedef struct A_Indirection477{478 NodeTag type;479 Node *arg; /* the thing being selected from */480 List *indirection; /* subscripts and/or field names and/or * */481} A_Indirection;482 483/*484 * A_ArrayExpr - an ARRAY[] construct485 */486typedef struct A_ArrayExpr487{488 NodeTag type;489 List *elements; /* array element expressions */490 int location; /* token location, or -1 if unknown */491} A_ArrayExpr;492 493/*494 * ResTarget -495 * result target (used in target list of pre-transformed parse trees)496 *497 * In a SELECT target list, 'name' is the column label from an498 * 'AS ColumnLabel' clause, or NULL if there was none, and 'val' is the499 * value expression itself. The 'indirection' field is not used.500 *501 * INSERT uses ResTarget in its target-column-names list. Here, 'name' is502 * the name of the destination column, 'indirection' stores any subscripts503 * attached to the destination, and 'val' is not used.504 *505 * In an UPDATE target list, 'name' is the name of the destination column,506 * 'indirection' stores any subscripts attached to the destination, and507 * 'val' is the expression to assign.508 *509 * See A_Indirection for more info about what can appear in 'indirection'.510 */511typedef struct ResTarget512{513 NodeTag type;514 char *name; /* column name or NULL */515 List *indirection; /* subscripts, field names, and '*', or NIL */516 Node *val; /* the value expression to compute or assign */517 int location; /* token location, or -1 if unknown */518} ResTarget;519 520/*521 * MultiAssignRef - element of a row source expression for UPDATE522 *523 * In an UPDATE target list, when we have SET (a,b,c) = row-valued-expression,524 * we generate separate ResTarget items for each of a,b,c. Their "val" trees525 * are MultiAssignRef nodes numbered 1..n, linking to a common copy of the526 * row-valued-expression (which parse analysis will process only once, when527 * handling the MultiAssignRef with colno=1).528 */529typedef struct MultiAssignRef530{531 NodeTag type;532 Node *source; /* the row-valued expression */533 int colno; /* column number for this target (1..n) */534 int ncolumns; /* number of targets in the construct */535} MultiAssignRef;536 537/*538 * SortBy - for ORDER BY clause539 */540typedef struct SortBy541{542 NodeTag type;543 Node *node; /* expression to sort on */544 SortByDir sortby_dir; /* ASC/DESC/USING/default */545 SortByNulls sortby_nulls; /* NULLS FIRST/LAST */546 List *useOp; /* name of op to use, if SORTBY_USING */547 int location; /* operator location, or -1 if none/unknown */548} SortBy;549 550/*551 * WindowDef - raw representation of WINDOW and OVER clauses552 *553 * For entries in a WINDOW list, "name" is the window name being defined.554 * For OVER clauses, we use "name" for the "OVER window" syntax, or "refname"555 * for the "OVER (window)" syntax, which is subtly different --- the latter556 * implies overriding the window frame clause.557 */558typedef struct WindowDef559{560 NodeTag type;561 char *name; /* window's own name */562 char *refname; /* referenced window name, if any */563 List *partitionClause; /* PARTITION BY expression list */564 List *orderClause; /* ORDER BY (list of SortBy) */565 int frameOptions; /* frame_clause options, see below */566 Node *startOffset; /* expression for starting bound, if any */567 Node *endOffset; /* expression for ending bound, if any */568 int location; /* parse location, or -1 if none/unknown */569} WindowDef;570 571/*572 * frameOptions is an OR of these bits. The NONDEFAULT and BETWEEN bits are573 * used so that ruleutils.c can tell which properties were specified and574 * which were defaulted; the correct behavioral bits must be set either way.575 * The START_foo and END_foo options must come in pairs of adjacent bits for576 * the convenience of gram.y, even though some of them are useless/invalid.577 */578#define FRAMEOPTION_NONDEFAULT 0x00001 /* any specified? */579#define FRAMEOPTION_RANGE 0x00002 /* RANGE behavior */580#define FRAMEOPTION_ROWS 0x00004 /* ROWS behavior */581#define FRAMEOPTION_GROUPS 0x00008 /* GROUPS behavior */582#define FRAMEOPTION_BETWEEN 0x00010 /* BETWEEN given? */583#define FRAMEOPTION_START_UNBOUNDED_PRECEDING 0x00020 /* start is U. P. */584#define FRAMEOPTION_END_UNBOUNDED_PRECEDING 0x00040 /* (disallowed) */585#define FRAMEOPTION_START_UNBOUNDED_FOLLOWING 0x00080 /* (disallowed) */586#define FRAMEOPTION_END_UNBOUNDED_FOLLOWING 0x00100 /* end is U. F. */587#define FRAMEOPTION_START_CURRENT_ROW 0x00200 /* start is C. R. */588#define FRAMEOPTION_END_CURRENT_ROW 0x00400 /* end is C. R. */589#define FRAMEOPTION_START_OFFSET_PRECEDING 0x00800 /* start is O. P. */590#define FRAMEOPTION_END_OFFSET_PRECEDING 0x01000 /* end is O. P. */591#define FRAMEOPTION_START_OFFSET_FOLLOWING 0x02000 /* start is O. F. */592#define FRAMEOPTION_END_OFFSET_FOLLOWING 0x04000 /* end is O. F. */593#define FRAMEOPTION_EXCLUDE_CURRENT_ROW 0x08000 /* omit C.R. */594#define FRAMEOPTION_EXCLUDE_GROUP 0x10000 /* omit C.R. & peers */595#define FRAMEOPTION_EXCLUDE_TIES 0x20000 /* omit C.R.'s peers */596 597#define FRAMEOPTION_START_OFFSET \598 (FRAMEOPTION_START_OFFSET_PRECEDING | FRAMEOPTION_START_OFFSET_FOLLOWING)599#define FRAMEOPTION_END_OFFSET \600 (FRAMEOPTION_END_OFFSET_PRECEDING | FRAMEOPTION_END_OFFSET_FOLLOWING)601#define FRAMEOPTION_EXCLUSION \602 (FRAMEOPTION_EXCLUDE_CURRENT_ROW | FRAMEOPTION_EXCLUDE_GROUP | \603 FRAMEOPTION_EXCLUDE_TIES)604 605#define FRAMEOPTION_DEFAULTS \606 (FRAMEOPTION_RANGE | FRAMEOPTION_START_UNBOUNDED_PRECEDING | \607 FRAMEOPTION_END_CURRENT_ROW)608 609/*610 * RangeSubselect - subquery appearing in a FROM clause611 */612typedef struct RangeSubselect613{614 NodeTag type;615 bool lateral; /* does it have LATERAL prefix? */616 Node *subquery; /* the untransformed sub-select clause */617 Alias *alias; /* table alias & optional column aliases */618} RangeSubselect;619 620/*621 * RangeFunction - function call appearing in a FROM clause622 *623 * functions is a List because we use this to represent the construct624 * ROWS FROM(func1(...), func2(...), ...). Each element of this list is a625 * two-element sublist, the first element being the untransformed function626 * call tree, and the second element being a possibly-empty list of ColumnDef627 * nodes representing any columndef list attached to that function within the628 * ROWS FROM() syntax.629 *630 * alias and coldeflist represent any alias and/or columndef list attached631 * at the top level. (We disallow coldeflist appearing both here and632 * per-function, but that's checked in parse analysis, not by the grammar.)633 */634typedef struct RangeFunction635{636 NodeTag type;637 bool lateral; /* does it have LATERAL prefix? */638 bool ordinality; /* does it have WITH ORDINALITY suffix? */639 bool is_rowsfrom; /* is result of ROWS FROM() syntax? */640 List *functions; /* per-function information, see above */641 Alias *alias; /* table alias & optional column aliases */642 List *coldeflist; /* list of ColumnDef nodes to describe result643 * of function returning RECORD */644} RangeFunction;645 646/*647 * RangeTableFunc - raw form of "table functions" such as XMLTABLE648 */649typedef struct RangeTableFunc650{651 NodeTag type;652 bool lateral; /* does it have LATERAL prefix? */653 Node *docexpr; /* document expression */654 Node *rowexpr; /* row generator expression */655 List *namespaces; /* list of namespaces as ResTarget */656 List *columns; /* list of RangeTableFuncCol */657 Alias *alias; /* table alias & optional column aliases */658 int location; /* token location, or -1 if unknown */659} RangeTableFunc;660 661/*662 * RangeTableFuncCol - one column in a RangeTableFunc->columns663 *664 * If for_ordinality is true (FOR ORDINALITY), then the column is an int4665 * column and the rest of the fields are ignored.666 */667typedef struct RangeTableFuncCol668{669 NodeTag type;670 char *colname; /* name of generated column */671 TypeName *typeName; /* type of generated column */672 bool for_ordinality; /* does it have FOR ORDINALITY? */673 bool is_not_null; /* does it have NOT NULL? */674 Node *colexpr; /* column filter expression */675 Node *coldefexpr; /* column default value expression */676 int location; /* token location, or -1 if unknown */677} RangeTableFuncCol;678 679/*680 * RangeTableSample - TABLESAMPLE appearing in a raw FROM clause681 *682 * This node, appearing only in raw parse trees, represents683 * <relation> TABLESAMPLE <method> (<params>) REPEATABLE (<num>)684 * Currently, the <relation> can only be a RangeVar, but we might in future685 * allow RangeSubselect and other options. Note that the RangeTableSample686 * is wrapped around the node representing the <relation>, rather than being687 * a subfield of it.688 */689typedef struct RangeTableSample690{691 NodeTag type;692 Node *relation; /* relation to be sampled */693 List *method; /* sampling method name (possibly qualified) */694 List *args; /* argument(s) for sampling method */695 Node *repeatable; /* REPEATABLE expression, or NULL if none */696 int location; /* method name location, or -1 if unknown */697} RangeTableSample;698 699/*700 * ColumnDef - column definition (used in various creates)701 *702 * If the column has a default value, we may have the value expression703 * in either "raw" form (an untransformed parse tree) or "cooked" form704 * (a post-parse-analysis, executable expression tree), depending on705 * how this ColumnDef node was created (by parsing, or by inheritance706 * from an existing relation). We should never have both in the same node!707 *708 * Similarly, we may have a COLLATE specification in either raw form709 * (represented as a CollateClause with arg==NULL) or cooked form710 * (the collation's OID).711 *712 * The constraints list may contain a CONSTR_DEFAULT item in a raw713 * parsetree produced by gram.y, but transformCreateStmt will remove714 * the item and set raw_default instead. CONSTR_DEFAULT items715 * should not appear in any subsequent processing.716 */717typedef struct ColumnDef718{719 NodeTag type;720 char *colname; /* name of column */721 TypeName *typeName; /* type of column */722 char *compression; /* compression method for column */723 int inhcount; /* number of times column is inherited */724 bool is_local; /* column has local (non-inherited) def'n */725 bool is_not_null; /* NOT NULL constraint specified? */726 bool is_from_type; /* column definition came from table type */727 char storage; /* attstorage setting, or 0 for default */728 char *storage_name; /* attstorage setting name or NULL for default */729 Node *raw_default; /* default value (untransformed parse tree) */730 Node *cooked_default; /* default value (transformed expr tree) */731 char identity; /* attidentity setting */732 RangeVar *identitySequence; /* to store identity sequence name for733 * ALTER TABLE ... ADD COLUMN */734 char generated; /* attgenerated setting */735 CollateClause *collClause; /* untransformed COLLATE spec, if any */736 Oid collOid; /* collation OID (InvalidOid if not set) */737 List *constraints; /* other constraints on column */738 List *fdwoptions; /* per-column FDW options */739 int location; /* parse location, or -1 if none/unknown */740} ColumnDef;741 742/*743 * TableLikeClause - CREATE TABLE ( ... LIKE ... ) clause744 */745typedef struct TableLikeClause746{747 NodeTag type;748 RangeVar *relation;749 bits32 options; /* OR of TableLikeOption flags */750 Oid relationOid; /* If table has been looked up, its OID */751} TableLikeClause;752 753typedef enum TableLikeOption754{755 CREATE_TABLE_LIKE_COMMENTS = 1 << 0,756 CREATE_TABLE_LIKE_COMPRESSION = 1 << 1,757 CREATE_TABLE_LIKE_CONSTRAINTS = 1 << 2,758 CREATE_TABLE_LIKE_DEFAULTS = 1 << 3,759 CREATE_TABLE_LIKE_GENERATED = 1 << 4,760 CREATE_TABLE_LIKE_IDENTITY = 1 << 5,761 CREATE_TABLE_LIKE_INDEXES = 1 << 6,762 CREATE_TABLE_LIKE_STATISTICS = 1 << 7,763 CREATE_TABLE_LIKE_STORAGE = 1 << 8,764 CREATE_TABLE_LIKE_ALL = PG_INT32_MAX765} TableLikeOption;766 767/*768 * IndexElem - index parameters (used in CREATE INDEX, and in ON CONFLICT)769 *770 * For a plain index attribute, 'name' is the name of the table column to771 * index, and 'expr' is NULL. For an index expression, 'name' is NULL and772 * 'expr' is the expression tree.773 */774typedef struct IndexElem775{776 NodeTag type;777 char *name; /* name of attribute to index, or NULL */778 Node *expr; /* expression to index, or NULL */779 char *indexcolname; /* name for index column; NULL = default */780 List *collation; /* name of collation; NIL = default */781 List *opclass; /* name of desired opclass; NIL = default */782 List *opclassopts; /* opclass-specific options, or NIL */783 SortByDir ordering; /* ASC/DESC/default */784 SortByNulls nulls_ordering; /* FIRST/LAST/default */785} IndexElem;786 787/*788 * DefElem - a generic "name = value" option definition789 *790 * In some contexts the name can be qualified. Also, certain SQL commands791 * allow a SET/ADD/DROP action to be attached to option settings, so it's792 * convenient to carry a field for that too. (Note: currently, it is our793 * practice that the grammar allows namespace and action only in statements794 * where they are relevant; C code can just ignore those fields in other795 * statements.)796 */797typedef enum DefElemAction798{799 DEFELEM_UNSPEC, /* no action given */800 DEFELEM_SET,801 DEFELEM_ADD,802 DEFELEM_DROP803} DefElemAction;804 805typedef struct DefElem806{807 NodeTag type;808 char *defnamespace; /* NULL if unqualified name */809 char *defname;810 Node *arg; /* typically Integer, Float, String, or811 * TypeName */812 DefElemAction defaction; /* unspecified action, or SET/ADD/DROP */813 int location; /* token location, or -1 if unknown */814} DefElem;815 816/*817 * LockingClause - raw representation of FOR [NO KEY] UPDATE/[KEY] SHARE818 * options819 *820 * Note: lockedRels == NIL means "all relations in query". Otherwise it821 * is a list of RangeVar nodes. (We use RangeVar mainly because it carries822 * a location field --- currently, parse analysis insists on unqualified823 * names in LockingClause.)824 */825typedef struct LockingClause826{827 NodeTag type;828 List *lockedRels; /* FOR [KEY] UPDATE/SHARE relations */829 LockClauseStrength strength;830 LockWaitPolicy waitPolicy; /* NOWAIT and SKIP LOCKED */831} LockingClause;832 833/*834 * XMLSERIALIZE (in raw parse tree only)835 */836typedef struct XmlSerialize837{838 NodeTag type;839 XmlOptionType xmloption; /* DOCUMENT or CONTENT */840 Node *expr;841 TypeName *typeName;842 bool indent; /* [NO] INDENT */843 int location; /* token location, or -1 if unknown */844} XmlSerialize;845 846/* Partitioning related definitions */847 848/*849 * PartitionElem - parse-time representation of a single partition key850 *851 * expr can be either a raw expression tree or a parse-analyzed expression.852 * We don't store these on-disk, though.853 */854typedef struct PartitionElem855{856 NodeTag type;857 char *name; /* name of column to partition on, or NULL */858 Node *expr; /* expression to partition on, or NULL */859 List *collation; /* name of collation; NIL = default */860 List *opclass; /* name of desired opclass; NIL = default */861 int location; /* token location, or -1 if unknown */862} PartitionElem;863 864typedef enum PartitionStrategy865{866 PARTITION_STRATEGY_LIST = 'l',867 PARTITION_STRATEGY_RANGE = 'r',868 PARTITION_STRATEGY_HASH = 'h'869} PartitionStrategy;870 871/*872 * PartitionSpec - parse-time representation of a partition key specification873 *874 * This represents the key space we will be partitioning on.875 */876typedef struct PartitionSpec877{878 NodeTag type;879 PartitionStrategy strategy;880 List *partParams; /* List of PartitionElems */881 int location; /* token location, or -1 if unknown */882} PartitionSpec;883 884/*885 * PartitionBoundSpec - a partition bound specification886 *887 * This represents the portion of the partition key space assigned to a888 * particular partition. These are stored on disk in pg_class.relpartbound.889 */890struct PartitionBoundSpec891{892 NodeTag type;893 894 char strategy; /* see PARTITION_STRATEGY codes above */895 bool is_default; /* is it a default partition bound? */896 897 /* Partitioning info for HASH strategy: */898 int modulus;899 int remainder;900 901 /* Partitioning info for LIST strategy: */902 List *listdatums; /* List of Consts (or A_Consts in raw tree) */903 904 /* Partitioning info for RANGE strategy: */905 List *lowerdatums; /* List of PartitionRangeDatums */906 List *upperdatums; /* List of PartitionRangeDatums */907 908 int location; /* token location, or -1 if unknown */909};910 911/*912 * PartitionRangeDatum - one of the values in a range partition bound913 *914 * This can be MINVALUE, MAXVALUE or a specific bounded value.915 */916typedef enum PartitionRangeDatumKind917{918 PARTITION_RANGE_DATUM_MINVALUE = -1, /* less than any other value */919 PARTITION_RANGE_DATUM_VALUE = 0, /* a specific (bounded) value */920 PARTITION_RANGE_DATUM_MAXVALUE = 1 /* greater than any other value */921} PartitionRangeDatumKind;922 923typedef struct PartitionRangeDatum924{925 NodeTag type;926 927 PartitionRangeDatumKind kind;928 Node *value; /* Const (or A_Const in raw tree), if kind is929 * PARTITION_RANGE_DATUM_VALUE, else NULL */930 931 int location; /* token location, or -1 if unknown */932} PartitionRangeDatum;933 934/*935 * PartitionCmd - info for ALTER TABLE/INDEX ATTACH/DETACH PARTITION commands936 */937typedef struct PartitionCmd938{939 NodeTag type;940 RangeVar *name; /* name of partition to attach/detach */941 PartitionBoundSpec *bound; /* FOR VALUES, if attaching */942 bool concurrent;943} PartitionCmd;944 945/****************************************************************************946 * Nodes for a Query tree947 ****************************************************************************/948 949/*--------------------950 * RangeTblEntry -951 * A range table is a List of RangeTblEntry nodes.952 *953 * A range table entry may represent a plain relation, a sub-select in954 * FROM, or the result of a JOIN clause. (Only explicit JOIN syntax955 * produces an RTE, not the implicit join resulting from multiple FROM956 * items. This is because we only need the RTE to deal with SQL features957 * like outer joins and join-output-column aliasing.) Other special958 * RTE types also exist, as indicated by RTEKind.959 *960 * Note that we consider RTE_RELATION to cover anything that has a pg_class961 * entry. relkind distinguishes the sub-cases.962 *963 * alias is an Alias node representing the AS alias-clause attached to the964 * FROM expression, or NULL if no clause.965 *966 * eref is the table reference name and column reference names (either967 * real or aliases). Note that system columns (OID etc) are not included968 * in the column list.969 * eref->aliasname is required to be present, and should generally be used970 * to identify the RTE for error messages etc.971 *972 * In RELATION RTEs, the colnames in both alias and eref are indexed by973 * physical attribute number; this means there must be colname entries for974 * dropped columns. When building an RTE we insert empty strings ("") for975 * dropped columns. Note however that a stored rule may have nonempty976 * colnames for columns dropped since the rule was created (and for that977 * matter the colnames might be out of date due to column renamings).978 * The same comments apply to FUNCTION RTEs when a function's return type979 * is a named composite type.980 *981 * In JOIN RTEs, the colnames in both alias and eref are one-to-one with982 * joinaliasvars entries. A JOIN RTE will omit columns of its inputs when983 * those columns are known to be dropped at parse time. Again, however,984 * a stored rule might contain entries for columns dropped since the rule985 * was created. (This is only possible for columns not actually referenced986 * in the rule.) When loading a stored rule, we replace the joinaliasvars987 * items for any such columns with null pointers. (We can't simply delete988 * them from the joinaliasvars list, because that would affect the attnums989 * of Vars referencing the rest of the list.)990 *991 * inh is true for relation references that should be expanded to include992 * inheritance children, if the rel has any. This *must* be false for993 * RTEs other than RTE_RELATION entries.994 *995 * inFromCl marks those range variables that are listed in the FROM clause.996 * It's false for RTEs that are added to a query behind the scenes, such997 * as the NEW and OLD variables for a rule, or the subqueries of a UNION.998 * This flag is not used during parsing (except in transformLockingClause,999 * q.v.); the parser now uses a separate "namespace" data structure to1000 * control visibility. But it is needed by ruleutils.c to determine1001 * whether RTEs should be shown in decompiled queries.1002 *1003 * securityQuals is a list of security barrier quals (boolean expressions),1004 * to be tested in the listed order before returning a row from the1005 * relation. It is always NIL in parser output. Entries are added by the1006 * rewriter to implement security-barrier views and/or row-level security.1007 * Note that the planner turns each boolean expression into an implicitly1008 * AND'ed sublist, as is its usual habit with qualification expressions.1009 *--------------------1010 */1011typedef enum RTEKind1012{1013 RTE_RELATION, /* ordinary relation reference */1014 RTE_SUBQUERY, /* subquery in FROM */1015 RTE_JOIN, /* join */1016 RTE_FUNCTION, /* function in FROM */1017 RTE_TABLEFUNC, /* TableFunc(.., column list) */1018 RTE_VALUES, /* VALUES (<exprlist>), (<exprlist>), ... */1019 RTE_CTE, /* common table expr (WITH list element) */1020 RTE_NAMEDTUPLESTORE, /* tuplestore, e.g. for AFTER triggers */1021 RTE_RESULT /* RTE represents an empty FROM clause; such1022 * RTEs are added by the planner, they're not1023 * present during parsing or rewriting */1024} RTEKind;1025 1026typedef struct RangeTblEntry1027{1028 pg_node_attr(custom_read_write, custom_query_jumble)1029 1030 NodeTag type;1031 1032 RTEKind rtekind; /* see above */1033 1034 /*1035 * XXX the fields applicable to only some rte kinds should be merged into1036 * a union. I didn't do this yet because the diffs would impact a lot of1037 * code that is being actively worked on. FIXME someday.1038 */1039 1040 /*1041 * Fields valid for a plain relation RTE (else zero):1042 *1043 * rellockmode is really LOCKMODE, but it's declared int to avoid having1044 * to include lock-related headers here. It must be RowExclusiveLock if1045 * the RTE is an INSERT/UPDATE/DELETE/MERGE target, else RowShareLock if1046 * the RTE is a SELECT FOR UPDATE/FOR SHARE target, else AccessShareLock.1047 *1048 * Note: in some cases, rule expansion may result in RTEs that are marked1049 * with RowExclusiveLock even though they are not the target of the1050 * current query; this happens if a DO ALSO rule simply scans the original1051 * target table. We leave such RTEs with their original lockmode so as to1052 * avoid getting an additional, lesser lock.1053 *1054 * perminfoindex is 1-based index of the RTEPermissionInfo belonging to1055 * this RTE in the containing struct's list of same; 0 if permissions need1056 * not be checked for this RTE.1057 *1058 * As a special case, relid, relkind, rellockmode, and perminfoindex can1059 * also be set (nonzero) in an RTE_SUBQUERY RTE. This occurs when we1060 * convert an RTE_RELATION RTE naming a view into an RTE_SUBQUERY1061 * containing the view's query. We still need to perform run-time locking1062 * and permission checks on the view, even though it's not directly used1063 * in the query anymore, and the most expedient way to do that is to1064 * retain these fields from the old state of the RTE.1065 *1066 * As a special case, RTE_NAMEDTUPLESTORE can also set relid to indicate1067 * that the tuple format of the tuplestore is the same as the referenced1068 * relation. This allows plans referencing AFTER trigger transition1069 * tables to be invalidated if the underlying table is altered.1070 */1071 Oid relid; /* OID of the relation */1072 char relkind; /* relation kind (see pg_class.relkind) */1073 int rellockmode; /* lock level that query requires on the rel */1074 struct TableSampleClause *tablesample; /* sampling info, or NULL */1075 Index perminfoindex;1076 1077 /*1078 * Fields valid for a subquery RTE (else NULL):1079 */1080 Query *subquery; /* the sub-query */1081 bool security_barrier; /* is from security_barrier view? */1082 1083 /*1084 * Fields valid for a join RTE (else NULL/zero):1085 *1086 * joinaliasvars is a list of (usually) Vars corresponding to the columns1087 * of the join result. An alias Var referencing column K of the join1088 * result can be replaced by the K'th element of joinaliasvars --- but to1089 * simplify the task of reverse-listing aliases correctly, we do not do1090 * that until planning time. In detail: an element of joinaliasvars can1091 * be a Var of one of the join's input relations, or such a Var with an1092 * implicit coercion to the join's output column type, or a COALESCE1093 * expression containing the two input column Vars (possibly coerced).1094 * Elements beyond the first joinmergedcols entries are always just Vars,1095 * and are never referenced from elsewhere in the query (that is, join1096 * alias Vars are generated only for merged columns). We keep these1097 * entries only because they're needed in expandRTE() and similar code.1098 *1099 * Vars appearing within joinaliasvars are marked with varnullingrels sets1100 * that describe the nulling effects of this join and lower ones. This is1101 * essential for FULL JOIN cases, because the COALESCE expression only1102 * describes the semantics correctly if its inputs have been nulled by the1103 * join. For other cases, it allows expandRTE() to generate a valid1104 * representation of the join's output without consulting additional1105 * parser state.1106 *1107 * Within a Query loaded from a stored rule, it is possible for non-merged1108 * joinaliasvars items to be null pointers, which are placeholders for1109 * (necessarily unreferenced) columns dropped since the rule was made.1110 * Also, once planning begins, joinaliasvars items can be almost anything,1111 * as a result of subquery-flattening substitutions.1112 *1113 * joinleftcols is an integer list of physical column numbers of the left1114 * join input rel that are included in the join; likewise joinrighttcols1115 * for the right join input rel. (Which rels those are can be determined1116 * from the associated JoinExpr.) If the join is USING/NATURAL, then the1117 * first joinmergedcols entries in each list identify the merged columns.1118 * The merged columns come first in the join output, then remaining1119 * columns of the left input, then remaining columns of the right.1120 *1121 * Note that input columns could have been dropped after creation of a1122 * stored rule, if they are not referenced in the query (in particular,1123 * merged columns could not be dropped); this is not accounted for in1124 * joinleftcols/joinrighttcols.1125 */1126 JoinType jointype; /* type of join */1127 int joinmergedcols; /* number of merged (JOIN USING) columns */1128 List *joinaliasvars; /* list of alias-var expansions */1129 List *joinleftcols; /* left-side input column numbers */1130 List *joinrightcols; /* right-side input column numbers */1131 1132 /*1133 * join_using_alias is an alias clause attached directly to JOIN/USING. It1134 * is different from the alias field (below) in that it does not hide the1135 * range variables of the tables being joined.1136 */1137 Alias *join_using_alias;1138 1139 /*1140 * Fields valid for a function RTE (else NIL/zero):1141 *1142 * When funcordinality is true, the eref->colnames list includes an alias1143 * for the ordinality column. The ordinality column is otherwise1144 * implicit, and must be accounted for "by hand" in places such as1145 * expandRTE().1146 */1147 List *functions; /* list of RangeTblFunction nodes */1148 bool funcordinality; /* is this called WITH ORDINALITY? */1149 1150 /*1151 * Fields valid for a TableFunc RTE (else NULL):1152 */1153 TableFunc *tablefunc;1154 1155 /*1156 * Fields valid for a values RTE (else NIL):1157 */1158 List *values_lists; /* list of expression lists */1159 1160 /*1161 * Fields valid for a CTE RTE (else NULL/zero):1162 */1163 char *ctename; /* name of the WITH list item */1164 Index ctelevelsup; /* number of query levels up */1165 bool self_reference; /* is this a recursive self-reference? */1166 1167 /*1168 * Fields valid for CTE, VALUES, ENR, and TableFunc RTEs (else NIL):1169 *1170 * We need these for CTE RTEs so that the types of self-referential1171 * columns are well-defined. For VALUES RTEs, storing these explicitly1172 * saves having to re-determine the info by scanning the values_lists. For1173 * ENRs, we store the types explicitly here (we could get the information1174 * from the catalogs if 'relid' was supplied, but we'd still need these1175 * for TupleDesc-based ENRs, so we might as well always store the type1176 * info here). For TableFuncs, these fields are redundant with data in1177 * the TableFunc node, but keeping them here allows some code sharing with1178 * the other cases.1179 *1180 * For ENRs only, we have to consider the possibility of dropped columns.1181 * A dropped column is included in these lists, but it will have zeroes in1182 * all three lists (as well as an empty-string entry in eref). Testing1183 * for zero coltype is the standard way to detect a dropped column.1184 */1185 List *coltypes; /* OID list of column type OIDs */1186 List *coltypmods; /* integer list of column typmods */1187 List *colcollations; /* OID list of column collation OIDs */1188 1189 /*1190 * Fields valid for ENR RTEs (else NULL/zero):1191 */1192 char *enrname; /* name of ephemeral named relation */1193 Cardinality enrtuples; /* estimated or actual from caller */1194 1195 /*1196 * Fields valid in all RTEs:1197 */1198 Alias *alias; /* user-written alias clause, if any */1199 Alias *eref; /* expanded reference names */1200 bool lateral; /* subquery, function, or values is LATERAL? */