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 setup_parse_fixed_parameters (ParseState *pstate, const Oid *paramTypes, int numParams)
 
void setup_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 286 of file parse_param.c.

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 }
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#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:168
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
Definition: parse_param.c:286
@ PARAM_EXTERN
Definition: primnodes.h:302
int paramid
Definition: primnodes.h:312
Oid paramtype
Definition: primnodes.h:313
ParamKind paramkind
Definition: primnodes.h:311
int location
Definition: primnodes.h:316
void * p_ref_hook_state
Definition: parse_node.h:225
Oid ** paramTypes
Definition: parse_param.c:50
int * numParams
Definition: parse_param.c:51

References ereport, errcode(), errmsg(), ERROR, expression_tree_walker, if(), 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().

◆ check_variable_parameters()

void check_variable_parameters ( ParseState pstate,
Query query 
)

Definition at line 268 of file parse_param.c.

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 }

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

Referenced by parse_analyze_varparams().

◆ fixed_paramref_hook()

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

Definition at line 99 of file parse_param.c.

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 }
#define OidIsValid(objectId)
Definition: c.h:711
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3014
#define makeNode(_type_)
Definition: nodes.h:165
const Oid * paramTypes
Definition: parse_param.c:38
Definition: nodes.h:118
int location
Definition: parsenodes.h:270
int number
Definition: parsenodes.h:269

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

Referenced by setup_parse_fixed_parameters().

◆ query_contains_extern_params()

bool query_contains_extern_params ( Query query)

Definition at line 330 of file parse_param.c.

331 {
332  return query_tree_walker(query,
334  NULL, 0);
335 }
static bool query_contains_extern_params_walker(Node *node, void *context)
Definition: parse_param.c:338

References query_contains_extern_params_walker(), and query_tree_walker.

Referenced by transformCreateTableAsStmt().

◆ query_contains_extern_params_walker()

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

Definition at line 338 of file parse_param.c.

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 }

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

Referenced by query_contains_extern_params().

◆ setup_parse_fixed_parameters()

void setup_parse_fixed_parameters ( ParseState pstate,
const Oid paramTypes,
int  numParams 
)

Definition at line 67 of file parse_param.c.

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

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

Referenced by parse_analyze_fixedparams().

◆ setup_parse_variable_parameters()

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

Definition at line 83 of file parse_param.c.

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 }
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
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:224

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

Referenced by parse_analyze_varparams().

◆ variable_coerce_param_hook()

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

Definition at line 186 of file parse_param.c.

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 }
int errdetail(const char *fmt,...)
Definition: elog.c:1039
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
int32 paramtypmod
Definition: primnodes.h:314
Oid paramcollid
Definition: primnodes.h:315

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

Referenced by setup_parse_variable_parameters().

◆ variable_paramref_hook()

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

Definition at line 131 of file parse_param.c.

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 }
#define palloc0_array(type, count)
Definition: fe_memutils.h:65
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:107
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:80
#define InvalidOid
Definition: postgres_ext.h:36
ParseExprKind p_expr_kind
Definition: parse_node.h:197

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

Referenced by setup_parse_variable_parameters().