1/*-------------------------------------------------------------------------
4 * handle parameters in parser
6 * This code covers two cases that are used within the core backend:
7 * * a fixed list of parameters with known types
8 * * an expandable list of parameters whose types can optionally
9 * be determined from context
10 * In both cases, only explicit $n references (ParamRef nodes) are supported.
12 * Note that other approaches to parameters are possible using the parser
13 * hooks defined in ParseState.
15 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
16 * Portions Copyright (c) 1994, Regents of the University of California
20 * src/backend/parser/parse_param.c
22 *-------------------------------------------------------------------------
44 * In the varparams case, the caller-supplied OID array (if any) can be
45 * re-palloc'd larger at need. A zero array entry means that parameter number
46 * hasn't been seen, while UNKNOWNOID means the parameter has been used but
47 * its type is not yet known.
58 Oid targetTypeId,
int32 targetTypeMod,
65 * Set up to process a query containing references to fixed parameters.
69 const Oid *paramTypes,
int numParams)
77 /* no need to use p_coerce_param_hook */
81 * Set up to process a query containing references to variable parameters.
85 Oid **paramTypes,
int *numParams)
97 * Transform a ParamRef using fixed parameter types.
103 int paramno = pref->
number;
106 /* Check parameter number is valid */
107 if (paramno <= 0 || paramno > parstate->
numParams ||
110 (
errcode(ERRCODE_UNDEFINED_PARAMETER),
111 errmsg(
"there is no parameter $%d", paramno),
116 param->paramid = paramno;
117 param->paramtype = parstate->
paramTypes[paramno - 1];
118 param->paramtypmod = -1;
122 return (
Node *) param;
126 * Transform a ParamRef using variable parameter types.
128 * The only difference here is we must enlarge the parameter type array
135 int paramno = pref->
number;
139 /* Check parameter number is in range */
142 (
errcode(ERRCODE_UNDEFINED_PARAMETER),
143 errmsg(
"there is no parameter $%d", paramno),
147 /* Need to enlarge param array */
156 /* Locate param's slot in array */
157 pptype = &(*parstate->
paramTypes)[paramno - 1];
159 /* If not seen before, initialize to UNKNOWN type */
161 *pptype = UNKNOWNOID;
164 * If the argument is of type void and it's procedure call, interpret it
165 * as unknown. This allows the JDBC driver to not have to distinguish
166 * function and procedure calls. See also another component of this hack
167 * in ParseFuncOrColumn().
170 *pptype = UNKNOWNOID;
174 param->paramid = paramno;
175 param->paramtype = *pptype;
176 param->paramtypmod = -1;
180 return (
Node *) param;
184 * Coerce a Param to a query-requested datatype, in the varparams case.
188 Oid targetTypeId,
int32 targetTypeMod,
194 * Input is a Param of previously undetermined type, and we want to
195 * update our knowledge of the Param's type.
201 if (paramno <= 0 ||
/* shouldn't happen, but... */
204 (
errcode(ERRCODE_UNDEFINED_PARAMETER),
205 errmsg(
"there is no parameter $%d", paramno),
208 if (paramTypes[paramno - 1] == UNKNOWNOID)
210 /* We've successfully resolved the type */
211 paramTypes[paramno - 1] = targetTypeId;
213 else if (paramTypes[paramno - 1] == targetTypeId)
215 /* We previously resolved the type, and it matches */
221 (
errcode(ERRCODE_AMBIGUOUS_PARAMETER),
222 errmsg(
"inconsistent types deduced for parameter $%d",
233 * Note: it is tempting here to set the Param's paramtypmod to
234 * targetTypeMod, but that is probably unwise because we have no
235 * infrastructure that enforces that the value delivered for a Param
236 * will match any particular typmod. Leaving it -1 ensures that a
237 * run-time length check/coercion will occur if needed.
242 * This module always sets a Param's collation to be the default for
243 * its datatype. If that's not what you want, you should be using the
244 * more general parser substitution hooks.
248 /* Use the leftmost of the param's and coercion's locations */
250 (param->
location < 0 || location < param->location))
253 return (
Node *) param;
256 /* Else signal to proceed with normal coercion */
261 * Check for consistent assignment of variable parameters after completion
262 * of parsing with parse_variable_parameters.
264 * Note: this code intentionally does not check that all parameter positions
265 * were used, nor that all got non-UNKNOWN types assigned. Caller of parser
266 * should enforce that if it's important.
273 /* If numParams is zero then no Params were generated, so no work */
281 * Traverse a fully-analyzed tree to verify that parameter symbols
282 * match their types. We need this because some Params might still
283 * be UNKNOWN, if there wasn't anything to force their coercion,
284 * and yet other instances seen later might have gotten coerced.
300 if (paramno <= 0 ||
/* shouldn't happen, but... */
303 (
errcode(ERRCODE_UNDEFINED_PARAMETER),
304 errmsg(
"there is no parameter $%d", paramno),
309 (
errcode(ERRCODE_AMBIGUOUS_PARAMETER),
310 errmsg(
"could not determine data type of parameter $%d",
318 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
328 * Check to see if a fully-parsed query tree contains any PARAM_EXTERN Params.
353 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
#define OidIsValid(objectId)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define palloc0_array(type, count)
if(TABLE==NULL||TABLE_index==NULL)
Oid get_typcollation(Oid typid)
#define query_tree_walker(q, w, c, f)
#define expression_tree_walker(n, w, c)
#define IsA(nodeptr, _type_)
#define repalloc0_array(pointer, type, oldcount, count)
int parser_errposition(ParseState *pstate, int location)
@ EXPR_KIND_CALL_ARGUMENT
void check_variable_parameters(ParseState *pstate, Query *query)
struct FixedParamState FixedParamState
bool query_contains_extern_params(Query *query)
void setup_parse_variable_parameters(ParseState *pstate, Oid **paramTypes, int *numParams)
static Node * variable_coerce_param_hook(ParseState *pstate, Param *param, Oid targetTypeId, int32 targetTypeMod, int location)
static Node * variable_paramref_hook(ParseState *pstate, ParamRef *pref)
void setup_parse_fixed_parameters(ParseState *pstate, const Oid *paramTypes, int numParams)
struct VarParamState VarParamState
static Node * fixed_paramref_hook(ParseState *pstate, ParamRef *pref)
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
static bool query_contains_extern_params_walker(Node *node, void *context)
ParseExprKind p_expr_kind
ParseParamRefHook p_paramref_hook
CoerceParamHook p_coerce_param_hook