mirror of
https://github.com/postgres/postgres.git
synced 2025-06-29 10:41:53 +03:00
Add infrastructure to support EphemeralNamedRelation references.
A QueryEnvironment concept is added, which allows new types of objects to be passed into queries from parsing on through execution. At this point, the only thing implemented is a collection of EphemeralNamedRelation objects -- relations which can be referenced by name in queries, but do not exist in the catalogs. The only type of ENR implemented is NamedTuplestore, but provision is made to add more types fairly easily. An ENR can carry its own TupleDesc or reference a relation in the catalogs by relid. Although these features can be used without SPI, convenience functions are added to SPI so that ENRs can easily be used by code run through SPI. The initial use of all this is going to be transition tables in AFTER triggers, but that will be added to each PL as a separate commit. An incidental effect of this patch is to produce a more informative error message if an attempt is made to modify the contents of a CTE from a referencing DML statement. No tests previously covered that possibility, so one is added. Kevin Grittner and Thomas Munro Reviewed by Heikki Linnakangas, David Fetter, and Thomas Munro with valuable comments and suggestions from many others
This commit is contained in:
@ -59,9 +59,12 @@ static Node *transformJoinUsingClause(ParseState *pstate,
|
||||
List *leftVars, List *rightVars);
|
||||
static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
|
||||
List *namespace);
|
||||
static RangeTblEntry *getRTEForSpecialRelationTypes(ParseState *pstate,
|
||||
RangeVar *rv);
|
||||
static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
|
||||
static RangeTblEntry *transformCTEReference(ParseState *pstate, RangeVar *r,
|
||||
CommonTableExpr *cte, Index levelsup);
|
||||
static RangeTblEntry *transformENRReference(ParseState *pstate, RangeVar *r);
|
||||
static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
|
||||
RangeSubselect *r);
|
||||
static RangeTblEntry *transformRangeFunction(ParseState *pstate,
|
||||
@ -181,6 +184,14 @@ setTargetTable(ParseState *pstate, RangeVar *relation,
|
||||
RangeTblEntry *rte;
|
||||
int rtindex;
|
||||
|
||||
/* So far special relations are immutable; so they cannot be targets. */
|
||||
rte = getRTEForSpecialRelationTypes(pstate, relation);
|
||||
if (rte != NULL)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("relation \"%s\" cannot be the target of a modifying statement",
|
||||
relation->relname)));
|
||||
|
||||
/* Close old target; this could only happen for multi-action rules */
|
||||
if (pstate->p_target_relation != NULL)
|
||||
heap_close(pstate->p_target_relation, NoLock);
|
||||
@ -434,6 +445,20 @@ transformCTEReference(ParseState *pstate, RangeVar *r,
|
||||
return rte;
|
||||
}
|
||||
|
||||
/*
|
||||
* transformENRReference --- transform a RangeVar that references an ephemeral
|
||||
* named relation
|
||||
*/
|
||||
static RangeTblEntry *
|
||||
transformENRReference(ParseState *pstate, RangeVar *r)
|
||||
{
|
||||
RangeTblEntry *rte;
|
||||
|
||||
rte = addRangeTableEntryForENR(pstate, r, true);
|
||||
|
||||
return rte;
|
||||
}
|
||||
|
||||
/*
|
||||
* transformRangeSubselect --- transform a sub-SELECT appearing in FROM
|
||||
*/
|
||||
@ -1021,6 +1046,24 @@ transformRangeTableSample(ParseState *pstate, RangeTableSample *rts)
|
||||
return tablesample;
|
||||
}
|
||||
|
||||
|
||||
static RangeTblEntry *
|
||||
getRTEForSpecialRelationTypes(ParseState *pstate, RangeVar *rv)
|
||||
{
|
||||
|
||||
CommonTableExpr *cte;
|
||||
Index levelsup;
|
||||
RangeTblEntry *rte = NULL;
|
||||
|
||||
cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
|
||||
if (cte)
|
||||
rte = transformCTEReference(pstate, rv, cte, levelsup);
|
||||
if (!rte && scanNameSpaceForENR(pstate, rv->relname))
|
||||
rte = transformENRReference(pstate, rv);
|
||||
|
||||
return rte;
|
||||
}
|
||||
|
||||
/*
|
||||
* transformFromClauseItem -
|
||||
* Transform a FROM-clause item, adding any required entries to the
|
||||
@ -1055,18 +1098,14 @@ transformFromClauseItem(ParseState *pstate, Node *n,
|
||||
RangeTblEntry *rte = NULL;
|
||||
int rtindex;
|
||||
|
||||
/* if it is an unqualified name, it might be a CTE reference */
|
||||
/*
|
||||
* if it is an unqualified name, it might be a CTE or tuplestore
|
||||
* reference
|
||||
*/
|
||||
if (!rv->schemaname)
|
||||
{
|
||||
CommonTableExpr *cte;
|
||||
Index levelsup;
|
||||
rte = getRTEForSpecialRelationTypes(pstate, rv);
|
||||
|
||||
cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
|
||||
if (cte)
|
||||
rte = transformCTEReference(pstate, rv, cte, levelsup);
|
||||
}
|
||||
|
||||
/* if not found as a CTE, must be a table reference */
|
||||
/* if not found above, must be a table reference */
|
||||
if (!rte)
|
||||
rte = transformTableEntry(pstate, rv);
|
||||
|
||||
|
Reference in New Issue
Block a user