mirror of
https://github.com/postgres/postgres.git
synced 2025-08-30 06:01:21 +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:
@@ -1471,7 +1471,8 @@ BeginCopy(ParseState *pstate,
|
||||
* DECLARE CURSOR and PREPARE.) XXX FIXME someday.
|
||||
*/
|
||||
rewritten = pg_analyze_and_rewrite(copyObject(raw_query),
|
||||
pstate->p_sourcetext, NULL, 0);
|
||||
pstate->p_sourcetext, NULL, 0,
|
||||
NULL);
|
||||
|
||||
/* check that we got back something we can work with */
|
||||
if (rewritten == NIL)
|
||||
@@ -1574,7 +1575,7 @@ BeginCopy(ParseState *pstate,
|
||||
cstate->queryDesc = CreateQueryDesc(plan, pstate->p_sourcetext,
|
||||
GetActiveSnapshot(),
|
||||
InvalidSnapshot,
|
||||
dest, NULL, 0);
|
||||
dest, NULL, NULL, 0);
|
||||
|
||||
/*
|
||||
* Call ExecutorStart to prepare the plan for execution.
|
||||
|
@@ -222,7 +222,8 @@ create_ctas_nodata(List *tlist, IntoClause *into)
|
||||
*/
|
||||
ObjectAddress
|
||||
ExecCreateTableAs(CreateTableAsStmt *stmt, const char *queryString,
|
||||
ParamListInfo params, char *completionTag)
|
||||
ParamListInfo params, QueryEnvironment *queryEnv,
|
||||
char *completionTag)
|
||||
{
|
||||
Query *query = castNode(Query, stmt->query);
|
||||
IntoClause *into = stmt->into;
|
||||
@@ -341,7 +342,7 @@ ExecCreateTableAs(CreateTableAsStmt *stmt, const char *queryString,
|
||||
/* Create a QueryDesc, redirecting output to our tuple receiver */
|
||||
queryDesc = CreateQueryDesc(plan, queryString,
|
||||
GetActiveSnapshot(), InvalidSnapshot,
|
||||
dest, params, 0);
|
||||
dest, params, queryEnv, 0);
|
||||
|
||||
/* call ExecutorStart to prepare the plan for execution */
|
||||
ExecutorStart(queryDesc, GetIntoRelEFlags(into));
|
||||
|
@@ -55,7 +55,8 @@ explain_get_index_name_hook_type explain_get_index_name_hook = NULL;
|
||||
|
||||
static void ExplainOneQuery(Query *query, int cursorOptions,
|
||||
IntoClause *into, ExplainState *es,
|
||||
const char *queryString, ParamListInfo params);
|
||||
const char *queryString, ParamListInfo params,
|
||||
QueryEnvironment *queryEnv);
|
||||
static void report_triggers(ResultRelInfo *rInfo, bool show_relname,
|
||||
ExplainState *es);
|
||||
static double elapsed_time(instr_time *starttime);
|
||||
@@ -142,7 +143,8 @@ static void escape_yaml(StringInfo buf, const char *str);
|
||||
*/
|
||||
void
|
||||
ExplainQuery(ParseState *pstate, ExplainStmt *stmt, const char *queryString,
|
||||
ParamListInfo params, DestReceiver *dest)
|
||||
ParamListInfo params, QueryEnvironment *queryEnv,
|
||||
DestReceiver *dest)
|
||||
{
|
||||
ExplainState *es = NewExplainState();
|
||||
TupOutputState *tstate;
|
||||
@@ -253,7 +255,7 @@ ExplainQuery(ParseState *pstate, ExplainStmt *stmt, const char *queryString,
|
||||
{
|
||||
ExplainOneQuery(castNode(Query, lfirst(l)),
|
||||
CURSOR_OPT_PARALLEL_OK, NULL, es,
|
||||
queryString, params);
|
||||
queryString, params, queryEnv);
|
||||
|
||||
/* Separate plans with an appropriate separator */
|
||||
if (lnext(l) != NULL)
|
||||
@@ -338,12 +340,14 @@ ExplainResultDesc(ExplainStmt *stmt)
|
||||
static void
|
||||
ExplainOneQuery(Query *query, int cursorOptions,
|
||||
IntoClause *into, ExplainState *es,
|
||||
const char *queryString, ParamListInfo params)
|
||||
const char *queryString, ParamListInfo params,
|
||||
QueryEnvironment *queryEnv)
|
||||
{
|
||||
/* planner will not cope with utility statements */
|
||||
if (query->commandType == CMD_UTILITY)
|
||||
{
|
||||
ExplainOneUtility(query->utilityStmt, into, es, queryString, params);
|
||||
ExplainOneUtility(query->utilityStmt, into, es, queryString, params,
|
||||
queryEnv);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -366,7 +370,8 @@ ExplainOneQuery(Query *query, int cursorOptions,
|
||||
INSTR_TIME_SUBTRACT(planduration, planstart);
|
||||
|
||||
/* run it (if needed) and produce output */
|
||||
ExplainOnePlan(plan, into, es, queryString, params, &planduration);
|
||||
ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
|
||||
&planduration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,7 +388,8 @@ ExplainOneQuery(Query *query, int cursorOptions,
|
||||
*/
|
||||
void
|
||||
ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es,
|
||||
const char *queryString, ParamListInfo params)
|
||||
const char *queryString, ParamListInfo params,
|
||||
QueryEnvironment *queryEnv)
|
||||
{
|
||||
if (utilityStmt == NULL)
|
||||
return;
|
||||
@@ -404,7 +410,7 @@ ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es,
|
||||
Assert(list_length(rewritten) == 1);
|
||||
ExplainOneQuery(castNode(Query, linitial(rewritten)),
|
||||
0, ctas->into, es,
|
||||
queryString, params);
|
||||
queryString, params, queryEnv);
|
||||
}
|
||||
else if (IsA(utilityStmt, DeclareCursorStmt))
|
||||
{
|
||||
@@ -423,11 +429,11 @@ ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es,
|
||||
Assert(list_length(rewritten) == 1);
|
||||
ExplainOneQuery(castNode(Query, linitial(rewritten)),
|
||||
dcs->options, NULL, es,
|
||||
queryString, params);
|
||||
queryString, params, queryEnv);
|
||||
}
|
||||
else if (IsA(utilityStmt, ExecuteStmt))
|
||||
ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
|
||||
queryString, params);
|
||||
queryString, params, queryEnv);
|
||||
else if (IsA(utilityStmt, NotifyStmt))
|
||||
{
|
||||
if (es->format == EXPLAIN_FORMAT_TEXT)
|
||||
@@ -460,7 +466,7 @@ ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es,
|
||||
void
|
||||
ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
|
||||
const char *queryString, ParamListInfo params,
|
||||
const instr_time *planduration)
|
||||
QueryEnvironment *queryEnv, const instr_time *planduration)
|
||||
{
|
||||
DestReceiver *dest;
|
||||
QueryDesc *queryDesc;
|
||||
@@ -505,7 +511,7 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
|
||||
/* Create a QueryDesc for the query */
|
||||
queryDesc = CreateQueryDesc(plannedstmt, queryString,
|
||||
GetActiveSnapshot(), InvalidSnapshot,
|
||||
dest, params, instrument_option);
|
||||
dest, params, queryEnv, instrument_option);
|
||||
|
||||
/* Select execution options */
|
||||
if (es->analyze)
|
||||
@@ -796,6 +802,7 @@ ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
|
||||
case T_TableFuncScan:
|
||||
case T_ValuesScan:
|
||||
case T_CteScan:
|
||||
case T_NamedTuplestoreScan:
|
||||
case T_WorkTableScan:
|
||||
*rels_used = bms_add_member(*rels_used,
|
||||
((Scan *) plan)->scanrelid);
|
||||
@@ -951,6 +958,9 @@ ExplainNode(PlanState *planstate, List *ancestors,
|
||||
case T_CteScan:
|
||||
pname = sname = "CTE Scan";
|
||||
break;
|
||||
case T_NamedTuplestoreScan:
|
||||
pname = sname = "Named Tuplestore Scan";
|
||||
break;
|
||||
case T_WorkTableScan:
|
||||
pname = sname = "WorkTable Scan";
|
||||
break;
|
||||
@@ -1389,6 +1399,7 @@ ExplainNode(PlanState *planstate, List *ancestors,
|
||||
case T_SeqScan:
|
||||
case T_ValuesScan:
|
||||
case T_CteScan:
|
||||
case T_NamedTuplestoreScan:
|
||||
case T_WorkTableScan:
|
||||
case T_SubqueryScan:
|
||||
show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
|
||||
@@ -2679,6 +2690,11 @@ ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
|
||||
objectname = rte->ctename;
|
||||
objecttag = "CTE Name";
|
||||
break;
|
||||
case T_NamedTuplestoreScan:
|
||||
Assert(rte->rtekind == RTE_NAMEDTUPLESTORE);
|
||||
objectname = rte->enrname;
|
||||
objecttag = "Tuplestore Name";
|
||||
break;
|
||||
case T_WorkTableScan:
|
||||
/* Assert it's on a self-reference CTE */
|
||||
Assert(rte->rtekind == RTE_CTE);
|
||||
|
@@ -721,7 +721,8 @@ execute_sql_string(const char *sql, const char *filename)
|
||||
stmt_list = pg_analyze_and_rewrite(parsetree,
|
||||
sql,
|
||||
NULL,
|
||||
0);
|
||||
0,
|
||||
NULL);
|
||||
stmt_list = pg_plan_queries(stmt_list, CURSOR_OPT_PARALLEL_OK, NULL);
|
||||
|
||||
foreach(lc2, stmt_list)
|
||||
@@ -739,7 +740,7 @@ execute_sql_string(const char *sql, const char *filename)
|
||||
qdesc = CreateQueryDesc(stmt,
|
||||
sql,
|
||||
GetActiveSnapshot(), NULL,
|
||||
dest, NULL, 0);
|
||||
dest, NULL, NULL, 0);
|
||||
|
||||
ExecutorStart(qdesc, 0);
|
||||
ExecutorRun(qdesc, ForwardScanDirection, 0, true);
|
||||
@@ -759,6 +760,7 @@ execute_sql_string(const char *sql, const char *filename)
|
||||
sql,
|
||||
PROCESS_UTILITY_QUERY,
|
||||
NULL,
|
||||
NULL,
|
||||
dest,
|
||||
NULL);
|
||||
}
|
||||
|
@@ -1623,7 +1623,7 @@ ImportForeignSchema(ImportForeignSchemaStmt *stmt)
|
||||
/* Execute statement */
|
||||
ProcessUtility(pstmt,
|
||||
cmd,
|
||||
PROCESS_UTILITY_SUBCOMMAND, NULL,
|
||||
PROCESS_UTILITY_SUBCOMMAND, NULL, NULL,
|
||||
None_Receiver, NULL);
|
||||
|
||||
/* Be sure to advance the command counter between subcommands */
|
||||
|
@@ -418,7 +418,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
|
||||
/* Create a QueryDesc, redirecting output to our tuple receiver */
|
||||
queryDesc = CreateQueryDesc(plan, queryString,
|
||||
GetActiveSnapshot(), InvalidSnapshot,
|
||||
dest, NULL, 0);
|
||||
dest, NULL, NULL, 0);
|
||||
|
||||
/* call ExecutorStart to prepare the plan for execution */
|
||||
ExecutorStart(queryDesc, EXEC_FLAG_WITHOUT_OIDS);
|
||||
|
@@ -91,7 +91,7 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
|
||||
* to see the unmodified raw parse tree.
|
||||
*/
|
||||
plansource = CreateCachedPlan(rawstmt, queryString,
|
||||
CreateCommandTag(stmt->query));
|
||||
CreateCommandTag(stmt->query), NULL);
|
||||
|
||||
/* Transform list of TypeNames to array of type OIDs */
|
||||
nargs = list_length(stmt->argtypes);
|
||||
@@ -243,7 +243,7 @@ ExecuteQuery(ExecuteStmt *stmt, IntoClause *intoClause,
|
||||
entry->plansource->query_string);
|
||||
|
||||
/* Replan if needed, and increment plan refcount for portal */
|
||||
cplan = GetCachedPlan(entry->plansource, paramLI, false);
|
||||
cplan = GetCachedPlan(entry->plansource, paramLI, false, NULL);
|
||||
plan_list = cplan->stmt_list;
|
||||
|
||||
/*
|
||||
@@ -551,7 +551,7 @@ FetchPreparedStatementTargetList(PreparedStatement *stmt)
|
||||
List *tlist;
|
||||
|
||||
/* Get the plan's primary targetlist */
|
||||
tlist = CachedPlanGetTargetList(stmt->plansource);
|
||||
tlist = CachedPlanGetTargetList(stmt->plansource, NULL);
|
||||
|
||||
/* Copy into caller's context in case plan gets invalidated */
|
||||
return copyObject(tlist);
|
||||
@@ -629,7 +629,8 @@ DropAllPreparedStatements(void)
|
||||
*/
|
||||
void
|
||||
ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
|
||||
const char *queryString, ParamListInfo params)
|
||||
const char *queryString, ParamListInfo params,
|
||||
QueryEnvironment *queryEnv)
|
||||
{
|
||||
PreparedStatement *entry;
|
||||
const char *query_string;
|
||||
@@ -668,7 +669,7 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
|
||||
}
|
||||
|
||||
/* Replan if needed, and acquire a transient refcount */
|
||||
cplan = GetCachedPlan(entry->plansource, paramLI, true);
|
||||
cplan = GetCachedPlan(entry->plansource, paramLI, true, queryEnv);
|
||||
|
||||
INSTR_TIME_SET_CURRENT(planduration);
|
||||
INSTR_TIME_SUBTRACT(planduration, planstart);
|
||||
@@ -681,9 +682,11 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
|
||||
PlannedStmt *pstmt = castNode(PlannedStmt, lfirst(p));
|
||||
|
||||
if (pstmt->commandType != CMD_UTILITY)
|
||||
ExplainOnePlan(pstmt, into, es, query_string, paramLI, &planduration);
|
||||
ExplainOnePlan(pstmt, into, es, query_string, paramLI, queryEnv,
|
||||
&planduration);
|
||||
else
|
||||
ExplainOneUtility(pstmt->utilityStmt, into, es, query_string, paramLI);
|
||||
ExplainOneUtility(pstmt->utilityStmt, into, es, query_string,
|
||||
paramLI, queryEnv);
|
||||
|
||||
/* No need for CommandCounterIncrement, as ExplainOnePlan did it */
|
||||
|
||||
|
@@ -194,6 +194,7 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString,
|
||||
queryString,
|
||||
PROCESS_UTILITY_SUBCOMMAND,
|
||||
NULL,
|
||||
NULL,
|
||||
None_Receiver,
|
||||
NULL);
|
||||
|
||||
|
@@ -354,6 +354,13 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
|
||||
* adjustments will be needed below.
|
||||
*/
|
||||
|
||||
if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is a partitioned table",
|
||||
RelationGetRelationName(rel)),
|
||||
errdetail("Triggers on partitioned tables cannot have transition tables.")));
|
||||
|
||||
if (stmt->timing != TRIGGER_TYPE_AFTER)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||
@@ -1173,7 +1180,7 @@ ConvertTriggerToFK(CreateTrigStmt *stmt, Oid funcoid)
|
||||
/* ... and execute it */
|
||||
ProcessUtility(wrapper,
|
||||
"(generated ALTER TABLE ADD FOREIGN KEY command)",
|
||||
PROCESS_UTILITY_SUBCOMMAND, NULL,
|
||||
PROCESS_UTILITY_SUBCOMMAND, NULL, NULL,
|
||||
None_Receiver, NULL);
|
||||
|
||||
/* Remove the matched item from the list */
|
||||
|
@@ -436,7 +436,7 @@ DefineView(ViewStmt *stmt, const char *queryString,
|
||||
rawstmt->stmt_location = stmt_location;
|
||||
rawstmt->stmt_len = stmt_len;
|
||||
|
||||
viewParse = parse_analyze(rawstmt, queryString, NULL, 0);
|
||||
viewParse = parse_analyze(rawstmt, queryString, NULL, 0, NULL);
|
||||
|
||||
/*
|
||||
* The grammar should ensure that the result is a single SELECT Query.
|
||||
|
Reference in New Issue
Block a user