PostgreSQL Source Code  git master
parse_param.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * parse_param.c
4  * handle parameters in parser
5  *
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.
11  *
12  * Note that other approaches to parameters are possible using the parser
13  * hooks defined in ParseState.
14  *
15  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
16  * Portions Copyright (c) 1994, Regents of the University of California
17  *
18  *
19  * IDENTIFICATION
20  * src/backend/parser/parse_param.c
21  *
22  *-------------------------------------------------------------------------
23  */
24 
25 #include "postgres.h"
26 
27 #include <limits.h>
28 
29 #include "catalog/pg_type.h"
30 #include "nodes/nodeFuncs.h"
31 #include "parser/parse_param.h"
32 #include "utils/builtins.h"
33 #include "utils/lsyscache.h"
34 
35 
36 typedef struct FixedParamState
37 {
38  const Oid *paramTypes; /* array of parameter type OIDs */
39  int numParams; /* number of array entries */
41 
42 /*
43  * In the varparams case, the caller-supplied OID array (if any) can be
44  * re-palloc'd larger at need. A zero array entry means that parameter number
45  * hasn't been seen, while UNKNOWNOID means the parameter has been used but
46  * its type is not yet known.
47  */
48 typedef struct VarParamState
49 {
50  Oid **paramTypes; /* array of parameter type OIDs */
51  int *numParams; /* number of array entries */
53 
54 static Node *fixed_paramref_hook(ParseState *pstate, ParamRef *pref);
55 static Node *variable_paramref_hook(ParseState *pstate, ParamRef *pref);
56 static Node *variable_coerce_param_hook(ParseState *pstate, Param *param,
57  Oid targetTypeId, int32 targetTypeMod,
58  int location);
59 static bool check_parameter_resolution_walker(Node *node, ParseState *pstate);
60 static bool query_contains_extern_params_walker(Node *node, void *context);
61 
62 
63 /*
64  * Set up to process a query containing references to fixed parameters.
65  */
66 void
68  const Oid *paramTypes, int numParams)
69 {
70  FixedParamState *parstate = palloc(sizeof(FixedParamState));
71 
72  parstate->paramTypes = paramTypes;
73  parstate->numParams = numParams;
74  pstate->p_ref_hook_state = (void *) parstate;
76  /* no need to use p_coerce_param_hook */
77 }
78 
79 /*
80  * Set up to process a query containing references to variable parameters.
81  */
82 void
84  Oid **paramTypes, int *numParams)
85 {
86  VarParamState *parstate = palloc(sizeof(VarParamState));
87 
88  parstate->paramTypes = paramTypes;
89  parstate->numParams = numParams;
90  pstate->p_ref_hook_state = (void *) parstate;
93 }
94 
95 /*
96  * Transform a ParamRef using fixed parameter types.
97  */
98 static Node *
100 {
101  FixedParamState *parstate = (FixedParamState *) pstate->p_ref_hook_state;
102  int paramno = pref->number;
103  Param *param;
104 
105  /* Check parameter number is valid */
106  if (paramno <= 0 || paramno > parstate->numParams ||
107  !OidIsValid(parstate->paramTypes[paramno - 1]))
108  ereport(ERROR,
109  (errcode(ERRCODE_UNDEFINED_PARAMETER),
110  errmsg("there is no parameter $%d", paramno),
111  parser_errposition(pstate, pref->location)));
112 
113  param = makeNode(Param);
114  param->paramkind = PARAM_EXTERN;
115  param->paramid = paramno;
116  param->paramtype = parstate->paramTypes[paramno - 1];
117  param->paramtypmod = -1;
118  param->paramcollid = get_typcollation(param->paramtype);
119  param->location = pref->location;
120 
121  return (Node *) param;
122 }
123 
124 /*
125  * Transform a ParamRef using variable parameter types.
126  *
127  * The only difference here is we must enlarge the parameter type array
128  * as needed.
129  */
130 static Node *
132 {
133  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
134  int paramno = pref->number;
135  Oid *pptype;
136  Param *param;
137 
138  /* Check parameter number is in range */
139  if (paramno <= 0 || paramno > INT_MAX / sizeof(Oid))
140  ereport(ERROR,
141  (errcode(ERRCODE_UNDEFINED_PARAMETER),
142  errmsg("there is no parameter $%d", paramno),
143  parser_errposition(pstate, pref->location)));
144  if (paramno > *parstate->numParams)
145  {
146  /* Need to enlarge param array */
147  if (*parstate->paramTypes)
148  *parstate->paramTypes = repalloc0_array(*parstate->paramTypes, Oid,
149  *parstate->numParams, paramno);
150  else
151  *parstate->paramTypes = palloc0_array(Oid, paramno);
152  *parstate->numParams = paramno;
153  }
154 
155  /* Locate param's slot in array */
156  pptype = &(*parstate->paramTypes)[paramno - 1];
157 
158  /* If not seen before, initialize to UNKNOWN type */
159  if (*pptype == InvalidOid)
160  *pptype = UNKNOWNOID;
161 
162  /*
163  * If the argument is of type void and it's procedure call, interpret it
164  * as unknown. This allows the JDBC driver to not have to distinguish
165  * function and procedure calls. See also another component of this hack
166  * in ParseFuncOrColumn().
167  */
168  if (*pptype == VOIDOID && pstate->p_expr_kind == EXPR_KIND_CALL_ARGUMENT)
169  *pptype = UNKNOWNOID;
170 
171  param = makeNode(Param);
172  param->paramkind = PARAM_EXTERN;
173  param->paramid = paramno;
174  param->paramtype = *pptype;
175  param->paramtypmod = -1;
176  param->paramcollid = get_typcollation(param->paramtype);
177  param->location = pref->location;
178 
179  return (Node *) param;
180 }
181 
182 /*
183  * Coerce a Param to a query-requested datatype, in the varparams case.
184  */
185 static Node *
187  Oid targetTypeId, int32 targetTypeMod,
188  int location)
189 {
190  if (param->paramkind == PARAM_EXTERN && param->paramtype == UNKNOWNOID)
191  {
192  /*
193  * Input is a Param of previously undetermined type, and we want to
194  * update our knowledge of the Param's type.
195  */
196  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
197  Oid *paramTypes = *parstate->paramTypes;
198  int paramno = param->paramid;
199 
200  if (paramno <= 0 || /* shouldn't happen, but... */
201  paramno > *parstate->numParams)
202  ereport(ERROR,
203  (errcode(ERRCODE_UNDEFINED_PARAMETER),
204  errmsg("there is no parameter $%d", paramno),
205  parser_errposition(pstate, param->location)));
206 
207  if (paramTypes[paramno - 1] == UNKNOWNOID)
208  {
209  /* We've successfully resolved the type */
210  paramTypes[paramno - 1] = targetTypeId;
211  }
212  else if (paramTypes[paramno - 1] == targetTypeId)
213  {
214  /* We previously resolved the type, and it matches */
215  }
216  else
217  {
218  /* Oops */
219  ereport(ERROR,
220  (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
221  errmsg("inconsistent types deduced for parameter $%d",
222  paramno),
223  errdetail("%s versus %s",
224  format_type_be(paramTypes[paramno - 1]),
225  format_type_be(targetTypeId)),
226  parser_errposition(pstate, param->location)));
227  }
228 
229  param->paramtype = targetTypeId;
230 
231  /*
232  * Note: it is tempting here to set the Param's paramtypmod to
233  * targetTypeMod, but that is probably unwise because we have no
234  * infrastructure that enforces that the value delivered for a Param
235  * will match any particular typmod. Leaving it -1 ensures that a
236  * run-time length check/coercion will occur if needed.
237  */
238  param->paramtypmod = -1;
239 
240  /*
241  * This module always sets a Param's collation to be the default for
242  * its datatype. If that's not what you want, you should be using the
243  * more general parser substitution hooks.
244  */
245  param->paramcollid = get_typcollation(param->paramtype);
246 
247  /* Use the leftmost of the param's and coercion's locations */
248  if (location >= 0 &&
249  (param->location < 0 || location < param->location))
250  param->location = location;
251 
252  return (Node *) param;
253  }
254 
255  /* Else signal to proceed with normal coercion */
256  return NULL;
257 }
258 
259 /*
260  * Check for consistent assignment of variable parameters after completion
261  * of parsing with parse_variable_parameters.
262  *
263  * Note: this code intentionally does not check that all parameter positions
264  * were used, nor that all got non-UNKNOWN types assigned. Caller of parser
265  * should enforce that if it's important.
266  */
267 void
269 {
270  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
271 
272  /* If numParams is zero then no Params were generated, so no work */
273  if (*parstate->numParams > 0)
274  (void) query_tree_walker(query,
276  (void *) pstate, 0);
277 }
278 
279 /*
280  * Traverse a fully-analyzed tree to verify that parameter symbols
281  * match their types. We need this because some Params might still
282  * be UNKNOWN, if there wasn't anything to force their coercion,
283  * and yet other instances seen later might have gotten coerced.
284  */
285 static bool
287 {
288  if (node == NULL)
289  return false;
290  if (IsA(node, Param))
291  {
292  Param *param = (Param *) node;
293 
294  if (param->paramkind == PARAM_EXTERN)
295  {
296  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
297  int paramno = param->paramid;
298 
299  if (paramno <= 0 || /* shouldn't happen, but... */
300  paramno > *parstate->numParams)
301  ereport(ERROR,
302  (errcode(ERRCODE_UNDEFINED_PARAMETER),
303  errmsg("there is no parameter $%d", paramno),
304  parser_errposition(pstate, param->location)));
305 
306  if (param->paramtype != (*parstate->paramTypes)[paramno - 1])
307  ereport(ERROR,
308  (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
309  errmsg("could not determine data type of parameter $%d",
310  paramno),
311  parser_errposition(pstate, param->location)));
312  }
313  return false;
314  }
315  if (IsA(node, Query))
316  {
317  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
318  return query_tree_walker((Query *) node,
320  (void *) pstate, 0);
321  }
323  (void *) pstate);
324 }
325 
326 /*
327  * Check to see if a fully-parsed query tree contains any PARAM_EXTERN Params.
328  */
329 bool
331 {
332  return query_tree_walker(query,
334  NULL, 0);
335 }
336 
337 static bool
339 {
340  if (node == NULL)
341  return false;
342  if (IsA(node, Param))
343  {
344  Param *param = (Param *) node;
345 
346  if (param->paramkind == PARAM_EXTERN)
347  return true;
348  return false;
349  }
350  if (IsA(node, Query))
351  {
352  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
353  return query_tree_walker((Query *) node,
355  context, 0);
356  }
358  context);
359 }
signed int int32
Definition: c.h:478
#define OidIsValid(objectId)
Definition: c.h:759
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3014
void * palloc(Size size)
Definition: mcxt.c:1226
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define makeNode(_type_)
Definition: nodes.h:176
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:110
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:80
void check_variable_parameters(ParseState *pstate, Query *query)
Definition: parse_param.c:268
struct FixedParamState FixedParamState
bool query_contains_extern_params(Query *query)
Definition: parse_param.c:330
void setup_parse_variable_parameters(ParseState *pstate, Oid **paramTypes, int *numParams)
Definition: parse_param.c:83
static Node * variable_coerce_param_hook(ParseState *pstate, Param *param, Oid targetTypeId, int32 targetTypeMod, int location)
Definition: parse_param.c:186
static Node * variable_paramref_hook(ParseState *pstate, ParamRef *pref)
Definition: parse_param.c:131
void setup_parse_fixed_parameters(ParseState *pstate, const Oid *paramTypes, int numParams)
Definition: parse_param.c:67
struct VarParamState VarParamState
static Node * fixed_paramref_hook(ParseState *pstate, ParamRef *pref)
Definition: parse_param.c:99
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
Definition: parse_param.c:286
static bool query_contains_extern_params_walker(Node *node, void *context)
Definition: parse_param.c:338
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
@ PARAM_EXTERN
Definition: primnodes.h:345
const Oid * paramTypes
Definition: parse_param.c:38
Definition: nodes.h:129
int location
Definition: parsenodes.h:303
int number
Definition: parsenodes.h:302
int paramid
Definition: primnodes.h:355
Oid paramtype
Definition: primnodes.h:356
ParamKind paramkind
Definition: primnodes.h:354
int location
Definition: primnodes.h:362
void * p_ref_hook_state
Definition: parse_node.h:238
ParseExprKind p_expr_kind
Definition: parse_node.h:210
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:236
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:237
Oid ** paramTypes
Definition: parse_param.c:50
int * numParams
Definition: parse_param.c:51