mirror of https://github.com/postgres/postgres
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 otherspull/3/merge
parent
25dc142a49
commit
18ce3a4ab2
@ -0,0 +1,198 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* nodeNamedtuplestorescan.c |
||||
* routines to handle NamedTuplestoreScan nodes. |
||||
* |
||||
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/executor/nodeNamedtuplestorescan.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#include "postgres.h" |
||||
|
||||
#include "executor/execdebug.h" |
||||
#include "executor/nodeNamedtuplestorescan.h" |
||||
#include "miscadmin.h" |
||||
#include "utils/queryenvironment.h" |
||||
|
||||
static TupleTableSlot *NamedTuplestoreScanNext(NamedTuplestoreScanState *node); |
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* NamedTuplestoreScanNext |
||||
* |
||||
* This is a workhorse for ExecNamedTuplestoreScan |
||||
* ---------------------------------------------------------------- |
||||
*/ |
||||
static TupleTableSlot * |
||||
NamedTuplestoreScanNext(NamedTuplestoreScanState *node) |
||||
{ |
||||
TupleTableSlot *slot; |
||||
|
||||
/* We intentionally do not support backward scan. */ |
||||
Assert(ScanDirectionIsForward(node->ss.ps.state->es_direction)); |
||||
|
||||
/*
|
||||
* Get the next tuple from tuplestore. Return NULL if no more tuples. |
||||
*/ |
||||
slot = node->ss.ss_ScanTupleSlot; |
||||
(void) tuplestore_gettupleslot(node->relation, true, false, slot); |
||||
return slot; |
||||
} |
||||
|
||||
/*
|
||||
* NamedTuplestoreScanRecheck -- access method routine to recheck a tuple in |
||||
* EvalPlanQual |
||||
*/ |
||||
static bool |
||||
NamedTuplestoreScanRecheck(NamedTuplestoreScanState *node, TupleTableSlot *slot) |
||||
{ |
||||
/* nothing to check */ |
||||
return true; |
||||
} |
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* ExecNamedTuplestoreScan(node) |
||||
* |
||||
* Scans the CTE sequentially and returns the next qualifying tuple. |
||||
* We call the ExecScan() routine and pass it the appropriate |
||||
* access method functions. |
||||
* ---------------------------------------------------------------- |
||||
*/ |
||||
TupleTableSlot * |
||||
ExecNamedTuplestoreScan(NamedTuplestoreScanState *node) |
||||
{ |
||||
return ExecScan(&node->ss, |
||||
(ExecScanAccessMtd) NamedTuplestoreScanNext, |
||||
(ExecScanRecheckMtd) NamedTuplestoreScanRecheck); |
||||
} |
||||
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* ExecInitNamedTuplestoreScan |
||||
* ---------------------------------------------------------------- |
||||
*/ |
||||
NamedTuplestoreScanState * |
||||
ExecInitNamedTuplestoreScan(NamedTuplestoreScan *node, EState *estate, int eflags) |
||||
{ |
||||
NamedTuplestoreScanState *scanstate; |
||||
EphemeralNamedRelation enr; |
||||
|
||||
/* check for unsupported flags */ |
||||
Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK))); |
||||
|
||||
/*
|
||||
* NamedTuplestoreScan should not have any children. |
||||
*/ |
||||
Assert(outerPlan(node) == NULL); |
||||
Assert(innerPlan(node) == NULL); |
||||
|
||||
/*
|
||||
* create new NamedTuplestoreScanState for node |
||||
*/ |
||||
scanstate = makeNode(NamedTuplestoreScanState); |
||||
scanstate->ss.ps.plan = (Plan *) node; |
||||
scanstate->ss.ps.state = estate; |
||||
|
||||
enr = get_ENR(estate->es_queryEnv, node->enrname); |
||||
if (!enr) |
||||
elog(ERROR, "executor could not find named tuplestore \"%s\"", |
||||
node->enrname); |
||||
|
||||
Assert(enr->reldata); |
||||
scanstate->relation = (Tuplestorestate *) enr->reldata; |
||||
scanstate->tupdesc = ENRMetadataGetTupDesc(&(enr->md)); |
||||
scanstate->readptr = |
||||
tuplestore_alloc_read_pointer(scanstate->relation, 0); |
||||
|
||||
/*
|
||||
* The new read pointer copies its position from read pointer 0, which |
||||
* could be anywhere, so explicitly rewind it. |
||||
*/ |
||||
tuplestore_rescan(scanstate->relation); |
||||
|
||||
/*
|
||||
* XXX: Should we add a function to free that read pointer when done? |
||||
* This was attempted, but it did not improve performance or memory usage |
||||
* in any tested cases. |
||||
*/ |
||||
|
||||
/*
|
||||
* Miscellaneous initialization |
||||
* |
||||
* create expression context for node |
||||
*/ |
||||
ExecAssignExprContext(estate, &scanstate->ss.ps); |
||||
|
||||
/*
|
||||
* initialize child expressions |
||||
*/ |
||||
scanstate->ss.ps.qual = |
||||
ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate); |
||||
|
||||
/*
|
||||
* tuple table initialization |
||||
*/ |
||||
ExecInitResultTupleSlot(estate, &scanstate->ss.ps); |
||||
ExecInitScanTupleSlot(estate, &scanstate->ss); |
||||
|
||||
/*
|
||||
* The scan tuple type is specified for the tuplestore. |
||||
*/ |
||||
ExecAssignScanType(&scanstate->ss, scanstate->tupdesc); |
||||
|
||||
/*
|
||||
* Initialize result tuple type and projection info. |
||||
*/ |
||||
ExecAssignResultTypeFromTL(&scanstate->ss.ps); |
||||
ExecAssignScanProjectionInfo(&scanstate->ss); |
||||
|
||||
return scanstate; |
||||
} |
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* ExecEndNamedTuplestoreScan |
||||
* |
||||
* frees any storage allocated through C routines. |
||||
* ---------------------------------------------------------------- |
||||
*/ |
||||
void |
||||
ExecEndNamedTuplestoreScan(NamedTuplestoreScanState *node) |
||||
{ |
||||
/*
|
||||
* Free exprcontext |
||||
*/ |
||||
ExecFreeExprContext(&node->ss.ps); |
||||
|
||||
/*
|
||||
* clean out the tuple table |
||||
*/ |
||||
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot); |
||||
ExecClearTuple(node->ss.ss_ScanTupleSlot); |
||||
} |
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* ExecReScanNamedTuplestoreScan |
||||
* |
||||
* Rescans the relation. |
||||
* ---------------------------------------------------------------- |
||||
*/ |
||||
void |
||||
ExecReScanNamedTuplestoreScan(NamedTuplestoreScanState *node) |
||||
{ |
||||
Tuplestorestate *tuplestorestate = node->relation; |
||||
|
||||
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot); |
||||
|
||||
ExecScanReScan(&node->ss); |
||||
|
||||
/*
|
||||
* Rewind my own pointer. |
||||
*/ |
||||
tuplestore_select_read_pointer(tuplestorestate, node->readptr); |
||||
tuplestore_rescan(tuplestorestate); |
||||
} |
||||
@ -0,0 +1,29 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* parse_enr.c |
||||
* parser support routines dealing with ephemeral named relations |
||||
* |
||||
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/parser/parse_enr.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "parser/parse_enr.h" |
||||
|
||||
bool |
||||
name_matches_visible_ENR(ParseState *pstate, const char *refname) |
||||
{ |
||||
return (get_visible_ENR_metadata(pstate->p_queryEnv, refname) != NULL); |
||||
} |
||||
|
||||
EphemeralNamedRelationMetadata |
||||
get_visible_ENR(ParseState *pstate, const char *refname) |
||||
{ |
||||
return get_visible_ENR_metadata(pstate->p_queryEnv, refname); |
||||
} |
||||
@ -0,0 +1,144 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* queryenvironment.c |
||||
* Query environment, to store context-specific values like ephemeral named |
||||
* relations. Initial use is for named tuplestores for delta information |
||||
* from "normal" relations. |
||||
* |
||||
* The initial implementation uses a list because the number of such relations |
||||
* in any one context is expected to be very small. If that becomes a |
||||
* performance problem, the implementation can be changed with no other impact |
||||
* on callers, since this is an opaque structure. This is the reason to |
||||
* require a create function. |
||||
* |
||||
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/backend/utils/misc/queryenvironment.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "access/heapam.h" |
||||
#include "utils/queryenvironment.h" |
||||
#include "utils/rel.h" |
||||
|
||||
/*
|
||||
* Private state of a query environment. |
||||
*/ |
||||
struct QueryEnvironment |
||||
{ |
||||
List *namedRelList; |
||||
}; |
||||
|
||||
|
||||
QueryEnvironment * |
||||
create_queryEnv() |
||||
{ |
||||
return (QueryEnvironment *) palloc0(sizeof(QueryEnvironment)); |
||||
} |
||||
|
||||
EphemeralNamedRelationMetadata |
||||
get_visible_ENR_metadata(QueryEnvironment *queryEnv, const char *refname) |
||||
{ |
||||
EphemeralNamedRelation enr; |
||||
|
||||
Assert(refname != NULL); |
||||
|
||||
if (queryEnv == NULL) |
||||
return NULL; |
||||
|
||||
enr = get_ENR(queryEnv, refname); |
||||
|
||||
if (enr) |
||||
return &(enr->md); |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
/*
|
||||
* Register a named relation for use in the given environment. |
||||
* |
||||
* If this is intended exclusively for planning purposes, the tstate field can |
||||
* be left NULL; |
||||
*/ |
||||
void |
||||
register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr) |
||||
{ |
||||
Assert(enr != NULL); |
||||
Assert(get_ENR(queryEnv, enr->md.name) == NULL); |
||||
|
||||
queryEnv->namedRelList = lappend(queryEnv->namedRelList, enr); |
||||
} |
||||
|
||||
/*
|
||||
* Unregister an ephemeral relation by name. This will probably be a rarely |
||||
* used function, but seems like it should be provided "just in case". |
||||
*/ |
||||
void |
||||
unregister_ENR(QueryEnvironment *queryEnv, const char *name) |
||||
{ |
||||
EphemeralNamedRelation match; |
||||
|
||||
match = get_ENR(queryEnv, name); |
||||
if (match) |
||||
queryEnv->namedRelList = list_delete(queryEnv->namedRelList, match); |
||||
} |
||||
|
||||
/*
|
||||
* This returns an ENR if there is a name match in the given collection. It |
||||
* must quietly return NULL if no match is found. |
||||
*/ |
||||
EphemeralNamedRelation |
||||
get_ENR(QueryEnvironment *queryEnv, const char *name) |
||||
{ |
||||
ListCell *lc; |
||||
|
||||
Assert(name != NULL); |
||||
|
||||
if (queryEnv == NULL) |
||||
return NULL; |
||||
|
||||
foreach(lc, queryEnv->namedRelList) |
||||
{ |
||||
EphemeralNamedRelation enr = (EphemeralNamedRelation) lfirst(lc); |
||||
|
||||
if (strcmp(enr->md.name, name) == 0) |
||||
return enr; |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
/*
|
||||
* Gets the TupleDesc for a Ephemeral Named Relation, based on which field was |
||||
* filled. |
||||
* |
||||
* When the TupleDesc is based on a relation from the catalogs, we count on |
||||
* that relation being used at the same time, so that appropriate locks will |
||||
* already be held. Locking here would be too late anyway. |
||||
*/ |
||||
TupleDesc |
||||
ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd) |
||||
{ |
||||
TupleDesc tupdesc; |
||||
|
||||
/* One, and only one, of these fields must be filled. */ |
||||
Assert((enrmd->reliddesc == InvalidOid) != (enrmd->tupdesc == NULL)); |
||||
|
||||
if (enrmd->tupdesc != NULL) |
||||
tupdesc = enrmd->tupdesc; |
||||
else |
||||
{ |
||||
Relation relation; |
||||
|
||||
relation = heap_open(enrmd->reliddesc, NoLock); |
||||
tupdesc = relation->rd_att; |
||||
heap_close(relation, NoLock); |
||||
} |
||||
|
||||
return tupdesc; |
||||
} |
||||
@ -0,0 +1,24 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* nodeNamedtuplestorescan.h |
||||
* |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/include/executor/nodeNamedtuplestorescan.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef NODENAMEDTUPLESTORESCAN_H |
||||
#define NODENAMEDTUPLESTORESCAN_H |
||||
|
||||
#include "nodes/execnodes.h" |
||||
|
||||
extern NamedTuplestoreScanState *ExecInitNamedTuplestoreScan(NamedTuplestoreScan *node, EState *estate, int eflags); |
||||
extern TupleTableSlot *ExecNamedTuplestoreScan(NamedTuplestoreScanState *node); |
||||
extern void ExecEndNamedTuplestoreScan(NamedTuplestoreScanState *node); |
||||
extern void ExecReScanNamedTuplestoreScan(NamedTuplestoreScanState *node); |
||||
|
||||
#endif /* NODENAMEDTUPLESTORESCAN_H */ |
||||
@ -0,0 +1,22 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* parse_enr.h |
||||
* Internal definitions for parser |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/include/parser/parse_enr.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef PARSE_ENR_H |
||||
#define PARSE_ENR_H |
||||
|
||||
#include "parser/parse_node.h" |
||||
|
||||
extern bool name_matches_visible_ENR(ParseState *pstate, const char *refname); |
||||
extern EphemeralNamedRelationMetadata get_visible_ENR(ParseState *pstate, const char *refname); |
||||
|
||||
#endif /* PARSE_ENR_H */ |
||||
@ -0,0 +1,74 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* queryenvironment.h |
||||
* Access to functions to mutate the query environment and retrieve the |
||||
* actual data related to entries (if any). |
||||
* |
||||
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/include/utils/queryenvironment.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef QUERYENVIRONMENT_H |
||||
#define QUERYENVIRONMENT_H |
||||
|
||||
#include "access/tupdesc.h" |
||||
|
||||
|
||||
typedef enum EphemeralNameRelationType |
||||
{ |
||||
ENR_NAMED_TUPLESTORE /* named tuplestore relation; e.g., deltas */ |
||||
} EphemeralNameRelationType; |
||||
|
||||
/*
|
||||
* Some ephemeral named relations must match some relation (e.g., trigger |
||||
* transition tables), so to properly handle cached plans and DDL, we should |
||||
* carry the OID of that relation. In other cases an ENR might be independent |
||||
* of any relation which is stored in the system catalogs, so we need to be |
||||
* able to directly store the TupleDesc. We never need both. |
||||
*/ |
||||
typedef struct EphemeralNamedRelationMetadataData |
||||
{ |
||||
char *name; /* name used to identify the relation */ |
||||
|
||||
/* only one of the next two fields should be used */ |
||||
Oid reliddesc; /* oid of relation to get tupdesc */ |
||||
TupleDesc tupdesc; /* description of result rows */ |
||||
|
||||
EphemeralNameRelationType enrtype; /* to identify type of relation */ |
||||
double enrtuples; /* estimated number of tuples */ |
||||
} EphemeralNamedRelationMetadataData; |
||||
|
||||
typedef EphemeralNamedRelationMetadataData *EphemeralNamedRelationMetadata; |
||||
|
||||
/*
|
||||
* Ephemeral Named Relation data; used for parsing named relations not in the |
||||
* catalog, like transition tables in AFTER triggers. |
||||
*/ |
||||
typedef struct EphemeralNamedRelationData |
||||
{ |
||||
EphemeralNamedRelationMetadataData md; |
||||
void *reldata; /* structure for execution-time access to data */ |
||||
} EphemeralNamedRelationData; |
||||
|
||||
typedef EphemeralNamedRelationData *EphemeralNamedRelation; |
||||
|
||||
/*
|
||||
* This is an opaque structure outside of queryenvironment.c itself. The |
||||
* intention is to be able to change the implementation or add new context |
||||
* features without needing to change existing code for use of existing |
||||
* features. |
||||
*/ |
||||
typedef struct QueryEnvironment QueryEnvironment; |
||||
|
||||
|
||||
extern QueryEnvironment *create_queryEnv(void); |
||||
extern EphemeralNamedRelationMetadata get_visible_ENR_metadata(QueryEnvironment *queryEnv, const char *refname); |
||||
extern void register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr); |
||||
extern void unregister_ENR(QueryEnvironment *queryEnv, const char *name); |
||||
extern EphemeralNamedRelation get_ENR(QueryEnvironment *queryEnv, const char *name); |
||||
extern TupleDesc ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd); |
||||
|
||||
#endif /* QUERYENVIRONMENT_H */ |
||||
Loading…
Reference in new issue