PostgreSQL Source Code  git master
parse_param.c File Reference
#include "postgres.h"
#include <limits.h>
#include "catalog/pg_type.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_param.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
Include dependency graph for parse_param.c:

Go to the source code of this file.

Data Structures

struct  FixedParamState
 
struct  VarParamState
 

Typedefs

typedef struct FixedParamState FixedParamState
 
typedef struct VarParamState VarParamState
 

Functions

static Nodefixed_paramref_hook (ParseState *pstate, ParamRef *pref)
 
static Nodevariable_paramref_hook (ParseState *pstate, ParamRef *pref)
 
static Nodevariable_coerce_param_hook (ParseState *pstate, Param *param, Oid targetTypeId, int32 targetTypeMod, int location)
 
static bool check_parameter_resolution_walker (Node *node, ParseState *pstate)
 
static bool query_contains_extern_params_walker (Node *node, void *context)
 
void parse_fixed_parameters (ParseState *pstate, Oid *paramTypes, int numParams)
 
void parse_variable_parameters (ParseState *pstate, Oid **paramTypes, int *numParams)
 
void check_variable_parameters (ParseState *pstate, Query *query)
 
bool query_contains_extern_params (Query *query)
 

Typedef Documentation

◆ FixedParamState

◆ VarParamState

typedef struct VarParamState VarParamState

Function Documentation

◆ check_parameter_resolution_walker()

static bool check_parameter_resolution_walker ( Node node,
ParseState pstate 
)
static

Definition at line 290 of file parse_param.c.

References ereport, errcode(), errmsg(), ERROR, expression_tree_walker(), IsA, Param::location, VarParamState::numParams, ParseState::p_ref_hook_state, PARAM_EXTERN, Param::paramid, Param::paramkind, Param::paramtype, VarParamState::paramTypes, parser_errposition(), and query_tree_walker().

Referenced by check_variable_parameters().

291 {
292  if (node == NULL)
293  return false;
294  if (IsA(node, Param))
295  {
296  Param *param = (Param *) node;
297 
298  if (param->paramkind == PARAM_EXTERN)
299  {
300  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
301  int paramno = param->paramid;
302 
303  if (paramno <= 0 || /* shouldn't happen, but... */
304  paramno > *parstate->numParams)
305  ereport(ERROR,
306  (errcode(ERRCODE_UNDEFINED_PARAMETER),
307  errmsg("there is no parameter $%d", paramno),
308  parser_errposition(pstate, param->location)));
309 
310  if (param->paramtype != (*parstate->paramTypes)[paramno - 1])
311  ereport(ERROR,
312  (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
313  errmsg("could not determine data type of parameter $%d",
314  paramno),
315  parser_errposition(pstate, param->location)));
316  }
317  return false;
318  }
319  if (IsA(node, Query))
320  {
321  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
322  return query_tree_walker((Query *) node,
324  (void *) pstate, 0);
325  }
327  (void *) pstate);
328 }
Oid ** paramTypes
Definition: parse_param.c:50
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2336
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
ParamKind paramkind
Definition: primnodes.h:262
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ERROR
Definition: elog.h:45
int location
Definition: primnodes.h:267
int * numParams
Definition: parse_param.c:51
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
Definition: parse_param.c:290
#define ereport(elevel,...)
Definition: elog.h:155
void * p_ref_hook_state
Definition: parse_node.h:223
int paramid
Definition: primnodes.h:263
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1885
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:915
Oid paramtype
Definition: primnodes.h:264

◆ check_variable_parameters()

void check_variable_parameters ( ParseState pstate,
Query query 
)

Definition at line 272 of file parse_param.c.

References check_parameter_resolution_walker(), VarParamState::numParams, ParseState::p_ref_hook_state, and query_tree_walker().

Referenced by parse_analyze_varparams().

273 {
274  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
275 
276  /* If numParams is zero then no Params were generated, so no work */
277  if (*parstate->numParams > 0)
278  (void) query_tree_walker(query,
280  (void *) pstate, 0);
281 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2336
int * numParams
Definition: parse_param.c:51
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
Definition: parse_param.c:290
void * p_ref_hook_state
Definition: parse_node.h:223

◆ fixed_paramref_hook()

static Node * fixed_paramref_hook ( ParseState pstate,
ParamRef pref 
)
static

Definition at line 99 of file parse_param.c.

References ereport, errcode(), errmsg(), ERROR, get_typcollation(), ParamRef::location, makeNode, ParamRef::number, FixedParamState::numParams, OidIsValid, ParseState::p_ref_hook_state, PARAM_EXTERN, FixedParamState::paramTypes, and parser_errposition().

Referenced by parse_fixed_parameters().

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 }
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:704
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:45
int number
Definition: parsenodes.h:246
#define ereport(elevel,...)
Definition: elog.h:155
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3003
int location
Definition: parsenodes.h:247
#define makeNode(_type_)
Definition: nodes.h:581
void * p_ref_hook_state
Definition: parse_node.h:223
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ parse_fixed_parameters()

void parse_fixed_parameters ( ParseState pstate,
Oid paramTypes,
int  numParams 
)

Definition at line 67 of file parse_param.c.

References fixed_paramref_hook(), FixedParamState::numParams, ParseState::p_paramref_hook, ParseState::p_ref_hook_state, palloc(), and FixedParamState::paramTypes.

Referenced by parse_analyze().

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 }
static Node * fixed_paramref_hook(ParseState *pstate, ParamRef *pref)
Definition: parse_param.c:99
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:221
void * p_ref_hook_state
Definition: parse_node.h:223
void * palloc(Size size)
Definition: mcxt.c:950

◆ parse_variable_parameters()

void parse_variable_parameters ( ParseState pstate,
Oid **  paramTypes,
int *  numParams 
)

Definition at line 83 of file parse_param.c.

References FixedParamState::numParams, VarParamState::numParams, ParseState::p_coerce_param_hook, ParseState::p_paramref_hook, ParseState::p_ref_hook_state, palloc(), FixedParamState::paramTypes, VarParamState::paramTypes, variable_coerce_param_hook(), and variable_paramref_hook().

Referenced by parse_analyze_varparams().

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 }
Oid ** paramTypes
Definition: parse_param.c:50
static Node * variable_coerce_param_hook(ParseState *pstate, Param *param, Oid targetTypeId, int32 targetTypeMod, int location)
Definition: parse_param.c:190
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:222
int * numParams
Definition: parse_param.c:51
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:221
static Node * variable_paramref_hook(ParseState *pstate, ParamRef *pref)
Definition: parse_param.c:131
void * p_ref_hook_state
Definition: parse_node.h:223
void * palloc(Size size)
Definition: mcxt.c:950

◆ query_contains_extern_params()

bool query_contains_extern_params ( Query query)

Definition at line 334 of file parse_param.c.

References query_contains_extern_params_walker(), and query_tree_walker().

Referenced by transformCreateTableAsStmt().

335 {
336  return query_tree_walker(query,
338  NULL, 0);
339 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2336
static bool query_contains_extern_params_walker(Node *node, void *context)
Definition: parse_param.c:342

◆ query_contains_extern_params_walker()

static bool query_contains_extern_params_walker ( Node node,
void *  context 
)
static

Definition at line 342 of file parse_param.c.

References expression_tree_walker(), IsA, PARAM_EXTERN, Param::paramkind, and query_tree_walker().

Referenced by query_contains_extern_params().

343 {
344  if (node == NULL)
345  return false;
346  if (IsA(node, Param))
347  {
348  Param *param = (Param *) node;
349 
350  if (param->paramkind == PARAM_EXTERN)
351  return true;
352  return false;
353  }
354  if (IsA(node, Query))
355  {
356  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
357  return query_tree_walker((Query *) node,
359  context, 0);
360  }
362  context);
363 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2336
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
static bool query_contains_extern_params_walker(Node *node, void *context)
Definition: parse_param.c:342
ParamKind paramkind
Definition: primnodes.h:262
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1885

◆ variable_coerce_param_hook()

static Node * variable_coerce_param_hook ( ParseState pstate,
Param param,
Oid  targetTypeId,
int32  targetTypeMod,
int  location 
)
static

Definition at line 190 of file parse_param.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), get_typcollation(), Param::location, VarParamState::numParams, ParseState::p_ref_hook_state, PARAM_EXTERN, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, FixedParamState::paramTypes, VarParamState::paramTypes, Param::paramtypmod, and parser_errposition().

Referenced by parse_variable_parameters().

193 {
194  if (param->paramkind == PARAM_EXTERN && param->paramtype == UNKNOWNOID)
195  {
196  /*
197  * Input is a Param of previously undetermined type, and we want to
198  * update our knowledge of the Param's type.
199  */
200  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
201  Oid *paramTypes = *parstate->paramTypes;
202  int paramno = param->paramid;
203 
204  if (paramno <= 0 || /* shouldn't happen, but... */
205  paramno > *parstate->numParams)
206  ereport(ERROR,
207  (errcode(ERRCODE_UNDEFINED_PARAMETER),
208  errmsg("there is no parameter $%d", paramno),
209  parser_errposition(pstate, param->location)));
210 
211  if (paramTypes[paramno - 1] == UNKNOWNOID)
212  {
213  /* We've successfully resolved the type */
214  paramTypes[paramno - 1] = targetTypeId;
215  }
216  else if (paramTypes[paramno - 1] == targetTypeId)
217  {
218  /* We previously resolved the type, and it matches */
219  }
220  else
221  {
222  /* Oops */
223  ereport(ERROR,
224  (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
225  errmsg("inconsistent types deduced for parameter $%d",
226  paramno),
227  errdetail("%s versus %s",
228  format_type_be(paramTypes[paramno - 1]),
229  format_type_be(targetTypeId)),
230  parser_errposition(pstate, param->location)));
231  }
232 
233  param->paramtype = targetTypeId;
234 
235  /*
236  * Note: it is tempting here to set the Param's paramtypmod to
237  * targetTypeMod, but that is probably unwise because we have no
238  * infrastructure that enforces that the value delivered for a Param
239  * will match any particular typmod. Leaving it -1 ensures that a
240  * run-time length check/coercion will occur if needed.
241  */
242  param->paramtypmod = -1;
243 
244  /*
245  * This module always sets a Param's collation to be the default for
246  * its datatype. If that's not what you want, you should be using the
247  * more general parser substitution hooks.
248  */
249  param->paramcollid = get_typcollation(param->paramtype);
250 
251  /* Use the leftmost of the param's and coercion's locations */
252  if (location >= 0 &&
253  (param->location < 0 || location < param->location))
254  param->location = location;
255 
256  return (Node *) param;
257  }
258 
259  /* Else signal to proceed with normal coercion */
260  return NULL;
261 }
Oid ** paramTypes
Definition: parse_param.c:50
ParamKind paramkind
Definition: primnodes.h:262
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:704
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:45
Oid paramcollid
Definition: primnodes.h:266
int location
Definition: primnodes.h:267
int * numParams
Definition: parse_param.c:51
int errdetail(const char *fmt,...)
Definition: elog.c:1048
#define ereport(elevel,...)
Definition: elog.h:155
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3003
int32 paramtypmod
Definition: primnodes.h:265
void * p_ref_hook_state
Definition: parse_node.h:223
int paramid
Definition: primnodes.h:263
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int errmsg(const char *fmt,...)
Definition: elog.c:915
Oid paramtype
Definition: primnodes.h:264

◆ variable_paramref_hook()

static Node * variable_paramref_hook ( ParseState pstate,
ParamRef pref 
)
static

Definition at line 131 of file parse_param.c.

References ereport, errcode(), errmsg(), ERROR, EXPR_KIND_CALL_ARGUMENT, get_typcollation(), InvalidOid, ParamRef::location, makeNode, MemSet, ParamRef::number, VarParamState::numParams, ParseState::p_expr_kind, ParseState::p_ref_hook_state, palloc(), PARAM_EXTERN, VarParamState::paramTypes, parser_errposition(), and repalloc().

Referenced by parse_variable_parameters().

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 = (Oid *) repalloc(*parstate->paramTypes,
149  paramno * sizeof(Oid));
150  else
151  *parstate->paramTypes = (Oid *) palloc(paramno * sizeof(Oid));
152  /* Zero out the previously-unreferenced slots */
153  MemSet(*parstate->paramTypes + *parstate->numParams,
154  0,
155  (paramno - *parstate->numParams) * sizeof(Oid));
156  *parstate->numParams = paramno;
157  }
158 
159  /* Locate param's slot in array */
160  pptype = &(*parstate->paramTypes)[paramno - 1];
161 
162  /* If not seen before, initialize to UNKNOWN type */
163  if (*pptype == InvalidOid)
164  *pptype = UNKNOWNOID;
165 
166  /*
167  * If the argument is of type void and it's procedure call, interpret it
168  * as unknown. This allows the JDBC driver to not have to distinguish
169  * function and procedure calls. See also another component of this hack
170  * in ParseFuncOrColumn().
171  */
172  if (*pptype == VOIDOID && pstate->p_expr_kind == EXPR_KIND_CALL_ARGUMENT)
173  *pptype = UNKNOWNOID;
174 
175  param = makeNode(Param);
176  param->paramkind = PARAM_EXTERN;
177  param->paramid = paramno;
178  param->paramtype = *pptype;
179  param->paramtypmod = -1;
180  param->paramcollid = get_typcollation(param->paramtype);
181  param->location = pref->location;
182 
183  return (Node *) param;
184 }
Oid ** paramTypes
Definition: parse_param.c:50
Definition: nodes.h:533
int errcode(int sqlerrcode)
Definition: elog.c:704
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:45
int * numParams
Definition: parse_param.c:51
int number
Definition: parsenodes.h:246
ParseExprKind p_expr_kind
Definition: parse_node.h:195
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3003
int location
Definition: parsenodes.h:247
#define makeNode(_type_)
Definition: nodes.h:581
void * p_ref_hook_state
Definition: parse_node.h:223
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915