PostgreSQL Source Code  git master
params.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "mb/stringinfo_mb.h"
#include "nodes/params.h"
#include "parser/parse_node.h"
#include "storage/shmem.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
Include dependency graph for params.c:

Go to the source code of this file.

Functions

static void paramlist_parser_setup (ParseState *pstate, void *arg)
 
static Nodeparamlist_param_ref (ParseState *pstate, ParamRef *pref)
 
ParamListInfo makeParamList (int numParams)
 
ParamListInfo copyParamList (ParamListInfo from)
 
Size EstimateParamListSpace (ParamListInfo paramLI)
 
void SerializeParamList (ParamListInfo paramLI, char **start_address)
 
ParamListInfo RestoreParamList (char **start_address)
 
char * BuildParamLogString (ParamListInfo params, char **knownTextValues, int maxlen)
 
void ParamsErrorCallback (void *arg)
 

Function Documentation

◆ BuildParamLogString()

char* BuildParamLogString ( ParamListInfo  params,
char **  knownTextValues,
int  maxlen 
)

Definition at line 334 of file params.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, appendStringInfo(), appendStringInfoString(), appendStringInfoStringQuoted(), buf, CurrentMemoryContext, StringInfoData::data, getTypeOutputInfo(), initStringInfo(), IsAbortedTransactionBlockState(), ParamExternData::isnull, MemoryContextDelete(), MemoryContextSwitchTo(), ParamListInfoData::numParams, OidIsValid, OidOutputFunctionCall(), ParamListInfoData::paramFetch, ParamListInfoData::params, ParamExternData::ptype, and ParamExternData::value.

Referenced by errdetail_params(), and exec_bind_message().

335 {
336  MemoryContext tmpCxt,
337  oldCxt;
339 
340  /*
341  * NB: think not of returning params->paramValuesStr! It may have been
342  * generated with a different maxlen, and so be unsuitable. Besides that,
343  * this is the function used to create that string.
344  */
345 
346  /*
347  * No work if the param fetch hook is in use. Also, it's not possible to
348  * do this in an aborted transaction. (It might be possible to improve on
349  * this last point when some knownTextValues exist, but it seems tricky.)
350  */
351  if (params->paramFetch != NULL ||
353  return NULL;
354 
355  /* Initialize the output stringinfo, in caller's memory context */
356  initStringInfo(&buf);
357 
358  /* Use a temporary context to call output functions, just in case */
360  "BuildParamLogString",
362  oldCxt = MemoryContextSwitchTo(tmpCxt);
363 
364  for (int paramno = 0; paramno < params->numParams; paramno++)
365  {
366  ParamExternData *param = &params->params[paramno];
367 
368  appendStringInfo(&buf,
369  "%s$%d = ",
370  paramno > 0 ? ", " : "",
371  paramno + 1);
372 
373  if (param->isnull || !OidIsValid(param->ptype))
374  appendStringInfoString(&buf, "NULL");
375  else
376  {
377  if (knownTextValues != NULL && knownTextValues[paramno] != NULL)
378  appendStringInfoStringQuoted(&buf, knownTextValues[paramno],
379  maxlen);
380  else
381  {
382  Oid typoutput;
383  bool typisvarlena;
384  char *pstring;
385 
386  getTypeOutputInfo(param->ptype, &typoutput, &typisvarlena);
387  pstring = OidOutputFunctionCall(typoutput, param->value);
388  appendStringInfoStringQuoted(&buf, pstring, maxlen);
389  }
390  }
391  }
392 
393  MemoryContextSwitchTo(oldCxt);
394  MemoryContextDelete(tmpCxt);
395 
396  return buf.data;
397 }
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Datum value
Definition: params.h:92
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2784
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:378
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
ParamFetchHook paramFetch
Definition: params.h:112
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:67
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1657
bool isnull
Definition: params.h:93
void appendStringInfoStringQuoted(StringInfo str, const char *s, int maxlen)
Definition: stringinfo_mb.c:34

◆ copyParamList()

ParamListInfo copyParamList ( ParamListInfo  from)

Definition at line 77 of file params.c.

References datumCopy(), get_typlenbyval(), i, ParamExternData::isnull, makeParamList(), ParamListInfoData::numParams, OidIsValid, ParamListInfoData::paramFetch, ParamListInfoData::params, ParamExternData::ptype, and ParamExternData::value.

Referenced by PerformCursorOpen(), and SPI_cursor_open_internal().

78 {
79  ParamListInfo retval;
80 
81  if (from == NULL || from->numParams <= 0)
82  return NULL;
83 
84  retval = makeParamList(from->numParams);
85 
86  for (int i = 0; i < from->numParams; i++)
87  {
88  ParamExternData *oprm;
89  ParamExternData *nprm = &retval->params[i];
90  ParamExternData prmdata;
91  int16 typLen;
92  bool typByVal;
93 
94  /* give hook a chance in case parameter is dynamic */
95  if (from->paramFetch != NULL)
96  oprm = from->paramFetch(from, i + 1, false, &prmdata);
97  else
98  oprm = &from->params[i];
99 
100  /* flat-copy the parameter info */
101  *nprm = *oprm;
102 
103  /* need datumCopy in case it's a pass-by-reference datatype */
104  if (nprm->isnull || !OidIsValid(nprm->ptype))
105  continue;
106  get_typlenbyval(nprm->ptype, &typLen, &typByVal);
107  nprm->value = datumCopy(nprm->value, typByVal, typLen);
108  }
109 
110  return retval;
111 }
signed short int16
Definition: c.h:361
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Datum value
Definition: params.h:92
ParamListInfo makeParamList(int numParams)
Definition: params.c:43
#define OidIsValid(objectId)
Definition: c.h:651
ParamFetchHook paramFetch
Definition: params.h:112
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2139
int i
bool isnull
Definition: params.h:93

◆ EstimateParamListSpace()

Size EstimateParamListSpace ( ParamListInfo  paramLI)

Definition at line 166 of file params.c.

References add_size(), datumEstimateSpace(), get_typlenbyval(), i, ParamExternData::isnull, ParamListInfoData::numParams, OidIsValid, ParamListInfoData::paramFetch, ParamListInfoData::params, ParamExternData::ptype, and ParamExternData::value.

Referenced by ExecInitParallelPlan().

167 {
168  int i;
169  Size sz = sizeof(int);
170 
171  if (paramLI == NULL || paramLI->numParams <= 0)
172  return sz;
173 
174  for (i = 0; i < paramLI->numParams; i++)
175  {
176  ParamExternData *prm;
177  ParamExternData prmdata;
178  Oid typeOid;
179  int16 typLen;
180  bool typByVal;
181 
182  /* give hook a chance in case parameter is dynamic */
183  if (paramLI->paramFetch != NULL)
184  prm = paramLI->paramFetch(paramLI, i + 1, false, &prmdata);
185  else
186  prm = &paramLI->params[i];
187 
188  typeOid = prm->ptype;
189 
190  sz = add_size(sz, sizeof(Oid)); /* space for type OID */
191  sz = add_size(sz, sizeof(uint16)); /* space for pflags */
192 
193  /* space for datum/isnull */
194  if (OidIsValid(typeOid))
195  get_typlenbyval(typeOid, &typLen, &typByVal);
196  else
197  {
198  /* If no type OID, assume by-value, like copyParamList does. */
199  typLen = sizeof(Datum);
200  typByVal = true;
201  }
202  sz = add_size(sz,
203  datumEstimateSpace(prm->value, prm->isnull, typByVal, typLen));
204  }
205 
206  return sz;
207 }
signed short int16
Definition: c.h:361
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Datum value
Definition: params.h:92
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
ParamFetchHook paramFetch
Definition: params.h:112
unsigned short uint16
Definition: c.h:373
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:360
uintptr_t Datum
Definition: postgres.h:367
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
size_t Size
Definition: c.h:473
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2139
int i
bool isnull
Definition: params.h:93

◆ makeParamList()

ParamListInfo makeParamList ( int  numParams)

Definition at line 43 of file params.c.

References offsetof, palloc(), and paramlist_parser_setup().

Referenced by _SPI_convert_params(), copyParamList(), EvaluateParams(), exec_bind_message(), exec_eval_using_params(), plpgsql_estate_setup(), postquel_sub_params(), and RestoreParamList().

44 {
45  ParamListInfo retval;
46  Size size;
47 
48  size = offsetof(ParamListInfoData, params) +
49  numParams * sizeof(ParamExternData);
50 
51  retval = (ParamListInfo) palloc(size);
52  retval->paramFetch = NULL;
53  retval->paramFetchArg = NULL;
54  retval->paramCompile = NULL;
55  retval->paramCompileArg = NULL;
56  retval->parserSetup = paramlist_parser_setup;
57  retval->parserSetupArg = (void *) retval;
58  retval->paramValuesStr = NULL;
59  retval->numParams = numParams;
60 
61  return retval;
62 }
struct ParamListInfoData * ParamListInfo
Definition: params.h:98
static void paramlist_parser_setup(ParseState *pstate, void *arg)
Definition: params.c:119
struct ParamExternData ParamExternData
size_t Size
Definition: c.h:473
void * palloc(Size size)
Definition: mcxt.c:949
#define offsetof(type, field)
Definition: c.h:668

◆ paramlist_param_ref()

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

Definition at line 130 of file params.c.

References get_typcollation(), ParamRef::location, makeNode, ParamRef::number, ParamListInfoData::numParams, OidIsValid, ParseState::p_ref_hook_state, PARAM_EXTERN, ParamListInfoData::paramFetch, and ParamListInfoData::params.

Referenced by paramlist_parser_setup().

131 {
132  ParamListInfo paramLI = (ParamListInfo) pstate->p_ref_hook_state;
133  int paramno = pref->number;
134  ParamExternData *prm;
135  ParamExternData prmdata;
136  Param *param;
137 
138  /* check parameter number is valid */
139  if (paramno <= 0 || paramno > paramLI->numParams)
140  return NULL;
141 
142  /* give hook a chance in case parameter is dynamic */
143  if (paramLI->paramFetch != NULL)
144  prm = paramLI->paramFetch(paramLI, paramno, false, &prmdata);
145  else
146  prm = &paramLI->params[paramno - 1];
147 
148  if (!OidIsValid(prm->ptype))
149  return NULL;
150 
151  param = makeNode(Param);
152  param->paramkind = PARAM_EXTERN;
153  param->paramid = paramno;
154  param->paramtype = prm->ptype;
155  param->paramtypmod = -1;
156  param->paramcollid = get_typcollation(param->paramtype);
157  param->location = pref->location;
158 
159  return (Node *) param;
160 }
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Definition: nodes.h:529
struct ParamListInfoData * ParamListInfo
Definition: params.h:98
#define OidIsValid(objectId)
Definition: c.h:651
ParamFetchHook paramFetch
Definition: params.h:112
int number
Definition: parsenodes.h:246
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2933
int location
Definition: parsenodes.h:247
#define makeNode(_type_)
Definition: nodes.h:577
void * p_ref_hook_state
Definition: parse_node.h:222

◆ paramlist_parser_setup()

static void paramlist_parser_setup ( ParseState pstate,
void *  arg 
)
static

Definition at line 119 of file params.c.

References arg, ParseState::p_paramref_hook, ParseState::p_ref_hook_state, and paramlist_param_ref().

Referenced by makeParamList().

120 {
122  /* no need to use p_coerce_param_hook */
123  pstate->p_ref_hook_state = arg;
124 }
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:220
static Node * paramlist_param_ref(ParseState *pstate, ParamRef *pref)
Definition: params.c:130
void * p_ref_hook_state
Definition: parse_node.h:222
void * arg

◆ ParamsErrorCallback()

void ParamsErrorCallback ( void *  arg)

Definition at line 406 of file params.c.

References errcontext, ParamsErrorCbData::params, ParamListInfoData::paramValuesStr, and ParamsErrorCbData::portalName.

Referenced by exec_bind_message(), and exec_execute_message().

407 {
409 
410  if (data == NULL ||
411  data->params == NULL ||
412  data->params->paramValuesStr == NULL)
413  return;
414 
415  if (data->portalName && data->portalName[0] != '\0')
416  errcontext("extended query \"%s\" with parameters: %s",
417  data->portalName, data->params->paramValuesStr);
418  else
419  errcontext("extended query with parameters: %s",
420  data->params->paramValuesStr);
421 }
ParamListInfo params
Definition: params.h:157
char * paramValuesStr
Definition: params.h:118
const char * portalName
Definition: params.h:156
#define errcontext
Definition: elog.h:185
void * arg

◆ RestoreParamList()

ParamListInfo RestoreParamList ( char **  start_address)

Definition at line 291 of file params.c.

References datumRestore(), i, ParamExternData::isnull, makeParamList(), ParamListInfoData::params, ParamExternData::pflags, ParamExternData::ptype, and ParamExternData::value.

Referenced by ExecParallelGetQueryDesc().

292 {
293  ParamListInfo paramLI;
294  int nparams;
295 
296  memcpy(&nparams, *start_address, sizeof(int));
297  *start_address += sizeof(int);
298 
299  paramLI = makeParamList(nparams);
300 
301  for (int i = 0; i < nparams; i++)
302  {
303  ParamExternData *prm = &paramLI->params[i];
304 
305  /* Read type OID. */
306  memcpy(&prm->ptype, *start_address, sizeof(Oid));
307  *start_address += sizeof(Oid);
308 
309  /* Read flags. */
310  memcpy(&prm->pflags, *start_address, sizeof(uint16));
311  *start_address += sizeof(uint16);
312 
313  /* Read datum/isnull. */
314  prm->value = datumRestore(start_address, &prm->isnull);
315  }
316 
317  return paramLI;
318 }
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Datum value
Definition: params.h:92
unsigned int Oid
Definition: postgres_ext.h:31
ParamListInfo makeParamList(int numParams)
Definition: params.c:43
unsigned short uint16
Definition: c.h:373
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:469
uint16 pflags
Definition: params.h:94
int i
bool isnull
Definition: params.h:93

◆ SerializeParamList()

void SerializeParamList ( ParamListInfo  paramLI,
char **  start_address 
)

Definition at line 228 of file params.c.

References datumSerialize(), get_typlenbyval(), i, ParamExternData::isnull, ParamListInfoData::numParams, OidIsValid, ParamListInfoData::paramFetch, ParamListInfoData::params, ParamExternData::pflags, ParamExternData::ptype, and ParamExternData::value.

Referenced by ExecInitParallelPlan().

229 {
230  int nparams;
231  int i;
232 
233  /* Write number of parameters. */
234  if (paramLI == NULL || paramLI->numParams <= 0)
235  nparams = 0;
236  else
237  nparams = paramLI->numParams;
238  memcpy(*start_address, &nparams, sizeof(int));
239  *start_address += sizeof(int);
240 
241  /* Write each parameter in turn. */
242  for (i = 0; i < nparams; i++)
243  {
244  ParamExternData *prm;
245  ParamExternData prmdata;
246  Oid typeOid;
247  int16 typLen;
248  bool typByVal;
249 
250  /* give hook a chance in case parameter is dynamic */
251  if (paramLI->paramFetch != NULL)
252  prm = paramLI->paramFetch(paramLI, i + 1, false, &prmdata);
253  else
254  prm = &paramLI->params[i];
255 
256  typeOid = prm->ptype;
257 
258  /* Write type OID. */
259  memcpy(*start_address, &typeOid, sizeof(Oid));
260  *start_address += sizeof(Oid);
261 
262  /* Write flags. */
263  memcpy(*start_address, &prm->pflags, sizeof(uint16));
264  *start_address += sizeof(uint16);
265 
266  /* Write datum/isnull. */
267  if (OidIsValid(typeOid))
268  get_typlenbyval(typeOid, &typLen, &typByVal);
269  else
270  {
271  /* If no type OID, assume by-value, like copyParamList does. */
272  typLen = sizeof(Datum);
273  typByVal = true;
274  }
275  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
276  start_address);
277  }
278 }
signed short int16
Definition: c.h:361
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Datum value
Definition: params.h:92
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
ParamFetchHook paramFetch
Definition: params.h:112
unsigned short uint16
Definition: c.h:373
uintptr_t Datum
Definition: postgres.h:367
uint16 pflags
Definition: params.h:94
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2139
int i
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:407
bool isnull
Definition: params.h:93