2002-08-27 06:55:12 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* prepare.c
|
|
|
|
* Prepareable SQL statements via PREPARE, EXECUTE and DEALLOCATE
|
|
|
|
*
|
2003-05-05 02:44:56 +02:00
|
|
|
* This module also implements storage of prepared statements that are
|
|
|
|
* accessed via the extended FE/BE query protocol.
|
|
|
|
*
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Copyright (c) 2002-2019, PostgreSQL Global Development Group
|
2002-08-27 06:55:12 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/commands/prepare.c
|
2002-08-27 06:55:12 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2017-02-23 21:57:08 +01:00
|
|
|
#include <limits.h>
|
|
|
|
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/xact.h"
|
2006-01-08 08:00:27 +01:00
|
|
|
#include "catalog/pg_type.h"
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
#include "commands/createas.h"
|
2002-08-27 06:55:12 +02:00
|
|
|
#include "commands/prepare.h"
|
2007-04-27 01:24:46 +02:00
|
|
|
#include "miscadmin.h"
|
2008-08-26 00:42:34 +02:00
|
|
|
#include "nodes/nodeFuncs.h"
|
2007-03-13 01:33:44 +01:00
|
|
|
#include "parser/analyze.h"
|
|
|
|
#include "parser/parse_coerce.h"
|
2011-03-20 01:29:08 +01:00
|
|
|
#include "parser/parse_collate.h"
|
2007-03-13 01:33:44 +01:00
|
|
|
#include "parser/parse_expr.h"
|
|
|
|
#include "parser/parse_type.h"
|
2002-08-27 06:55:12 +02:00
|
|
|
#include "rewrite/rewriteHandler.h"
|
|
|
|
#include "tcop/pquery.h"
|
|
|
|
#include "tcop/utility.h"
|
2006-01-08 08:00:27 +01:00
|
|
|
#include "utils/builtins.h"
|
2008-03-26 19:48:59 +01:00
|
|
|
#include "utils/snapmgr.h"
|
2011-09-09 19:23:41 +02:00
|
|
|
#include "utils/timestamp.h"
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2006-04-25 16:11:59 +02:00
|
|
|
|
2002-08-27 06:55:12 +02:00
|
|
|
/*
|
|
|
|
* The hash table in which prepared queries are stored. This is
|
|
|
|
* per-backend: query plans are not shared between backends.
|
2003-05-05 02:44:56 +02:00
|
|
|
* The keys for this hash table are the arguments to PREPARE and EXECUTE
|
|
|
|
* (statement names); the entries are PreparedStatement structs.
|
2002-08-27 06:55:12 +02:00
|
|
|
*/
|
|
|
|
static HTAB *prepared_queries = NULL;
|
|
|
|
|
|
|
|
static void InitQueryHashTable(void);
|
2007-03-13 01:33:44 +01:00
|
|
|
static ParamListInfo EvaluateParams(PreparedStatement *pstmt, List *params,
|
2019-05-22 19:04:48 +02:00
|
|
|
const char *queryString, EState *estate);
|
2007-03-13 01:33:44 +01:00
|
|
|
static Datum build_regtype_array(Oid *param_types, int num_params);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implements the 'PREPARE' utility statement.
|
|
|
|
*/
|
|
|
|
void
|
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
2017-01-14 22:02:35 +01:00
|
|
|
PrepareQuery(PrepareStmt *stmt, const char *queryString,
|
|
|
|
int stmt_location, int stmt_len)
|
2002-08-27 06:55:12 +02:00
|
|
|
{
|
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
2017-01-14 22:02:35 +01:00
|
|
|
RawStmt *rawstmt;
|
2011-09-16 06:42:53 +02:00
|
|
|
CachedPlanSource *plansource;
|
2007-03-13 01:33:44 +01:00
|
|
|
Oid *argtypes = NULL;
|
|
|
|
int nargs;
|
2004-12-12 21:17:06 +01:00
|
|
|
Query *query;
|
2011-09-16 06:42:53 +02:00
|
|
|
List *query_list;
|
2007-03-13 01:33:44 +01:00
|
|
|
int i;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
/*
|
|
|
|
* Disallow empty-string statement name (conflicts with protocol-level
|
|
|
|
* unnamed statement).
|
|
|
|
*/
|
|
|
|
if (!stmt->name || stmt->name[0] == '\0')
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PSTATEMENT_DEFINITION),
|
|
|
|
errmsg("invalid statement name: must not be empty")));
|
2002-08-27 06:55:12 +02:00
|
|
|
|
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
2017-01-14 22:02:35 +01:00
|
|
|
/*
|
|
|
|
* Need to wrap the contained statement in a RawStmt node to pass it to
|
|
|
|
* parse analysis.
|
|
|
|
*
|
|
|
|
* Because parse analysis scribbles on the raw querytree, we must make a
|
|
|
|
* copy to ensure we don't modify the passed-in tree. FIXME someday.
|
|
|
|
*/
|
|
|
|
rawstmt = makeNode(RawStmt);
|
|
|
|
rawstmt->stmt = (Node *) copyObject(stmt->query);
|
|
|
|
rawstmt->stmt_location = stmt_location;
|
|
|
|
rawstmt->stmt_len = stmt_len;
|
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/*
|
|
|
|
* Create the CachedPlanSource before we do parse analysis, since it needs
|
|
|
|
* to see the unmodified raw parse tree.
|
|
|
|
*/
|
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
2017-01-14 22:02:35 +01:00
|
|
|
plansource = CreateCachedPlan(rawstmt, queryString,
|
2017-04-01 22:21:05 +02:00
|
|
|
CreateCommandTag(stmt->query));
|
2011-09-16 06:42:53 +02:00
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
/* Transform list of TypeNames to array of type OIDs */
|
|
|
|
nargs = list_length(stmt->argtypes);
|
|
|
|
|
|
|
|
if (nargs)
|
|
|
|
{
|
|
|
|
ParseState *pstate;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* typenameTypeId wants a ParseState to carry the source query string.
|
|
|
|
* Is it worth refactoring its API to avoid this?
|
|
|
|
*/
|
|
|
|
pstate = make_parsestate(NULL);
|
|
|
|
pstate->p_sourcetext = queryString;
|
|
|
|
|
|
|
|
argtypes = (Oid *) palloc(nargs * sizeof(Oid));
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
foreach(l, stmt->argtypes)
|
|
|
|
{
|
|
|
|
TypeName *tn = lfirst(l);
|
2010-10-25 20:40:46 +02:00
|
|
|
Oid toid = typenameTypeId(pstate, tn);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
|
|
|
argtypes[i++] = toid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Analyze the statement using these parameter types (any parameters
|
|
|
|
* passed in from above us will not be visible to it), allowing
|
|
|
|
* information about unknown parameters to be deduced from context.
|
|
|
|
*/
|
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
2017-01-14 22:02:35 +01:00
|
|
|
query = parse_analyze_varparams(rawstmt, queryString,
|
2007-06-24 00:12:52 +02:00
|
|
|
&argtypes, &nargs);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that all parameter types were determined.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
|
|
|
Oid argtype = argtypes[i];
|
|
|
|
|
|
|
|
if (argtype == InvalidOid || argtype == UNKNOWNOID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDETERMINATE_DATATYPE),
|
|
|
|
errmsg("could not determine data type of parameter $%d",
|
|
|
|
i + 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-08-05 05:14:58 +02:00
|
|
|
* grammar only allows PreparableStmt, so this check should be redundant
|
2007-03-13 01:33:44 +01:00
|
|
|
*/
|
|
|
|
switch (query->commandType)
|
2003-05-05 02:44:56 +02:00
|
|
|
{
|
|
|
|
case CMD_SELECT:
|
|
|
|
case CMD_INSERT:
|
|
|
|
case CMD_UPDATE:
|
|
|
|
case CMD_DELETE:
|
2007-04-28 00:05:49 +02:00
|
|
|
/* OK */
|
2003-05-05 02:44:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PSTATEMENT_DEFINITION),
|
|
|
|
errmsg("utility statements cannot be prepared")));
|
2003-05-05 02:44:56 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
/* Rewrite the query. The result could be 0, 1, or many queries. */
|
2004-12-12 21:17:06 +01:00
|
|
|
query_list = QueryRewrite(query);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/* Finish filling in the CachedPlanSource */
|
|
|
|
CompleteCachedPlan(plansource,
|
|
|
|
query_list,
|
|
|
|
NULL,
|
|
|
|
argtypes,
|
|
|
|
nargs,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2016-12-06 17:11:54 +01:00
|
|
|
CURSOR_OPT_PARALLEL_OK, /* allow parallel mode */
|
2011-09-16 06:42:53 +02:00
|
|
|
true); /* fixed result */
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2005-05-24 06:18:04 +02:00
|
|
|
/*
|
2007-03-13 01:33:44 +01:00
|
|
|
* Save the results.
|
2005-05-24 06:18:04 +02:00
|
|
|
*/
|
2003-05-05 02:44:56 +02:00
|
|
|
StorePreparedStatement(stmt->name,
|
2011-09-16 06:42:53 +02:00
|
|
|
plansource,
|
2006-01-08 08:00:27 +01:00
|
|
|
true);
|
2002-08-27 06:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
* ExecuteQuery --- implement the 'EXECUTE' utility statement.
|
|
|
|
*
|
|
|
|
* This code also supports CREATE TABLE ... AS EXECUTE. That case is
|
2014-05-06 18:12:18 +02:00
|
|
|
* indicated by passing a non-null intoClause. The DestReceiver is already
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
* set up correctly for CREATE TABLE AS, but we still have to make a few
|
|
|
|
* other adjustments here.
|
2008-07-21 17:26:55 +02:00
|
|
|
*
|
|
|
|
* Note: this is one of very few places in the code that needs to deal with
|
|
|
|
* two query strings at once. The passed-in queryString is that of the
|
|
|
|
* EXECUTE, which we might need for error reporting while processing the
|
|
|
|
* parameter expressions. The query_string that we copy from the plan
|
|
|
|
* source is that of the original PREPARE.
|
2002-08-27 06:55:12 +02:00
|
|
|
*/
|
|
|
|
void
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
ExecuteQuery(ExecuteStmt *stmt, IntoClause *intoClause,
|
|
|
|
const char *queryString, ParamListInfo params,
|
2005-11-29 02:25:50 +01:00
|
|
|
DestReceiver *dest, char *completionTag)
|
2002-08-27 06:55:12 +02:00
|
|
|
{
|
2003-05-05 02:44:56 +02:00
|
|
|
PreparedStatement *entry;
|
2007-03-13 01:33:44 +01:00
|
|
|
CachedPlan *cplan;
|
2007-02-20 18:32:18 +01:00
|
|
|
List *plan_list;
|
2002-08-27 06:55:12 +02:00
|
|
|
ParamListInfo paramLI = NULL;
|
2003-02-03 00:46:38 +01:00
|
|
|
EState *estate = NULL;
|
2003-05-02 22:54:36 +02:00
|
|
|
Portal portal;
|
Adjust things so that the query_string of a cached plan and the sourceText of
a portal are never NULL, but reliably provide the source text of the query.
It turns out that there was only one place that was really taking a short-cut,
which was the 'EXECUTE' utility statement. That doesn't seem like a
sufficiently critical performance hotspot to justify not offering a guarantee
of validity of the portal source text. Fix it to copy the source text over
from the cached plan. Add Asserts in the places that set up cached plans and
portals to reject null source strings, and simplify a bunch of places that
formerly needed to guard against nulls.
There may be a few places that cons up statements for execution without
having any source text at all; I found one such in ConvertTriggerToFK().
It seems sufficient to inject a phony source string in such a case,
for instance
ProcessUtility((Node *) atstmt,
"(generated ALTER TABLE ADD FOREIGN KEY command)",
NULL, false, None_Receiver, NULL);
We should take a second look at the usage of debug_query_string,
particularly the recently added current_query() SQL function.
ITAGAKI Takahiro and Tom Lane
2008-07-18 22:26:06 +02:00
|
|
|
char *query_string;
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
int eflags;
|
|
|
|
long count;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
/* Look it up in the hash table */
|
2003-05-05 02:44:56 +02:00
|
|
|
entry = FetchPreparedStatement(stmt->name, true);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/* Shouldn't find a non-fixed-result cached plan */
|
2007-03-13 01:33:44 +01:00
|
|
|
if (!entry->plansource->fixed_result)
|
|
|
|
elog(ERROR, "EXECUTE does not support variable-result cached plans");
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
/* Evaluate parameters, if any */
|
2007-03-13 01:33:44 +01:00
|
|
|
if (entry->plansource->num_params > 0)
|
2002-08-27 06:55:12 +02:00
|
|
|
{
|
2003-02-03 00:46:38 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Need an EState to evaluate parameters; must not delete it till end
|
2014-05-06 18:12:18 +02:00
|
|
|
* of query, in case parameters are pass-by-reference. Note that the
|
2011-09-16 06:42:53 +02:00
|
|
|
* passed-in "params" could possibly be referenced in the parameter
|
|
|
|
* expressions.
|
2003-02-03 00:46:38 +01:00
|
|
|
*/
|
|
|
|
estate = CreateExecutorState();
|
2005-11-29 02:25:50 +01:00
|
|
|
estate->es_param_list_info = params;
|
2007-03-13 01:33:44 +01:00
|
|
|
paramLI = EvaluateParams(entry, stmt->params,
|
|
|
|
queryString, estate);
|
2002-08-27 06:55:12 +02:00
|
|
|
}
|
|
|
|
|
2006-01-18 07:49:30 +01:00
|
|
|
/* Create a new portal to run the query in */
|
2003-05-02 22:54:36 +02:00
|
|
|
portal = CreateNewPortal();
|
2006-01-18 07:49:30 +01:00
|
|
|
/* Don't display the portal in pg_cursors, it is for internal use only */
|
|
|
|
portal->visible = false;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
Adjust things so that the query_string of a cached plan and the sourceText of
a portal are never NULL, but reliably provide the source text of the query.
It turns out that there was only one place that was really taking a short-cut,
which was the 'EXECUTE' utility statement. That doesn't seem like a
sufficiently critical performance hotspot to justify not offering a guarantee
of validity of the portal source text. Fix it to copy the source text over
from the cached plan. Add Asserts in the places that set up cached plans and
portals to reject null source strings, and simplify a bunch of places that
formerly needed to guard against nulls.
There may be a few places that cons up statements for execution without
having any source text at all; I found one such in ConvertTriggerToFK().
It seems sufficient to inject a phony source string in such a case,
for instance
ProcessUtility((Node *) atstmt,
"(generated ALTER TABLE ADD FOREIGN KEY command)",
NULL, false, None_Receiver, NULL);
We should take a second look at the usage of debug_query_string,
particularly the recently added current_query() SQL function.
ITAGAKI Takahiro and Tom Lane
2008-07-18 22:26:06 +02:00
|
|
|
/* Copy the plan's saved query string into the portal's memory */
|
2017-12-16 23:26:26 +01:00
|
|
|
query_string = MemoryContextStrdup(portal->portalContext,
|
Adjust things so that the query_string of a cached plan and the sourceText of
a portal are never NULL, but reliably provide the source text of the query.
It turns out that there was only one place that was really taking a short-cut,
which was the 'EXECUTE' utility statement. That doesn't seem like a
sufficiently critical performance hotspot to justify not offering a guarantee
of validity of the portal source text. Fix it to copy the source text over
from the cached plan. Add Asserts in the places that set up cached plans and
portals to reject null source strings, and simplify a bunch of places that
formerly needed to guard against nulls.
There may be a few places that cons up statements for execution without
having any source text at all; I found one such in ConvertTriggerToFK().
It seems sufficient to inject a phony source string in such a case,
for instance
ProcessUtility((Node *) atstmt,
"(generated ALTER TABLE ADD FOREIGN KEY command)",
NULL, false, None_Receiver, NULL);
We should take a second look at the usage of debug_query_string,
particularly the recently added current_query() SQL function.
ITAGAKI Takahiro and Tom Lane
2008-07-18 22:26:06 +02:00
|
|
|
entry->plansource->query_string);
|
|
|
|
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
/* Replan if needed, and increment plan refcount for portal */
|
2017-04-01 06:17:18 +02:00
|
|
|
cplan = GetCachedPlan(entry->plansource, paramLI, false, NULL);
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
plan_list = cplan->stmt_list;
|
|
|
|
|
2003-05-02 22:54:36 +02:00
|
|
|
/*
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
* For CREATE TABLE ... AS EXECUTE, we must verify that the prepared
|
|
|
|
* statement is one that produces tuples. Currently we insist that it be
|
2014-05-06 18:12:18 +02:00
|
|
|
* a plain old SELECT. In future we might consider supporting other
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
* things such as INSERT ... RETURNING, but there are a couple of issues
|
|
|
|
* to be settled first, notably how WITH NO DATA should be handled in such
|
|
|
|
* a case (do we really want to suppress execution?) and how to pass down
|
|
|
|
* the OID-determining eflags (PortalStart won't handle them in such a
|
|
|
|
* case, and for that matter it's not clear the executor will either).
|
|
|
|
*
|
2012-06-10 21:20:04 +02:00
|
|
|
* For CREATE TABLE ... AS EXECUTE, we also have to ensure that the proper
|
|
|
|
* eflags and fetch count are passed to PortalStart/PortalRun.
|
2003-05-02 22:54:36 +02:00
|
|
|
*/
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
if (intoClause)
|
2003-05-02 22:54:36 +02:00
|
|
|
{
|
2007-02-20 18:32:18 +01:00
|
|
|
PlannedStmt *pstmt;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2007-02-20 18:32:18 +01:00
|
|
|
if (list_length(plan_list) != 1)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("prepared statement is not a SELECT")));
|
Improve castNode notation by introducing list-extraction-specific variants.
This extends the castNode() notation introduced by commit 5bcab1114 to
provide, in one step, extraction of a list cell's pointer and coercion to
a concrete node type. For example, "lfirst_node(Foo, lc)" is the same
as "castNode(Foo, lfirst(lc))". Almost half of the uses of castNode
that have appeared so far include a list extraction call, so this is
pretty widely useful, and it saves a few more keystrokes compared to the
old way.
As with the previous patch, back-patch the addition of these macros to
pg_list.h, so that the notation will be available when back-patching.
Patch by me, after an idea of Andrew Gierth's.
Discussion: https://postgr.es/m/14197.1491841216@sss.pgh.pa.us
2017-04-10 19:51:29 +02:00
|
|
|
pstmt = linitial_node(PlannedStmt, plan_list);
|
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
2017-01-14 22:02:35 +01:00
|
|
|
if (pstmt->commandType != CMD_SELECT)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("prepared statement is not a SELECT")));
|
2002-12-15 17:17:59 +01:00
|
|
|
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
/* Set appropriate eflags */
|
|
|
|
eflags = GetIntoRelEFlags(intoClause);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
/* And tell PortalRun whether to run to completion or not */
|
|
|
|
if (intoClause->skipData)
|
|
|
|
count = 0;
|
|
|
|
else
|
|
|
|
count = FETCH_ALL;
|
2007-03-13 01:33:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
/* Plain old EXECUTE */
|
|
|
|
eflags = 0;
|
|
|
|
count = FETCH_ALL;
|
2003-05-02 22:54:36 +02:00
|
|
|
}
|
2002-12-15 17:17:59 +01:00
|
|
|
|
2003-05-02 22:54:36 +02:00
|
|
|
PortalDefineQuery(portal,
|
2006-08-08 03:23:15 +02:00
|
|
|
NULL,
|
Adjust things so that the query_string of a cached plan and the sourceText of
a portal are never NULL, but reliably provide the source text of the query.
It turns out that there was only one place that was really taking a short-cut,
which was the 'EXECUTE' utility statement. That doesn't seem like a
sufficiently critical performance hotspot to justify not offering a guarantee
of validity of the portal source text. Fix it to copy the source text over
from the cached plan. Add Asserts in the places that set up cached plans and
portals to reject null source strings, and simplify a bunch of places that
formerly needed to guard against nulls.
There may be a few places that cons up statements for execution without
having any source text at all; I found one such in ConvertTriggerToFK().
It seems sufficient to inject a phony source string in such a case,
for instance
ProcessUtility((Node *) atstmt,
"(generated ALTER TABLE ADD FOREIGN KEY command)",
NULL, false, None_Receiver, NULL);
We should take a second look at the usage of debug_query_string,
particularly the recently added current_query() SQL function.
ITAGAKI Takahiro and Tom Lane
2008-07-18 22:26:06 +02:00
|
|
|
query_string,
|
2007-03-13 01:33:44 +01:00
|
|
|
entry->plansource->commandTag,
|
2003-05-02 22:54:36 +02:00
|
|
|
plan_list,
|
2007-03-13 01:33:44 +01:00
|
|
|
cplan);
|
2002-12-15 17:17:59 +01:00
|
|
|
|
2003-05-02 22:54:36 +02:00
|
|
|
/*
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
* Run the portal as appropriate.
|
2003-05-02 22:54:36 +02:00
|
|
|
*/
|
2012-11-26 21:55:43 +01:00
|
|
|
PortalStart(portal, paramLI, eflags, GetActiveSnapshot());
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2017-03-23 18:05:48 +01:00
|
|
|
(void) PortalRun(portal, count, false, true, dest, dest, completionTag);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2003-05-02 22:54:36 +02:00
|
|
|
PortalDrop(portal, false);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2003-02-03 00:46:38 +01:00
|
|
|
if (estate)
|
|
|
|
FreeExecutorState(estate);
|
2002-12-15 17:17:59 +01:00
|
|
|
|
|
|
|
/* No need to pfree other memory, MemoryContext will be reset */
|
2002-08-27 06:55:12 +02:00
|
|
|
}
|
|
|
|
|
2003-02-03 00:46:38 +01:00
|
|
|
/*
|
2007-03-13 01:33:44 +01:00
|
|
|
* EvaluateParams: evaluate a list of parameters.
|
|
|
|
*
|
|
|
|
* pstmt: statement we are getting parameters for.
|
|
|
|
* params: list of given parameter expressions (raw parser output!)
|
|
|
|
* queryString: source text for error messages.
|
|
|
|
* estate: executor state to use.
|
|
|
|
*
|
|
|
|
* Returns a filled-in ParamListInfo -- this can later be passed to
|
|
|
|
* CreateQueryDesc(), which allows the executor to make use of the parameters
|
|
|
|
* during query execution.
|
2003-02-03 00:46:38 +01:00
|
|
|
*/
|
|
|
|
static ParamListInfo
|
2007-03-13 01:33:44 +01:00
|
|
|
EvaluateParams(PreparedStatement *pstmt, List *params,
|
|
|
|
const char *queryString, EState *estate)
|
2003-02-03 00:46:38 +01:00
|
|
|
{
|
2007-03-13 01:33:44 +01:00
|
|
|
Oid *param_types = pstmt->plansource->param_types;
|
|
|
|
int num_params = pstmt->plansource->num_params;
|
|
|
|
int nparams = list_length(params);
|
|
|
|
ParseState *pstate;
|
2003-08-04 02:43:34 +02:00
|
|
|
ParamListInfo paramLI;
|
|
|
|
List *exprstates;
|
2007-03-13 01:33:44 +01:00
|
|
|
ListCell *l;
|
|
|
|
int i;
|
2003-02-03 00:46:38 +01:00
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
if (nparams != num_params)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
errmsg("wrong number of parameters for prepared statement \"%s\"",
|
|
|
|
pstmt->stmt_name),
|
2007-03-13 01:33:44 +01:00
|
|
|
errdetail("Expected %d parameters but got %d.",
|
|
|
|
num_params, nparams)));
|
|
|
|
|
|
|
|
/* Quick exit if no parameters */
|
|
|
|
if (num_params == 0)
|
2006-04-22 03:26:01 +02:00
|
|
|
return NULL;
|
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* We have to run parse analysis for the expressions. Since the parser is
|
|
|
|
* not cool about scribbling on its input, copy first.
|
2007-03-13 01:33:44 +01:00
|
|
|
*/
|
2017-03-09 21:18:59 +01:00
|
|
|
params = copyObject(params);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
|
|
|
pstate = make_parsestate(NULL);
|
|
|
|
pstate->p_sourcetext = queryString;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
foreach(l, params)
|
|
|
|
{
|
|
|
|
Node *expr = lfirst(l);
|
|
|
|
Oid expected_type_id = param_types[i];
|
|
|
|
Oid given_type_id;
|
|
|
|
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 17:35:33 +02:00
|
|
|
expr = transformExpr(pstate, expr, EXPR_KIND_EXECUTE_PARAMETER);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
|
|
|
given_type_id = exprType(expr);
|
|
|
|
|
|
|
|
expr = coerce_to_target_type(pstate, expr, given_type_id,
|
|
|
|
expected_type_id, -1,
|
|
|
|
COERCION_ASSIGNMENT,
|
2008-08-29 01:09:48 +02:00
|
|
|
COERCE_IMPLICIT_CAST,
|
|
|
|
-1);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
|
|
|
if (expr == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
|
|
|
errmsg("parameter $%d of type %s cannot be coerced to the expected type %s",
|
|
|
|
i + 1,
|
|
|
|
format_type_be(given_type_id),
|
|
|
|
format_type_be(expected_type_id)),
|
2019-11-29 09:10:17 +01:00
|
|
|
errhint("You will need to rewrite or cast the expression."),
|
|
|
|
parser_errposition(pstate, exprLocation(lfirst(l)))));
|
2007-03-13 01:33:44 +01:00
|
|
|
|
2011-03-20 01:29:08 +01:00
|
|
|
/* Take care of collations in the finished expression. */
|
|
|
|
assign_expr_collations(pstate, expr);
|
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
lfirst(l) = expr;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare the expressions for execution */
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
exprstates = ExecPrepareExprList(params, estate);
|
2003-02-03 00:46:38 +01:00
|
|
|
|
2019-03-14 13:30:09 +01:00
|
|
|
paramLI = makeParamList(num_params);
|
2003-02-03 00:46:38 +01:00
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
i = 0;
|
|
|
|
foreach(l, exprstates)
|
2003-02-03 00:46:38 +01:00
|
|
|
{
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
ExprState *n = (ExprState *) lfirst(l);
|
2006-04-22 03:26:01 +02:00
|
|
|
ParamExternData *prm = ¶mLI->params[i];
|
2003-02-03 00:46:38 +01:00
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
prm->ptype = param_types[i];
|
2011-09-16 06:42:53 +02:00
|
|
|
prm->pflags = PARAM_FLAG_CONST;
|
2006-04-22 03:26:01 +02:00
|
|
|
prm->value = ExecEvalExprSwitchContext(n,
|
|
|
|
GetPerTupleExprContext(estate),
|
2017-01-19 23:12:38 +01:00
|
|
|
&prm->isnull);
|
2003-02-03 00:46:38 +01:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return paramLI;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-27 06:55:12 +02:00
|
|
|
/*
|
|
|
|
* Initialize query hash table upon first use.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
InitQueryHashTable(void)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
HASHCTL hash_ctl;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
MemSet(&hash_ctl, 0, sizeof(hash_ctl));
|
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
hash_ctl.keysize = NAMEDATALEN;
|
|
|
|
hash_ctl.entrysize = sizeof(PreparedStatement);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
prepared_queries = hash_create("Prepared Queries",
|
|
|
|
32,
|
|
|
|
&hash_ctl,
|
|
|
|
HASH_ELEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store all the data pertaining to a query in the hash table using
|
2011-09-16 06:42:53 +02:00
|
|
|
* the specified key. The passed CachedPlanSource should be "unsaved"
|
|
|
|
* in case we get an error here; we'll save it once we've created the hash
|
|
|
|
* table entry.
|
2002-08-27 06:55:12 +02:00
|
|
|
*/
|
2003-05-05 02:44:56 +02:00
|
|
|
void
|
|
|
|
StorePreparedStatement(const char *stmt_name,
|
2011-09-16 06:42:53 +02:00
|
|
|
CachedPlanSource *plansource,
|
2006-01-08 08:00:27 +01:00
|
|
|
bool from_sql)
|
2002-08-27 06:55:12 +02:00
|
|
|
{
|
2003-05-05 02:44:56 +02:00
|
|
|
PreparedStatement *entry;
|
2011-09-16 06:42:53 +02:00
|
|
|
TimestampTz cur_ts = GetCurrentStatementStartTimestamp();
|
2002-09-04 22:31:48 +02:00
|
|
|
bool found;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
/* Initialize the hash table, if necessary */
|
|
|
|
if (!prepared_queries)
|
|
|
|
InitQueryHashTable();
|
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/* Add entry to hash table */
|
2003-05-05 02:44:56 +02:00
|
|
|
entry = (PreparedStatement *) hash_search(prepared_queries,
|
2006-09-27 20:40:10 +02:00
|
|
|
stmt_name,
|
2003-05-05 02:44:56 +02:00
|
|
|
HASH_ENTER,
|
|
|
|
&found);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2005-05-29 06:23:07 +02:00
|
|
|
/* Shouldn't get a duplicate entry */
|
|
|
|
if (found)
|
2011-09-16 06:42:53 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_PSTATEMENT),
|
|
|
|
errmsg("prepared statement \"%s\" already exists",
|
|
|
|
stmt_name)));
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
/* Fill in the hash table entry */
|
|
|
|
entry->plansource = plansource;
|
2007-02-20 18:32:18 +01:00
|
|
|
entry->from_sql = from_sql;
|
2011-09-16 06:42:53 +02:00
|
|
|
entry->prepare_time = cur_ts;
|
|
|
|
|
|
|
|
/* Now it's safe to move the CachedPlanSource to permanent memory */
|
|
|
|
SaveCachedPlan(plansource);
|
2002-08-27 06:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-02-03 00:46:38 +01:00
|
|
|
* Lookup an existing query in the hash table. If the query does not
|
2003-07-20 23:56:35 +02:00
|
|
|
* actually exist, throw ereport(ERROR) or return NULL per second parameter.
|
2007-03-13 01:33:44 +01:00
|
|
|
*
|
|
|
|
* Note: this does not force the referenced plancache entry to be valid,
|
|
|
|
* since not all callers care.
|
2002-08-27 06:55:12 +02:00
|
|
|
*/
|
2003-05-05 02:44:56 +02:00
|
|
|
PreparedStatement *
|
|
|
|
FetchPreparedStatement(const char *stmt_name, bool throwError)
|
2002-08-27 06:55:12 +02:00
|
|
|
{
|
2003-05-05 02:44:56 +02:00
|
|
|
PreparedStatement *entry;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the hash table hasn't been initialized, it can't be storing
|
|
|
|
* anything, therefore it couldn't possibly store our plan.
|
|
|
|
*/
|
2003-05-05 02:44:56 +02:00
|
|
|
if (prepared_queries)
|
|
|
|
entry = (PreparedStatement *) hash_search(prepared_queries,
|
2006-09-27 20:40:10 +02:00
|
|
|
stmt_name,
|
2003-05-05 02:44:56 +02:00
|
|
|
HASH_FIND,
|
|
|
|
NULL);
|
|
|
|
else
|
|
|
|
entry = NULL;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
if (!entry && throwError)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_PSTATEMENT),
|
|
|
|
errmsg("prepared statement \"%s\" does not exist",
|
|
|
|
stmt_name)));
|
2002-08-27 06:55:12 +02:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2003-05-06 23:51:42 +02:00
|
|
|
/*
|
|
|
|
* Given a prepared statement, determine the result tupledesc it will
|
|
|
|
* produce. Returns NULL if the execution will not return tuples.
|
|
|
|
*
|
|
|
|
* Note: the result is created or copied into current memory context.
|
|
|
|
*/
|
|
|
|
TupleDesc
|
2003-08-08 23:42:59 +02:00
|
|
|
FetchPreparedStatementResultDesc(PreparedStatement *stmt)
|
2003-05-06 23:51:42 +02:00
|
|
|
{
|
2007-03-13 01:33:44 +01:00
|
|
|
/*
|
|
|
|
* Since we don't allow prepared statements' result tupdescs to change,
|
2011-09-16 06:42:53 +02:00
|
|
|
* there's no need to worry about revalidating the cached plan here.
|
2007-03-13 01:33:44 +01:00
|
|
|
*/
|
|
|
|
Assert(stmt->plansource->fixed_result);
|
|
|
|
if (stmt->plansource->resultDesc)
|
|
|
|
return CreateTupleDescCopy(stmt->plansource->resultDesc);
|
|
|
|
else
|
|
|
|
return NULL;
|
2005-12-14 18:06:28 +01:00
|
|
|
}
|
|
|
|
|
2005-06-22 19:45:46 +02:00
|
|
|
/*
|
|
|
|
* Given a prepared statement that returns tuples, extract the query
|
2014-05-06 18:12:18 +02:00
|
|
|
* targetlist. Returns NIL if the statement doesn't have a determinable
|
2005-06-22 19:45:46 +02:00
|
|
|
* targetlist.
|
|
|
|
*
|
2007-03-13 01:33:44 +01:00
|
|
|
* Note: this is pretty ugly, but since it's only used in corner cases like
|
|
|
|
* Describe Statement on an EXECUTE command, we don't worry too much about
|
|
|
|
* efficiency.
|
2005-06-22 19:45:46 +02:00
|
|
|
*/
|
|
|
|
List *
|
|
|
|
FetchPreparedStatementTargetList(PreparedStatement *stmt)
|
|
|
|
{
|
2007-03-13 01:33:44 +01:00
|
|
|
List *tlist;
|
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/* Get the plan's primary targetlist */
|
2017-04-01 06:17:18 +02:00
|
|
|
tlist = CachedPlanGetTargetList(stmt->plansource, NULL);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/* Copy into caller's context in case plan gets invalidated */
|
2017-03-09 21:18:59 +01:00
|
|
|
return copyObject(tlist);
|
2005-06-22 19:45:46 +02:00
|
|
|
}
|
|
|
|
|
2002-08-27 06:55:12 +02:00
|
|
|
/*
|
|
|
|
* Implements the 'DEALLOCATE' utility statement: deletes the
|
|
|
|
* specified plan from storage.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
DeallocateQuery(DeallocateStmt *stmt)
|
|
|
|
{
|
2007-04-12 08:53:49 +02:00
|
|
|
if (stmt->name)
|
|
|
|
DropPreparedStatement(stmt->name, true);
|
|
|
|
else
|
|
|
|
DropAllPreparedStatements();
|
2003-05-05 02:44:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal version of DEALLOCATE
|
|
|
|
*
|
|
|
|
* If showError is false, dropping a nonexistent statement is a no-op.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
DropPreparedStatement(const char *stmt_name, bool showError)
|
|
|
|
{
|
|
|
|
PreparedStatement *entry;
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
/* Find the query's hash table entry; raise error if wanted */
|
|
|
|
entry = FetchPreparedStatement(stmt_name, showError);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
if (entry)
|
|
|
|
{
|
2007-03-13 01:33:44 +01:00
|
|
|
/* Release the plancache entry */
|
|
|
|
DropCachedPlan(entry->plansource);
|
2002-08-27 06:55:12 +02:00
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
/* Now we can remove the hash table entry */
|
|
|
|
hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
|
|
|
|
}
|
2003-02-03 00:46:38 +01:00
|
|
|
}
|
|
|
|
|
2007-04-12 08:53:49 +02:00
|
|
|
/*
|
|
|
|
* Drop all cached statements.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
DropAllPreparedStatements(void)
|
|
|
|
{
|
|
|
|
HASH_SEQ_STATUS seq;
|
|
|
|
PreparedStatement *entry;
|
|
|
|
|
|
|
|
/* nothing cached */
|
|
|
|
if (!prepared_queries)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* walk over cache */
|
|
|
|
hash_seq_init(&seq, prepared_queries);
|
|
|
|
while ((entry = hash_seq_search(&seq)) != NULL)
|
|
|
|
{
|
|
|
|
/* Release the plancache entry */
|
|
|
|
DropCachedPlan(entry->plansource);
|
|
|
|
|
|
|
|
/* Now we can remove the hash table entry */
|
|
|
|
hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-03 00:46:38 +01:00
|
|
|
/*
|
|
|
|
* Implements the 'EXPLAIN EXECUTE' utility statement.
|
2009-01-02 21:42:00 +01:00
|
|
|
*
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
* "into" is NULL unless we are doing EXPLAIN CREATE TABLE AS EXECUTE,
|
|
|
|
* in which case executing the query should result in creating that table.
|
|
|
|
*
|
2009-01-02 21:42:00 +01:00
|
|
|
* Note: the passed-in queryString is that of the EXPLAIN EXECUTE,
|
|
|
|
* not the original PREPARE; we get the latter string from the plancache.
|
2003-02-03 00:46:38 +01:00
|
|
|
*/
|
|
|
|
void
|
Restructure SELECT INTO's parsetree representation into CreateTableAsStmt.
Making this operation look like a utility statement seems generally a good
idea, and particularly so in light of the desire to provide command
triggers for utility statements. The original choice of representing it as
SELECT with an IntoClause appendage had metastasized into rather a lot of
places, unfortunately, so that this patch is a great deal more complicated
than one might at first expect.
In particular, keeping EXPLAIN working for SELECT INTO and CREATE TABLE AS
subcommands required restructuring some EXPLAIN-related APIs. Add-on code
that calls ExplainOnePlan or ExplainOneUtility, or uses
ExplainOneQuery_hook, will need adjustment.
Also, the cases PREPARE ... SELECT INTO and CREATE RULE ... SELECT INTO,
which formerly were accepted though undocumented, are no longer accepted.
The PREPARE case can be replaced with use of CREATE TABLE AS EXECUTE.
The CREATE RULE case doesn't seem to have much real-world use (since the
rule would work only once before failing with "table already exists"),
so we'll not bother with that one.
Both SELECT INTO and CREATE TABLE AS still return a command tag of
"SELECT nnnn". There was some discussion of returning "CREATE TABLE nnnn",
but for the moment backwards compatibility wins the day.
Andres Freund and Tom Lane
2012-03-20 02:37:19 +01:00
|
|
|
ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
|
2017-04-01 06:17:18 +02:00
|
|
|
const char *queryString, ParamListInfo params,
|
|
|
|
QueryEnvironment *queryEnv)
|
2003-02-03 00:46:38 +01:00
|
|
|
{
|
2003-05-05 02:44:56 +02:00
|
|
|
PreparedStatement *entry;
|
2009-01-02 21:42:00 +01:00
|
|
|
const char *query_string;
|
2007-03-13 01:33:44 +01:00
|
|
|
CachedPlan *cplan;
|
2007-02-20 18:32:18 +01:00
|
|
|
List *plan_list;
|
|
|
|
ListCell *p;
|
2003-02-03 00:46:38 +01:00
|
|
|
ParamListInfo paramLI = NULL;
|
|
|
|
EState *estate = NULL;
|
2017-03-08 21:14:03 +01:00
|
|
|
instr_time planstart;
|
|
|
|
instr_time planduration;
|
|
|
|
|
|
|
|
INSTR_TIME_SET_CURRENT(planstart);
|
2003-02-03 00:46:38 +01:00
|
|
|
|
|
|
|
/* Look it up in the hash table */
|
2003-05-05 02:44:56 +02:00
|
|
|
entry = FetchPreparedStatement(execstmt->name, true);
|
2003-02-03 00:46:38 +01:00
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/* Shouldn't find a non-fixed-result cached plan */
|
2007-03-13 01:33:44 +01:00
|
|
|
if (!entry->plansource->fixed_result)
|
|
|
|
elog(ERROR, "EXPLAIN EXECUTE does not support variable-result cached plans");
|
|
|
|
|
2009-01-02 21:42:00 +01:00
|
|
|
query_string = entry->plansource->query_string;
|
|
|
|
|
2003-02-03 00:46:38 +01:00
|
|
|
/* Evaluate parameters, if any */
|
2007-03-13 01:33:44 +01:00
|
|
|
if (entry->plansource->num_params)
|
2003-02-03 00:46:38 +01:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Need an EState to evaluate parameters; must not delete it till end
|
2014-05-06 18:12:18 +02:00
|
|
|
* of query, in case parameters are pass-by-reference. Note that the
|
2011-09-16 06:42:53 +02:00
|
|
|
* passed-in "params" could possibly be referenced in the parameter
|
|
|
|
* expressions.
|
2003-02-03 00:46:38 +01:00
|
|
|
*/
|
|
|
|
estate = CreateExecutorState();
|
2005-11-29 02:25:50 +01:00
|
|
|
estate->es_param_list_info = params;
|
2007-03-13 01:33:44 +01:00
|
|
|
paramLI = EvaluateParams(entry, execstmt->params,
|
|
|
|
queryString, estate);
|
2003-02-03 00:46:38 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
/* Replan if needed, and acquire a transient refcount */
|
2017-04-01 06:17:18 +02:00
|
|
|
cplan = GetCachedPlan(entry->plansource, paramLI, true, queryEnv);
|
2011-09-16 06:42:53 +02:00
|
|
|
|
2017-03-08 21:14:03 +01:00
|
|
|
INSTR_TIME_SET_CURRENT(planduration);
|
|
|
|
INSTR_TIME_SUBTRACT(planduration, planstart);
|
|
|
|
|
2011-09-16 06:42:53 +02:00
|
|
|
plan_list = cplan->stmt_list;
|
|
|
|
|
2003-02-03 00:46:38 +01:00
|
|
|
/* Explain each query */
|
2007-02-20 18:32:18 +01:00
|
|
|
foreach(p, plan_list)
|
2003-02-03 00:46:38 +01:00
|
|
|
{
|
Improve castNode notation by introducing list-extraction-specific variants.
This extends the castNode() notation introduced by commit 5bcab1114 to
provide, in one step, extraction of a list cell's pointer and coercion to
a concrete node type. For example, "lfirst_node(Foo, lc)" is the same
as "castNode(Foo, lfirst(lc))". Almost half of the uses of castNode
that have appeared so far include a list extraction call, so this is
pretty widely useful, and it saves a few more keystrokes compared to the
old way.
As with the previous patch, back-patch the addition of these macros to
pg_list.h, so that the notation will be available when back-patching.
Patch by me, after an idea of Andrew Gierth's.
Discussion: https://postgr.es/m/14197.1491841216@sss.pgh.pa.us
2017-04-10 19:51:29 +02:00
|
|
|
PlannedStmt *pstmt = lfirst_node(PlannedStmt, p);
|
2003-02-03 00:46:38 +01:00
|
|
|
|
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
2017-01-14 22:02:35 +01:00
|
|
|
if (pstmt->commandType != CMD_UTILITY)
|
2017-04-01 06:17:18 +02:00
|
|
|
ExplainOnePlan(pstmt, into, es, query_string, paramLI, queryEnv,
|
|
|
|
&planduration);
|
2007-03-13 01:33:44 +01:00
|
|
|
else
|
2017-04-01 06:17:18 +02:00
|
|
|
ExplainOneUtility(pstmt->utilityStmt, into, es, query_string,
|
|
|
|
paramLI, queryEnv);
|
2003-02-03 00:46:38 +01:00
|
|
|
|
|
|
|
/* No need for CommandCounterIncrement, as ExplainOnePlan did it */
|
|
|
|
|
2009-08-10 07:46:50 +02:00
|
|
|
/* Separate plans with an appropriate separator */
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
if (lnext(plan_list, p) != NULL)
|
2009-08-10 07:46:50 +02:00
|
|
|
ExplainSeparatePlans(es);
|
2003-02-03 00:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (estate)
|
|
|
|
FreeExecutorState(estate);
|
2007-03-13 01:33:44 +01:00
|
|
|
|
|
|
|
ReleaseCachedPlan(cplan, true);
|
2002-08-27 06:55:12 +02:00
|
|
|
}
|
2006-01-08 08:00:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This set returning function reads all the prepared statements and
|
2006-01-16 19:15:31 +01:00
|
|
|
* returns a set of (name, statement, prepare_time, param_types, from_sql).
|
2006-01-08 08:00:27 +01:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_prepared_statement(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-04-27 01:24:46 +02:00
|
|
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
Tuplestorestate *tupstore;
|
|
|
|
MemoryContext per_query_ctx;
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
/* check to see if caller supports us returning a tuplestore */
|
|
|
|
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("set-valued function called in context that cannot accept a set")));
|
|
|
|
if (!(rsinfo->allowedModes & SFRM_Materialize))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2019-12-24 16:37:13 +01:00
|
|
|
errmsg("materialize mode required, but it is not allowed in this context")));
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2007-04-27 01:24:46 +02:00
|
|
|
/* need to build tuplestore in query context */
|
|
|
|
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
|
|
|
|
oldcontext = MemoryContextSwitchTo(per_query_ctx);
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2007-04-27 01:24:46 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* build tupdesc for result tuples. This must match the definition of the
|
|
|
|
* pg_prepared_statements view in system_views.sql
|
2007-04-27 01:24:46 +02:00
|
|
|
*/
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
tupdesc = CreateTemplateTupleDesc(5);
|
2007-04-27 01:24:46 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
|
|
|
|
TEXTOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "statement",
|
|
|
|
TEXTOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "prepare_time",
|
|
|
|
TIMESTAMPTZOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "parameter_types",
|
|
|
|
REGTYPEARRAYOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "from_sql",
|
|
|
|
BOOLOID, -1, 0);
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2007-04-27 01:24:46 +02:00
|
|
|
/*
|
|
|
|
* We put all the tuples into a tuplestore in one scan of the hashtable.
|
|
|
|
* This avoids any issue of the hashtable possibly changing between calls.
|
|
|
|
*/
|
2008-10-29 01:00:39 +01:00
|
|
|
tupstore =
|
|
|
|
tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
|
|
|
|
false, work_mem);
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2009-12-29 18:40:59 +01:00
|
|
|
/* generate junk in short-term context */
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
|
2007-04-27 01:24:46 +02:00
|
|
|
/* hash table might be uninitialized */
|
|
|
|
if (prepared_queries)
|
|
|
|
{
|
|
|
|
HASH_SEQ_STATUS hash_seq;
|
|
|
|
PreparedStatement *prep_stmt;
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2007-04-27 01:24:46 +02:00
|
|
|
hash_seq_init(&hash_seq, prepared_queries);
|
|
|
|
while ((prep_stmt = hash_seq_search(&hash_seq)) != NULL)
|
|
|
|
{
|
|
|
|
Datum values[5];
|
|
|
|
bool nulls[5];
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2007-04-27 01:24:46 +02:00
|
|
|
MemSet(nulls, 0, sizeof(nulls));
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
values[0] = CStringGetTextDatum(prep_stmt->stmt_name);
|
Adjust things so that the query_string of a cached plan and the sourceText of
a portal are never NULL, but reliably provide the source text of the query.
It turns out that there was only one place that was really taking a short-cut,
which was the 'EXECUTE' utility statement. That doesn't seem like a
sufficiently critical performance hotspot to justify not offering a guarantee
of validity of the portal source text. Fix it to copy the source text over
from the cached plan. Add Asserts in the places that set up cached plans and
portals to reject null source strings, and simplify a bunch of places that
formerly needed to guard against nulls.
There may be a few places that cons up statements for execution without
having any source text at all; I found one such in ConvertTriggerToFK().
It seems sufficient to inject a phony source string in such a case,
for instance
ProcessUtility((Node *) atstmt,
"(generated ALTER TABLE ADD FOREIGN KEY command)",
NULL, false, None_Receiver, NULL);
We should take a second look at the usage of debug_query_string,
particularly the recently added current_query() SQL function.
ITAGAKI Takahiro and Tom Lane
2008-07-18 22:26:06 +02:00
|
|
|
values[1] = CStringGetTextDatum(prep_stmt->plansource->query_string);
|
2007-04-27 01:24:46 +02:00
|
|
|
values[2] = TimestampTzGetDatum(prep_stmt->prepare_time);
|
|
|
|
values[3] = build_regtype_array(prep_stmt->plansource->param_types,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
prep_stmt->plansource->num_params);
|
2007-04-27 01:24:46 +02:00
|
|
|
values[4] = BoolGetDatum(prep_stmt->from_sql);
|
|
|
|
|
2008-03-25 20:26:54 +01:00
|
|
|
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
|
2007-04-27 01:24:46 +02:00
|
|
|
}
|
2006-01-08 08:00:27 +01:00
|
|
|
}
|
|
|
|
|
2007-04-27 01:24:46 +02:00
|
|
|
/* clean up and return the tuplestore */
|
|
|
|
tuplestore_donestoring(tupstore);
|
|
|
|
|
|
|
|
rsinfo->returnMode = SFRM_Materialize;
|
|
|
|
rsinfo->setResult = tupstore;
|
|
|
|
rsinfo->setDesc = tupdesc;
|
|
|
|
|
|
|
|
return (Datum) 0;
|
2006-01-08 08:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-03-13 01:33:44 +01:00
|
|
|
* This utility function takes a C array of Oids, and returns a Datum
|
|
|
|
* pointing to a one-dimensional Postgres array of regtypes. An empty
|
|
|
|
* array is returned as a zero-element array, not NULL.
|
2006-01-08 08:00:27 +01:00
|
|
|
*/
|
|
|
|
static Datum
|
2007-03-13 01:33:44 +01:00
|
|
|
build_regtype_array(Oid *param_types, int num_params)
|
2006-01-08 08:00:27 +01:00
|
|
|
{
|
2006-01-16 19:15:31 +01:00
|
|
|
Datum *tmp_ary;
|
|
|
|
ArrayType *result;
|
2007-03-13 01:33:44 +01:00
|
|
|
int i;
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
tmp_ary = (Datum *) palloc(num_params * sizeof(Datum));
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2007-03-13 01:33:44 +01:00
|
|
|
for (i = 0; i < num_params; i++)
|
|
|
|
tmp_ary[i] = ObjectIdGetDatum(param_types[i]);
|
2006-01-08 08:00:27 +01:00
|
|
|
|
2006-01-16 19:15:31 +01:00
|
|
|
/* XXX: this hardcodes assumptions about the regtype type */
|
2007-03-13 01:33:44 +01:00
|
|
|
result = construct_array(tmp_ary, num_params, REGTYPEOID, 4, true, 'i');
|
2006-01-16 19:15:31 +01:00
|
|
|
return PointerGetDatum(result);
|
2006-01-08 08:00:27 +01:00
|
|
|
}
|