On Thu, Jan 19, 2023 at 09:42:03AM +0100, Peter Eisentraut wrote: > I see that in the 0003 patch, most location fields now have an explicit > markup with query_jumble_ignore. I thought we had previously resolved to > consider location fields to be automatically ignored unless explicitly > included (like for the Const node). This appears to invert that? Am I > missing something?
As a result, I have rebased the patch set to use the two-attribute approach: query_jumble_ignore and query_jumble_location. On top of the three previous patches, I am adding 0004 to implement a GUC able to switch the computation of the utility statements between what I am calling "string" to compute the query IDs based on the hash of the query string and the previous default, or "jumble", to use the parsed tree, with a few more tests to see the difference. Perhaps it is not worth bothering, but it could be possible that some users don't want to pay the penalty of doing the query jumbling with the parsed tree for utilities, as well.. -- Michael
From 43a5bdffdb9dbe3ecb40e9bf8a5f0e9f12ceff32 Mon Sep 17 00:00:00 2001 From: Michael Paquier <mich...@paquier.xyz> Date: Wed, 18 Jan 2023 14:26:12 +0900 Subject: [PATCH v4 1/4] Rework format of comments for nodes This is similar to 835d476, except that this one is to add node properties related to query jumbling. --- src/include/nodes/parsenodes.h | 261 ++++++++++++------ src/include/nodes/plannodes.h | 3 +- src/include/nodes/primnodes.h | 469 ++++++++++++++++++++++----------- 3 files changed, 487 insertions(+), 246 deletions(-) diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index cfeca96d53..eec51e3ee2 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -123,7 +123,8 @@ typedef struct Query CmdType commandType; /* select|insert|update|delete|merge|utility */ - QuerySource querySource; /* where did I come from? */ + /* where did I come from? */ + QuerySource querySource; /* * query identifier (can be set by plugins); ignored for equal, as it @@ -131,39 +132,58 @@ typedef struct Query */ uint64 queryId pg_node_attr(equal_ignore, read_write_ignore, read_as(0)); - bool canSetTag; /* do I set the command result tag? */ + /* do I set the command result tag? */ + bool canSetTag; Node *utilityStmt; /* non-null if commandType == CMD_UTILITY */ - int resultRelation; /* rtable index of target relation for - * INSERT/UPDATE/DELETE/MERGE; 0 for SELECT */ + /* + * rtable index of target relation for INSERT/UPDATE/DELETE/MERGE; 0 for + * SELECT. + */ + int resultRelation; - bool hasAggs; /* has aggregates in tlist or havingQual */ - bool hasWindowFuncs; /* has window functions in tlist */ - bool hasTargetSRFs; /* has set-returning functions in tlist */ - bool hasSubLinks; /* has subquery SubLink */ - bool hasDistinctOn; /* distinctClause is from DISTINCT ON */ - bool hasRecursive; /* WITH RECURSIVE was specified */ - bool hasModifyingCTE; /* has INSERT/UPDATE/DELETE in WITH */ - bool hasForUpdate; /* FOR [KEY] UPDATE/SHARE was specified */ - bool hasRowSecurity; /* rewriter has applied some RLS policy */ - - bool isReturn; /* is a RETURN statement */ + /* has aggregates in tlist or havingQual */ + bool hasAggs; + /* has window functions in tlist */ + bool hasWindowFuncs; + /* has set-returning functions in tlist */ + bool hasTargetSRFs; + /* has subquery SubLink */ + bool hasSubLinks; + /* distinctClause is from DISTINCT ON */ + bool hasDistinctOn; + /* WITH RECURSIVE was specified */ + bool hasRecursive; + /* has INSERT/UPDATE/DELETE in WITH */ + bool hasModifyingCTE; + /* FOR [KEY] UPDATE/SHARE was specified */ + bool hasForUpdate; + /* rewriter has applied some RLS policy */ + bool hasRowSecurity; + /* is a RETURN statement */ + bool isReturn; List *cteList; /* WITH list (of CommonTableExpr's) */ List *rtable; /* list of range table entries */ - List *rteperminfos; /* list of RTEPermissionInfo nodes for the - * rtable entries having perminfoindex > 0 */ + + /* + * list of RTEPermissionInfo nodes for the rtable entries having + * perminfoindex > 0 + */ + List *rteperminfos; FromExpr *jointree; /* table join tree (FROM and WHERE clauses); * also USING clause for MERGE */ List *mergeActionList; /* list of actions for MERGE (only) */ - bool mergeUseOuterJoin; /* whether to use outer join */ + /* whether to use outer join */ + bool mergeUseOuterJoin; List *targetList; /* target list (of TargetEntry) */ - OverridingKind override; /* OVERRIDING clause */ + /* OVERRIDING clause */ + OverridingKind override; OnConflictExpr *onConflict; /* ON CONFLICT DO [NOTHING | UPDATE] */ @@ -191,11 +211,14 @@ typedef struct Query 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 */ + /* + * A list of pg_constraint OIDs that the query depends on to be + * semantically valid + */ + List *constraintDeps; - List *withCheckOptions; /* a list of WithCheckOption's (added - * during rewrite) */ + /* a list of WithCheckOption's (added during rewrite) */ + List *withCheckOptions; /* * The following two fields identify the portion of the source text string @@ -203,8 +226,10 @@ typedef struct Query * 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" */ + /* start location, or -1 if unknown */ + int stmt_location; + /* length in bytes; 0 means "rest of string" */ + int stmt_len; } Query; @@ -239,7 +264,8 @@ typedef struct TypeName List *typmods; /* type modifier expression(s) */ int32 typemod; /* prespecified type modifier */ List *arrayBounds; /* array bounds */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } TypeName; /* @@ -259,7 +285,8 @@ typedef struct ColumnRef { NodeTag type; List *fields; /* field names (String nodes) or A_Star */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } ColumnRef; /* @@ -269,7 +296,8 @@ typedef struct ParamRef { NodeTag type; int number; /* the number of the parameter */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } ParamRef; /* @@ -302,7 +330,8 @@ typedef struct A_Expr List *name; /* possibly-qualified name of operator */ Node *lexpr; /* left argument, or NULL if none */ Node *rexpr; /* right argument, or NULL if none */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } A_Expr; /* @@ -328,7 +357,8 @@ typedef struct A_Const NodeTag type; union ValUnion val; bool isnull; /* SQL NULL constant */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } A_Const; /* @@ -339,7 +369,8 @@ typedef struct TypeCast NodeTag type; Node *arg; /* the expression being casted */ TypeName *typeName; /* the target type */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } TypeCast; /* @@ -350,7 +381,8 @@ typedef struct CollateClause NodeTag type; Node *arg; /* input expression */ List *collname; /* possibly-qualified collation name */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } CollateClause; /* @@ -370,7 +402,8 @@ typedef struct RoleSpec NodeTag type; RoleSpecType roletype; /* Type of this rolespec */ char *rolename; /* filled only for ROLESPEC_CSTRING */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } RoleSpec; /* @@ -400,7 +433,8 @@ typedef struct FuncCall bool agg_distinct; /* arguments were labeled DISTINCT */ bool func_variadic; /* last argument was labeled VARIADIC */ CoercionForm funcformat; /* how to display this node */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } FuncCall; /* @@ -457,7 +491,8 @@ typedef struct A_ArrayExpr { NodeTag type; List *elements; /* array element expressions */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } A_ArrayExpr; /* @@ -484,7 +519,8 @@ typedef struct ResTarget char *name; /* column name or NULL */ List *indirection; /* subscripts, field names, and '*', or NIL */ Node *val; /* the value expression to compute or assign */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } ResTarget; /* @@ -514,7 +550,8 @@ typedef struct SortBy SortByDir sortby_dir; /* ASC/DESC/USING/default */ SortByNulls sortby_nulls; /* NULLS FIRST/LAST */ List *useOp; /* name of op to use, if SORTBY_USING */ - int location; /* operator location, or -1 if none/unknown */ + /* token location, or -1 if unknown */ + int location; } SortBy; /* @@ -535,7 +572,8 @@ typedef struct WindowDef int frameOptions; /* frame_clause options, see below */ Node *startOffset; /* expression for starting bound, if any */ Node *endOffset; /* expression for ending bound, if any */ - int location; /* parse location, or -1 if none/unknown */ + /* token location, or -1 if unknown */ + int location; } WindowDef; /* @@ -625,7 +663,8 @@ typedef struct RangeTableFunc List *namespaces; /* list of namespaces as ResTarget */ List *columns; /* list of RangeTableFuncCol */ Alias *alias; /* table alias & optional column aliases */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } RangeTableFunc; /* @@ -643,7 +682,8 @@ typedef struct RangeTableFuncCol bool is_not_null; /* does it have NOT NULL? */ Node *colexpr; /* column filter expression */ Node *coldefexpr; /* column default value expression */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } RangeTableFuncCol; /* @@ -663,7 +703,8 @@ typedef struct RangeTableSample List *method; /* sampling method name (possibly qualified) */ List *args; /* argument(s) for sampling method */ Node *repeatable; /* REPEATABLE expression, or NULL if none */ - int location; /* method name location, or -1 if unknown */ + /* method name location, or -1 if unknown */ + int location; } RangeTableSample; /* @@ -706,7 +747,8 @@ typedef struct ColumnDef 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 */ + /* parse location, or -1 if unknown */ + int location; } ColumnDef; /* @@ -780,7 +822,8 @@ typedef struct DefElem Node *arg; /* typically Integer, Float, String, or * TypeName */ DefElemAction defaction; /* unspecified action, or SET/ADD/DROP */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } DefElem; /* @@ -809,7 +852,8 @@ typedef struct XmlSerialize XmlOptionType xmloption; /* DOCUMENT or CONTENT */ Node *expr; TypeName *typeName; - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } XmlSerialize; /* Partitioning related definitions */ @@ -827,7 +871,8 @@ typedef struct PartitionElem Node *expr; /* expression to partition on, or NULL */ List *collation; /* name of collation; NIL = default */ List *opclass; /* name of desired opclass; NIL = default */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } PartitionElem; typedef enum PartitionStrategy @@ -847,7 +892,8 @@ typedef struct PartitionSpec NodeTag type; PartitionStrategy strategy; List *partParams; /* List of PartitionElems */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } PartitionSpec; /* @@ -874,7 +920,8 @@ struct PartitionBoundSpec List *lowerdatums; /* List of PartitionRangeDatums */ List *upperdatums; /* List of PartitionRangeDatums */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; }; /* @@ -897,7 +944,8 @@ typedef struct PartitionRangeDatum Node *value; /* Const (or A_Const in raw tree), if kind is * PARTITION_RANGE_DATUM_VALUE, else NULL */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } PartitionRangeDatum; /* @@ -1223,14 +1271,21 @@ typedef struct RangeTblFunction NodeTag type; Node *funcexpr; /* expression tree for func call */ - int funccolcount; /* number of columns it contributes to RTE */ + /* number of columns it contributes to RTE */ + int funccolcount; /* These fields record the contents of a column definition list, if any: */ - List *funccolnames; /* column names (list of String) */ - List *funccoltypes; /* OID list of column type OIDs */ - List *funccoltypmods; /* integer list of column typmods */ - List *funccolcollations; /* OID list of column collation OIDs */ + /* column names (list of String) */ + List *funccolnames; + /* OID list of column type OIDs */ + List *funccoltypes; + /* integer list of column typmods */ + List *funccoltypmods; + /* OID list of column collation OIDs */ + List *funccolcollations; + /* This is set during planning for use by the executor: */ - Bitmapset *funcparams; /* PARAM_EXEC Param IDs affecting this func */ + /* PARAM_EXEC Param IDs affecting this func */ + Bitmapset *funcparams; } RangeTblFunction; /* @@ -1337,7 +1392,8 @@ typedef struct SortGroupClause Oid eqop; /* the equality operator ('=' op) */ Oid sortop; /* the ordering operator ('<' op), or 0 */ bool nulls_first; /* do NULLs come before normal values? */ - bool hashable; /* can eqop be implemented by hashing? */ + /* can eqop be implemented by hashing? */ + bool hashable; } SortGroupClause; /* @@ -1427,21 +1483,31 @@ typedef struct GroupingSet typedef struct WindowClause { NodeTag type; - char *name; /* window name (NULL in an OVER clause) */ - char *refname; /* referenced window name, if any */ + /* window name (NULL in an OVER clause) */ + char *name; + /* referenced window name, if any */ + char *refname; List *partitionClause; /* PARTITION BY list */ - List *orderClause; /* ORDER BY list */ + /* ORDER BY list */ + List *orderClause; int frameOptions; /* frame_clause options, see WindowDef */ Node *startOffset; /* expression for starting bound, if any */ Node *endOffset; /* expression for ending bound, if any */ - List *runCondition; /* qual to help short-circuit execution */ - Oid startInRangeFunc; /* in_range function for startOffset */ - Oid endInRangeFunc; /* in_range function for endOffset */ - Oid inRangeColl; /* collation for in_range tests */ - bool inRangeAsc; /* use ASC sort order for in_range tests? */ - bool inRangeNullsFirst; /* nulls sort first for in_range tests? */ + /* qual to help short-circuit execution */ + List *runCondition; + /* in_range function for startOffset */ + Oid startInRangeFunc; + /* in_range function for endOffset */ + Oid endInRangeFunc; + /* collation for in_range tests */ + Oid inRangeColl; + /* use ASC sort order for in_range tests? */ + bool inRangeAsc; + /* nulls sort first for in_range tests? */ + bool inRangeNullsFirst; Index winref; /* ID referenced by window functions */ - bool copiedOrder; /* did we copy orderClause from refname? */ + /* did we copy orderClause from refname? */ + bool copiedOrder; } WindowClause; /* @@ -1477,7 +1543,8 @@ typedef struct WithClause NodeTag type; List *ctes; /* list of CommonTableExprs */ bool recursive; /* true = WITH RECURSIVE */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } WithClause; /* @@ -1492,7 +1559,8 @@ typedef struct InferClause List *indexElems; /* IndexElems to infer unique index */ Node *whereClause; /* qualification (partial-index predicate) */ char *conname; /* Constraint name, or NULL if unnamed */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } InferClause; /* @@ -1508,7 +1576,8 @@ typedef struct OnConflictClause InferClause *infer; /* Optional index inference clause */ List *targetList; /* the target list (of ResTarget) */ Node *whereClause; /* qualifications */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } OnConflictClause; /* @@ -1558,15 +1627,25 @@ typedef struct CommonTableExpr Node *ctequery; /* the CTE's subquery */ CTESearchClause *search_clause; CTECycleClause *cycle_clause; - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; /* These fields are set during parse analysis: */ - bool cterecursive; /* is this CTE actually recursive? */ - int cterefcount; /* number of RTEs referencing this CTE - * (excluding internal self-references) */ - List *ctecolnames; /* list of output column names */ - List *ctecoltypes; /* OID list of output column type OIDs */ - List *ctecoltypmods; /* integer list of output column typmods */ - List *ctecolcollations; /* OID list of column collation OIDs */ + /* is this CTE actually recursive? */ + bool cterecursive; + + /* + * Number of RTEs referencing this CTE (excluding internal + * self-references) + */ + int cterefcount; + /* list of output column names */ + List *ctecolnames; + /* OID list of output column type OIDs */ + List *ctecoltypes; + /* integer list of output column typmods */ + List *ctecoltypmods; + /* OID list of column collation OIDs */ + List *ctecolcollations; } CommonTableExpr; /* Convenience macro to get the output tlist of a CTE's query */ @@ -1603,10 +1682,12 @@ typedef struct MergeAction NodeTag type; bool matched; /* true=MATCHED, false=NOT MATCHED */ CmdType commandType; /* INSERT/UPDATE/DELETE/DO NOTHING */ - OverridingKind override; /* OVERRIDING clause */ + /* OVERRIDING clause */ + OverridingKind override; Node *qual; /* transformed WHEN conditions */ List *targetList; /* the target list (of TargetEntry) */ - List *updateColnos; /* target attribute numbers of an UPDATE */ + /* target attribute numbers of an UPDATE */ + List *updateColnos; } MergeAction; /* @@ -1645,7 +1726,8 @@ typedef struct RawStmt { NodeTag type; Node *stmt; /* raw parse tree */ - int stmt_location; /* start location, or -1 if unknown */ + /* start location, or -1 if unknown */ + int stmt_location; int stmt_len; /* length in bytes; 0 means "rest of string" */ } RawStmt; @@ -1816,10 +1898,14 @@ typedef struct SetOperationStmt /* Eventually add fields for CORRESPONDING spec here */ /* Fields derived during parse analysis: */ - List *colTypes; /* OID list of output column type OIDs */ - List *colTypmods; /* integer list of output column typmods */ - List *colCollations; /* OID list of output column collation OIDs */ - List *groupClauses; /* a list of SortGroupClause's */ + /* OID list of output column type OIDs */ + List *colTypes; + /* integer list of output column typmods */ + List *colTypmods; + /* OID list of output column collation OIDs */ + List *colCollations; + /* a list of SortGroupClause's */ + List *groupClauses; /* groupClauses is NIL if UNION ALL, but must be set otherwise */ } SetOperationStmt; @@ -1849,7 +1935,8 @@ typedef struct PLAssignStmt List *indirection; /* subscripts and field names, if any */ int nnames; /* number of names to use in ColumnRef */ SelectStmt *val; /* the PL/pgSQL expression to assign */ - int location; /* name's token location, or -1 if unknown */ + /* name's token location, or -1 if unknown */ + int location; } PLAssignStmt; @@ -2356,7 +2443,8 @@ typedef struct Constraint char *conname; /* Constraint name, or NULL if unnamed */ bool deferrable; /* DEFERRABLE? */ bool initdeferred; /* INITIALLY DEFERRED? */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; /* Fields used for constraints with expressions (CHECK and DEFAULT): */ bool is_no_inherit; /* is constraint non-inheritable? */ @@ -3765,7 +3853,8 @@ typedef struct PublicationObjSpec PublicationObjSpecType pubobjtype; /* type of this publication object */ char *name; PublicationTable *pubtable; - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } PublicationObjSpec; typedef struct CreatePublicationStmt diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h index c1234fcf36..f33d7fe167 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -99,7 +99,8 @@ typedef struct PlannedStmt 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 */ + /* start location, or -1 if unknown */ + int stmt_location; int stmt_len; /* length in bytes; 0 means "rest of string" */ } PlannedStmt; diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 83e40e56d3..8d80c90ce7 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -98,19 +98,32 @@ typedef struct RangeVar typedef struct TableFunc { NodeTag type; - List *ns_uris; /* list of namespace URI expressions */ - List *ns_names; /* list of namespace names or NULL */ - Node *docexpr; /* input document expression */ - Node *rowexpr; /* row filter expression */ - List *colnames; /* column names (list of String) */ - List *coltypes; /* OID list of column type OIDs */ - List *coltypmods; /* integer list of column typmods */ - List *colcollations; /* OID list of column collation OIDs */ - List *colexprs; /* list of column filter expressions */ - List *coldefexprs; /* list of column default expressions */ - Bitmapset *notnulls; /* nullability flag for each output column */ - int ordinalitycol; /* counts from 0; -1 if none specified */ - int location; /* token location, or -1 if unknown */ + /* list of namespace URI expressions */ + List *ns_uris; + /* list of namespace names or NULL */ + List *ns_names; + /* input document expression */ + Node *docexpr; + /* row filter expression */ + Node *rowexpr; + /* column names (list of String) */ + List *colnames; + /* OID list of column type OIDs */ + List *coltypes; + /* integer list of column typmods */ + List *coltypmods; + /* OID list of column collation OIDs */ + List *colcollations; + /* list of column filter expressions */ + List *colexprs; + /* list of column default expressions */ + List *coldefexprs; + /* nullability flag for each output column */ + Bitmapset *notnulls; + /* counts from 0; -1 if none specified */ + int ordinalitycol; + /* token location, or -1 if unknown */ + int location; } TableFunc; /* @@ -256,18 +269,27 @@ typedef struct Const pg_node_attr(custom_copy_equal, custom_read_write) Expr xpr; - Oid consttype; /* pg_type OID of the constant's datatype */ - int32 consttypmod; /* typmod value, if any */ - Oid constcollid; /* OID of collation, or InvalidOid if none */ - int constlen; /* typlen of the constant's datatype */ - Datum constvalue; /* the constant's value */ - bool constisnull; /* whether the constant is null (if true, - * constvalue is undefined) */ - bool constbyval; /* whether this datatype is passed by value. - * If true, then all the information is stored - * in the Datum. If false, then the Datum - * contains a pointer to the information. */ - int location; /* token location, or -1 if unknown */ + /* pg_type OID of the constant's datatype */ + Oid consttype; + /* typmod value, if any */ + int32 consttypmod; + /* OID of collation, or InvalidOid if none */ + Oid constcollid; + /* typlen of the constant's datatype */ + int constlen; + /* the constant's value */ + Datum constvalue; + /* whether the constant is null (if true, constvalue is undefined) */ + bool constisnull; + + /* + * Whether this datatype is passed by value. If true, then all the + * information is stored in the Datum. If false, then the Datum contains + * a pointer to the information. + */ + bool constbyval; + /* token location, or -1 if unknown */ + int location; } Const; /* @@ -311,9 +333,12 @@ typedef struct Param ParamKind paramkind; /* kind of parameter. See above */ int paramid; /* numeric ID for parameter */ Oid paramtype; /* pg_type OID of parameter's datatype */ - int32 paramtypmod; /* typmod value, if known */ - Oid paramcollid; /* OID of collation, or InvalidOid if none */ - int location; /* token location, or -1 if unknown */ + /* typmod value, if known */ + int32 paramtypmod; + /* OID of collation, or InvalidOid if none */ + Oid paramcollid; + /* token location, or -1 if unknown */ + int location; } Param; /* @@ -486,16 +511,26 @@ typedef struct GroupingFunc typedef struct WindowFunc { Expr xpr; - Oid winfnoid; /* pg_proc Oid of the function */ - Oid wintype; /* type Oid of result of the window function */ - Oid wincollid; /* OID of collation of result */ - Oid inputcollid; /* OID of collation that function should use */ - List *args; /* arguments to the window function */ - Expr *aggfilter; /* FILTER expression, if any */ - Index winref; /* index of associated WindowClause */ - bool winstar; /* true if argument list was really '*' */ - bool winagg; /* is function a simple aggregate? */ - int location; /* token location, or -1 if unknown */ + /* pg_proc Oid of the function */ + Oid winfnoid; + /* type Oid of result of the window function */ + Oid wintype; + /* OID of collation of result */ + Oid wincollid; + /* OID of collation that function should use */ + Oid inputcollid; + /* arguments to the window function */ + List *args; + /* FILTER expression, if any */ + Expr *aggfilter; + /* index of associated WindowClause */ + Index winref; + /* true if argument list was really '*' */ + bool winstar; + /* is function a simple aggregate? */ + bool winagg; + /* token location, or -1 if unknown */ + int location; } WindowFunc; /* @@ -539,20 +574,28 @@ typedef struct WindowFunc typedef struct SubscriptingRef { Expr xpr; - Oid refcontainertype; /* type of the container proper */ - Oid refelemtype; /* the container type's pg_type.typelem */ - Oid refrestype; /* type of the SubscriptingRef's result */ - int32 reftypmod; /* typmod of the result */ - Oid refcollid; /* collation of result, or InvalidOid if none */ - List *refupperindexpr; /* expressions that evaluate to upper - * container indexes */ - List *reflowerindexpr; /* expressions that evaluate to lower - * container indexes, or NIL for single - * container element */ - Expr *refexpr; /* the expression that evaluates to a - * container value */ - Expr *refassgnexpr; /* expression for the source value, or NULL if - * fetch */ + /* type of the container proper */ + Oid refcontainertype; + /* the container type's pg_type.typelem */ + Oid refelemtype; + /* type of the SubscriptingRef's result */ + Oid refrestype; + /* typmod of the result */ + int32 reftypmod; + /* collation of result, or InvalidOid if none */ + Oid refcollid; + /* expressions that evaluate to upper container indexes */ + List *refupperindexpr; + + /* + * expressions that evaluate to lower container indexes, or NIL for single + * container element. + */ + List *reflowerindexpr; + /* the expression that evaluates to a container value */ + Expr *refexpr; + /* expression for the source value, or NULL if fetch */ + Expr *refassgnexpr; } SubscriptingRef; /* @@ -595,16 +638,28 @@ typedef enum CoercionForm typedef struct FuncExpr { Expr xpr; - Oid funcid; /* PG_PROC OID of the function */ - Oid funcresulttype; /* PG_TYPE OID of result value */ - bool funcretset; /* true if function returns set */ - bool funcvariadic; /* true if variadic arguments have been - * combined into an array last argument */ - CoercionForm funcformat; /* how to display this function call */ - Oid funccollid; /* OID of collation of result */ - Oid inputcollid; /* OID of collation that function should use */ - List *args; /* arguments to the function */ - int location; /* token location, or -1 if unknown */ + /* PG_PROC OID of the function */ + Oid funcid; + /* PG_TYPE OID of result value */ + Oid funcresulttype; + /* true if function returns set */ + bool funcretset; + + /* + * true if variadic arguments have been combined into an array last + * argument + */ + bool funcvariadic; + /* how to display this function call */ + CoercionForm funcformat; + /* OID of collation of result */ + Oid funccollid; + /* OID of collation that function should use */ + Oid inputcollid; + /* arguments to the function */ + List *args; + /* token location, or -1 if unknown */ + int location; } FuncExpr; /* @@ -624,10 +679,14 @@ typedef struct FuncExpr typedef struct NamedArgExpr { Expr xpr; - Expr *arg; /* the argument expression */ - char *name; /* the name */ - int argnumber; /* argument's number in positional notation */ - int location; /* argument name location, or -1 if unknown */ + /* the argument expression */ + Expr *arg; + /* the name */ + char *name; + /* argument's number in positional notation */ + int argnumber; + /* argument name location, or -1 if unknown */ + int location; } NamedArgExpr; /* @@ -765,7 +824,8 @@ typedef struct BoolExpr Expr xpr; BoolExprType boolop; List *args; /* arguments to this expression */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } BoolExpr; /* @@ -838,9 +898,12 @@ typedef struct SubLink SubLinkType subLinkType; /* see above */ int subLinkId; /* ID (1..n); 0 if not MULTIEXPR */ Node *testexpr; /* outer-query test for ALL/ANY/ROWCOMPARE */ - List *operName; /* originally specified operator name */ - Node *subselect; /* subselect as Query* or raw parsetree */ - int location; /* token location, or -1 if unknown */ + /* originally specified operator name */ + List *operName; + /* subselect as Query* or raw parsetree */ + Node *subselect; + /* token location, or -1 if unknown */ + int location; } SubLink; /* @@ -948,10 +1011,12 @@ typedef struct FieldSelect Expr xpr; Expr *arg; /* input expression */ AttrNumber fieldnum; /* attribute number of field to extract */ - Oid resulttype; /* type of the field (result type of this - * node) */ - int32 resulttypmod; /* output typmod (usually -1) */ - Oid resultcollid; /* OID of collation of the field */ + /* type of the field (result type of this node) */ + Oid resulttype; + /* output typmod (usually -1) */ + int32 resulttypmod; + /* OID of collation of the field */ + Oid resultcollid; } FieldSelect; /* ---------------- @@ -977,8 +1042,10 @@ typedef struct FieldStore Expr xpr; Expr *arg; /* input tuple value */ List *newvals; /* new value(s) for field(s) */ - List *fieldnums; /* integer list of field attnums */ - Oid resulttype; /* type of result (same as type of arg) */ + /* integer list of field attnums */ + List *fieldnums; + /* type of result (same as type of arg) */ + Oid resulttype; /* Like RowExpr, we deliberately omit a typmod and collation here */ } FieldStore; @@ -1000,10 +1067,14 @@ typedef struct RelabelType Expr xpr; Expr *arg; /* input expression */ Oid resulttype; /* output type of coercion expression */ - int32 resulttypmod; /* output typmod (usually -1) */ - Oid resultcollid; /* OID of collation, or InvalidOid if none */ - CoercionForm relabelformat; /* how to display this node */ - int location; /* token location, or -1 if unknown */ + /* output typmod (usually -1) */ + int32 resulttypmod; + /* OID of collation, or InvalidOid if none */ + Oid resultcollid; + /* how to display this node */ + CoercionForm relabelformat; + /* token location, or -1 if unknown */ + int location; } RelabelType; /* ---------------- @@ -1021,9 +1092,12 @@ typedef struct CoerceViaIO Expr *arg; /* input expression */ Oid resulttype; /* output type of coercion */ /* output typmod is not stored, but is presumed -1 */ - Oid resultcollid; /* OID of collation, or InvalidOid if none */ - CoercionForm coerceformat; /* how to display this node */ - int location; /* token location, or -1 if unknown */ + /* OID of collation, or InvalidOid if none */ + Oid resultcollid; + /* how to display this node */ + CoercionForm coerceformat; + /* token location, or -1 if unknown */ + int location; } CoerceViaIO; /* ---------------- @@ -1045,10 +1119,14 @@ typedef struct ArrayCoerceExpr Expr *arg; /* input expression (yields an array) */ Expr *elemexpr; /* expression representing per-element work */ Oid resulttype; /* output type of coercion (an array type) */ - int32 resulttypmod; /* output typmod (also element typmod) */ - Oid resultcollid; /* OID of collation, or InvalidOid if none */ - CoercionForm coerceformat; /* how to display this node */ - int location; /* token location, or -1 if unknown */ + /* output typmod (also element typmod) */ + int32 resulttypmod; + /* OID of collation, or InvalidOid if none */ + Oid resultcollid; + /* how to display this node */ + CoercionForm coerceformat; + /* token location, or -1 if unknown */ + int location; } ArrayCoerceExpr; /* ---------------- @@ -1070,8 +1148,10 @@ typedef struct ConvertRowtypeExpr Expr *arg; /* input expression */ Oid resulttype; /* output type (always a composite type) */ /* Like RowExpr, we deliberately omit a typmod and collation here */ - CoercionForm convertformat; /* how to display this node */ - int location; /* token location, or -1 if unknown */ + /* how to display this node */ + CoercionForm convertformat; + /* token location, or -1 if unknown */ + int location; } ConvertRowtypeExpr; /*---------- @@ -1086,7 +1166,8 @@ typedef struct CollateExpr Expr xpr; Expr *arg; /* input expression */ Oid collOid; /* collation's OID */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } CollateExpr; /*---------- @@ -1114,12 +1195,15 @@ typedef struct CollateExpr typedef struct CaseExpr { Expr xpr; - Oid casetype; /* type of expression result */ - Oid casecollid; /* OID of collation, or InvalidOid if none */ + /* type of expression result */ + Oid casetype; + /* OID of collation, or InvalidOid if none */ + Oid casecollid; Expr *arg; /* implicit equality comparison argument */ List *args; /* the arguments (list of WHEN clauses) */ Expr *defresult; /* the default result (ELSE clause) */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } CaseExpr; /* @@ -1130,7 +1214,8 @@ typedef struct CaseWhen Expr xpr; Expr *expr; /* condition expression */ Expr *result; /* substitution result */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } CaseWhen; /* @@ -1157,8 +1242,10 @@ typedef struct CaseTestExpr { Expr xpr; Oid typeId; /* type for substituted value */ - int32 typeMod; /* typemod for substituted value */ - Oid collation; /* collation for the substituted value */ + /* typemod for substituted value */ + int32 typeMod; + /* collation for the substituted value */ + Oid collation; } CaseTestExpr; /* @@ -1172,12 +1259,18 @@ typedef struct CaseTestExpr typedef struct ArrayExpr { Expr xpr; - Oid array_typeid; /* type of expression result */ - Oid array_collid; /* OID of collation, or InvalidOid if none */ - Oid element_typeid; /* common type of array elements */ - List *elements; /* the array elements or sub-arrays */ - bool multidims; /* true if elements are sub-arrays */ - int location; /* token location, or -1 if unknown */ + /* type of expression result */ + Oid array_typeid; + /* OID of collation, or InvalidOid if none */ + Oid array_collid; + /* common type of array elements */ + Oid element_typeid; + /* the array elements or sub-arrays */ + List *elements; + /* true if elements are sub-arrays */ + bool multidims; + /* token location, or -1 if unknown */ + int location; } ArrayExpr; /* @@ -1205,7 +1298,9 @@ typedef struct RowExpr { Expr xpr; List *args; /* the fields */ - Oid row_typeid; /* RECORDOID or a composite type's ID */ + + /* RECORDOID or a composite type's ID */ + Oid row_typeid; /* * row_typeid cannot be a domain over composite, only plain composite. To @@ -1219,9 +1314,15 @@ typedef struct RowExpr * We don't need to store a collation either. The result type is * necessarily composite, and composite types never have a collation. */ - CoercionForm row_format; /* how to display this node */ - List *colnames; /* list of String, or NIL */ - int location; /* token location, or -1 if unknown */ + + /* how to display this node */ + CoercionForm row_format; + + /* list of String, or NIL */ + List *colnames; + + /* token location, or -1 if unknown */ + int location; } RowExpr; /* @@ -1252,12 +1353,19 @@ typedef enum RowCompareType typedef struct RowCompareExpr { Expr xpr; - RowCompareType rctype; /* LT LE GE or GT, never EQ or NE */ - List *opnos; /* OID list of pairwise comparison ops */ - List *opfamilies; /* OID list of containing operator families */ - List *inputcollids; /* OID list of collations for comparisons */ - List *largs; /* the left-hand input arguments */ - List *rargs; /* the right-hand input arguments */ + + /* LT LE GE or GT, never EQ or NE */ + RowCompareType rctype; + /* OID list of pairwise comparison ops */ + List *opnos; + /* OID list of containing operator families */ + List *opfamilies; + /* OID list of collations for comparisons */ + List *inputcollids; + /* the left-hand input arguments */ + List *largs; + /* the right-hand input arguments */ + List *rargs; } RowCompareExpr; /* @@ -1266,10 +1374,14 @@ typedef struct RowCompareExpr typedef struct CoalesceExpr { Expr xpr; - Oid coalescetype; /* type of expression result */ - Oid coalescecollid; /* OID of collation, or InvalidOid if none */ - List *args; /* the arguments */ - int location; /* token location, or -1 if unknown */ + /* type of expression result */ + Oid coalescetype; + /* OID of collation, or InvalidOid if none */ + Oid coalescecollid; + /* the arguments */ + List *args; + /* token location, or -1 if unknown */ + int location; } CoalesceExpr; /* @@ -1284,12 +1396,18 @@ typedef enum MinMaxOp typedef struct MinMaxExpr { Expr xpr; - Oid minmaxtype; /* common type of arguments and result */ - Oid minmaxcollid; /* OID of collation of result */ - Oid inputcollid; /* OID of collation that function should use */ - MinMaxOp op; /* function to execute */ - List *args; /* the arguments */ - int location; /* token location, or -1 if unknown */ + /* common type of arguments and result */ + Oid minmaxtype; + /* OID of collation of result */ + Oid minmaxcollid; + /* OID of collation that function should use */ + Oid inputcollid; + /* function to execute */ + MinMaxOp op; + /* the arguments */ + List *args; + /* token location, or -1 if unknown */ + int location; } MinMaxExpr; /* @@ -1324,15 +1442,23 @@ typedef enum XmlOptionType typedef struct XmlExpr { Expr xpr; - XmlExprOp op; /* xml function ID */ - char *name; /* name in xml(NAME foo ...) syntaxes */ - List *named_args; /* non-XML expressions for xml_attributes */ - List *arg_names; /* parallel list of String values */ - List *args; /* list of expressions */ - XmlOptionType xmloption; /* DOCUMENT or CONTENT */ - Oid type; /* target type/typmod for XMLSERIALIZE */ + /* xml function ID */ + XmlExprOp op; + /* name in xml(NAME foo ...) syntaxes */ + char *name; + /* non-XML expressions for xml_attributes */ + List *named_args; + /* parallel list of String values */ + List *arg_names; + /* list of expressions */ + List *args; + /* DOCUMENT or CONTENT */ + XmlOptionType xmloption; + /* target type/typmod for XMLSERIALIZE */ + Oid type; int32 typmod; - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } XmlExpr; /* ---------------- @@ -1364,8 +1490,10 @@ typedef struct NullTest Expr xpr; Expr *arg; /* input expression */ NullTestType nulltesttype; /* IS NULL, IS NOT NULL */ - bool argisrow; /* T to perform field-by-field null checks */ - int location; /* token location, or -1 if unknown */ + /* T to perform field-by-field null checks */ + bool argisrow; + /* token location, or -1 if unknown */ + int location; } NullTest; /* @@ -1387,7 +1515,8 @@ typedef struct BooleanTest Expr xpr; Expr *arg; /* input expression */ BoolTestType booltesttype; /* test type */ - int location; /* token location, or -1 if unknown */ + /* token location, or -1 if unknown */ + int location; } BooleanTest; /* @@ -1404,10 +1533,14 @@ typedef struct CoerceToDomain Expr xpr; Expr *arg; /* input expression */ Oid resulttype; /* domain type ID (result type) */ - int32 resulttypmod; /* output typmod (currently always -1) */ - Oid resultcollid; /* OID of collation, or InvalidOid if none */ - CoercionForm coercionformat; /* how to display this node */ - int location; /* token location, or -1 if unknown */ + /* output typmod (currently always -1) */ + int32 resulttypmod; + /* OID of collation, or InvalidOid if none */ + Oid resultcollid; + /* how to display this node */ + CoercionForm coercionformat; + /* token location, or -1 if unknown */ + int location; } CoerceToDomain; /* @@ -1422,10 +1555,14 @@ typedef struct CoerceToDomain typedef struct CoerceToDomainValue { Expr xpr; - Oid typeId; /* type for substituted value */ - int32 typeMod; /* typemod for substituted value */ - Oid collation; /* collation for the substituted value */ - int location; /* token location, or -1 if unknown */ + /* type for substituted value */ + Oid typeId; + /* typemod for substituted value */ + int32 typeMod; + /* collation for the substituted value */ + Oid collation; + /* token location, or -1 if unknown */ + int location; } CoerceToDomainValue; /* @@ -1438,10 +1575,14 @@ typedef struct CoerceToDomainValue typedef struct SetToDefault { Expr xpr; - Oid typeId; /* type for substituted value */ - int32 typeMod; /* typemod for substituted value */ - Oid collation; /* collation for the substituted value */ - int location; /* token location, or -1 if unknown */ + /* type for substituted value */ + Oid typeId; + /* typemod for substituted value */ + int32 typeMod; + /* collation for the substituted value */ + Oid collation; + /* token location, or -1 if unknown */ + int location; } SetToDefault; /* @@ -1552,15 +1693,20 @@ typedef struct InferenceElem typedef struct TargetEntry { Expr xpr; - Expr *expr; /* expression to evaluate */ - AttrNumber resno; /* attribute number (see notes above) */ - char *resname; /* name of the column (could be NULL) */ - Index ressortgroupref; /* nonzero if referenced by a sort/group - * clause */ - Oid resorigtbl; /* OID of column's source table */ - AttrNumber resorigcol; /* column's number in source table */ - bool resjunk; /* set to true to eliminate the attribute from - * final target list */ + /* expression to evaluate */ + Expr *expr; + /* attribute number (see notes above) */ + AttrNumber resno; + /* name of the column (could be NULL) */ + char *resname; + /* nonzero if referenced by a sort/group clause */ + Index ressortgroupref; + /* OID of column's source table */ + Oid resorigtbl; + /* column's number in source table */ + AttrNumber resorigcol; + /* set to true to eliminate the attribute from final target list */ + bool resjunk; } TargetEntry; @@ -1642,11 +1788,16 @@ typedef struct JoinExpr bool isNatural; /* Natural join? Will need to shape table */ Node *larg; /* left subtree */ Node *rarg; /* right subtree */ - List *usingClause; /* USING clause, if any (list of String) */ - Alias *join_using_alias; /* alias attached to USING clause, if any */ - Node *quals; /* qualifiers on join, if any */ - Alias *alias; /* user-written alias clause, if any */ - int rtindex; /* RT index assigned for join, or 0 */ + /* USING clause, if any (list of String) */ + List *usingClause; + /* alias attached to USING clause, if any */ + Alias *join_using_alias; + /* qualifiers on join, if any */ + Node *quals; + /* user-written alias clause, if any */ + Alias *alias; + /* RT index assigned for join, or 0 */ + int rtindex; } JoinExpr; /*---------- -- 2.39.0
From cd672f8feb4c533c2ef1bbfb31088efc64ce7160 Mon Sep 17 00:00:00 2001 From: Michael Paquier <mich...@paquier.xyz> Date: Wed, 18 Jan 2023 14:37:07 +0900 Subject: [PATCH v4 2/4] Move query jumble code to src/backend/nodes/ This will ease a follow-up move that will generate automatically this code: - queryjumble.c -> queryjumblefuncs.c - utils/queryjumble.h -> nodes/queryjumble.h --- src/include/{utils => nodes}/queryjumble.h | 2 +- src/include/parser/analyze.h | 2 +- src/backend/nodes/Makefile | 1 + src/backend/nodes/meson.build | 1 + .../{utils/misc/queryjumble.c => nodes/queryjumblefuncs.c} | 6 +++--- src/backend/parser/analyze.c | 2 +- src/backend/postmaster/postmaster.c | 2 +- src/backend/utils/misc/Makefile | 1 - src/backend/utils/misc/guc_tables.c | 2 +- src/backend/utils/misc/meson.build | 1 - contrib/pg_stat_statements/pg_stat_statements.c | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) rename src/include/{utils => nodes}/queryjumble.h (98%) rename src/backend/{utils/misc/queryjumble.c => nodes/queryjumblefuncs.c} (99%) diff --git a/src/include/utils/queryjumble.h b/src/include/nodes/queryjumble.h similarity index 98% rename from src/include/utils/queryjumble.h rename to src/include/nodes/queryjumble.h index d372801410..204b8f74fd 100644 --- a/src/include/utils/queryjumble.h +++ b/src/include/nodes/queryjumble.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * src/include/utils/queryjumble.h + * src/include/nodes/queryjumble.h * *------------------------------------------------------------------------- */ diff --git a/src/include/parser/analyze.h b/src/include/parser/analyze.h index c97be6efcf..1cef1833a6 100644 --- a/src/include/parser/analyze.h +++ b/src/include/parser/analyze.h @@ -15,8 +15,8 @@ #define ANALYZE_H #include "nodes/params.h" +#include "nodes/queryjumble.h" #include "parser/parse_node.h" -#include "utils/queryjumble.h" /* Hook for plugins to get control at end of parse analysis */ typedef void (*post_parse_analyze_hook_type) (ParseState *pstate, diff --git a/src/backend/nodes/Makefile b/src/backend/nodes/Makefile index 7c594be583..af12c64878 100644 --- a/src/backend/nodes/Makefile +++ b/src/backend/nodes/Makefile @@ -27,6 +27,7 @@ OBJS = \ outfuncs.o \ params.o \ print.o \ + queryjumblefuncs.o \ read.o \ readfuncs.o \ tidbitmap.o \ diff --git a/src/backend/nodes/meson.build b/src/backend/nodes/meson.build index 2ff7dbac1d..9230515e7f 100644 --- a/src/backend/nodes/meson.build +++ b/src/backend/nodes/meson.build @@ -10,6 +10,7 @@ backend_sources += files( 'nodes.c', 'params.c', 'print.c', + 'queryjumblefuncs.c', 'read.c', 'tidbitmap.c', 'value.c', diff --git a/src/backend/utils/misc/queryjumble.c b/src/backend/nodes/queryjumblefuncs.c similarity index 99% rename from src/backend/utils/misc/queryjumble.c rename to src/backend/nodes/queryjumblefuncs.c index 328995a7dc..16084842a3 100644 --- a/src/backend/utils/misc/queryjumble.c +++ b/src/backend/nodes/queryjumblefuncs.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------- * - * queryjumble.c + * queryjumblefuncs.c * Query normalization and fingerprinting. * * Normalization is a process whereby similar queries, typically differing only @@ -26,7 +26,7 @@ * * * IDENTIFICATION - * src/backend/utils/misc/queryjumble.c + * src/backend/nodes/queryjumblefuncs.c * *------------------------------------------------------------------------- */ @@ -34,8 +34,8 @@ #include "common/hashfn.h" #include "miscadmin.h" +#include "nodes/queryjumble.h" #include "parser/scansup.h" -#include "utils/queryjumble.h" #define JUMBLE_SIZE 1024 /* query serialization buffer size */ diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 5b90974e83..4a817b75ad 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -30,6 +30,7 @@ #include "miscadmin.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" +#include "nodes/queryjumble.h" #include "optimizer/optimizer.h" #include "parser/analyze.h" #include "parser/parse_agg.h" @@ -50,7 +51,6 @@ #include "utils/backend_status.h" #include "utils/builtins.h" #include "utils/guc.h" -#include "utils/queryjumble.h" #include "utils/rel.h" #include "utils/syscache.h" diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 9cedc1b9f0..f05a26d255 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -102,6 +102,7 @@ #include "libpq/libpq.h" #include "libpq/pqformat.h" #include "libpq/pqsignal.h" +#include "nodes/queryjumble.h" #include "pg_getopt.h" #include "pgstat.h" #include "port/pg_bswap.h" @@ -126,7 +127,6 @@ #include "utils/memutils.h" #include "utils/pidfile.h" #include "utils/ps_status.h" -#include "utils/queryjumble.h" #include "utils/timeout.h" #include "utils/timestamp.h" #include "utils/varlena.h" diff --git a/src/backend/utils/misc/Makefile b/src/backend/utils/misc/Makefile index b9ee4eb48a..2910032930 100644 --- a/src/backend/utils/misc/Makefile +++ b/src/backend/utils/misc/Makefile @@ -26,7 +26,6 @@ OBJS = \ pg_rusage.o \ ps_status.o \ queryenvironment.o \ - queryjumble.o \ rls.o \ sampling.o \ superuser.o \ diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c index 5025e80f89..f9bfbbbd95 100644 --- a/src/backend/utils/misc/guc_tables.c +++ b/src/backend/utils/misc/guc_tables.c @@ -43,6 +43,7 @@ #include "jit/jit.h" #include "libpq/auth.h" #include "libpq/libpq.h" +#include "nodes/queryjumble.h" #include "optimizer/cost.h" #include "optimizer/geqo.h" #include "optimizer/optimizer.h" @@ -77,7 +78,6 @@ #include "utils/pg_locale.h" #include "utils/portal.h" #include "utils/ps_status.h" -#include "utils/queryjumble.h" #include "utils/inval.h" #include "utils/xml.h" diff --git a/src/backend/utils/misc/meson.build b/src/backend/utils/misc/meson.build index e3e99ec5cb..f719c97c05 100644 --- a/src/backend/utils/misc/meson.build +++ b/src/backend/utils/misc/meson.build @@ -11,7 +11,6 @@ backend_sources += files( 'pg_rusage.c', 'ps_status.c', 'queryenvironment.c', - 'queryjumble.c', 'rls.c', 'sampling.c', 'superuser.c', diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index a7a72783e5..ad1fe44496 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -55,6 +55,7 @@ #include "jit/jit.h" #include "mb/pg_wchar.h" #include "miscadmin.h" +#include "nodes/queryjumble.h" #include "optimizer/planner.h" #include "parser/analyze.h" #include "parser/parsetree.h" @@ -69,7 +70,6 @@ #include "tcop/utility.h" #include "utils/acl.h" #include "utils/builtins.h" -#include "utils/queryjumble.h" #include "utils/memutils.h" #include "utils/timestamp.h" -- 2.39.0
From a304b483d6b9101daf7494830af46d19da464161 Mon Sep 17 00:00:00 2001 From: Michael Paquier <mich...@paquier.xyz> Date: Fri, 20 Jan 2023 13:22:40 +0900 Subject: [PATCH v4 3/4] Support for automated query jumble with all Nodes This applies query jumbling in a consistent way to all the Nodes, including DDLs & friends. --- src/include/nodes/bitmapset.h | 2 +- src/include/nodes/nodes.h | 7 + src/include/nodes/parsenodes.h | 126 ++-- src/include/nodes/primnodes.h | 269 ++++---- src/backend/nodes/README | 1 + src/backend/nodes/gen_node_support.pl | 105 +++- src/backend/nodes/meson.build | 2 +- src/backend/nodes/queryjumblefuncs.c | 855 ++++++-------------------- 8 files changed, 509 insertions(+), 858 deletions(-) diff --git a/src/include/nodes/bitmapset.h b/src/include/nodes/bitmapset.h index 0dca6bc5fa..3d2225e1ae 100644 --- a/src/include/nodes/bitmapset.h +++ b/src/include/nodes/bitmapset.h @@ -50,7 +50,7 @@ typedef int32 signedbitmapword; /* must be the matching signed type */ typedef struct Bitmapset { - pg_node_attr(custom_copy_equal, special_read_write) + pg_node_attr(custom_copy_equal, special_read_write, no_query_jumble) NodeTag type; int nwords; /* number of words in array */ diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 10752e8011..6ecd944a90 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -59,6 +59,8 @@ typedef enum NodeTag * * - no_copy_equal: Shorthand for both no_copy and no_equal. * + * - no_query_jumble: Does not support jumble() at all. + * * - no_read: Does not support nodeRead() at all. * * - nodetag_only: Does not support copyObject(), equal(), outNode(), @@ -97,6 +99,11 @@ typedef enum NodeTag * - equal_ignore_if_zero: Ignore the field for equality if it is zero. * (Otherwise, compare normally.) * + * - query_jumble_ignore: Ignore the field for the query jumbling. + * + * - query_jumble_location: Mark the field as a location to track. This is + * only allowed for integer fields that include "location" in their name. + * * - read_as(VALUE): In nodeRead(), replace the field's value with VALUE. * * - read_write_ignore: Ignore the field for read/write. This is only allowed diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index eec51e3ee2..826ca0f84a 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -116,6 +116,11 @@ typedef uint64 AclMode; /* a bitmask of privilege bits */ * * Planning converts a Query tree into a Plan tree headed by a PlannedStmt * node --- the Query structure is not used by the executor. + * + * All the fields ignored for the query jumbling are not semantically + * significant (such as alias names), as is ignored anything that can + * be deduced from child nodes (else we'd just be double-hashing that + * piece of information). */ typedef struct Query { @@ -124,45 +129,47 @@ typedef struct Query CmdType commandType; /* select|insert|update|delete|merge|utility */ /* where did I come from? */ - QuerySource querySource; + QuerySource querySource pg_node_attr(query_jumble_ignore); /* * query identifier (can be set by plugins); ignored for equal, as it - * might not be set; also not stored + * might not be set; also not stored. This is the result of the query + * jumble, hence ignored. */ - uint64 queryId pg_node_attr(equal_ignore, read_write_ignore, read_as(0)); + uint64 queryId pg_node_attr(equal_ignore, query_jumble_ignore, read_write_ignore, read_as(0)); /* do I set the command result tag? */ - bool canSetTag; + bool canSetTag pg_node_attr(query_jumble_ignore); Node *utilityStmt; /* non-null if commandType == CMD_UTILITY */ /* * rtable index of target relation for INSERT/UPDATE/DELETE/MERGE; 0 for - * SELECT. + * SELECT. This is ignored in the query jumble as unrelated to the + * compilation of the query ID. */ - int resultRelation; + int resultRelation pg_node_attr(query_jumble_ignore); /* has aggregates in tlist or havingQual */ - bool hasAggs; + bool hasAggs pg_node_attr(query_jumble_ignore); /* has window functions in tlist */ - bool hasWindowFuncs; + bool hasWindowFuncs pg_node_attr(query_jumble_ignore); /* has set-returning functions in tlist */ - bool hasTargetSRFs; + bool hasTargetSRFs pg_node_attr(query_jumble_ignore); /* has subquery SubLink */ - bool hasSubLinks; + bool hasSubLinks pg_node_attr(query_jumble_ignore); /* distinctClause is from DISTINCT ON */ - bool hasDistinctOn; + bool hasDistinctOn pg_node_attr(query_jumble_ignore); /* WITH RECURSIVE was specified */ - bool hasRecursive; + bool hasRecursive pg_node_attr(query_jumble_ignore); /* has INSERT/UPDATE/DELETE in WITH */ - bool hasModifyingCTE; + bool hasModifyingCTE pg_node_attr(query_jumble_ignore); /* FOR [KEY] UPDATE/SHARE was specified */ - bool hasForUpdate; + bool hasForUpdate pg_node_attr(query_jumble_ignore); /* rewriter has applied some RLS policy */ - bool hasRowSecurity; + bool hasRowSecurity pg_node_attr(query_jumble_ignore); /* is a RETURN statement */ - bool isReturn; + bool isReturn pg_node_attr(query_jumble_ignore); List *cteList; /* WITH list (of CommonTableExpr's) */ @@ -172,18 +179,18 @@ typedef struct Query * list of RTEPermissionInfo nodes for the rtable entries having * perminfoindex > 0 */ - List *rteperminfos; + List *rteperminfos pg_node_attr(query_jumble_ignore); FromExpr *jointree; /* table join tree (FROM and WHERE clauses); * also USING clause for MERGE */ List *mergeActionList; /* list of actions for MERGE (only) */ /* whether to use outer join */ - bool mergeUseOuterJoin; + bool mergeUseOuterJoin pg_node_attr(query_jumble_ignore); List *targetList; /* target list (of TargetEntry) */ /* OVERRIDING clause */ - OverridingKind override; + OverridingKind override pg_node_attr(query_jumble_ignore); OnConflictExpr *onConflict; /* ON CONFLICT DO [NOTHING | UPDATE] */ @@ -215,10 +222,10 @@ typedef struct Query * A list of pg_constraint OIDs that the query depends on to be * semantically valid */ - List *constraintDeps; + List *constraintDeps pg_node_attr(query_jumble_ignore); /* a list of WithCheckOption's (added during rewrite) */ - List *withCheckOptions; + List *withCheckOptions pg_node_attr(query_jumble_ignore); /* * The following two fields identify the portion of the source text string @@ -229,7 +236,7 @@ typedef struct Query /* start location, or -1 if unknown */ int stmt_location; /* length in bytes; 0 means "rest of string" */ - int stmt_len; + int stmt_len pg_node_attr(query_jumble_ignore); } Query; @@ -1042,7 +1049,7 @@ typedef enum RTEKind typedef struct RangeTblEntry { - pg_node_attr(custom_read_write) + pg_node_attr(custom_read_write, no_query_jumble) NodeTag type; @@ -1265,6 +1272,8 @@ typedef struct RTEPermissionInfo * time. We do however remember how many columns we thought the type had * (including dropped columns!), so that we can successfully ignore any * columns added after the query was parsed. + * + * The query jumbling needs only to track the function expression. */ typedef struct RangeTblFunction { @@ -1272,20 +1281,20 @@ typedef struct RangeTblFunction Node *funcexpr; /* expression tree for func call */ /* number of columns it contributes to RTE */ - int funccolcount; + int funccolcount pg_node_attr(query_jumble_ignore); /* These fields record the contents of a column definition list, if any: */ /* column names (list of String) */ - List *funccolnames; + List *funccolnames pg_node_attr(query_jumble_ignore); /* OID list of column type OIDs */ - List *funccoltypes; + List *funccoltypes pg_node_attr(query_jumble_ignore); /* integer list of column typmods */ - List *funccoltypmods; + List *funccoltypmods pg_node_attr(query_jumble_ignore); /* OID list of column collation OIDs */ - List *funccolcollations; + List *funccolcollations pg_node_attr(query_jumble_ignore); /* This is set during planning for use by the executor: */ /* PARAM_EXEC Param IDs affecting this func */ - Bitmapset *funcparams; + Bitmapset *funcparams pg_node_attr(query_jumble_ignore); } RangeTblFunction; /* @@ -1393,7 +1402,7 @@ typedef struct SortGroupClause Oid sortop; /* the ordering operator ('<' op), or 0 */ bool nulls_first; /* do NULLs come before normal values? */ /* can eqop be implemented by hashing? */ - bool hashable; + bool hashable pg_node_attr(query_jumble_ignore); } SortGroupClause; /* @@ -1458,7 +1467,7 @@ typedef enum GroupingSetKind typedef struct GroupingSet { NodeTag type; - GroupingSetKind kind; + GroupingSetKind kind pg_node_attr(query_jumble_ignore); List *content; int location; } GroupingSet; @@ -1479,35 +1488,38 @@ typedef struct GroupingSet * When refname isn't null, the partitionClause is always copied from there; * the orderClause might or might not be copied (see copiedOrder); the framing * options are never copied, per spec. + * + * The information relevant for the query jumbling is the partition clause + * type and its bounds. */ typedef struct WindowClause { NodeTag type; /* window name (NULL in an OVER clause) */ - char *name; + char *name pg_node_attr(query_jumble_ignore); /* referenced window name, if any */ - char *refname; + char *refname pg_node_attr(query_jumble_ignore); List *partitionClause; /* PARTITION BY list */ /* ORDER BY list */ - List *orderClause; + List *orderClause pg_node_attr(query_jumble_ignore); int frameOptions; /* frame_clause options, see WindowDef */ Node *startOffset; /* expression for starting bound, if any */ Node *endOffset; /* expression for ending bound, if any */ /* qual to help short-circuit execution */ - List *runCondition; + List *runCondition pg_node_attr(query_jumble_ignore); /* in_range function for startOffset */ - Oid startInRangeFunc; + Oid startInRangeFunc pg_node_attr(query_jumble_ignore); /* in_range function for endOffset */ - Oid endInRangeFunc; + Oid endInRangeFunc pg_node_attr(query_jumble_ignore); /* collation for in_range tests */ - Oid inRangeColl; + Oid inRangeColl pg_node_attr(query_jumble_ignore); /* use ASC sort order for in_range tests? */ - bool inRangeAsc; + bool inRangeAsc pg_node_attr(query_jumble_ignore); /* nulls sort first for in_range tests? */ - bool inRangeNullsFirst; + bool inRangeNullsFirst pg_node_attr(query_jumble_ignore); Index winref; /* ID referenced by window functions */ /* did we copy orderClause from refname? */ - bool copiedOrder; + bool copiedOrder pg_node_attr(query_jumble_ignore); } WindowClause; /* @@ -1625,27 +1637,27 @@ typedef struct CommonTableExpr CTEMaterialize ctematerialized; /* is this an optimization fence? */ /* SelectStmt/InsertStmt/etc before parse analysis, Query afterwards: */ Node *ctequery; /* the CTE's subquery */ - CTESearchClause *search_clause; - CTECycleClause *cycle_clause; + CTESearchClause *search_clause pg_node_attr(query_jumble_ignore); + CTECycleClause *cycle_clause pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; /* These fields are set during parse analysis: */ /* is this CTE actually recursive? */ - bool cterecursive; + bool cterecursive pg_node_attr(query_jumble_ignore); /* * Number of RTEs referencing this CTE (excluding internal - * self-references) + * self-references), irrelevant for query jumbling. */ - int cterefcount; + int cterefcount pg_node_attr(query_jumble_ignore); /* list of output column names */ - List *ctecolnames; + List *ctecolnames pg_node_attr(query_jumble_ignore); /* OID list of output column type OIDs */ - List *ctecoltypes; + List *ctecoltypes pg_node_attr(query_jumble_ignore); /* integer list of output column typmods */ - List *ctecoltypmods; + List *ctecoltypmods pg_node_attr(query_jumble_ignore); /* OID list of column collation OIDs */ - List *ctecolcollations; + List *ctecolcollations pg_node_attr(query_jumble_ignore); } CommonTableExpr; /* Convenience macro to get the output tlist of a CTE's query */ @@ -1683,11 +1695,11 @@ typedef struct MergeAction bool matched; /* true=MATCHED, false=NOT MATCHED */ CmdType commandType; /* INSERT/UPDATE/DELETE/DO NOTHING */ /* OVERRIDING clause */ - OverridingKind override; + OverridingKind override pg_node_attr(query_jumble_ignore); Node *qual; /* transformed WHEN conditions */ List *targetList; /* the target list (of TargetEntry) */ /* target attribute numbers of an UPDATE */ - List *updateColnos; + List *updateColnos pg_node_attr(query_jumble_ignore); } MergeAction; /* @@ -1897,15 +1909,15 @@ typedef struct SetOperationStmt Node *rarg; /* right child */ /* Eventually add fields for CORRESPONDING spec here */ - /* Fields derived during parse analysis: */ + /* Fields derived during parse analysis (irrelevant for query jumbling): */ /* OID list of output column type OIDs */ - List *colTypes; + List *colTypes pg_node_attr(query_jumble_ignore); /* integer list of output column typmods */ - List *colTypmods; + List *colTypmods pg_node_attr(query_jumble_ignore); /* OID list of output column collation OIDs */ - List *colCollations; + List *colCollations pg_node_attr(query_jumble_ignore); /* a list of SortGroupClause's */ - List *groupClauses; + List *groupClauses pg_node_attr(query_jumble_ignore); /* groupClauses is NIL if UNION ALL, but must be set otherwise */ } SetOperationStmt; diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 8d80c90ce7..8a4f64adc2 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -99,29 +99,29 @@ typedef struct TableFunc { NodeTag type; /* list of namespace URI expressions */ - List *ns_uris; + List *ns_uris pg_node_attr(query_jumble_ignore); /* list of namespace names or NULL */ - List *ns_names; + List *ns_names pg_node_attr(query_jumble_ignore); /* input document expression */ Node *docexpr; /* row filter expression */ Node *rowexpr; /* column names (list of String) */ - List *colnames; + List *colnames pg_node_attr(query_jumble_ignore); /* OID list of column type OIDs */ - List *coltypes; + List *coltypes pg_node_attr(query_jumble_ignore); /* integer list of column typmods */ - List *coltypmods; + List *coltypmods pg_node_attr(query_jumble_ignore); /* OID list of column collation OIDs */ - List *colcollations; + List *colcollations pg_node_attr(query_jumble_ignore); /* list of column filter expressions */ List *colexprs; /* list of column default expressions */ - List *coldefexprs; + List *coldefexprs pg_node_attr(query_jumble_ignore); /* nullability flag for each output column */ - Bitmapset *notnulls; + Bitmapset *notnulls pg_node_attr(query_jumble_ignore); /* counts from 0; -1 if none specified */ - int ordinalitycol; + int ordinalitycol pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } TableFunc; @@ -230,11 +230,11 @@ typedef struct Var AttrNumber varattno; /* pg_type OID for the type of this var */ - Oid vartype; + Oid vartype pg_node_attr(query_jumble_ignore); /* pg_attribute typmod value */ - int32 vartypmod; + int32 vartypmod pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid varcollid; + Oid varcollid pg_node_attr(query_jumble_ignore); /* * for subquery variables referencing outer relations; 0 in a normal var, @@ -248,9 +248,9 @@ typedef struct Var * their varno/varattno match. */ /* syntactic relation index (0 if unknown) */ - Index varnosyn pg_node_attr(equal_ignore); + Index varnosyn pg_node_attr(equal_ignore, query_jumble_ignore); /* syntactic attribute number */ - AttrNumber varattnosyn pg_node_attr(equal_ignore); + AttrNumber varattnosyn pg_node_attr(equal_ignore, query_jumble_ignore); /* token location, or -1 if unknown */ int location; @@ -263,6 +263,8 @@ typedef struct Var * must be in non-extended form (4-byte header, no compression or external * references). This ensures that the Const node is self-contained and makes * it more likely that equal() will see logically identical values as equal. + * + * Only the constant type OID is relevant for the query jumbling. */ typedef struct Const { @@ -272,24 +274,27 @@ typedef struct Const /* pg_type OID of the constant's datatype */ Oid consttype; /* typmod value, if any */ - int32 consttypmod; + int32 consttypmod pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid constcollid; + Oid constcollid pg_node_attr(query_jumble_ignore); /* typlen of the constant's datatype */ - int constlen; + int constlen pg_node_attr(query_jumble_ignore); /* the constant's value */ - Datum constvalue; + Datum constvalue pg_node_attr(query_jumble_ignore); /* whether the constant is null (if true, constvalue is undefined) */ - bool constisnull; + bool constisnull pg_node_attr(query_jumble_ignore); /* * Whether this datatype is passed by value. If true, then all the * information is stored in the Datum. If false, then the Datum contains * a pointer to the information. */ - bool constbyval; - /* token location, or -1 if unknown */ - int location; + bool constbyval pg_node_attr(query_jumble_ignore); + /* + * token location, or -1 if unknown. All constants are tracked as + * locations in query jumbling, to be marked as parameters. + */ + int location pg_node_attr(query_jumble_location); } Const; /* @@ -327,6 +332,7 @@ typedef enum ParamKind PARAM_MULTIEXPR } ParamKind; +/* typmod and collation information are irrelevant for the query jumbling. */ typedef struct Param { Expr xpr; @@ -334,9 +340,9 @@ typedef struct Param int paramid; /* numeric ID for parameter */ Oid paramtype; /* pg_type OID of parameter's datatype */ /* typmod value, if known */ - int32 paramtypmod; + int32 paramtypmod pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid paramcollid; + Oid paramcollid pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } Param; @@ -389,6 +395,9 @@ typedef struct Param * and can share the result. Aggregates with same 'transno' but different * 'aggno' can share the same transition state, only the final function needs * to be called separately. + * + * Information related to collations, transition types and internal states + * are irrelevant for the query jumbling. */ typedef struct Aggref { @@ -398,22 +407,22 @@ typedef struct Aggref Oid aggfnoid; /* type Oid of result of the aggregate */ - Oid aggtype; + Oid aggtype pg_node_attr(query_jumble_ignore); /* OID of collation of result */ - Oid aggcollid; + Oid aggcollid pg_node_attr(query_jumble_ignore); /* OID of collation that function should use */ - Oid inputcollid; + Oid inputcollid pg_node_attr(query_jumble_ignore); /* * type Oid of aggregate's transition value; ignored for equal since it * might not be set yet */ - Oid aggtranstype pg_node_attr(equal_ignore); + Oid aggtranstype pg_node_attr(equal_ignore, query_jumble_ignore); /* type Oids of direct and aggregated args */ - List *aggargtypes; + List *aggargtypes pg_node_attr(query_jumble_ignore); /* direct arguments, if an ordered-set agg */ List *aggdirectargs; @@ -431,31 +440,31 @@ typedef struct Aggref Expr *aggfilter; /* true if argument list was really '*' */ - bool aggstar; + bool aggstar pg_node_attr(query_jumble_ignore); /* * true if variadic arguments have been combined into an array last * argument */ - bool aggvariadic; + bool aggvariadic pg_node_attr(query_jumble_ignore); /* aggregate kind (see pg_aggregate.h) */ - char aggkind; + char aggkind pg_node_attr(query_jumble_ignore); /* aggregate input already sorted */ - bool aggpresorted pg_node_attr(equal_ignore); + bool aggpresorted pg_node_attr(equal_ignore, query_jumble_ignore); /* > 0 if agg belongs to outer query */ - Index agglevelsup; + Index agglevelsup pg_node_attr(query_jumble_ignore); /* expected agg-splitting mode of parent Agg */ - AggSplit aggsplit; + AggSplit aggsplit pg_node_attr(query_jumble_ignore); /* unique ID within the Agg node */ - int aggno; + int aggno pg_node_attr(query_jumble_ignore); /* unique ID of transition state in the Agg */ - int aggtransno; + int aggtransno pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; @@ -484,19 +493,22 @@ typedef struct Aggref * * In raw parse output we have only the args list; parse analysis fills in the * refs list, and the planner fills in the cols list. + * + * All the fields used as information for an internal state are irrelevant + * for the query jumbling. */ typedef struct GroupingFunc { Expr xpr; /* arguments, not evaluated but kept for benefit of EXPLAIN etc. */ - List *args; + List *args pg_node_attr(query_jumble_ignore); /* ressortgrouprefs of arguments */ List *refs pg_node_attr(equal_ignore); /* actual column positions set by planner */ - List *cols pg_node_attr(equal_ignore); + List *cols pg_node_attr(equal_ignore, query_jumble_ignore); /* same as Aggref.agglevelsup */ Index agglevelsup; @@ -507,6 +519,9 @@ typedef struct GroupingFunc /* * WindowFunc + * + * Collation information is irrelevant for the query jumbling, as is the + * internal state information of the node like "winstar" and "winagg". */ typedef struct WindowFunc { @@ -514,11 +529,11 @@ typedef struct WindowFunc /* pg_proc Oid of the function */ Oid winfnoid; /* type Oid of result of the window function */ - Oid wintype; + Oid wintype pg_node_attr(query_jumble_ignore); /* OID of collation of result */ - Oid wincollid; + Oid wincollid pg_node_attr(query_jumble_ignore); /* OID of collation that function should use */ - Oid inputcollid; + Oid inputcollid pg_node_attr(query_jumble_ignore); /* arguments to the window function */ List *args; /* FILTER expression, if any */ @@ -526,9 +541,9 @@ typedef struct WindowFunc /* index of associated WindowClause */ Index winref; /* true if argument list was really '*' */ - bool winstar; + bool winstar pg_node_attr(query_jumble_ignore); /* is function a simple aggregate? */ - bool winagg; + bool winagg pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } WindowFunc; @@ -567,6 +582,8 @@ typedef struct WindowFunc * subscripting logic. Likewise, reftypmod and refcollid will match the * container's properties in a store, but could be different in a fetch. * + * Any internal state data is ignored for the query jumbling. + * * Note: for the cases where a container is returned, if refexpr yields a R/W * expanded container, then the implementation is allowed to modify that * object in-place and return the same object. @@ -575,15 +592,15 @@ typedef struct SubscriptingRef { Expr xpr; /* type of the container proper */ - Oid refcontainertype; + Oid refcontainertype pg_node_attr(query_jumble_ignore); /* the container type's pg_type.typelem */ - Oid refelemtype; + Oid refelemtype pg_node_attr(query_jumble_ignore); /* type of the SubscriptingRef's result */ - Oid refrestype; + Oid refrestype pg_node_attr(query_jumble_ignore); /* typmod of the result */ - int32 reftypmod; + int32 reftypmod pg_node_attr(query_jumble_ignore); /* collation of result, or InvalidOid if none */ - Oid refcollid; + Oid refcollid pg_node_attr(query_jumble_ignore); /* expressions that evaluate to upper container indexes */ List *refupperindexpr; @@ -634,6 +651,9 @@ typedef enum CoercionForm /* * FuncExpr - expression node for a function call + * + * Collation information is irrelevant for the query jumbling, only the + * arguments and the function OID matter. */ typedef struct FuncExpr { @@ -641,21 +661,21 @@ typedef struct FuncExpr /* PG_PROC OID of the function */ Oid funcid; /* PG_TYPE OID of result value */ - Oid funcresulttype; + Oid funcresulttype pg_node_attr(query_jumble_ignore); /* true if function returns set */ - bool funcretset; + bool funcretset pg_node_attr(query_jumble_ignore); /* * true if variadic arguments have been combined into an array last * argument */ - bool funcvariadic; + bool funcvariadic pg_node_attr(query_jumble_ignore); /* how to display this function call */ - CoercionForm funcformat; + CoercionForm funcformat pg_node_attr(query_jumble_ignore); /* OID of collation of result */ - Oid funccollid; + Oid funccollid pg_node_attr(query_jumble_ignore); /* OID of collation that function should use */ - Oid inputcollid; + Oid inputcollid pg_node_attr(query_jumble_ignore); /* arguments to the function */ List *args; /* token location, or -1 if unknown */ @@ -682,7 +702,7 @@ typedef struct NamedArgExpr /* the argument expression */ Expr *arg; /* the name */ - char *name; + char *name pg_node_attr(query_jumble_ignore); /* argument's number in positional notation */ int argnumber; /* argument name location, or -1 if unknown */ @@ -698,6 +718,9 @@ typedef struct NamedArgExpr * of the node. The planner makes sure it is valid before passing the node * tree to the executor, but during parsing/planning opfuncid can be 0. * Therefore, equal() will accept a zero value as being equal to other values. + * + * Internal state information and collation data is irrelevant for the query + * jumbling. */ typedef struct OpExpr { @@ -707,19 +730,19 @@ typedef struct OpExpr Oid opno; /* PG_PROC OID of underlying function */ - Oid opfuncid pg_node_attr(equal_ignore_if_zero); + Oid opfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore); /* PG_TYPE OID of result value */ - Oid opresulttype; + Oid opresulttype pg_node_attr(query_jumble_ignore); /* true if operator returns set */ - bool opretset; + bool opretset pg_node_attr(query_jumble_ignore); /* OID of collation of result */ - Oid opcollid; + Oid opcollid pg_node_attr(query_jumble_ignore); /* OID of collation that operator should use */ - Oid inputcollid; + Oid inputcollid pg_node_attr(query_jumble_ignore); /* arguments to the operator (1 or 2) */ List *args; @@ -775,6 +798,10 @@ typedef OpExpr NullIfExpr; * Similar to OpExpr, opfuncid, hashfuncid, and negfuncid are not necessarily * filled in right away, so will be ignored for equality if they are not set * yet. + * + * + * OID entries of the internal function types are irrelevant for the query + * jumbling, but the operator OID and the arguments are. */ typedef struct ScalarArrayOpExpr { @@ -784,19 +811,19 @@ typedef struct ScalarArrayOpExpr Oid opno; /* PG_PROC OID of comparison function */ - Oid opfuncid pg_node_attr(equal_ignore_if_zero); + Oid opfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore); /* PG_PROC OID of hash func or InvalidOid */ - Oid hashfuncid pg_node_attr(equal_ignore_if_zero); + Oid hashfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore); /* PG_PROC OID of negator of opfuncid function or InvalidOid. See above */ - Oid negfuncid pg_node_attr(equal_ignore_if_zero); + Oid negfuncid pg_node_attr(equal_ignore_if_zero, query_jumble_ignore); /* true for ANY, false for ALL */ bool useOr; /* OID of collation that operator should use */ - Oid inputcollid; + Oid inputcollid pg_node_attr(query_jumble_ignore); /* the scalar and array operands */ List *args; @@ -899,7 +926,7 @@ typedef struct SubLink int subLinkId; /* ID (1..n); 0 if not MULTIEXPR */ Node *testexpr; /* outer-query test for ALL/ANY/ROWCOMPARE */ /* originally specified operator name */ - List *operName; + List *operName pg_node_attr(query_jumble_ignore); /* subselect as Query* or raw parsetree */ Node *subselect; /* token location, or -1 if unknown */ @@ -1012,11 +1039,11 @@ typedef struct FieldSelect Expr *arg; /* input expression */ AttrNumber fieldnum; /* attribute number of field to extract */ /* type of the field (result type of this node) */ - Oid resulttype; + Oid resulttype pg_node_attr(query_jumble_ignore); /* output typmod (usually -1) */ - int32 resulttypmod; + int32 resulttypmod pg_node_attr(query_jumble_ignore); /* OID of collation of the field */ - Oid resultcollid; + Oid resultcollid pg_node_attr(query_jumble_ignore); } FieldSelect; /* ---------------- @@ -1043,9 +1070,9 @@ typedef struct FieldStore Expr *arg; /* input tuple value */ List *newvals; /* new value(s) for field(s) */ /* integer list of field attnums */ - List *fieldnums; + List *fieldnums pg_node_attr(query_jumble_ignore); /* type of result (same as type of arg) */ - Oid resulttype; + Oid resulttype pg_node_attr(query_jumble_ignore); /* Like RowExpr, we deliberately omit a typmod and collation here */ } FieldStore; @@ -1068,11 +1095,11 @@ typedef struct RelabelType Expr *arg; /* input expression */ Oid resulttype; /* output type of coercion expression */ /* output typmod (usually -1) */ - int32 resulttypmod; + int32 resulttypmod pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid resultcollid; + Oid resultcollid pg_node_attr(query_jumble_ignore); /* how to display this node */ - CoercionForm relabelformat; + CoercionForm relabelformat pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } RelabelType; @@ -1093,9 +1120,9 @@ typedef struct CoerceViaIO Oid resulttype; /* output type of coercion */ /* output typmod is not stored, but is presumed -1 */ /* OID of collation, or InvalidOid if none */ - Oid resultcollid; + Oid resultcollid pg_node_attr(query_jumble_ignore); /* how to display this node */ - CoercionForm coerceformat; + CoercionForm coerceformat pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } CoerceViaIO; @@ -1120,11 +1147,11 @@ typedef struct ArrayCoerceExpr Expr *elemexpr; /* expression representing per-element work */ Oid resulttype; /* output type of coercion (an array type) */ /* output typmod (also element typmod) */ - int32 resulttypmod; + int32 resulttypmod pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid resultcollid; + Oid resultcollid pg_node_attr(query_jumble_ignore); /* how to display this node */ - CoercionForm coerceformat; + CoercionForm coerceformat pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } ArrayCoerceExpr; @@ -1149,7 +1176,7 @@ typedef struct ConvertRowtypeExpr Oid resulttype; /* output type (always a composite type) */ /* Like RowExpr, we deliberately omit a typmod and collation here */ /* how to display this node */ - CoercionForm convertformat; + CoercionForm convertformat pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } ConvertRowtypeExpr; @@ -1196,9 +1223,9 @@ typedef struct CaseExpr { Expr xpr; /* type of expression result */ - Oid casetype; + Oid casetype pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid casecollid; + Oid casecollid pg_node_attr(query_jumble_ignore); Expr *arg; /* implicit equality comparison argument */ List *args; /* the arguments (list of WHEN clauses) */ Expr *defresult; /* the default result (ELSE clause) */ @@ -1243,9 +1270,9 @@ typedef struct CaseTestExpr Expr xpr; Oid typeId; /* type for substituted value */ /* typemod for substituted value */ - int32 typeMod; + int32 typeMod pg_node_attr(query_jumble_ignore); /* collation for the substituted value */ - Oid collation; + Oid collation pg_node_attr(query_jumble_ignore); } CaseTestExpr; /* @@ -1260,15 +1287,15 @@ typedef struct ArrayExpr { Expr xpr; /* type of expression result */ - Oid array_typeid; + Oid array_typeid pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid array_collid; + Oid array_collid pg_node_attr(query_jumble_ignore); /* common type of array elements */ - Oid element_typeid; + Oid element_typeid pg_node_attr(query_jumble_ignore); /* the array elements or sub-arrays */ List *elements; /* true if elements are sub-arrays */ - bool multidims; + bool multidims pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } ArrayExpr; @@ -1300,7 +1327,7 @@ typedef struct RowExpr List *args; /* the fields */ /* RECORDOID or a composite type's ID */ - Oid row_typeid; + Oid row_typeid pg_node_attr(query_jumble_ignore); /* * row_typeid cannot be a domain over composite, only plain composite. To @@ -1316,10 +1343,10 @@ typedef struct RowExpr */ /* how to display this node */ - CoercionForm row_format; + CoercionForm row_format pg_node_attr(query_jumble_ignore); /* list of String, or NIL */ - List *colnames; + List *colnames pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; @@ -1357,11 +1384,11 @@ typedef struct RowCompareExpr /* LT LE GE or GT, never EQ or NE */ RowCompareType rctype; /* OID list of pairwise comparison ops */ - List *opnos; + List *opnos pg_node_attr(query_jumble_ignore); /* OID list of containing operator families */ - List *opfamilies; + List *opfamilies pg_node_attr(query_jumble_ignore); /* OID list of collations for comparisons */ - List *inputcollids; + List *inputcollids pg_node_attr(query_jumble_ignore); /* the left-hand input arguments */ List *largs; /* the right-hand input arguments */ @@ -1375,9 +1402,9 @@ typedef struct CoalesceExpr { Expr xpr; /* type of expression result */ - Oid coalescetype; + Oid coalescetype pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid coalescecollid; + Oid coalescecollid pg_node_attr(query_jumble_ignore); /* the arguments */ List *args; /* token location, or -1 if unknown */ @@ -1397,11 +1424,11 @@ typedef struct MinMaxExpr { Expr xpr; /* common type of arguments and result */ - Oid minmaxtype; + Oid minmaxtype pg_node_attr(query_jumble_ignore); /* OID of collation of result */ - Oid minmaxcollid; + Oid minmaxcollid pg_node_attr(query_jumble_ignore); /* OID of collation that function should use */ - Oid inputcollid; + Oid inputcollid pg_node_attr(query_jumble_ignore); /* function to execute */ MinMaxOp op; /* the arguments */ @@ -1445,18 +1472,18 @@ typedef struct XmlExpr /* xml function ID */ XmlExprOp op; /* name in xml(NAME foo ...) syntaxes */ - char *name; + char *name pg_node_attr(query_jumble_ignore); /* non-XML expressions for xml_attributes */ List *named_args; /* parallel list of String values */ - List *arg_names; + List *arg_names pg_node_attr(query_jumble_ignore); /* list of expressions */ List *args; /* DOCUMENT or CONTENT */ - XmlOptionType xmloption; + XmlOptionType xmloption pg_node_attr(query_jumble_ignore); /* target type/typmod for XMLSERIALIZE */ - Oid type; - int32 typmod; + Oid type pg_node_attr(query_jumble_ignore); + int32 typmod pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } XmlExpr; @@ -1491,7 +1518,7 @@ typedef struct NullTest Expr *arg; /* input expression */ NullTestType nulltesttype; /* IS NULL, IS NOT NULL */ /* T to perform field-by-field null checks */ - bool argisrow; + bool argisrow pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } NullTest; @@ -1527,6 +1554,8 @@ typedef struct BooleanTest * checked will be determined. If the value passes, it is returned as the * result; if not, an error is raised. Note that this is equivalent to * RelabelType in the scenario where no constraints are applied. + * + * typemod and collation are irrelevant for the query jumbling. */ typedef struct CoerceToDomain { @@ -1534,11 +1563,11 @@ typedef struct CoerceToDomain Expr *arg; /* input expression */ Oid resulttype; /* domain type ID (result type) */ /* output typmod (currently always -1) */ - int32 resulttypmod; + int32 resulttypmod pg_node_attr(query_jumble_ignore); /* OID of collation, or InvalidOid if none */ - Oid resultcollid; + Oid resultcollid pg_node_attr(query_jumble_ignore); /* how to display this node */ - CoercionForm coercionformat; + CoercionForm coercionformat pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } CoerceToDomain; @@ -1558,9 +1587,9 @@ typedef struct CoerceToDomainValue /* type for substituted value */ Oid typeId; /* typemod for substituted value */ - int32 typeMod; + int32 typeMod pg_node_attr(query_jumble_ignore); /* collation for the substituted value */ - Oid collation; + Oid collation pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } CoerceToDomainValue; @@ -1571,6 +1600,8 @@ typedef struct CoerceToDomainValue * This is not an executable expression: it must be replaced by the actual * column default expression during rewriting. But it is convenient to * treat it as an expression node during parsing and rewriting. + * + * typemod and collation are irrelevant for the query jumbling. */ typedef struct SetToDefault { @@ -1578,9 +1609,9 @@ typedef struct SetToDefault /* type for substituted value */ Oid typeId; /* typemod for substituted value */ - int32 typeMod; + int32 typeMod pg_node_attr(query_jumble_ignore); /* collation for the substituted value */ - Oid collation; + Oid collation pg_node_attr(query_jumble_ignore); /* token location, or -1 if unknown */ int location; } SetToDefault; @@ -1698,15 +1729,15 @@ typedef struct TargetEntry /* attribute number (see notes above) */ AttrNumber resno; /* name of the column (could be NULL) */ - char *resname; + char *resname pg_node_attr(query_jumble_ignore); /* nonzero if referenced by a sort/group clause */ Index ressortgroupref; /* OID of column's source table */ - Oid resorigtbl; + Oid resorigtbl pg_node_attr(query_jumble_ignore); /* column's number in source table */ - AttrNumber resorigcol; + AttrNumber resorigcol pg_node_attr(query_jumble_ignore); /* set to true to eliminate the attribute from final target list */ - bool resjunk; + bool resjunk pg_node_attr(query_jumble_ignore); } TargetEntry; @@ -1789,13 +1820,13 @@ typedef struct JoinExpr Node *larg; /* left subtree */ Node *rarg; /* right subtree */ /* USING clause, if any (list of String) */ - List *usingClause; + List *usingClause pg_node_attr(query_jumble_ignore); /* alias attached to USING clause, if any */ - Alias *join_using_alias; + Alias *join_using_alias pg_node_attr(query_jumble_ignore); /* qualifiers on join, if any */ Node *quals; /* user-written alias clause, if any */ - Alias *alias; + Alias *alias pg_node_attr(query_jumble_ignore); /* RT index assigned for join, or 0 */ int rtindex; } JoinExpr; diff --git a/src/backend/nodes/README b/src/backend/nodes/README index 489a67eb89..7cf6e3b041 100644 --- a/src/backend/nodes/README +++ b/src/backend/nodes/README @@ -51,6 +51,7 @@ FILES IN THIS DIRECTORY (src/backend/nodes/) readfuncs.c - convert text representation back to a node tree (*) makefuncs.c - creator functions for some common node types nodeFuncs.c - some other general-purpose manipulation functions + queryjumblefuncs.c - compute a node tree for query jumbling (*) (*) - Most functions in these files are generated by gen_node_support.pl and #include'd there. diff --git a/src/backend/nodes/gen_node_support.pl b/src/backend/nodes/gen_node_support.pl index b3c1ead496..27b9e4e630 100644 --- a/src/backend/nodes/gen_node_support.pl +++ b/src/backend/nodes/gen_node_support.pl @@ -121,6 +121,8 @@ my %node_type_info; my @no_copy; # node types we don't want equal support for my @no_equal; +# node types we don't want jumble support for +my @no_query_jumble; # node types we don't want read support for my @no_read; # node types we don't want read/write support for @@ -155,12 +157,13 @@ my @extra_tags = qw( # This is a regular node, but we skip parsing it from its header file # since we won't use its internal structure here anyway. push @node_types, qw(List); -# Lists are specially treated in all four support files, too. +# Lists are specially treated in all five support files, too. # (Ideally we'd mark List as "special copy/equal" not "no copy/equal". # But until there's other use-cases for that, just hot-wire the tests # that would need to distinguish.) push @no_copy, qw(List); push @no_equal, qw(List); +push @no_query_jumble, qw(List); push @special_read_write, qw(List); # Nodes with custom copy/equal implementations are skipped from @@ -332,6 +335,10 @@ foreach my $infile (@ARGV) push @no_copy, $in_struct; push @no_equal, $in_struct; } + elsif ($attr eq 'no_query_jumble') + { + push @no_query_jumble, $in_struct; + } elsif ($attr eq 'no_read') { push @no_read, $in_struct; @@ -457,6 +464,8 @@ foreach my $infile (@ARGV) equal_as_scalar equal_ignore equal_ignore_if_zero + query_jumble_ignore + query_jumble_location read_write_ignore write_only_relids write_only_nondefault_pathtarget @@ -1225,6 +1234,100 @@ close $ofs; close $rfs; +# queryjumblefuncs.c + +push @output_files, 'queryjumblefuncs.funcs.c'; +open my $jff, '>', "$output_path/queryjumblefuncs.funcs.c$tmpext" or die $!; +push @output_files, 'queryjumblefuncs.switch.c'; +open my $jfs, '>', "$output_path/queryjumblefuncs.switch.c$tmpext" or die $!; + +printf $jff $header_comment, 'queryjumblefuncs.funcs.c'; +printf $jfs $header_comment, 'queryjumblefuncs.switch.c'; + +print $jff $node_includes; + +foreach my $n (@node_types) +{ + next if elem $n, @abstract_types; + next if elem $n, @nodetag_only; + my $struct_no_query_jumble = (elem $n, @no_query_jumble); + + print $jfs "\t\t\tcase T_${n}:\n" + . "\t\t\t\t_jumble${n}(jstate, expr);\n" + . "\t\t\t\tbreak;\n" + unless $struct_no_query_jumble; + + print $jff " +static void +_jumble${n}(JumbleState *jstate, Node *node) +{ +\t${n} *expr = (${n} *) node;\n +" unless $struct_no_query_jumble; + + # print instructions for each field + foreach my $f (@{ $node_type_info{$n}->{fields} }) + { + my $t = $node_type_info{$n}->{field_types}{$f}; + my @a = @{ $node_type_info{$n}->{field_attrs}{$f} }; + my $query_jumble_ignore = $struct_no_query_jumble; + my $query_jumble_location = 0; + + # extract per-field attributes + foreach my $a (@a) + { + if ($a eq 'query_jumble_ignore') + { + $query_jumble_ignore = 1; + } + elsif ($a eq 'query_jumble_location') + { + $query_jumble_location = 1; + } + } + + # node type + if (($t =~ /^(\w+)\*$/ or $t =~ /^struct\s+(\w+)\*$/) + and elem $1, @node_types) + { + print $jff "\tJUMBLE_NODE($f);\n" + unless $query_jumble_ignore; + } + elsif ($t eq 'int' && $f =~ 'location$') + { + # Track the node's location only if directly requested. + if ($query_jumble_location) + { + print $jff "\tJUMBLE_LOCATION($f);\n" + unless $query_jumble_ignore; + } + } + elsif ($t eq 'char*') + { + print $jff "\tJUMBLE_STRING($f);\n" + unless $query_jumble_ignore; + } + else + { + print $jff "\tJUMBLE_FIELD($f);\n" + unless $query_jumble_ignore; + } + } + + # Some nodes have no attributes like CheckPointStmt, + # so tweak things for empty contents. + if (scalar(@{ $node_type_info{$n}->{fields} }) == 0) + { + print $jff "\t(void) expr;\n" + unless $struct_no_query_jumble; + } + + print $jff "} +" unless $struct_no_query_jumble; +} + +close $jff; +close $jfs; + # now rename the temporary files to their final names foreach my $file (@output_files) { diff --git a/src/backend/nodes/meson.build b/src/backend/nodes/meson.build index 9230515e7f..31467a12d3 100644 --- a/src/backend/nodes/meson.build +++ b/src/backend/nodes/meson.build @@ -10,7 +10,6 @@ backend_sources += files( 'nodes.c', 'params.c', 'print.c', - 'queryjumblefuncs.c', 'read.c', 'tidbitmap.c', 'value.c', @@ -21,6 +20,7 @@ backend_sources += files( nodefunc_sources = files( 'copyfuncs.c', 'equalfuncs.c', + 'queryjumblefuncs.c', 'outfuncs.c', 'readfuncs.c', ) diff --git a/src/backend/nodes/queryjumblefuncs.c b/src/backend/nodes/queryjumblefuncs.c index 16084842a3..278150fba0 100644 --- a/src/backend/nodes/queryjumblefuncs.c +++ b/src/backend/nodes/queryjumblefuncs.c @@ -21,7 +21,7 @@ * tree(s) generated from the query. The executor can then use this value * to blame query costs on the proper queryId. * - * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group + * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * @@ -45,15 +45,12 @@ int compute_query_id = COMPUTE_QUERY_ID_AUTO; /* True when compute_query_id is ON, or AUTO and a module requests them */ bool query_id_enabled = false; -static uint64 compute_utility_query_id(const char *query_text, - int query_location, int query_len); static void AppendJumble(JumbleState *jstate, const unsigned char *item, Size size); -static void JumbleQueryInternal(JumbleState *jstate, Query *query); -static void JumbleRangeTable(JumbleState *jstate, List *rtable); -static void JumbleRowMarks(JumbleState *jstate, List *rowMarks); -static void JumbleExpr(JumbleState *jstate, Node *node); static void RecordConstLocation(JumbleState *jstate, int location); +static void _jumbleNode(JumbleState *jstate, Node *node); +static void _jumbleList(JumbleState *jstate, Node *node); +static void _jumbleRangeTblEntry(JumbleState *jstate, Node *node); /* * Given a possibly multi-statement source string, confine our attention to the @@ -105,38 +102,29 @@ JumbleQuery(Query *query, const char *querytext) Assert(IsQueryIdEnabled()); - if (query->utilityStmt) - { - query->queryId = compute_utility_query_id(querytext, - query->stmt_location, - query->stmt_len); - } - else - { - jstate = (JumbleState *) palloc(sizeof(JumbleState)); + jstate = (JumbleState *) palloc(sizeof(JumbleState)); - /* Set up workspace for query jumbling */ - jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE); - jstate->jumble_len = 0; - jstate->clocations_buf_size = 32; - jstate->clocations = (LocationLen *) - palloc(jstate->clocations_buf_size * sizeof(LocationLen)); - jstate->clocations_count = 0; - jstate->highest_extern_param_id = 0; + /* Set up workspace for query jumbling */ + jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE); + jstate->jumble_len = 0; + jstate->clocations_buf_size = 32; + jstate->clocations = (LocationLen *) + palloc(jstate->clocations_buf_size * sizeof(LocationLen)); + jstate->clocations_count = 0; + jstate->highest_extern_param_id = 0; - /* Compute query ID and mark the Query node with it */ - JumbleQueryInternal(jstate, query); - query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble, - jstate->jumble_len, - 0)); + /* Compute query ID and mark the Query node with it */ + _jumbleNode(jstate, (Node *) query); + query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble, + jstate->jumble_len, + 0)); - /* - * If we are unlucky enough to get a hash of zero, use 1 instead, to - * prevent confusion with the utility-statement case. - */ - if (query->queryId == UINT64CONST(0)) - query->queryId = UINT64CONST(1); - } + /* + * If we are unlucky enough to get a hash of zero, use 1 instead, to + * prevent confusion with the utility-statement case. + */ + if (query->queryId == UINT64CONST(0)) + query->queryId = UINT64CONST(1); return jstate; } @@ -154,34 +142,6 @@ EnableQueryId(void) query_id_enabled = true; } -/* - * Compute a query identifier for the given utility query string. - */ -static uint64 -compute_utility_query_id(const char *query_text, int query_location, int query_len) -{ - uint64 queryId; - const char *sql; - - /* - * Confine our attention to the relevant part of the string, if the query - * is a portion of a multi-statement source string. - */ - sql = CleanQuerytext(query_text, &query_location, &query_len); - - queryId = DatumGetUInt64(hash_any_extended((const unsigned char *) sql, - query_len, 0)); - - /* - * If we are unlucky enough to get a hash of zero(invalid), use queryID as - * 2 instead, queryID 1 is already in use for normal statements. - */ - if (queryId == UINT64CONST(0)) - queryId = UINT64CONST(2); - - return queryId; -} - /* * AppendJumble: Append a value that is substantive in a given query to * the current jumble. @@ -219,621 +179,6 @@ AppendJumble(JumbleState *jstate, const unsigned char *item, Size size) jstate->jumble_len = jumble_len; } -/* - * Wrappers around AppendJumble to encapsulate details of serialization - * of individual local variable elements. - */ -#define APP_JUMB(item) \ - AppendJumble(jstate, (const unsigned char *) &(item), sizeof(item)) -#define APP_JUMB_STRING(str) \ - AppendJumble(jstate, (const unsigned char *) (str), strlen(str) + 1) - -/* - * JumbleQueryInternal: Selectively serialize the query tree, appending - * significant data to the "query jumble" while ignoring nonsignificant data. - * - * Rule of thumb for what to include is that we should ignore anything not - * semantically significant (such as alias names) as well as anything that can - * be deduced from child nodes (else we'd just be double-hashing that piece - * of information). - */ -static void -JumbleQueryInternal(JumbleState *jstate, Query *query) -{ - Assert(IsA(query, Query)); - Assert(query->utilityStmt == NULL); - - APP_JUMB(query->commandType); - /* resultRelation is usually predictable from commandType */ - JumbleExpr(jstate, (Node *) query->cteList); - JumbleRangeTable(jstate, query->rtable); - JumbleExpr(jstate, (Node *) query->jointree); - JumbleExpr(jstate, (Node *) query->mergeActionList); - JumbleExpr(jstate, (Node *) query->targetList); - JumbleExpr(jstate, (Node *) query->onConflict); - JumbleExpr(jstate, (Node *) query->returningList); - JumbleExpr(jstate, (Node *) query->groupClause); - APP_JUMB(query->groupDistinct); - JumbleExpr(jstate, (Node *) query->groupingSets); - JumbleExpr(jstate, query->havingQual); - JumbleExpr(jstate, (Node *) query->windowClause); - JumbleExpr(jstate, (Node *) query->distinctClause); - JumbleExpr(jstate, (Node *) query->sortClause); - JumbleExpr(jstate, query->limitOffset); - JumbleExpr(jstate, query->limitCount); - APP_JUMB(query->limitOption); - JumbleRowMarks(jstate, query->rowMarks); - JumbleExpr(jstate, query->setOperations); -} - -/* - * Jumble a range table - */ -static void -JumbleRangeTable(JumbleState *jstate, List *rtable) -{ - ListCell *lc; - - foreach(lc, rtable) - { - RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc); - - APP_JUMB(rte->rtekind); - switch (rte->rtekind) - { - case RTE_RELATION: - APP_JUMB(rte->relid); - JumbleExpr(jstate, (Node *) rte->tablesample); - APP_JUMB(rte->inh); - break; - case RTE_SUBQUERY: - JumbleQueryInternal(jstate, rte->subquery); - break; - case RTE_JOIN: - APP_JUMB(rte->jointype); - break; - case RTE_FUNCTION: - JumbleExpr(jstate, (Node *) rte->functions); - break; - case RTE_TABLEFUNC: - JumbleExpr(jstate, (Node *) rte->tablefunc); - break; - case RTE_VALUES: - JumbleExpr(jstate, (Node *) rte->values_lists); - break; - case RTE_CTE: - - /* - * Depending on the CTE name here isn't ideal, but it's the - * only info we have to identify the referenced WITH item. - */ - APP_JUMB_STRING(rte->ctename); - APP_JUMB(rte->ctelevelsup); - break; - case RTE_NAMEDTUPLESTORE: - APP_JUMB_STRING(rte->enrname); - break; - case RTE_RESULT: - break; - default: - elog(ERROR, "unrecognized RTE kind: %d", (int) rte->rtekind); - break; - } - } -} - -/* - * Jumble a rowMarks list - */ -static void -JumbleRowMarks(JumbleState *jstate, List *rowMarks) -{ - ListCell *lc; - - foreach(lc, rowMarks) - { - RowMarkClause *rowmark = lfirst_node(RowMarkClause, lc); - - if (!rowmark->pushedDown) - { - APP_JUMB(rowmark->rti); - APP_JUMB(rowmark->strength); - APP_JUMB(rowmark->waitPolicy); - } - } -} - -/* - * Jumble an expression tree - * - * In general this function should handle all the same node types that - * expression_tree_walker() does, and therefore it's coded to be as parallel - * to that function as possible. However, since we are only invoked on - * queries immediately post-parse-analysis, we need not handle node types - * that only appear in planning. - * - * Note: the reason we don't simply use expression_tree_walker() is that the - * point of that function is to support tree walkers that don't care about - * most tree node types, but here we care about all types. We should complain - * about any unrecognized node type. - */ -static void -JumbleExpr(JumbleState *jstate, Node *node) -{ - ListCell *temp; - - if (node == NULL) - return; - - /* Guard against stack overflow due to overly complex expressions */ - check_stack_depth(); - - /* - * We always emit the node's NodeTag, then any additional fields that are - * considered significant, and then we recurse to any child nodes. - */ - APP_JUMB(node->type); - - switch (nodeTag(node)) - { - case T_Var: - { - Var *var = (Var *) node; - - APP_JUMB(var->varno); - APP_JUMB(var->varattno); - APP_JUMB(var->varlevelsup); - } - break; - case T_Const: - { - Const *c = (Const *) node; - - /* We jumble only the constant's type, not its value */ - APP_JUMB(c->consttype); - /* Also, record its parse location for query normalization */ - RecordConstLocation(jstate, c->location); - } - break; - case T_Param: - { - Param *p = (Param *) node; - - APP_JUMB(p->paramkind); - APP_JUMB(p->paramid); - APP_JUMB(p->paramtype); - /* Also, track the highest external Param id */ - if (p->paramkind == PARAM_EXTERN && - p->paramid > jstate->highest_extern_param_id) - jstate->highest_extern_param_id = p->paramid; - } - break; - case T_Aggref: - { - Aggref *expr = (Aggref *) node; - - APP_JUMB(expr->aggfnoid); - JumbleExpr(jstate, (Node *) expr->aggdirectargs); - JumbleExpr(jstate, (Node *) expr->args); - JumbleExpr(jstate, (Node *) expr->aggorder); - JumbleExpr(jstate, (Node *) expr->aggdistinct); - JumbleExpr(jstate, (Node *) expr->aggfilter); - } - break; - case T_GroupingFunc: - { - GroupingFunc *grpnode = (GroupingFunc *) node; - - JumbleExpr(jstate, (Node *) grpnode->refs); - APP_JUMB(grpnode->agglevelsup); - } - break; - case T_WindowFunc: - { - WindowFunc *expr = (WindowFunc *) node; - - APP_JUMB(expr->winfnoid); - APP_JUMB(expr->winref); - JumbleExpr(jstate, (Node *) expr->args); - JumbleExpr(jstate, (Node *) expr->aggfilter); - } - break; - case T_SubscriptingRef: - { - SubscriptingRef *sbsref = (SubscriptingRef *) node; - - JumbleExpr(jstate, (Node *) sbsref->refupperindexpr); - JumbleExpr(jstate, (Node *) sbsref->reflowerindexpr); - JumbleExpr(jstate, (Node *) sbsref->refexpr); - JumbleExpr(jstate, (Node *) sbsref->refassgnexpr); - } - break; - case T_FuncExpr: - { - FuncExpr *expr = (FuncExpr *) node; - - APP_JUMB(expr->funcid); - JumbleExpr(jstate, (Node *) expr->args); - } - break; - case T_NamedArgExpr: - { - NamedArgExpr *nae = (NamedArgExpr *) node; - - APP_JUMB(nae->argnumber); - JumbleExpr(jstate, (Node *) nae->arg); - } - break; - case T_OpExpr: - case T_DistinctExpr: /* struct-equivalent to OpExpr */ - case T_NullIfExpr: /* struct-equivalent to OpExpr */ - { - OpExpr *expr = (OpExpr *) node; - - APP_JUMB(expr->opno); - JumbleExpr(jstate, (Node *) expr->args); - } - break; - case T_ScalarArrayOpExpr: - { - ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node; - - APP_JUMB(expr->opno); - APP_JUMB(expr->useOr); - JumbleExpr(jstate, (Node *) expr->args); - } - break; - case T_BoolExpr: - { - BoolExpr *expr = (BoolExpr *) node; - - APP_JUMB(expr->boolop); - JumbleExpr(jstate, (Node *) expr->args); - } - break; - case T_SubLink: - { - SubLink *sublink = (SubLink *) node; - - APP_JUMB(sublink->subLinkType); - APP_JUMB(sublink->subLinkId); - JumbleExpr(jstate, (Node *) sublink->testexpr); - JumbleQueryInternal(jstate, castNode(Query, sublink->subselect)); - } - break; - case T_FieldSelect: - { - FieldSelect *fs = (FieldSelect *) node; - - APP_JUMB(fs->fieldnum); - JumbleExpr(jstate, (Node *) fs->arg); - } - break; - case T_FieldStore: - { - FieldStore *fstore = (FieldStore *) node; - - JumbleExpr(jstate, (Node *) fstore->arg); - JumbleExpr(jstate, (Node *) fstore->newvals); - } - break; - case T_RelabelType: - { - RelabelType *rt = (RelabelType *) node; - - APP_JUMB(rt->resulttype); - JumbleExpr(jstate, (Node *) rt->arg); - } - break; - case T_CoerceViaIO: - { - CoerceViaIO *cio = (CoerceViaIO *) node; - - APP_JUMB(cio->resulttype); - JumbleExpr(jstate, (Node *) cio->arg); - } - break; - case T_ArrayCoerceExpr: - { - ArrayCoerceExpr *acexpr = (ArrayCoerceExpr *) node; - - APP_JUMB(acexpr->resulttype); - JumbleExpr(jstate, (Node *) acexpr->arg); - JumbleExpr(jstate, (Node *) acexpr->elemexpr); - } - break; - case T_ConvertRowtypeExpr: - { - ConvertRowtypeExpr *crexpr = (ConvertRowtypeExpr *) node; - - APP_JUMB(crexpr->resulttype); - JumbleExpr(jstate, (Node *) crexpr->arg); - } - break; - case T_CollateExpr: - { - CollateExpr *ce = (CollateExpr *) node; - - APP_JUMB(ce->collOid); - JumbleExpr(jstate, (Node *) ce->arg); - } - break; - case T_CaseExpr: - { - CaseExpr *caseexpr = (CaseExpr *) node; - - JumbleExpr(jstate, (Node *) caseexpr->arg); - foreach(temp, caseexpr->args) - { - CaseWhen *when = lfirst_node(CaseWhen, temp); - - JumbleExpr(jstate, (Node *) when->expr); - JumbleExpr(jstate, (Node *) when->result); - } - JumbleExpr(jstate, (Node *) caseexpr->defresult); - } - break; - case T_CaseTestExpr: - { - CaseTestExpr *ct = (CaseTestExpr *) node; - - APP_JUMB(ct->typeId); - } - break; - case T_ArrayExpr: - JumbleExpr(jstate, (Node *) ((ArrayExpr *) node)->elements); - break; - case T_RowExpr: - JumbleExpr(jstate, (Node *) ((RowExpr *) node)->args); - break; - case T_RowCompareExpr: - { - RowCompareExpr *rcexpr = (RowCompareExpr *) node; - - APP_JUMB(rcexpr->rctype); - JumbleExpr(jstate, (Node *) rcexpr->largs); - JumbleExpr(jstate, (Node *) rcexpr->rargs); - } - break; - case T_CoalesceExpr: - JumbleExpr(jstate, (Node *) ((CoalesceExpr *) node)->args); - break; - case T_MinMaxExpr: - { - MinMaxExpr *mmexpr = (MinMaxExpr *) node; - - APP_JUMB(mmexpr->op); - JumbleExpr(jstate, (Node *) mmexpr->args); - } - break; - case T_XmlExpr: - { - XmlExpr *xexpr = (XmlExpr *) node; - - APP_JUMB(xexpr->op); - JumbleExpr(jstate, (Node *) xexpr->named_args); - JumbleExpr(jstate, (Node *) xexpr->args); - } - break; - case T_NullTest: - { - NullTest *nt = (NullTest *) node; - - APP_JUMB(nt->nulltesttype); - JumbleExpr(jstate, (Node *) nt->arg); - } - break; - case T_BooleanTest: - { - BooleanTest *bt = (BooleanTest *) node; - - APP_JUMB(bt->booltesttype); - JumbleExpr(jstate, (Node *) bt->arg); - } - break; - case T_CoerceToDomain: - { - CoerceToDomain *cd = (CoerceToDomain *) node; - - APP_JUMB(cd->resulttype); - JumbleExpr(jstate, (Node *) cd->arg); - } - break; - case T_CoerceToDomainValue: - { - CoerceToDomainValue *cdv = (CoerceToDomainValue *) node; - - APP_JUMB(cdv->typeId); - } - break; - case T_SetToDefault: - { - SetToDefault *sd = (SetToDefault *) node; - - APP_JUMB(sd->typeId); - } - break; - case T_CurrentOfExpr: - { - CurrentOfExpr *ce = (CurrentOfExpr *) node; - - APP_JUMB(ce->cvarno); - if (ce->cursor_name) - APP_JUMB_STRING(ce->cursor_name); - APP_JUMB(ce->cursor_param); - } - break; - case T_NextValueExpr: - { - NextValueExpr *nve = (NextValueExpr *) node; - - APP_JUMB(nve->seqid); - APP_JUMB(nve->typeId); - } - break; - case T_InferenceElem: - { - InferenceElem *ie = (InferenceElem *) node; - - APP_JUMB(ie->infercollid); - APP_JUMB(ie->inferopclass); - JumbleExpr(jstate, ie->expr); - } - break; - case T_TargetEntry: - { - TargetEntry *tle = (TargetEntry *) node; - - APP_JUMB(tle->resno); - APP_JUMB(tle->ressortgroupref); - JumbleExpr(jstate, (Node *) tle->expr); - } - break; - case T_RangeTblRef: - { - RangeTblRef *rtr = (RangeTblRef *) node; - - APP_JUMB(rtr->rtindex); - } - break; - case T_JoinExpr: - { - JoinExpr *join = (JoinExpr *) node; - - APP_JUMB(join->jointype); - APP_JUMB(join->isNatural); - APP_JUMB(join->rtindex); - JumbleExpr(jstate, join->larg); - JumbleExpr(jstate, join->rarg); - JumbleExpr(jstate, join->quals); - } - break; - case T_FromExpr: - { - FromExpr *from = (FromExpr *) node; - - JumbleExpr(jstate, (Node *) from->fromlist); - JumbleExpr(jstate, from->quals); - } - break; - case T_OnConflictExpr: - { - OnConflictExpr *conf = (OnConflictExpr *) node; - - APP_JUMB(conf->action); - JumbleExpr(jstate, (Node *) conf->arbiterElems); - JumbleExpr(jstate, conf->arbiterWhere); - JumbleExpr(jstate, (Node *) conf->onConflictSet); - JumbleExpr(jstate, conf->onConflictWhere); - APP_JUMB(conf->constraint); - APP_JUMB(conf->exclRelIndex); - JumbleExpr(jstate, (Node *) conf->exclRelTlist); - } - break; - case T_MergeAction: - { - MergeAction *mergeaction = (MergeAction *) node; - - APP_JUMB(mergeaction->matched); - APP_JUMB(mergeaction->commandType); - JumbleExpr(jstate, mergeaction->qual); - JumbleExpr(jstate, (Node *) mergeaction->targetList); - } - break; - case T_List: - foreach(temp, (List *) node) - { - JumbleExpr(jstate, (Node *) lfirst(temp)); - } - break; - case T_IntList: - foreach(temp, (List *) node) - { - APP_JUMB(lfirst_int(temp)); - } - break; - case T_SortGroupClause: - { - SortGroupClause *sgc = (SortGroupClause *) node; - - APP_JUMB(sgc->tleSortGroupRef); - APP_JUMB(sgc->eqop); - APP_JUMB(sgc->sortop); - APP_JUMB(sgc->nulls_first); - } - break; - case T_GroupingSet: - { - GroupingSet *gsnode = (GroupingSet *) node; - - JumbleExpr(jstate, (Node *) gsnode->content); - } - break; - case T_WindowClause: - { - WindowClause *wc = (WindowClause *) node; - - APP_JUMB(wc->winref); - APP_JUMB(wc->frameOptions); - JumbleExpr(jstate, (Node *) wc->partitionClause); - JumbleExpr(jstate, (Node *) wc->orderClause); - JumbleExpr(jstate, wc->startOffset); - JumbleExpr(jstate, wc->endOffset); - } - break; - case T_CommonTableExpr: - { - CommonTableExpr *cte = (CommonTableExpr *) node; - - /* we store the string name because RTE_CTE RTEs need it */ - APP_JUMB_STRING(cte->ctename); - APP_JUMB(cte->ctematerialized); - JumbleQueryInternal(jstate, castNode(Query, cte->ctequery)); - } - break; - case T_SetOperationStmt: - { - SetOperationStmt *setop = (SetOperationStmt *) node; - - APP_JUMB(setop->op); - APP_JUMB(setop->all); - JumbleExpr(jstate, setop->larg); - JumbleExpr(jstate, setop->rarg); - } - break; - case T_RangeTblFunction: - { - RangeTblFunction *rtfunc = (RangeTblFunction *) node; - - JumbleExpr(jstate, rtfunc->funcexpr); - } - break; - case T_TableFunc: - { - TableFunc *tablefunc = (TableFunc *) node; - - JumbleExpr(jstate, tablefunc->docexpr); - JumbleExpr(jstate, tablefunc->rowexpr); - JumbleExpr(jstate, (Node *) tablefunc->colexprs); - } - break; - case T_TableSampleClause: - { - TableSampleClause *tsc = (TableSampleClause *) node; - - APP_JUMB(tsc->tsmhandler); - JumbleExpr(jstate, (Node *) tsc->args); - JumbleExpr(jstate, (Node *) tsc->repeatable); - } - break; - default: - /* Only a warning, since we can stumble along anyway */ - elog(WARNING, "unrecognized node type: %d", - (int) nodeTag(node)); - break; - } -} - /* * Record location of constant within query string of query tree * that is currently being walked. @@ -859,3 +204,155 @@ RecordConstLocation(JumbleState *jstate, int location) jstate->clocations_count++; } } + +#define JUMBLE_NODE(item) \ + _jumbleNode(jstate, (Node *) expr->item) +#define JUMBLE_LOCATION(location) \ + RecordConstLocation(jstate, expr->location) +#define JUMBLE_FIELD(item) \ + AppendJumble(jstate, (const unsigned char *) &(expr->item), sizeof(expr->item)) +#define JUMBLE_FIELD_SINGLE(item) \ + AppendJumble(jstate, (const unsigned char *) &(item), sizeof(item)) +#define JUMBLE_STRING(str) \ +do { \ + if (expr->str) \ + AppendJumble(jstate, (const unsigned char *) (expr->str), strlen(expr->str) + 1); \ +} while(0) + +#include "queryjumblefuncs.funcs.c" + +static void +_jumbleNode(JumbleState *jstate, Node *node) +{ + Node *expr = node; + + if (expr == NULL) + return; + + /* Guard against stack overflow due to overly complex expressions */ + check_stack_depth(); + + /* + * We always emit the node's NodeTag, then any additional fields that are + * considered significant, and then we recurse to any child nodes. + */ + JUMBLE_FIELD(type); + + switch (nodeTag(expr)) + { +#include "queryjumblefuncs.switch.c" + + case T_List: + case T_IntList: + case T_OidList: + case T_XidList: + _jumbleList(jstate, expr); + break; + + case T_RangeTblEntry: + _jumbleRangeTblEntry(jstate, expr); + break; + + default: + /* Only a warning, since we can stumble along anyway */ + elog(WARNING, "unrecognized node type: %d", + (int) nodeTag(expr)); + break; + } + + /* Special cases */ + switch (nodeTag(expr)) + { + case T_Param: + { + Param *p = (Param *) node; + + /* Also, track the highest external Param id */ + if (p->paramkind == PARAM_EXTERN && + p->paramid > jstate->highest_extern_param_id) + jstate->highest_extern_param_id = p->paramid; + } + break; + default: + break; + } +} + +static void +_jumbleList(JumbleState *jstate, Node *node) +{ + List *expr = (List *) node; + ListCell *l; + + switch (expr->type) + { + case T_List: + foreach(l, expr) + _jumbleNode(jstate, lfirst(l)); + break; + case T_IntList: + foreach(l, expr) + JUMBLE_FIELD_SINGLE(lfirst_int(l)); + break; + case T_OidList: + foreach(l, expr) + JUMBLE_FIELD_SINGLE(lfirst_oid(l)); + break; + case T_XidList: + foreach(l, expr) + JUMBLE_FIELD_SINGLE(lfirst_xid(l)); + break; + default: + elog(ERROR, "unrecognized list node type: %d", + (int) expr->type); + return; + } +} + +static void +_jumbleRangeTblEntry(JumbleState *jstate, Node *node) +{ + RangeTblEntry *expr = (RangeTblEntry *) node; + + JUMBLE_FIELD(rtekind); + switch (expr->rtekind) + { + case RTE_RELATION: + JUMBLE_FIELD(relid); + JUMBLE_NODE(tablesample); + JUMBLE_FIELD(inh); + break; + case RTE_SUBQUERY: + JUMBLE_NODE(subquery); + break; + case RTE_JOIN: + JUMBLE_FIELD(jointype); + break; + case RTE_FUNCTION: + JUMBLE_NODE(functions); + break; + case RTE_TABLEFUNC: + JUMBLE_NODE(tablefunc); + break; + case RTE_VALUES: + JUMBLE_NODE(values_lists); + break; + case RTE_CTE: + + /* + * Depending on the CTE name here isn't ideal, but it's the only + * info we have to identify the referenced WITH item. + */ + JUMBLE_STRING(ctename); + JUMBLE_FIELD(ctelevelsup); + break; + case RTE_NAMEDTUPLESTORE: + JUMBLE_STRING(enrname); + break; + case RTE_RESULT: + break; + default: + elog(ERROR, "unrecognized RTE kind: %d", (int) expr->rtekind); + break; + } +} -- 2.39.0
From ffaa79144ff34eeaa89b55cbced2a182310ea522 Mon Sep 17 00:00:00 2001 From: Michael Paquier <mich...@paquier.xyz> Date: Fri, 20 Jan 2023 13:27:29 +0900 Subject: [PATCH v4 4/4] Add GUC utility_query_id This GUC has two modes to control the computation method of query IDs for utilities: - 'string', the default, to hash the string query. - 'jumble', to use the parsed tree. --- src/include/nodes/queryjumble.h | 7 ++ src/backend/nodes/queryjumblefuncs.c | 81 ++++++++++++++----- src/backend/utils/misc/guc_tables.c | 16 ++++ src/backend/utils/misc/postgresql.conf.sample | 1 + doc/src/sgml/config.sgml | 18 +++++ .../expected/pg_stat_statements.out | 31 +++++++ .../sql/pg_stat_statements.sql | 17 ++++ 7 files changed, 151 insertions(+), 20 deletions(-) diff --git a/src/include/nodes/queryjumble.h b/src/include/nodes/queryjumble.h index 204b8f74fd..261aea6bcf 100644 --- a/src/include/nodes/queryjumble.h +++ b/src/include/nodes/queryjumble.h @@ -59,8 +59,15 @@ enum ComputeQueryIdType COMPUTE_QUERY_ID_REGRESS }; +enum UtilityQueryIdType +{ + UTILITY_QUERY_ID_STRING, + UTILITY_QUERY_ID_JUMBLE +}; + /* GUC parameters */ extern PGDLLIMPORT int compute_query_id; +extern PGDLLIMPORT int utility_query_id; extern const char *CleanQuerytext(const char *query, int *location, int *len); diff --git a/src/backend/nodes/queryjumblefuncs.c b/src/backend/nodes/queryjumblefuncs.c index 278150fba0..dd9ab8f353 100644 --- a/src/backend/nodes/queryjumblefuncs.c +++ b/src/backend/nodes/queryjumblefuncs.c @@ -41,12 +41,15 @@ /* GUC parameters */ int compute_query_id = COMPUTE_QUERY_ID_AUTO; +int utility_query_id = UTILITY_QUERY_ID_STRING; /* True when compute_query_id is ON, or AUTO and a module requests them */ bool query_id_enabled = false; static void AppendJumble(JumbleState *jstate, const unsigned char *item, Size size); +static uint64 compute_utility_query_id(const char *query_text, + int query_location, int query_len); static void RecordConstLocation(JumbleState *jstate, int location); static void _jumbleNode(JumbleState *jstate, Node *node); static void _jumbleList(JumbleState *jstate, Node *node); @@ -102,29 +105,39 @@ JumbleQuery(Query *query, const char *querytext) Assert(IsQueryIdEnabled()); - jstate = (JumbleState *) palloc(sizeof(JumbleState)); + if (query->utilityStmt && + compute_query_id == UTILITY_QUERY_ID_STRING) + { + query->queryId = compute_utility_query_id(querytext, + query->stmt_location, + query->stmt_len); + } + else + { + jstate = (JumbleState *) palloc(sizeof(JumbleState)); - /* Set up workspace for query jumbling */ - jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE); - jstate->jumble_len = 0; - jstate->clocations_buf_size = 32; - jstate->clocations = (LocationLen *) - palloc(jstate->clocations_buf_size * sizeof(LocationLen)); - jstate->clocations_count = 0; - jstate->highest_extern_param_id = 0; + /* Set up workspace for query jumbling */ + jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE); + jstate->jumble_len = 0; + jstate->clocations_buf_size = 32; + jstate->clocations = (LocationLen *) + palloc(jstate->clocations_buf_size * sizeof(LocationLen)); + jstate->clocations_count = 0; + jstate->highest_extern_param_id = 0; - /* Compute query ID and mark the Query node with it */ - _jumbleNode(jstate, (Node *) query); - query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble, - jstate->jumble_len, - 0)); + /* Compute query ID and mark the Query node with it */ + _jumbleNode(jstate, (Node *) query); + query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble, + jstate->jumble_len, + 0)); - /* - * If we are unlucky enough to get a hash of zero, use 1 instead, to - * prevent confusion with the utility-statement case. - */ - if (query->queryId == UINT64CONST(0)) - query->queryId = UINT64CONST(1); + /* + * If we are unlucky enough to get a hash of zero, use 1 instead, to + * prevent confusion with the utility-statement case. + */ + if (query->queryId == UINT64CONST(0)) + query->queryId = UINT64CONST(1); + } return jstate; } @@ -142,6 +155,34 @@ EnableQueryId(void) query_id_enabled = true; } +/* + * Compute a query identifier for the given utility query string. + */ +static uint64 +compute_utility_query_id(const char *query_text, int query_location, int query_len) +{ + uint64 queryId; + const char *sql; + + /* + * Confine our attention to the relevant part of the string, if the query + * is a portion of a multi-statement source string. + */ + sql = CleanQuerytext(query_text, &query_location, &query_len); + + queryId = DatumGetUInt64(hash_any_extended((const unsigned char *) sql, + query_len, 0)); + + /* + * If we are unlucky enough to get a hash of zero(invalid), use queryID as + * 2 instead, queryID 1 is already in use for normal statements. + */ + if (queryId == UINT64CONST(0)) + queryId = UINT64CONST(2); + + return queryId; +} + /* * AppendJumble: Append a value that is substantive in a given query to * the current jumble. diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c index f9bfbbbd95..869e8a3a6f 100644 --- a/src/backend/utils/misc/guc_tables.c +++ b/src/backend/utils/misc/guc_tables.c @@ -294,6 +294,12 @@ static const struct config_enum_entry compute_query_id_options[] = { {NULL, 0, false} }; +static const struct config_enum_entry utility_query_id_options[] = { + {"string", UTILITY_QUERY_ID_STRING, false}, + {"jumble", UTILITY_QUERY_ID_JUMBLE, false}, + {NULL, 0, false} +}; + /* * Although only "on", "off", and "partition" are documented, we * accept all the likely variants of "on" and "off". @@ -4563,6 +4569,16 @@ struct config_enum ConfigureNamesEnum[] = NULL, NULL, NULL }, + { + {"utility_query_id", PGC_SUSET, STATS_MONITORING, + gettext_noop("Controls method computing query ID for utilities."), + NULL + }, + &utility_query_id, + UTILITY_QUERY_ID_STRING, utility_query_id_options, + NULL, NULL, NULL + }, + { {"constraint_exclusion", PGC_USERSET, QUERY_TUNING_OTHER, gettext_noop("Enables the planner to use constraints to optimize queries."), diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 4cceda4162..4d43c9d3c4 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -622,6 +622,7 @@ # - Monitoring - #compute_query_id = auto +#utility_query_id = string # string, jumble #log_statement_stats = off #log_parser_stats = off #log_planner_stats = off diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 89d53f2a64..70c55f1a79 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -8203,6 +8203,24 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv; </listitem> </varlistentry> + <varlistentry id="guc-utility-query-id" xreflabel="utility_query_id"> + <term><varname>utility_query_id</varname> (<type>enum</type>) + <indexterm> + <primary><varname>utility_query_id</varname> configuration parameter</primary> + </indexterm> + </term> + <listitem> + <para> + Controls the method used to compute the query identifier of a utility + query. Valid values are <literal>string</literal> to use a hash of the + query string and <literal>jumble</literal> to compute the query + identifier depending on the parsed tree of the utility query (less + performant, but allows for more parameterization of the queries + involved). The default is <literal>string</literal>. + </para> + </listitem> + </varlistentry> + <varlistentry id="guc-log-statement-stats"> <term><varname>log_statement_stats</varname> (<type>boolean</type>) <indexterm> diff --git a/contrib/pg_stat_statements/expected/pg_stat_statements.out b/contrib/pg_stat_statements/expected/pg_stat_statements.out index 9ac5c87c3a..8bdf8beec3 100644 --- a/contrib/pg_stat_statements/expected/pg_stat_statements.out +++ b/contrib/pg_stat_statements/expected/pg_stat_statements.out @@ -554,6 +554,7 @@ DROP TABLE pgss_a, pgss_b CASCADE; -- utility commands -- SET pg_stat_statements.track_utility = TRUE; +SET utility_query_id = 'string'; SELECT pg_stat_statements_reset(); pg_stat_statements_reset -------------------------- @@ -592,6 +593,36 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C"; SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 0 | 0 (9 rows) +SELECT pg_stat_statements_reset(); + pg_stat_statements_reset +-------------------------- + +(1 row) + +SET utility_query_id = 'jumble'; +-- These queries have a different string, but the same parsing +-- representation. +Begin; +Create Table test_utility_query (a int); +Drop Table test_utility_query; +Commit; +BEGIN; +CREATE TABLE test_utility_query (a int); +DROP TABLE test_utility_query; +COMMIT; +SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C"; + query | calls | rows +------------------------------------------------------------------------------+-------+------ + Begin | 2 | 0 + Commit | 2 | 0 + Create Table test_utility_query (a int) | 2 | 0 + Drop Table test_utility_query | 2 | 0 + SELECT pg_stat_statements_reset() | 1 | 1 + SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 0 | 0 + SET utility_query_id = 'jumble' | 1 | 0 +(7 rows) + +RESET utility_query_id; -- -- Track the total number of rows retrieved or affected by the utility -- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW, diff --git a/contrib/pg_stat_statements/sql/pg_stat_statements.sql b/contrib/pg_stat_statements/sql/pg_stat_statements.sql index 8f5c866225..81d663f81c 100644 --- a/contrib/pg_stat_statements/sql/pg_stat_statements.sql +++ b/contrib/pg_stat_statements/sql/pg_stat_statements.sql @@ -258,6 +258,7 @@ DROP TABLE pgss_a, pgss_b CASCADE; -- utility commands -- SET pg_stat_statements.track_utility = TRUE; +SET utility_query_id = 'string'; SELECT pg_stat_statements_reset(); SELECT 1; @@ -272,6 +273,22 @@ DROP FUNCTION PLUS_TWO(INTEGER); SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C"; +SELECT pg_stat_statements_reset(); +SET utility_query_id = 'jumble'; +-- These queries have a different string, but the same parsing +-- representation. +Begin; +Create Table test_utility_query (a int); +Drop Table test_utility_query; +Commit; +BEGIN; +CREATE TABLE test_utility_query (a int); +DROP TABLE test_utility_query; +COMMIT; + +SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C"; +RESET utility_query_id; + -- -- Track the total number of rows retrieved or affected by the utility -- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW, -- 2.39.0
signature.asc
Description: PGP signature