2009-10-31 02:41:31 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* parse_param.c
|
|
|
|
* handle parameters in parser
|
|
|
|
*
|
|
|
|
* This code covers two cases that are used within the core backend:
|
|
|
|
* * a fixed list of parameters with known types
|
|
|
|
* * an expandable list of parameters whose types can optionally
|
|
|
|
* be determined from context
|
|
|
|
* In both cases, only explicit $n references (ParamRef nodes) are supported.
|
|
|
|
*
|
|
|
|
* Note that other approaches to parameters are possible using the parser
|
|
|
|
* hooks defined in ParseState.
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
2009-10-31 02:41:31 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/parser/parse_param.c
|
2009-10-31 02:41:31 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "nodes/nodeFuncs.h"
|
|
|
|
#include "parser/parse_param.h"
|
|
|
|
#include "utils/builtins.h"
|
2011-02-08 22:04:18 +01:00
|
|
|
#include "utils/lsyscache.h"
|
2009-10-31 02:41:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct FixedParamState
|
|
|
|
{
|
|
|
|
Oid *paramTypes; /* array of parameter type OIDs */
|
|
|
|
int numParams; /* number of array entries */
|
|
|
|
} FixedParamState;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the varparams case, the caller-supplied OID array (if any) can be
|
|
|
|
* re-palloc'd larger at need. A zero array entry means that parameter number
|
|
|
|
* hasn't been seen, while UNKNOWNOID means the parameter has been used but
|
|
|
|
* its type is not yet known.
|
|
|
|
*/
|
|
|
|
typedef struct VarParamState
|
|
|
|
{
|
|
|
|
Oid **paramTypes; /* array of parameter type OIDs */
|
|
|
|
int *numParams; /* number of array entries */
|
|
|
|
} VarParamState;
|
|
|
|
|
|
|
|
static Node *fixed_paramref_hook(ParseState *pstate, ParamRef *pref);
|
|
|
|
static Node *variable_paramref_hook(ParseState *pstate, ParamRef *pref);
|
|
|
|
static Node *variable_coerce_param_hook(ParseState *pstate, Param *param,
|
2010-02-26 03:01:40 +01:00
|
|
|
Oid targetTypeId, int32 targetTypeMod,
|
|
|
|
int location);
|
2009-10-31 02:41:31 +01:00
|
|
|
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate);
|
Clean up the mess around EXPLAIN and materialized views.
Revert the matview-related changes in explain.c's API, as per recent
complaint from Robert Haas. The reason for these appears to have been
principally some ill-considered choices around having intorel_startup do
what ought to be parse-time checking, plus a poor arrangement for passing
it the view parsetree it needs to store into pg_rewrite when creating a
materialized view. Do the latter by having parse analysis stick a copy
into the IntoClause, instead of doing it at runtime. (On the whole,
I seriously question the choice to represent CREATE MATERIALIZED VIEW as a
variant of SELECT INTO/CREATE TABLE AS, because that means injecting even
more complexity into what was already a horrid legacy kluge. However,
I didn't go so far as to rethink that choice ... yet.)
I also moved several error checks into matview parse analysis, and
made the check for external Params in a matview more accurate.
In passing, clean things up a bit more around interpretOidsOption(),
and fix things so that we can use that to force no-oids for views,
sequences, etc, thereby eliminating the need to cons up "oids = false"
options when creating them.
catversion bump due to change in IntoClause. (I wonder though if we
really need readfuncs/outfuncs support for IntoClause anymore.)
2013-04-13 01:25:20 +02:00
|
|
|
static bool query_contains_extern_params_walker(Node *node, void *context);
|
2009-10-31 02:41:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up to process a query containing references to fixed parameters.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
parse_fixed_parameters(ParseState *pstate,
|
|
|
|
Oid *paramTypes, int numParams)
|
|
|
|
{
|
|
|
|
FixedParamState *parstate = palloc(sizeof(FixedParamState));
|
|
|
|
|
|
|
|
parstate->paramTypes = paramTypes;
|
|
|
|
parstate->numParams = numParams;
|
|
|
|
pstate->p_ref_hook_state = (void *) parstate;
|
|
|
|
pstate->p_paramref_hook = fixed_paramref_hook;
|
2010-08-19 18:54:43 +02:00
|
|
|
/* no need to use p_coerce_param_hook */
|
2009-10-31 02:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up to process a query containing references to variable parameters.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
parse_variable_parameters(ParseState *pstate,
|
|
|
|
Oid **paramTypes, int *numParams)
|
|
|
|
{
|
|
|
|
VarParamState *parstate = palloc(sizeof(VarParamState));
|
|
|
|
|
|
|
|
parstate->paramTypes = paramTypes;
|
|
|
|
parstate->numParams = numParams;
|
|
|
|
pstate->p_ref_hook_state = (void *) parstate;
|
|
|
|
pstate->p_paramref_hook = variable_paramref_hook;
|
|
|
|
pstate->p_coerce_param_hook = variable_coerce_param_hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Transform a ParamRef using fixed parameter types.
|
|
|
|
*/
|
|
|
|
static Node *
|
|
|
|
fixed_paramref_hook(ParseState *pstate, ParamRef *pref)
|
|
|
|
{
|
|
|
|
FixedParamState *parstate = (FixedParamState *) pstate->p_ref_hook_state;
|
|
|
|
int paramno = pref->number;
|
|
|
|
Param *param;
|
|
|
|
|
2010-01-13 02:17:07 +01:00
|
|
|
/* Check parameter number is valid */
|
|
|
|
if (paramno <= 0 || paramno > parstate->numParams ||
|
|
|
|
!OidIsValid(parstate->paramTypes[paramno - 1]))
|
2009-10-31 02:41:31 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_PARAMETER),
|
|
|
|
errmsg("there is no parameter $%d", paramno),
|
|
|
|
parser_errposition(pstate, pref->location)));
|
|
|
|
|
|
|
|
param = makeNode(Param);
|
|
|
|
param->paramkind = PARAM_EXTERN;
|
|
|
|
param->paramid = paramno;
|
|
|
|
param->paramtype = parstate->paramTypes[paramno - 1];
|
|
|
|
param->paramtypmod = -1;
|
2011-03-20 01:29:08 +01:00
|
|
|
param->paramcollid = get_typcollation(param->paramtype);
|
2009-10-31 02:41:31 +01:00
|
|
|
param->location = pref->location;
|
|
|
|
|
|
|
|
return (Node *) param;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Transform a ParamRef using variable parameter types.
|
|
|
|
*
|
|
|
|
* The only difference here is we must enlarge the parameter type array
|
|
|
|
* as needed.
|
|
|
|
*/
|
|
|
|
static Node *
|
|
|
|
variable_paramref_hook(ParseState *pstate, ParamRef *pref)
|
|
|
|
{
|
|
|
|
VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
|
|
|
|
int paramno = pref->number;
|
|
|
|
Oid *pptype;
|
|
|
|
Param *param;
|
|
|
|
|
|
|
|
/* Check parameter number is in range */
|
|
|
|
if (paramno <= 0 || paramno > INT_MAX / sizeof(Oid))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_PARAMETER),
|
|
|
|
errmsg("there is no parameter $%d", paramno),
|
|
|
|
parser_errposition(pstate, pref->location)));
|
|
|
|
if (paramno > *parstate->numParams)
|
|
|
|
{
|
|
|
|
/* Need to enlarge param array */
|
|
|
|
if (*parstate->paramTypes)
|
|
|
|
*parstate->paramTypes = (Oid *) repalloc(*parstate->paramTypes,
|
|
|
|
paramno * sizeof(Oid));
|
|
|
|
else
|
|
|
|
*parstate->paramTypes = (Oid *) palloc(paramno * sizeof(Oid));
|
|
|
|
/* Zero out the previously-unreferenced slots */
|
|
|
|
MemSet(*parstate->paramTypes + *parstate->numParams,
|
|
|
|
0,
|
|
|
|
(paramno - *parstate->numParams) * sizeof(Oid));
|
|
|
|
*parstate->numParams = paramno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate param's slot in array */
|
|
|
|
pptype = &(*parstate->paramTypes)[paramno - 1];
|
|
|
|
|
|
|
|
/* If not seen before, initialize to UNKNOWN type */
|
|
|
|
if (*pptype == InvalidOid)
|
|
|
|
*pptype = UNKNOWNOID;
|
|
|
|
|
|
|
|
param = makeNode(Param);
|
|
|
|
param->paramkind = PARAM_EXTERN;
|
|
|
|
param->paramid = paramno;
|
|
|
|
param->paramtype = *pptype;
|
|
|
|
param->paramtypmod = -1;
|
2011-03-20 01:29:08 +01:00
|
|
|
param->paramcollid = get_typcollation(param->paramtype);
|
2009-10-31 02:41:31 +01:00
|
|
|
param->location = pref->location;
|
|
|
|
|
|
|
|
return (Node *) param;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Coerce a Param to a query-requested datatype, in the varparams case.
|
|
|
|
*/
|
|
|
|
static Node *
|
|
|
|
variable_coerce_param_hook(ParseState *pstate, Param *param,
|
|
|
|
Oid targetTypeId, int32 targetTypeMod,
|
|
|
|
int location)
|
|
|
|
{
|
|
|
|
if (param->paramkind == PARAM_EXTERN && param->paramtype == UNKNOWNOID)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Input is a Param of previously undetermined type, and we want to
|
|
|
|
* update our knowledge of the Param's type.
|
|
|
|
*/
|
|
|
|
VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
|
|
|
|
Oid *paramTypes = *parstate->paramTypes;
|
|
|
|
int paramno = param->paramid;
|
|
|
|
|
|
|
|
if (paramno <= 0 || /* shouldn't happen, but... */
|
|
|
|
paramno > *parstate->numParams)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_PARAMETER),
|
|
|
|
errmsg("there is no parameter $%d", paramno),
|
|
|
|
parser_errposition(pstate, param->location)));
|
|
|
|
|
|
|
|
if (paramTypes[paramno - 1] == UNKNOWNOID)
|
|
|
|
{
|
|
|
|
/* We've successfully resolved the type */
|
|
|
|
paramTypes[paramno - 1] = targetTypeId;
|
|
|
|
}
|
|
|
|
else if (paramTypes[paramno - 1] == targetTypeId)
|
|
|
|
{
|
|
|
|
/* We previously resolved the type, and it matches */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-14 16:38:30 +01:00
|
|
|
/* Oops */
|
2009-10-31 02:41:31 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_AMBIGUOUS_PARAMETER),
|
|
|
|
errmsg("inconsistent types deduced for parameter $%d",
|
|
|
|
paramno),
|
|
|
|
errdetail("%s versus %s",
|
|
|
|
format_type_be(paramTypes[paramno - 1]),
|
|
|
|
format_type_be(targetTypeId)),
|
|
|
|
parser_errposition(pstate, param->location)));
|
|
|
|
}
|
|
|
|
|
|
|
|
param->paramtype = targetTypeId;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: it is tempting here to set the Param's paramtypmod to
|
|
|
|
* targetTypeMod, but that is probably unwise because we have no
|
|
|
|
* infrastructure that enforces that the value delivered for a Param
|
|
|
|
* will match any particular typmod. Leaving it -1 ensures that a
|
|
|
|
* run-time length check/coercion will occur if needed.
|
|
|
|
*/
|
|
|
|
param->paramtypmod = -1;
|
|
|
|
|
2011-03-25 01:30:14 +01:00
|
|
|
/*
|
|
|
|
* This module always sets a Param's collation to be the default for
|
2011-04-10 17:42:00 +02:00
|
|
|
* its datatype. If that's not what you want, you should be using the
|
|
|
|
* more general parser substitution hooks.
|
2011-03-25 01:30:14 +01:00
|
|
|
*/
|
2011-03-20 01:29:08 +01:00
|
|
|
param->paramcollid = get_typcollation(param->paramtype);
|
|
|
|
|
2009-10-31 02:41:31 +01:00
|
|
|
/* Use the leftmost of the param's and coercion's locations */
|
|
|
|
if (location >= 0 &&
|
|
|
|
(param->location < 0 || location < param->location))
|
|
|
|
param->location = location;
|
|
|
|
|
|
|
|
return (Node *) param;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Else signal to proceed with normal coercion */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for consistent assignment of variable parameters after completion
|
|
|
|
* of parsing with parse_variable_parameters.
|
|
|
|
*
|
|
|
|
* Note: this code intentionally does not check that all parameter positions
|
2014-05-06 18:12:18 +02:00
|
|
|
* were used, nor that all got non-UNKNOWN types assigned. Caller of parser
|
2009-10-31 02:41:31 +01:00
|
|
|
* should enforce that if it's important.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
check_variable_parameters(ParseState *pstate, Query *query)
|
|
|
|
{
|
|
|
|
VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
|
|
|
|
|
|
|
|
/* If numParams is zero then no Params were generated, so no work */
|
|
|
|
if (*parstate->numParams > 0)
|
|
|
|
(void) query_tree_walker(query,
|
|
|
|
check_parameter_resolution_walker,
|
|
|
|
(void *) pstate, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse a fully-analyzed tree to verify that parameter symbols
|
|
|
|
* match their types. We need this because some Params might still
|
|
|
|
* be UNKNOWN, if there wasn't anything to force their coercion,
|
|
|
|
* and yet other instances seen later might have gotten coerced.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
check_parameter_resolution_walker(Node *node, ParseState *pstate)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
if (IsA(node, Param))
|
|
|
|
{
|
|
|
|
Param *param = (Param *) node;
|
|
|
|
|
|
|
|
if (param->paramkind == PARAM_EXTERN)
|
|
|
|
{
|
|
|
|
VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
|
|
|
|
int paramno = param->paramid;
|
|
|
|
|
|
|
|
if (paramno <= 0 || /* shouldn't happen, but... */
|
|
|
|
paramno > *parstate->numParams)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_PARAMETER),
|
|
|
|
errmsg("there is no parameter $%d", paramno),
|
|
|
|
parser_errposition(pstate, param->location)));
|
|
|
|
|
|
|
|
if (param->paramtype != (*parstate->paramTypes)[paramno - 1])
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_AMBIGUOUS_PARAMETER),
|
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("could not determine data type of parameter $%d",
|
|
|
|
paramno),
|
2009-10-31 02:41:31 +01:00
|
|
|
parser_errposition(pstate, param->location)));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsA(node, Query))
|
|
|
|
{
|
|
|
|
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
|
|
|
|
return query_tree_walker((Query *) node,
|
|
|
|
check_parameter_resolution_walker,
|
|
|
|
(void *) pstate, 0);
|
|
|
|
}
|
|
|
|
return expression_tree_walker(node, check_parameter_resolution_walker,
|
|
|
|
(void *) pstate);
|
|
|
|
}
|
Clean up the mess around EXPLAIN and materialized views.
Revert the matview-related changes in explain.c's API, as per recent
complaint from Robert Haas. The reason for these appears to have been
principally some ill-considered choices around having intorel_startup do
what ought to be parse-time checking, plus a poor arrangement for passing
it the view parsetree it needs to store into pg_rewrite when creating a
materialized view. Do the latter by having parse analysis stick a copy
into the IntoClause, instead of doing it at runtime. (On the whole,
I seriously question the choice to represent CREATE MATERIALIZED VIEW as a
variant of SELECT INTO/CREATE TABLE AS, because that means injecting even
more complexity into what was already a horrid legacy kluge. However,
I didn't go so far as to rethink that choice ... yet.)
I also moved several error checks into matview parse analysis, and
made the check for external Params in a matview more accurate.
In passing, clean things up a bit more around interpretOidsOption(),
and fix things so that we can use that to force no-oids for views,
sequences, etc, thereby eliminating the need to cons up "oids = false"
options when creating them.
catversion bump due to change in IntoClause. (I wonder though if we
really need readfuncs/outfuncs support for IntoClause anymore.)
2013-04-13 01:25:20 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if a fully-parsed query tree contains any PARAM_EXTERN Params.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
query_contains_extern_params(Query *query)
|
|
|
|
{
|
|
|
|
return query_tree_walker(query,
|
|
|
|
query_contains_extern_params_walker,
|
|
|
|
NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
query_contains_extern_params_walker(Node *node, void *context)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
if (IsA(node, Param))
|
|
|
|
{
|
|
|
|
Param *param = (Param *) node;
|
|
|
|
|
|
|
|
if (param->paramkind == PARAM_EXTERN)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsA(node, Query))
|
|
|
|
{
|
|
|
|
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
|
|
|
|
return query_tree_walker((Query *) node,
|
|
|
|
query_contains_extern_params_walker,
|
|
|
|
context, 0);
|
|
|
|
}
|
|
|
|
return expression_tree_walker(node, query_contains_extern_params_walker,
|
|
|
|
context);
|
|
|
|
}
|