diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2017-01-14 16:02:35 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2017-01-14 16:02:35 -0500 |
commit | ab1f0c8225714aaa18d2f9ca4f80cd009f145421 (patch) | |
tree | 76cddd3b6b74474384d799fd61a4f211094ea55b /src/backend/tcop/postgres.c | |
parent | 75abb955dfef064f2fbc5c043f37fff8d0262ffe (diff) | |
download | postgresql-ab1f0c8225714aaa18d2f9ca4f80cd009f145421.tar.gz postgresql-ab1f0c8225714aaa18d2f9ca4f80cd009f145421.zip |
Change representation of statement lists, and add statement location info.
This patch makes several changes that improve the consistency of
representation of lists of statements. It's always been the case
that the output of parse analysis is a list of Query nodes, whatever
the types of the individual statements in the list. This patch brings
similar consistency to the outputs of raw parsing and planning steps:
* The output of raw parsing is now always a list of RawStmt nodes;
the statement-type-dependent nodes are one level down from that.
* The output of pg_plan_queries() is now always a list of PlannedStmt
nodes, even for utility statements. In the case of a utility statement,
"planning" just consists of wrapping a CMD_UTILITY PlannedStmt around
the utility node. This list representation is now used in Portal and
CachedPlan plan lists, replacing the former convention of intermixing
PlannedStmts with bare utility-statement nodes.
Now, every list of statements has a consistent head-node type depending
on how far along it is in processing. This allows changing many places
that formerly used generic "Node *" pointers to use a more specific
pointer type, thus reducing the number of IsA() tests and casts needed,
as well as improving code clarity.
Also, the post-parse-analysis representation of DECLARE CURSOR is changed
so that it looks more like EXPLAIN, PREPARE, etc. That is, the contained
SELECT remains a child of the DeclareCursorStmt rather than getting flipped
around to be the other way. It's now true for both Query and PlannedStmt
that utilityStmt is non-null if and only if commandType is CMD_UTILITY.
That allows simplifying a lot of places that were testing both fields.
(I think some of those were just defensive programming, but in many places,
it was actually necessary to avoid confusing DECLARE CURSOR with SELECT.)
Because PlannedStmt carries a canSetTag field, we're also able to get rid
of some ad-hoc rules about how to reconstruct canSetTag for a bare utility
statement; specifically, the assumption that a utility is canSetTag if and
only if it's the only one in its list. While I see no near-term need for
relaxing that restriction, it's nice to get rid of the ad-hocery.
The API of ProcessUtility() is changed so that what it's passed is the
wrapper PlannedStmt not just the bare utility statement. This will affect
all users of ProcessUtility_hook, but the changes are pretty trivial; see
the affected contrib modules for examples of the minimum change needed.
(Most compilers should give pointer-type-mismatch warnings for uncorrected
code.)
There's also a change in the API of ExplainOneQuery_hook, to pass through
cursorOptions instead of expecting hook functions to know what to pick.
This is needed because of the DECLARE CURSOR changes, but really should
have been done in 9.6; it's unlikely that any extant hook functions
know about using CURSOR_OPT_PARALLEL_OK.
Finally, teach gram.y to save statement boundary locations in RawStmt
nodes, and pass those through to Query and PlannedStmt nodes. This allows
more intelligent handling of cases where a source query string contains
multiple statements. This patch doesn't actually do anything with the
information, but a follow-on patch will. (Passing this information through
cleanly is the true motivation for these changes; while I think this is all
good cleanup, it's unlikely we'd have bothered without this end goal.)
catversion bump because addition of location fields to struct Query
affects stored rules.
This patch is by me, but it owes a good deal to Fabien Coelho who did
a lot of preliminary work on the problem, and also reviewed the patch.
Discussion: https://postgr.es/m/alpine.DEB.2.20.1612200926310.29821@lancre
Diffstat (limited to 'src/backend/tcop/postgres.c')
-rw-r--r-- | src/backend/tcop/postgres.c | 99 |
1 files changed, 47 insertions, 52 deletions
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 05b2e57b71d..bb89cce8cb9 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -183,8 +183,8 @@ static int errdetail_recovery_conflict(void); static void start_xact_command(void); static void finish_xact_command(void); static bool IsTransactionExitStmt(Node *parsetree); -static bool IsTransactionExitStmtList(List *parseTrees); -static bool IsTransactionStmtList(List *parseTrees); +static bool IsTransactionExitStmtList(List *pstmts); +static bool IsTransactionStmtList(List *pstmts); static void drop_unnamed_stmt(void); static void SigHupHandler(SIGNAL_ARGS); static void log_disconnections(int code, Datum arg); @@ -588,8 +588,8 @@ ProcessClientWriteInterrupt(bool blocked) /* * Do raw parsing (only). * - * A list of parsetrees is returned, since there might be multiple - * commands in the given string. + * A list of parsetrees (RawStmt nodes) is returned, since there might be + * multiple commands in the given string. * * NOTE: for interactive queries, it is important to keep this routine * separate from the analysis & rewrite stages. Analysis and rewriting @@ -641,7 +641,7 @@ pg_parse_query(const char *query_string) * NOTE: for reasons mentioned above, this must be separate from raw parsing. */ List * -pg_analyze_and_rewrite(Node *parsetree, const char *query_string, +pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string, Oid *paramTypes, int numParams) { Query *query; @@ -676,7 +676,7 @@ pg_analyze_and_rewrite(Node *parsetree, const char *query_string, * hooks instead of a fixed list of parameter datatypes. */ List * -pg_analyze_and_rewrite_params(Node *parsetree, +pg_analyze_and_rewrite_params(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg) @@ -833,8 +833,10 @@ pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams) /* * Generate plans for a list of already-rewritten queries. * - * Normal optimizable statements generate PlannedStmt entries in the result - * list. Utility statements are simply represented by their statement nodes. + * For normal optimizable statements, invoke the planner. For utility + * statements, just make a wrapper PlannedStmt node. + * + * The result is a list of PlannedStmt nodes. */ List * pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams) @@ -845,16 +847,21 @@ pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams) foreach(query_list, querytrees) { Query *query = (Query *) lfirst(query_list); - Node *stmt; + PlannedStmt *stmt; if (query->commandType == CMD_UTILITY) { - /* Utility commands have no plans. */ - stmt = query->utilityStmt; + /* Utility commands require no planning. */ + stmt = makeNode(PlannedStmt); + stmt->commandType = CMD_UTILITY; + stmt->canSetTag = query->canSetTag; + stmt->utilityStmt = query->utilityStmt; + stmt->stmt_location = query->stmt_location; + stmt->stmt_len = query->stmt_len; } else { - stmt = (Node *) pg_plan_query(query, cursorOptions, boundParams); + stmt = pg_plan_query(query, cursorOptions, boundParams); } stmt_list = lappend(stmt_list, stmt); @@ -955,7 +962,7 @@ exec_simple_query(const char *query_string) */ foreach(parsetree_item, parsetree_list) { - Node *parsetree = (Node *) lfirst(parsetree_item); + RawStmt *parsetree = (RawStmt *) lfirst(parsetree_item); bool snapshot_set = false; const char *commandTag; char completionTag[COMPLETION_TAG_BUFSIZE]; @@ -971,7 +978,7 @@ exec_simple_query(const char *query_string) * do any special start-of-SQL-command processing needed by the * destination. */ - commandTag = CreateCommandTag(parsetree); + commandTag = CreateCommandTag(parsetree->stmt); set_ps_display(commandTag, false); @@ -986,7 +993,7 @@ exec_simple_query(const char *query_string) * state, but not many...) */ if (IsAbortedTransactionBlockState() && - !IsTransactionExitStmt(parsetree)) + !IsTransactionExitStmt(parsetree->stmt)) ereport(ERROR, (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION), errmsg("current transaction is aborted, " @@ -1061,9 +1068,9 @@ exec_simple_query(const char *query_string) * backward compatibility...) */ format = 0; /* TEXT is default */ - if (IsA(parsetree, FetchStmt)) + if (IsA(parsetree->stmt, FetchStmt)) { - FetchStmt *stmt = (FetchStmt *) parsetree; + FetchStmt *stmt = (FetchStmt *) parsetree->stmt; if (!stmt->ismove) { @@ -1102,7 +1109,7 @@ exec_simple_query(const char *query_string) PortalDrop(portal, false); - if (IsA(parsetree, TransactionStmt)) + if (IsA(parsetree->stmt, TransactionStmt)) { /* * If this was a transaction control statement, commit it. We will @@ -1194,7 +1201,7 @@ exec_parse_message(const char *query_string, /* string to execute */ MemoryContext unnamed_stmt_context = NULL; MemoryContext oldcontext; List *parsetree_list; - Node *raw_parse_tree; + RawStmt *raw_parse_tree; const char *commandTag; List *querytree_list; CachedPlanSource *psrc; @@ -1279,12 +1286,12 @@ exec_parse_message(const char *query_string, /* string to execute */ bool snapshot_set = false; int i; - raw_parse_tree = (Node *) linitial(parsetree_list); + raw_parse_tree = (RawStmt *) linitial(parsetree_list); /* * Get the command name for possible use in status display. */ - commandTag = CreateCommandTag(raw_parse_tree); + commandTag = CreateCommandTag(raw_parse_tree->stmt); /* * If we are in an aborted transaction, reject all commands except @@ -1295,7 +1302,7 @@ exec_parse_message(const char *query_string, /* string to execute */ * state, but not many...) */ if (IsAbortedTransactionBlockState() && - !IsTransactionExitStmt(raw_parse_tree)) + !IsTransactionExitStmt(raw_parse_tree->stmt)) ereport(ERROR, (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION), errmsg("current transaction is aborted, " @@ -1552,7 +1559,7 @@ exec_bind_message(StringInfo input_message) * functions. */ if (IsAbortedTransactionBlockState() && - (!IsTransactionExitStmt(psrc->raw_parse_tree) || + (!IsTransactionExitStmt(psrc->raw_parse_tree->stmt) || numParams != 0)) ereport(ERROR, (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION), @@ -2140,11 +2147,11 @@ errdetail_execute(List *raw_parsetree_list) foreach(parsetree_item, raw_parsetree_list) { - Node *parsetree = (Node *) lfirst(parsetree_item); + RawStmt *parsetree = (RawStmt *) lfirst(parsetree_item); - if (IsA(parsetree, ExecuteStmt)) + if (IsA(parsetree->stmt, ExecuteStmt)) { - ExecuteStmt *stmt = (ExecuteStmt *) parsetree; + ExecuteStmt *stmt = (ExecuteStmt *) parsetree->stmt; PreparedStatement *pstmt; pstmt = FetchPreparedStatement(stmt->name, false); @@ -2488,45 +2495,33 @@ IsTransactionExitStmt(Node *parsetree) return false; } -/* Test a list that might contain Query nodes or bare parsetrees */ +/* Test a list that contains PlannedStmt nodes */ static bool -IsTransactionExitStmtList(List *parseTrees) +IsTransactionExitStmtList(List *pstmts) { - if (list_length(parseTrees) == 1) + if (list_length(pstmts) == 1) { - Node *stmt = (Node *) linitial(parseTrees); - - if (IsA(stmt, Query)) - { - Query *query = (Query *) stmt; + PlannedStmt *pstmt = (PlannedStmt *) linitial(pstmts); - if (query->commandType == CMD_UTILITY && - IsTransactionExitStmt(query->utilityStmt)) - return true; - } - else if (IsTransactionExitStmt(stmt)) + Assert(IsA(pstmt, PlannedStmt)); + if (pstmt->commandType == CMD_UTILITY && + IsTransactionExitStmt(pstmt->utilityStmt)) return true; } return false; } -/* Test a list that might contain Query nodes or bare parsetrees */ +/* Test a list that contains PlannedStmt nodes */ static bool -IsTransactionStmtList(List *parseTrees) +IsTransactionStmtList(List *pstmts) { - if (list_length(parseTrees) == 1) + if (list_length(pstmts) == 1) { - Node *stmt = (Node *) linitial(parseTrees); + PlannedStmt *pstmt = (PlannedStmt *) linitial(pstmts); - if (IsA(stmt, Query)) - { - Query *query = (Query *) stmt; - - if (query->commandType == CMD_UTILITY && - IsA(query->utilityStmt, TransactionStmt)) - return true; - } - else if (IsA(stmt, TransactionStmt)) + Assert(IsA(pstmt, PlannedStmt)); + if (pstmt->commandType == CMD_UTILITY && + IsA(pstmt->utilityStmt, TransactionStmt)) return true; } return false; |