PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

Function Documentation

static bool check_parameter_resolution_walker ( Node node,
ParseState pstate 
)
static

Definition at line 281 of file parse_param.c.

References ereport, errcode(), errmsg(), ERROR, expression_tree_walker(), IsA, Param::location, NULL, 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().

282 {
283  if (node == NULL)
284  return false;
285  if (IsA(node, Param))
286  {
287  Param *param = (Param *) node;
288 
289  if (param->paramkind == PARAM_EXTERN)
290  {
291  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
292  int paramno = param->paramid;
293 
294  if (paramno <= 0 || /* shouldn't happen, but... */
295  paramno > *parstate->numParams)
296  ereport(ERROR,
297  (errcode(ERRCODE_UNDEFINED_PARAMETER),
298  errmsg("there is no parameter $%d", paramno),
299  parser_errposition(pstate, param->location)));
300 
301  if (param->paramtype != (*parstate->paramTypes)[paramno - 1])
302  ereport(ERROR,
303  (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
304  errmsg("could not determine data type of parameter $%d",
305  paramno),
306  parser_errposition(pstate, param->location)));
307  }
308  return false;
309  }
310  if (IsA(node, Query))
311  {
312  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
313  return query_tree_walker((Query *) node,
315  (void *) pstate, 0);
316  }
318  (void *) pstate);
319 }
Oid ** paramTypes
Definition: parse_param.c:50
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2244
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
ParamKind paramkind
Definition: primnodes.h:222
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int location
Definition: primnodes.h:227
int * numParams
Definition: parse_param.c:51
#define ereport(elevel, rest)
Definition: elog.h:122
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
Definition: parse_param.c:281
#define NULL
Definition: c.h:226
void * p_ref_hook_state
Definition: parse_node.h:206
int paramid
Definition: primnodes.h:223
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1857
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid paramtype
Definition: primnodes.h:224
void check_variable_parameters ( ParseState pstate,
Query query 
)

Definition at line 263 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().

264 {
265  VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
266 
267  /* If numParams is zero then no Params were generated, so no work */
268  if (*parstate->numParams > 0)
269  (void) query_tree_walker(query,
271  (void *) pstate, 0);
272 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2244
int * numParams
Definition: parse_param.c:51
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
Definition: parse_param.c:281
void * p_ref_hook_state
Definition: parse_node.h:206
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:508
int errcode(int sqlerrcode)
Definition: elog.c:575
#define OidIsValid(objectId)
Definition: c.h:533
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int number
Definition: parsenodes.h:236
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2749
int location
Definition: parsenodes.h:237
#define makeNode(_type_)
Definition: nodes.h:556
void * p_ref_hook_state
Definition: parse_node.h:206
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
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:204
void * p_ref_hook_state
Definition: parse_node.h:206
void * palloc(Size size)
Definition: mcxt.c:891
void parse_variable_parameters ( ParseState pstate,
Oid **  paramTypes,
int *  numParams 
)

Definition at line 83 of file parse_param.c.

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().

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:181
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:205
int * numParams
Definition: parse_param.c:51
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:204
static Node * variable_paramref_hook(ParseState *pstate, ParamRef *pref)
Definition: parse_param.c:131
void * p_ref_hook_state
Definition: parse_node.h:206
void * palloc(Size size)
Definition: mcxt.c:891
bool query_contains_extern_params ( Query query)

Definition at line 325 of file parse_param.c.

References NULL, query_contains_extern_params_walker(), and query_tree_walker().

Referenced by transformCreateTableAsStmt().

326 {
327  return query_tree_walker(query,
329  NULL, 0);
330 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2244
static bool query_contains_extern_params_walker(Node *node, void *context)
Definition: parse_param.c:333
#define NULL
Definition: c.h:226
static bool query_contains_extern_params_walker ( Node node,
void *  context 
)
static

Definition at line 333 of file parse_param.c.

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

Referenced by query_contains_extern_params().

334 {
335  if (node == NULL)
336  return false;
337  if (IsA(node, Param))
338  {
339  Param *param = (Param *) node;
340 
341  if (param->paramkind == PARAM_EXTERN)
342  return true;
343  return false;
344  }
345  if (IsA(node, Query))
346  {
347  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
348  return query_tree_walker((Query *) node,
350  context, 0);
351  }
353  context);
354 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2244
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
static bool query_contains_extern_params_walker(Node *node, void *context)
Definition: parse_param.c:333
ParamKind paramkind
Definition: primnodes.h:222
#define NULL
Definition: c.h:226
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1857
static Node * variable_coerce_param_hook ( ParseState pstate,
Param param,
Oid  targetTypeId,
int32  targetTypeMod,
int  location 
)
static

Definition at line 181 of file parse_param.c.

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

Referenced by parse_variable_parameters().

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

Definition at line 131 of file parse_param.c.

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

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  param = makeNode(Param);
167  param->paramkind = PARAM_EXTERN;
168  param->paramid = paramno;
169  param->paramtype = *pptype;
170  param->paramtypmod = -1;
171  param->paramcollid = get_typcollation(param->paramtype);
172  param->location = pref->location;
173 
174  return (Node *) param;
175 }
Oid ** paramTypes
Definition: parse_param.c:50
Definition: nodes.h:508
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:852
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
int * numParams
Definition: parse_param.c:51
#define ereport(elevel, rest)
Definition: elog.h:122
int number
Definition: parsenodes.h:236
#define InvalidOid
Definition: postgres_ext.h:36
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2749
int location
Definition: parsenodes.h:237
#define makeNode(_type_)
Definition: nodes.h:556
void * p_ref_hook_state
Definition: parse_node.h:206
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
#define UNKNOWNOID
Definition: pg_type.h:423
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797