#define RELKIND_RELATION 'r'/* ordinary table */ #define RELKIND_INDEX 'i'/* secondary index */ #define RELKIND_SEQUENCE 'S'/* sequence object */ #define RELKIND_TOASTVALUE 't'/* for out-of-line values */ #define RELKIND_VIEW 'v'/* view */ #define RELKIND_MATVIEW 'm'/* materialized view */ #define RELKIND_COMPOSITE_TYPE 'c'/* composite type */ #define RELKIND_FOREIGN_TABLE 'f'/* foreign table */ #define RELKIND_PARTITIONED_TABLE 'p'/* partitioned table */ #define RELKIND_PARTITIONED_INDEX 'I'/* partitioned index */ #define RELPERSISTENCE_PERMANENT 'p'/* regular table */ #define RELPERSISTENCE_UNLOGGED 'u'/* unlogged permanent table */ #define RELPERSISTENCE_TEMP 't'/* temporary table */ /* default selection for replica identity (primary key or nothing) */ #define REPLICA_IDENTITY_DEFAULT 'd' /* no replica identity is logged for this relation */ #define REPLICA_IDENTITY_NOTHING 'n' /* all columns are logged as replica identity */ #define REPLICA_IDENTITY_FULL 'f' /* * an explicitly chosen candidate key's columns are used as replica identity. * Note this will still be set if the index has been dropped; in that case it * has the same meaning as 'n'. */ #define REPLICA_IDENTITY_INDEX 'i'
CreateStmt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
typedefstructCreateStmt { NodeTag type; RangeVar *relation; /* relation to create */ List *tableElts; /* column definitions (list of ColumnDef) */ List *inhRelations; /* relations to inherit from (list of * RangeVar) */ PartitionBoundSpec *partbound; /* FOR VALUES clause */ PartitionSpec *partspec; /* PARTITION BY clause */ TypeName *ofTypename; /* OF typename 类型名 */ List *constraints; /* constraints (list of Constraint nodes) 约束*/ List *options; /* options from WITH clause with 语句参数 */ OnCommitAction oncommit; /* what do we do at COMMIT? */ char *tablespacename; /* table space to use, or NULL 表空间 */ char *accessMethod; /* table access method 访问方法 */ bool if_not_exists; /* just do nothing if it already exists? 表是否存在*/ } CreateStmt;
typedefstructRangeVar { NodeTag type; char *catalogname; /* the catalog (database) name, or NULL */ char *schemaname; /* the schema name, or NULL */ char *relname; /* the relation/sequence name */ bool inh; /* expand rel by inheritance? recursively act * on children? */ char relpersistence; /* see RELPERSISTENCE_* in pg_class.h */ Alias *alias; /* table alias & optional column aliases */ int location; /* token location, or -1 if unknown */ } RangeVar;
/* * Alias - * specifies an alias for a range variable; the alias might also * specify renaming of columns within the table. * 为RangeVar指定别名.别名可能同时重命名了表列. * * Note: colnames is a list of Value nodes (always strings). In Alias structs * associated with RTEs, there may be entries corresponding to dropped * columns; these are normally empty strings (""). See parsenodes.h for info. * 注意:colnames是Value节点(通常是字符串)链表. * 在与RTEs相关的Alias结构体中,可能有跟已删除的列对应的条目. * 这些通常是空字符串("").详细可参考parsenodes.h */ typedefstructAlias { NodeTag type; //别名 char *aliasname; /* aliased rel name (never qualified) */ //列别名链表 List *colnames; /* optional list of column aliases */ } Alias;
/* * ColumnDef - column definition (used in various creates) * * If the column has a default value, we may have the value expression * in either "raw" form (an untransformed parse tree) or "cooked" form * (a post-parse-analysis, executable expression tree), depending on * how this ColumnDef node was created (by parsing, or by inheritance * from an existing relation). We should never have both in the same node! * * Similarly, we may have a COLLATE specification in either raw form * (represented as a CollateClause with arg==NULL) or cooked form * (the collation's OID). * * The constraints list may contain a CONSTR_DEFAULT item in a raw * parsetree produced by gram.y, but transformCreateStmt will remove * the item and set raw_default instead. CONSTR_DEFAULT items * should not appear in any subsequent processing. */ typedefstructColumnDef { NodeTag type; char *colname; /* name of column */ TypeName *typeName; /* type of column */ char *compression; /* compression method for column */ int inhcount; /* number of times column is inherited */ bool is_local; /* column has local (non-inherited) def'n */ bool is_not_null; /* NOT NULL constraint specified? */ bool is_from_type; /* column definition came from table type */ char storage; /* attstorage setting, or 0 for default */ Node *raw_default; /* default value (untransformed parse tree) */ Node *cooked_default; /* default value (transformed expr tree) */ char identity; /* attidentity setting */ RangeVar *identitySequence; /* to store identity sequence name for * ALTER TABLE ... ADD COLUMN */ char generated; /* attgenerated setting */ CollateClause *collClause; /* untransformed COLLATE spec, if any */ Oid collOid; /* collation OID (InvalidOid if not set) */ List *constraints; /* other constraints on column */ List *fdwoptions; /* per-column FDW options */ int location; /* parse location, or -1 if none/unknown */ } ColumnDef;
/* Fields used for most/all constraint types: */ char *conname; /* Constraint name, or NULL if unnamed 约束名 */ bool deferrable; /* DEFERRABLE? 可延迟 */ bool initdeferred; /* INITIALLY DEFERRED? */ int location; /* token location, or -1 if unknown */
/* Fields used for constraints with expressions (CHECK and DEFAULT): */ bool is_no_inherit; /* is constraint non-inheritable? 不可继承的约束*/ Node *raw_expr; /* expr, as untransformed parse tree 表达式,作为未转化解析树*/ char *cooked_expr; /* expr, as nodeToString representation */ char generated_when; /* ALWAYS or BY DEFAULT */
/* Fields used for unique constraints (UNIQUE and PRIMARY KEY): */ List *keys; /* String nodes naming referenced key key列 * column(s) */ List *including; /* String nodes naming referenced nonkey nonkey列 * column(s) */
/* Fields used for EXCLUSION constraints: */ List *exclusions; /* list of (IndexElem, operator name) pairs */
/* Fields used for index constraints (UNIQUE, PRIMARY KEY, EXCLUSION): */ List *options; /* options from WITH clause */ char *indexname; /* existing index to use; otherwise NULL 索引名 */ char *indexspace; /* index tablespace; NULL for default 索引对应的表空间 */ bool reset_default_tblspc; /* reset default_tablespace prior to * creating the index */ /* These could be, but currently are not, used for UNIQUE/PKEY: */ char *access_method; /* index access method; NULL for default 访问方法 */ Node *where_clause; /* partial index predicate where子句信息 */
/* Fields used for FOREIGN KEY constraints: */ RangeVar *pktable; /* Primary key table 主键信息 */ List *fk_attrs; /* Attributes of foreign key 外键属性列表*/ List *pk_attrs; /* Corresponding attrs in PK table 对应的主键属性*/ char fk_matchtype; /* FULL, PARTIAL, SIMPLE */ char fk_upd_action; /* ON UPDATE action */ char fk_del_action; /* ON DELETE action */ List *old_conpfeqop; /* pg_constraint.conpfeqop of my former self */ Oid old_pktable_oid; /* pg_constraint.confrelid of my former * self */
/* Fields used for constraints that allow a NOT VALID specification */ bool skip_validation; /* skip validation of existing rows? */ bool initially_valid; /* mark the new constraint as valid? */ } Constraint; ------------------------------------------------------------------------------------- typedefenumConstrType/* types of constraints */ { CONSTR_NULL, /* not standard SQL, but a lot of people * expect it */ CONSTR_NOTNULL, CONSTR_DEFAULT, CONSTR_IDENTITY, CONSTR_GENERATED, CONSTR_CHECK, CONSTR_PRIMARY, CONSTR_UNIQUE, CONSTR_EXCLUSION, CONSTR_FOREIGN, CONSTR_ATTR_DEFERRABLE, /* attributes for previous constraint node */ CONSTR_ATTR_NOT_DEFERRABLE, CONSTR_ATTR_DEFERRED, CONSTR_ATTR_IMMEDIATE } ConstrType;
/* * Query - * Parse analysis turns all statements into a Query tree * for further processing by the rewriter and planner. * 对解析树进行分析生成查询树,继而供后续重写器和计划器处理 * Utility statements (i.e. non-optimizable statements) have the * utilityStmt field set, and the rest of the Query is mostly dummy. * * Planning converts a Query tree into a Plan tree headed by a PlannedStmt * node --- the Query structure is not used by the executor. * * 计划器将查询树转变成计划树,其head为 PlannedStmt节点 */ typedefstructQuery { NodeTag type;
QuerySource querySource; /* where did I come from? */
uint64 queryId; /* query identifier (can be set by plugins) query 标识符*/
bool canSetTag; /* do I set the command result tag? */
Node *utilityStmt; /* non-null if commandType == CMD_UTILITY */
int resultRelation; /* rtable index of target relation for * INSERT/UPDATE/DELETE; 0 for SELECT */
bool hasAggs; /* has aggregates in tlist or havingQual agg */ bool hasWindowFuncs; /* has window functions in tlist 是否有窗口函数 */ bool hasTargetSRFs; /* has set-returning functions in tlist 是否设有returning functions */ bool hasSubLinks; /* has subquery SubLink 子查询链 */ bool hasDistinctOn; /* distinctClause is from DISTINCT ON 是否有distinct子句 */ bool hasRecursive; /* WITH RECURSIVE was specified */ bool hasModifyingCTE; /* has INSERT/UPDATE/DELETE in WITH */ bool hasForUpdate; /* FOR [KEY] UPDATE/SHARE was specified 是否指定for update */ bool hasRowSecurity; /* rewriter has applied some RLS policy */
bool isReturn; /* is a RETURN statement return 查询*/
List *cteList; /* WITH list (of CommonTableExpr's) */
List *rtable; /* list of range table entries 范围表项 */ FromExpr *jointree; /* table join tree (FROM and WHERE clauses) join tree */
List *targetList; /* target list (of TargetEntry) 投影列表*/
OverridingKind override; /* OVERRIDING clause */
OnConflictExpr *onConflict; /* ON CONFLICT DO [NOTHING | UPDATE] 冲突*/
List *returningList; /* return-values list (of TargetEntry) 返回链表*/
List *groupClause; /* a list of SortGroupClause's */ bool groupDistinct; /* is the group by clause distinct? */
List *groupingSets; /* a list of GroupingSet's if present */
Node *havingQual; /* qualifications applied to groups */
List *windowClause; /* a list of WindowClause's */
List *distinctClause; /* a list of SortGroupClause's */
List *sortClause; /* a list of SortGroupClause's */
Node *limitOffset; /* # of result tuples to skip (int8 expr) 偏移*/ Node *limitCount; /* # of result tuples to return (int8 expr) 计数*/ LimitOption limitOption; /* limit type */
List *rowMarks; /* a list of RowMarkClause's */
Node *setOperations; /* set-operation tree if this is top level of * a UNION/INTERSECT/EXCEPT query */
List *constraintDeps; /* a list of pg_constraint OIDs that the query * depends on to be semantically valid */
List *withCheckOptions; /* a list of WithCheckOption's (added * during rewrite) */
/* * The following two fields identify the portion of the source text string * containing this query. They are typically only populated in top-level * Queries, not in sub-queries. When not set, they might both be zero, or * both be -1 meaning "unknown". */ int stmt_location; /* start location, or -1 if unknown */ int stmt_len; /* length in bytes; 0 means "rest of string" */ } Query;
/* ---------------- * PlannedStmt node * * The output of the planner is a Plan tree headed by a PlannedStmt node. * PlannedStmt holds the "one time" information needed by the executor. * * 计划器对此处理生成一个头部为 PlannedStmt node 计划树 * DDL语句其 commandType == CMD_UTILITY * For simplicity in APIs, we also wrap utility statements in PlannedStmt * nodes; in such cases, commandType == CMD_UTILITY, the statement itself * is in the utilityStmt field, and the rest of the struct is mostly dummy. * (We do use canSetTag, stmt_location, stmt_len, and possibly queryId.) * ---------------- */ typedefstructPlannedStmt { NodeTag type;
uint64 queryId; /* query identifier (copied from Query) */
bool hasReturning; /* is it insert|update|delete RETURNING? */
bool hasModifyingCTE; /* has insert|update|delete in WITH? */ bool canSetTag; /* do I set the command result tag? */
bool transientPlan; /* redo plan when TransactionXmin changes? */
bool dependsOnRole; /* is plan specific to current role? */
bool parallelModeNeeded; /* parallel mode required to execute? 是否为并行模式 */
int jitFlags; /* which forms of JIT should be performed JIT 执行形式 */
structPlan *planTree; /* tree of Plan nodes */// plan nodes树
List *rtable; /* list of RangeTblEntry nodes */// 范围链表
/* rtable indexes of target relations for INSERT/UPDATE/DELETE */ List *resultRelations; /* integer list of RT indexes, or NIL */// 范围表索引
List *appendRelations; /* list of AppendRelInfo nodes */
List *subplans; /* Plan trees for SubPlan expressions; note * that some could be NULL */
Bitmapset *rewindPlanIDs; /* indices of subplans that require REWIND */
List *rowMarks; /* a list of PlanRowMark's */
List *relationOids; /* OIDs of relations the plan depends on */// relation oid
List *invalItems; /* other dependencies, as PlanInvalItems */
List *paramExecTypes; /* type OIDs for PARAM_EXEC Params */
Node *utilityStmt; /* non-null if this is utility stmt */ /* statement location in source string (copied from Query) */ int stmt_location; /* start location, or -1 if unknown */ int stmt_len; /* length in bytes; 0 means "rest of string" */ } PlannedStmt;