mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-14 10:27:04 +08:00
The core idea of this patch is to make the parser generate join alias Vars (that is, ones with varno pointing to a JOIN RTE) only when the alias Var is actually different from any raw join input, that is a type coercion and/or COALESCE is necessary to generate the join output value. Otherwise just generate varno/varattno pointing to the relevant join input column. In effect, this means that the planner's flatten_join_alias_vars() transformation is already done in the parser, for all cases except (a) columns that are merged by JOIN USING and are transformed in the process, and (b) whole-row join Vars. In principle that would allow us to skip doing flatten_join_alias_vars() in many more queries than we do now, but we don't have quite enough infrastructure to know that we can do so --- in particular there's no cheap way to know whether there are any whole-row join Vars. I'm not sure if it's worth the trouble to add a Query-level flag for that, and in any case it seems like fit material for a separate patch. But even without skipping the work entirely, this should make flatten_join_alias_vars() faster, particularly where there are nested joins that it previously had to flatten recursively. An essential part of this change is to replace Var nodes' varnoold/varoattno fields with varnosyn/varattnosyn, which have considerably more tightly-defined meanings than the old fields: when they differ from varno/varattno, they identify the Var's position in an aliased JOIN RTE, and the join alias is what ruleutils.c should print for the Var. This is necessary because the varno change destroyed ruleutils.c's ability to find the JOIN RTE from the Var's varno. Another way in which this change broke ruleutils.c is that it's no longer feasible to determine, from a JOIN RTE's joinaliasvars list, which join columns correspond to which columns of the join's immediate input relations. (If those are sub-joins, the joinaliasvars entries may point to columns of their base relations, not the sub-joins.) But that was a horrid mess requiring a lot of fragile assumptions already, so let's just bite the bullet and add some more JOIN RTE fields to make it more straightforward to figure that out. I added two integer-List fields containing the relevant column numbers from the left and right input rels, plus a count of how many merged columns there are. This patch depends on the ParseNamespaceColumn infrastructure that I added in commit 5815696bc. The biggest bit of code change is restructuring transformFromClauseItem's handling of JOINs so that the ParseNamespaceColumn data is propagated upward correctly. Other than that and the ruleutils fixes, everything pretty much just works, though some processing is now inessential. I grabbed two pieces of low-hanging fruit in that line: 1. In find_expr_references, we don't need to recurse into join alias Vars anymore. There aren't any except for references to merged USING columns, which are more properly handled when we scan the join's RTE. This change actually fixes an edge-case issue: we will now record a dependency on any type-coercion function present in a USING column's joinaliasvar, even if that join column has no references in the query text. The odds of the missing dependency causing a problem seem quite small: you'd have to posit somebody dropping an implicit cast between two data types, without removing the types themselves, and then having a stored rule containing a whole-row Var for a join whose USING merge depends on that cast. So I don't feel a great need to change this in the back branches. But in theory this way is more correct. 2. markRTEForSelectPriv and markTargetListOrigin don't need to recurse into join alias Vars either, because the cases they care about don't apply to alias Vars for USING columns that are semantically distinct from the underlying columns. This removes the only case in which markVarForSelectPriv could be called with NULL for the RTE, so adjust the comments to describe that hack as being strictly internal to markRTEForSelectPriv. catversion bump required due to changes in stored rules. Discussion: https://postgr.es/m/7115.1577986646@sss.pgh.pa.us
124 lines
4.9 KiB
C
124 lines
4.9 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* parse_relation.h
|
|
* prototypes for parse_relation.c.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/parser/parse_relation.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef PARSE_RELATION_H
|
|
#define PARSE_RELATION_H
|
|
|
|
#include "parser/parse_node.h"
|
|
|
|
|
|
extern ParseNamespaceItem *refnameNamespaceItem(ParseState *pstate,
|
|
const char *schemaname,
|
|
const char *refname,
|
|
int location,
|
|
int *sublevels_up);
|
|
extern CommonTableExpr *scanNameSpaceForCTE(ParseState *pstate,
|
|
const char *refname,
|
|
Index *ctelevelsup);
|
|
extern bool scanNameSpaceForENR(ParseState *pstate, const char *refname);
|
|
extern void checkNameSpaceConflicts(ParseState *pstate, List *namespace1,
|
|
List *namespace2);
|
|
extern ParseNamespaceItem *GetNSItemByRangeTablePosn(ParseState *pstate,
|
|
int varno,
|
|
int sublevels_up);
|
|
extern RangeTblEntry *GetRTEByRangeTablePosn(ParseState *pstate,
|
|
int varno,
|
|
int sublevels_up);
|
|
extern CommonTableExpr *GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte,
|
|
int rtelevelsup);
|
|
extern Node *scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem,
|
|
int sublevels_up, const char *colname,
|
|
int location);
|
|
extern Node *colNameToVar(ParseState *pstate, const char *colname, bool localonly,
|
|
int location);
|
|
extern void markVarForSelectPriv(ParseState *pstate, Var *var,
|
|
RangeTblEntry *rte);
|
|
extern Relation parserOpenTable(ParseState *pstate, const RangeVar *relation,
|
|
int lockmode);
|
|
extern ParseNamespaceItem *addRangeTableEntry(ParseState *pstate,
|
|
RangeVar *relation,
|
|
Alias *alias,
|
|
bool inh,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForRelation(ParseState *pstate,
|
|
Relation rel,
|
|
int lockmode,
|
|
Alias *alias,
|
|
bool inh,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForSubquery(ParseState *pstate,
|
|
Query *subquery,
|
|
Alias *alias,
|
|
bool lateral,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForFunction(ParseState *pstate,
|
|
List *funcnames,
|
|
List *funcexprs,
|
|
List *coldeflists,
|
|
RangeFunction *rangefunc,
|
|
bool lateral,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForValues(ParseState *pstate,
|
|
List *exprs,
|
|
List *coltypes,
|
|
List *coltypmods,
|
|
List *colcollations,
|
|
Alias *alias,
|
|
bool lateral,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForTableFunc(ParseState *pstate,
|
|
TableFunc *tf,
|
|
Alias *alias,
|
|
bool lateral,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForJoin(ParseState *pstate,
|
|
List *colnames,
|
|
ParseNamespaceColumn *nscolumns,
|
|
JoinType jointype,
|
|
int nummergedcols,
|
|
List *aliasvars,
|
|
List *leftcols,
|
|
List *rightcols,
|
|
Alias *alias,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForCTE(ParseState *pstate,
|
|
CommonTableExpr *cte,
|
|
Index levelsup,
|
|
RangeVar *rv,
|
|
bool inFromCl);
|
|
extern ParseNamespaceItem *addRangeTableEntryForENR(ParseState *pstate,
|
|
RangeVar *rv,
|
|
bool inFromCl);
|
|
extern bool isLockedRefname(ParseState *pstate, const char *refname);
|
|
extern void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem,
|
|
bool addToJoinList,
|
|
bool addToRelNameSpace, bool addToVarNameSpace);
|
|
extern void errorMissingRTE(ParseState *pstate, RangeVar *relation) pg_attribute_noreturn();
|
|
extern void errorMissingColumn(ParseState *pstate,
|
|
const char *relname, const char *colname, int location) pg_attribute_noreturn();
|
|
extern void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
|
|
int location, bool include_dropped,
|
|
List **colnames, List **colvars);
|
|
extern List *expandNSItemVars(ParseNamespaceItem *nsitem,
|
|
int sublevels_up, int location,
|
|
List **colnames);
|
|
extern List *expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem,
|
|
int sublevels_up, int location);
|
|
extern int attnameAttNum(Relation rd, const char *attname, bool sysColOK);
|
|
extern const NameData *attnumAttName(Relation rd, int attid);
|
|
extern Oid attnumTypeId(Relation rd, int attid);
|
|
extern Oid attnumCollationId(Relation rd, int attid);
|
|
extern bool isQueryUsingTempRelation(Query *query);
|
|
|
|
#endif /* PARSE_RELATION_H */
|