PostgreSQL Source Code  git master
params.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * params.c
4  * Support for finding the values associated with Param nodes.
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/nodes/params.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "access/xact.h"
19 #include "mb/stringinfo_mb.h"
20 #include "nodes/bitmapset.h"
21 #include "nodes/params.h"
22 #include "storage/shmem.h"
23 #include "utils/datum.h"
24 #include "utils/lsyscache.h"
25 #include "utils/memutils.h"
26 
27 
28 /*
29  * Allocate and initialize a new ParamListInfo structure.
30  *
31  * To make a new structure for the "dynamic" way (with hooks), pass 0 for
32  * numParams and set numParams manually.
33  */
35 makeParamList(int numParams)
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 }
55 
56 /*
57  * Copy a ParamListInfo structure.
58  *
59  * The result is allocated in CurrentMemoryContext.
60  *
61  * Note: the intent of this function is to make a static, self-contained
62  * set of parameter values. If dynamic parameter hooks are present, we
63  * intentionally do not copy them into the result. Rather, we forcibly
64  * instantiate all available parameter values and copy the datum values.
65  *
66  * paramValuesStr is not copied, either.
67  */
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 }
104 
105 /*
106  * Estimate the amount of space required to serialize a ParamListInfo.
107  */
108 Size
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 }
151 
152 /*
153  * Serialize a ParamListInfo structure into caller-provided storage.
154  *
155  * We write the number of parameters first, as a 4-byte integer, and then
156  * write details for each parameter in turn. The details for each parameter
157  * consist of a 4-byte type OID, 2 bytes of flags, and then the datum as
158  * serialized by datumSerialize(). The caller is responsible for ensuring
159  * that there is enough storage to store the number of bytes that will be
160  * written; use EstimateParamListSpace to find out how many will be needed.
161  * *start_address is updated to point to the byte immediately following those
162  * written.
163  *
164  * RestoreParamList can be used to recreate a ParamListInfo based on the
165  * serialized representation; this will be a static, self-contained copy
166  * just as copyParamList would create.
167  *
168  * paramValuesStr is not included.
169  */
170 void
171 SerializeParamList(ParamListInfo paramLI, char **start_address)
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 }
222 
223 /*
224  * Copy a ParamListInfo structure.
225  *
226  * The result is allocated in CurrentMemoryContext.
227  *
228  * Note: the intent of this function is to make a static, self-contained
229  * set of parameter values. If dynamic parameter hooks are present, we
230  * intentionally do not copy them into the result. Rather, we forcibly
231  * instantiate all available parameter values and copy the datum values.
232  */
234 RestoreParamList(char **start_address)
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 }
262 
263 /*
264  * BuildParamLogString
265  * Return a string that represent the parameter list, for logging.
266  *
267  * If caller already knows textual representations for some parameters, it can
268  * pass an array of exactly params->numParams values as knownTextValues, which
269  * can contain NULLs for any unknown individual values. NULL can be given if
270  * no parameters are known.
271  *
272  * If maxlen is not zero, that's the maximum number of characters of the
273  * input string printed; an ellipsis is added if more characters exist.
274  * (Added quotes are not considered.)
275  */
276 char *
277 BuildParamLogString(ParamListInfo params, char **knownTextValues, int maxlen)
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 }
340 
341 /*
342  * ParamsErrorCallback - callback for printing parameters in error context
343  *
344  * Note that this is a no-op unless BuildParamLogString has been called
345  * beforehand.
346  */
347 void
349 {
350  ParamsErrorCbData *data = (ParamsErrorCbData *) arg;
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 }
signed short int16
Definition: c.h:346
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
ParamListInfo copyParamList(ParamListInfo from)
Definition: params.c:69
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
ParamListInfo params
Definition: params.h:157
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:375
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
struct ParamListInfoData * ParamListInfo
Definition: params.h:98
unsigned int Oid
Definition: postgres_ext.h:31
ParamListInfo makeParamList(int numParams)
Definition: params.c:35
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:109
#define OidIsValid(objectId)
Definition: c.h:645
ParamFetchHook paramFetch
Definition: params.h:112
unsigned short uint16
Definition: c.h:358
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:234
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * BuildParamLogString(ParamListInfo params, char **knownTextValues, int maxlen)
Definition: params.c:277
static char * buf
Definition: pg_test_fsync.c:67
char * paramValuesStr
Definition: params.h:118
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
struct ParamExternData ParamExternData
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:334
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
uintptr_t Datum
Definition: postgres.h:367
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:443
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
const char * portalName
Definition: params.h:156
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:171
uint16 pflags
Definition: params.h:94
size_t Size
Definition: c.h:467
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2029
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1655
void * palloc(Size size)
Definition: mcxt.c:949
int i
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:381
#define errcontext
Definition: elog.h:183
void ParamsErrorCallback(void *arg)
Definition: params.c:348
void * arg
bool isnull
Definition: params.h:93
void appendStringInfoStringQuoted(StringInfo str, const char *s, int maxlen)
Definition: stringinfo_mb.c:34
#define offsetof(type, field)
Definition: c.h:662