PostgreSQL Source Code  git master
params.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "mb/stringinfo_mb.h"
#include "nodes/bitmapset.h"
#include "nodes/params.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

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

278 {
279  MemoryContext tmpCxt,
280  oldCxt;
282 
283  /*
284  * NB: think not of returning params->paramValuesStr! It may have been
285  * generated with a different maxlen, and so unsuitable.
286  */
287 
288  /*
289  * No work if the param fetch hook is in use. Also, it's not possible to
290  * do this in an aborted transaction. (It might be possible to improve on
291  * this last point when some knownTextValues exist, but it seems tricky.)
292  */
293  if (params->paramFetch != NULL ||
295  return NULL;
296 
297  /* Initialize the output stringinfo, in caller's memory context */
298  initStringInfo(&buf);
299 
300  /* Use a temporary context to call output functions, just in case */
302  "BuildParamLogString",
304  oldCxt = MemoryContextSwitchTo(tmpCxt);
305 
306  for (int paramno = 0; paramno < params->numParams; paramno++)
307  {
308  ParamExternData *param = &params->params[paramno];
309 
310  appendStringInfo(&buf,
311  "%s$%d = ",
312  paramno > 0 ? ", " : "",
313  paramno + 1);
314 
315  if (param->isnull || !OidIsValid(param->ptype))
316  appendStringInfoString(&buf, "NULL");
317  else
318  {
319  if (knownTextValues != NULL && knownTextValues[paramno] != NULL)
320  appendStringInfoStringQuoted(&buf, knownTextValues[paramno],
321  maxlen);
322  else
323  {
324  Oid typoutput;
325  bool typisvarlena;
326  char *pstring;
327 
328  getTypeOutputInfo(param->ptype, &typoutput, &typisvarlena);
329  pstring = OidOutputFunctionCall(typoutput, param->value);
330  appendStringInfoStringQuoted(&buf, pstring, maxlen);
331  }
332  }
333  }
334 
335  MemoryContextSwitchTo(oldCxt);
336  MemoryContextDelete(tmpCxt);
337 
338  return buf.data;
339 }
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:2674
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:375
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
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:1655
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 69 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().

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

◆ EstimateParamListSpace()

Size EstimateParamListSpace ( ParamListInfo  paramLI)

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

110 {
111  int i;
112  Size sz = sizeof(int);
113 
114  if (paramLI == NULL || paramLI->numParams <= 0)
115  return sz;
116 
117  for (i = 0; i < paramLI->numParams; i++)
118  {
119  ParamExternData *prm;
120  ParamExternData prmdata;
121  Oid typeOid;
122  int16 typLen;
123  bool typByVal;
124 
125  /* give hook a chance in case parameter is dynamic */
126  if (paramLI->paramFetch != NULL)
127  prm = paramLI->paramFetch(paramLI, i + 1, false, &prmdata);
128  else
129  prm = &paramLI->params[i];
130 
131  typeOid = prm->ptype;
132 
133  sz = add_size(sz, sizeof(Oid)); /* space for type OID */
134  sz = add_size(sz, sizeof(uint16)); /* space for pflags */
135 
136  /* space for datum/isnull */
137  if (OidIsValid(typeOid))
138  get_typlenbyval(typeOid, &typLen, &typByVal);
139  else
140  {
141  /* If no type OID, assume by-value, like copyParamList does. */
142  typLen = sizeof(Datum);
143  typByVal = true;
144  }
145  sz = add_size(sz,
146  datumEstimateSpace(prm->value, prm->isnull, typByVal, typLen));
147  }
148 
149  return sz;
150 }
signed short int16
Definition: c.h:346
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:645
ParamFetchHook paramFetch
Definition: params.h:112
unsigned short uint16
Definition: c.h:358
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:334
uintptr_t Datum
Definition: postgres.h:367
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
size_t Size
Definition: c.h:467
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2029
int i
bool isnull
Definition: params.h:93

◆ makeParamList()

ParamListInfo makeParamList ( int  numParams)

Definition at line 35 of file params.c.

References offsetof, and palloc().

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

36 {
37  ParamListInfo retval;
38  Size size;
39 
40  size = offsetof(ParamListInfoData, params) +
41  numParams * sizeof(ParamExternData);
42 
43  retval = (ParamListInfo) palloc(size);
44  retval->paramFetch = NULL;
45  retval->paramFetchArg = NULL;
46  retval->paramCompile = NULL;
47  retval->paramCompileArg = NULL;
48  retval->parserSetup = NULL;
49  retval->parserSetupArg = NULL;
50  retval->paramValuesStr = NULL;
51  retval->numParams = numParams;
52 
53  return retval;
54 }
struct ParamListInfoData * ParamListInfo
Definition: params.h:98
struct ParamExternData ParamExternData
size_t Size
Definition: c.h:467
void * palloc(Size size)
Definition: mcxt.c:949
#define offsetof(type, field)
Definition: c.h:662

◆ ParamsErrorCallback()

void ParamsErrorCallback ( void *  arg)

Definition at line 348 of file params.c.

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

Referenced by exec_bind_message(), and exec_execute_message().

349 {
351 
352  if (data == NULL ||
353  data->params == NULL ||
354  data->params->paramValuesStr == NULL)
355  return;
356 
357  if (data->portalName && data->portalName[0] != '\0')
358  errcontext("extended query \"%s\" with parameters: %s",
359  data->portalName, data->params->paramValuesStr);
360  else
361  errcontext("extended query with parameters: %s",
362  data->params->paramValuesStr);
363 }
ParamListInfo params
Definition: params.h:157
char * paramValuesStr
Definition: params.h:118
const char * portalName
Definition: params.h:156
#define errcontext
Definition: elog.h:183
void * arg

◆ RestoreParamList()

ParamListInfo RestoreParamList ( char **  start_address)

Definition at line 234 of file params.c.

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

Referenced by ExecParallelGetQueryDesc().

235 {
236  ParamListInfo paramLI;
237  int nparams;
238 
239  memcpy(&nparams, *start_address, sizeof(int));
240  *start_address += sizeof(int);
241 
242  paramLI = makeParamList(nparams);
243 
244  for (int i = 0; i < nparams; i++)
245  {
246  ParamExternData *prm = &paramLI->params[i];
247 
248  /* Read type OID. */
249  memcpy(&prm->ptype, *start_address, sizeof(Oid));
250  *start_address += sizeof(Oid);
251 
252  /* Read flags. */
253  memcpy(&prm->pflags, *start_address, sizeof(uint16));
254  *start_address += sizeof(uint16);
255 
256  /* Read datum/isnull. */
257  prm->value = datumRestore(start_address, &prm->isnull);
258  }
259 
260  return paramLI;
261 }
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:35
unsigned short uint16
Definition: c.h:358
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:443
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 171 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().

172 {
173  int nparams;
174  int i;
175 
176  /* Write number of parameters. */
177  if (paramLI == NULL || paramLI->numParams <= 0)
178  nparams = 0;
179  else
180  nparams = paramLI->numParams;
181  memcpy(*start_address, &nparams, sizeof(int));
182  *start_address += sizeof(int);
183 
184  /* Write each parameter in turn. */
185  for (i = 0; i < nparams; i++)
186  {
187  ParamExternData *prm;
188  ParamExternData prmdata;
189  Oid typeOid;
190  int16 typLen;
191  bool typByVal;
192 
193  /* give hook a chance in case parameter is dynamic */
194  if (paramLI->paramFetch != NULL)
195  prm = paramLI->paramFetch(paramLI, i + 1, false, &prmdata);
196  else
197  prm = &paramLI->params[i];
198 
199  typeOid = prm->ptype;
200 
201  /* Write type OID. */
202  memcpy(*start_address, &typeOid, sizeof(Oid));
203  *start_address += sizeof(Oid);
204 
205  /* Write flags. */
206  memcpy(*start_address, &prm->pflags, sizeof(uint16));
207  *start_address += sizeof(uint16);
208 
209  /* Write datum/isnull. */
210  if (OidIsValid(typeOid))
211  get_typlenbyval(typeOid, &typLen, &typByVal);
212  else
213  {
214  /* If no type OID, assume by-value, like copyParamList does. */
215  typLen = sizeof(Datum);
216  typByVal = true;
217  }
218  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
219  start_address);
220  }
221 }
signed short int16
Definition: c.h:346
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:645
ParamFetchHook paramFetch
Definition: params.h:112
unsigned short uint16
Definition: c.h:358
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:2029
int i
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:381
bool isnull
Definition: params.h:93