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 "utils/memutils.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 287 of file parse_param.c.

288{
289 if (node == NULL)
290 return false;
291 if (IsA(node, Param))
292 {
293 Param *param = (Param *) node;
294
295 if (param->paramkind == PARAM_EXTERN)
296 {
297 VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
298 int paramno = param->paramid;
299
300 if (paramno <= 0 || /* shouldn't happen, but... */
301 paramno > *parstate->numParams)
303 (errcode(ERRCODE_UNDEFINED_PARAMETER),
304 errmsg("there is no parameter $%d", paramno),
305 parser_errposition(pstate, param->location)));
306
307 if (param->paramtype != (*parstate->paramTypes)[paramno - 1])
309 (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
310 errmsg("could not determine data type of parameter $%d",
311 paramno),
312 parser_errposition(pstate, param->location)));
313 }
314 return false;
315 }
316 if (IsA(node, Query))
317 {
318 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
319 return query_tree_walker((Query *) node,
321 pstate, 0);
322 }
324 pstate);
325}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:158
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
static bool check_parameter_resolution_walker(Node *node, ParseState *pstate)
Definition: parse_param.c:287
@ PARAM_EXTERN
Definition: primnodes.h:367
ParseLoc location
Definition: primnodes.h:384
int paramid
Definition: primnodes.h:377
Oid paramtype
Definition: primnodes.h:378
ParamKind paramkind
Definition: primnodes.h:376
void * p_ref_hook_state
Definition: parse_node.h:258
Oid ** paramTypes
Definition: parse_param.c:51
int * numParams
Definition: parse_param.c:52

References check_parameter_resolution_walker(), 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_parameter_resolution_walker(), and check_variable_parameters().

◆ check_variable_parameters()

void check_variable_parameters ( ParseState pstate,
Query query 
)

Definition at line 269 of file parse_param.c.

270{
271 VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
272
273 /* If numParams is zero then no Params were generated, so no work */
274 if (*parstate->numParams > 0)
275 (void) query_tree_walker(query,
277 pstate, 0);
278}

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

Referenced by parse_analyze_varparams(), and transformExplainStmt().

◆ fixed_paramref_hook()

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

Definition at line 100 of file parse_param.c.

101{
102 FixedParamState *parstate = (FixedParamState *) pstate->p_ref_hook_state;
103 int paramno = pref->number;
104 Param *param;
105
106 /* Check parameter number is valid */
107 if (paramno <= 0 || paramno > parstate->numParams ||
108 !OidIsValid(parstate->paramTypes[paramno - 1]))
110 (errcode(ERRCODE_UNDEFINED_PARAMETER),
111 errmsg("there is no parameter $%d", paramno),
112 parser_errposition(pstate, pref->location)));
113
114 param = makeNode(Param);
115 param->paramkind = PARAM_EXTERN;
116 param->paramid = paramno;
117 param->paramtype = parstate->paramTypes[paramno - 1];
118 param->paramtypmod = -1;
119 param->paramcollid = get_typcollation(param->paramtype);
120 param->location = pref->location;
121
122 return (Node *) param;
123}
#define OidIsValid(objectId)
Definition: c.h:729
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3056
#define makeNode(_type_)
Definition: nodes.h:155
const Oid * paramTypes
Definition: parse_param.c:39
Definition: nodes.h:129
ParseLoc location
Definition: parsenodes.h:307
int number
Definition: parsenodes.h:306

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 331 of file parse_param.c.

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

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 339 of file parse_param.c.

340{
341 if (node == NULL)
342 return false;
343 if (IsA(node, Param))
344 {
345 Param *param = (Param *) node;
346
347 if (param->paramkind == PARAM_EXTERN)
348 return true;
349 return false;
350 }
351 if (IsA(node, Query))
352 {
353 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
354 return query_tree_walker((Query *) node,
356 context, 0);
357 }
359 context);
360}

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

Referenced by query_contains_extern_params(), and query_contains_extern_params_walker().

◆ setup_parse_fixed_parameters()

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

Definition at line 68 of file parse_param.c.

70{
71 FixedParamState *parstate = palloc(sizeof(FixedParamState));
72
73 parstate->paramTypes = paramTypes;
74 parstate->numParams = numParams;
75 pstate->p_ref_hook_state = parstate;
77 /* no need to use p_coerce_param_hook */
78}
void * palloc(Size size)
Definition: mcxt.c:1317
static Node * fixed_paramref_hook(ParseState *pstate, ParamRef *pref)
Definition: parse_param.c:100
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:256

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 84 of file parse_param.c.

86{
87 VarParamState *parstate = palloc(sizeof(VarParamState));
88
89 parstate->paramTypes = paramTypes;
90 parstate->numParams = numParams;
91 pstate->p_ref_hook_state = parstate;
94}
static Node * variable_coerce_param_hook(ParseState *pstate, Param *param, Oid targetTypeId, int32 targetTypeMod, int location)
Definition: parse_param.c:187
static Node * variable_paramref_hook(ParseState *pstate, ParamRef *pref)
Definition: parse_param.c:132
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:257

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(), and transformExplainStmt().

◆ variable_coerce_param_hook()

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

Definition at line 187 of file parse_param.c.

190{
191 if (param->paramkind == PARAM_EXTERN && param->paramtype == UNKNOWNOID)
192 {
193 /*
194 * Input is a Param of previously undetermined type, and we want to
195 * update our knowledge of the Param's type.
196 */
197 VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
198 Oid *paramTypes = *parstate->paramTypes;
199 int paramno = param->paramid;
200
201 if (paramno <= 0 || /* shouldn't happen, but... */
202 paramno > *parstate->numParams)
204 (errcode(ERRCODE_UNDEFINED_PARAMETER),
205 errmsg("there is no parameter $%d", paramno),
206 parser_errposition(pstate, param->location)));
207
208 if (paramTypes[paramno - 1] == UNKNOWNOID)
209 {
210 /* We've successfully resolved the type */
211 paramTypes[paramno - 1] = targetTypeId;
212 }
213 else if (paramTypes[paramno - 1] == targetTypeId)
214 {
215 /* We previously resolved the type, and it matches */
216 }
217 else
218 {
219 /* Oops */
221 (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
222 errmsg("inconsistent types deduced for parameter $%d",
223 paramno),
224 errdetail("%s versus %s",
225 format_type_be(paramTypes[paramno - 1]),
226 format_type_be(targetTypeId)),
227 parser_errposition(pstate, param->location)));
228 }
229
230 param->paramtype = targetTypeId;
231
232 /*
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.
238 */
239 param->paramtypmod = -1;
240
241 /*
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.
245 */
246 param->paramcollid = get_typcollation(param->paramtype);
247
248 /* Use the leftmost of the param's and coercion's locations */
249 if (location >= 0 &&
250 (param->location < 0 || location < param->location))
251 param->location = location;
252
253 return (Node *) param;
254 }
255
256 /* Else signal to proceed with normal coercion */
257 return NULL;
258}
int errdetail(const char *fmt,...)
Definition: elog.c:1203
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
unsigned int Oid
Definition: postgres_ext.h:31

References ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), get_typcollation(), if(), Param::location, VarParamState::numParams, ParseState::p_ref_hook_state, PARAM_EXTERN, Param::paramid, Param::paramkind, Param::paramtype, VarParamState::paramTypes, 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 132 of file parse_param.c.

133{
134 VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
135 int paramno = pref->number;
136 Oid *pptype;
137 Param *param;
138
139 /* Check parameter number is in range */
140 if (paramno <= 0 || paramno > MaxAllocSize / sizeof(Oid))
142 (errcode(ERRCODE_UNDEFINED_PARAMETER),
143 errmsg("there is no parameter $%d", paramno),
144 parser_errposition(pstate, pref->location)));
145 if (paramno > *parstate->numParams)
146 {
147 /* Need to enlarge param array */
148 if (*parstate->paramTypes)
149 *parstate->paramTypes = repalloc0_array(*parstate->paramTypes, Oid,
150 *parstate->numParams, paramno);
151 else
152 *parstate->paramTypes = palloc0_array(Oid, paramno);
153 *parstate->numParams = paramno;
154 }
155
156 /* Locate param's slot in array */
157 pptype = &(*parstate->paramTypes)[paramno - 1];
158
159 /* If not seen before, initialize to UNKNOWN type */
160 if (*pptype == InvalidOid)
161 *pptype = UNKNOWNOID;
162
163 /*
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().
168 */
169 if (*pptype == VOIDOID && pstate->p_expr_kind == EXPR_KIND_CALL_ARGUMENT)
170 *pptype = UNKNOWNOID;
171
172 param = makeNode(Param);
173 param->paramkind = PARAM_EXTERN;
174 param->paramid = paramno;
175 param->paramtype = *pptype;
176 param->paramtypmod = -1;
177 param->paramcollid = get_typcollation(param->paramtype);
178 param->location = pref->location;
179
180 return (Node *) param;
181}
#define MaxAllocSize
Definition: fe_memutils.h:22
#define palloc0_array(type, count)
Definition: fe_memutils.h:77
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:81
#define InvalidOid
Definition: postgres_ext.h:36
ParseExprKind p_expr_kind
Definition: parse_node.h:230

References ereport, errcode(), errmsg(), ERROR, EXPR_KIND_CALL_ARGUMENT, get_typcollation(), if(), InvalidOid, ParamRef::location, makeNode, MaxAllocSize, 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().