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-2018, 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 "nodes/bitmapset.h"
19 #include "nodes/params.h"
20 #include "storage/shmem.h"
21 #include "utils/datum.h"
22 #include "utils/lsyscache.h"
23 
24 
25 /*
26  * Copy a ParamListInfo structure.
27  *
28  * The result is allocated in CurrentMemoryContext.
29  *
30  * Note: the intent of this function is to make a static, self-contained
31  * set of parameter values. If dynamic parameter hooks are present, we
32  * intentionally do not copy them into the result. Rather, we forcibly
33  * instantiate all available parameter values and copy the datum values.
34  */
37 {
38  ParamListInfo retval;
39  Size size;
40  int i;
41 
42  if (from == NULL || from->numParams <= 0)
43  return NULL;
44 
45  size = offsetof(ParamListInfoData, params) +
46  from->numParams * sizeof(ParamExternData);
47 
48  retval = (ParamListInfo) palloc(size);
49  retval->paramFetch = NULL;
50  retval->paramFetchArg = NULL;
51  retval->paramCompile = NULL;
52  retval->paramCompileArg = NULL;
53  retval->parserSetup = NULL;
54  retval->parserSetupArg = NULL;
55  retval->numParams = from->numParams;
56 
57  for (i = 0; i < from->numParams; i++)
58  {
59  ParamExternData *oprm;
60  ParamExternData *nprm = &retval->params[i];
61  ParamExternData prmdata;
62  int16 typLen;
63  bool typByVal;
64 
65  /* give hook a chance in case parameter is dynamic */
66  if (from->paramFetch != NULL)
67  oprm = from->paramFetch(from, i + 1, false, &prmdata);
68  else
69  oprm = &from->params[i];
70 
71  /* flat-copy the parameter info */
72  *nprm = *oprm;
73 
74  /* need datumCopy in case it's a pass-by-reference datatype */
75  if (nprm->isnull || !OidIsValid(nprm->ptype))
76  continue;
77  get_typlenbyval(nprm->ptype, &typLen, &typByVal);
78  nprm->value = datumCopy(nprm->value, typByVal, typLen);
79  }
80 
81  return retval;
82 }
83 
84 /*
85  * Estimate the amount of space required to serialize a ParamListInfo.
86  */
87 Size
89 {
90  int i;
91  Size sz = sizeof(int);
92 
93  if (paramLI == NULL || paramLI->numParams <= 0)
94  return sz;
95 
96  for (i = 0; i < paramLI->numParams; i++)
97  {
98  ParamExternData *prm;
99  ParamExternData prmdata;
100  Oid typeOid;
101  int16 typLen;
102  bool typByVal;
103 
104  /* give hook a chance in case parameter is dynamic */
105  if (paramLI->paramFetch != NULL)
106  prm = paramLI->paramFetch(paramLI, i + 1, false, &prmdata);
107  else
108  prm = &paramLI->params[i];
109 
110  typeOid = prm->ptype;
111 
112  sz = add_size(sz, sizeof(Oid)); /* space for type OID */
113  sz = add_size(sz, sizeof(uint16)); /* space for pflags */
114 
115  /* space for datum/isnull */
116  if (OidIsValid(typeOid))
117  get_typlenbyval(typeOid, &typLen, &typByVal);
118  else
119  {
120  /* If no type OID, assume by-value, like copyParamList does. */
121  typLen = sizeof(Datum);
122  typByVal = true;
123  }
124  sz = add_size(sz,
125  datumEstimateSpace(prm->value, prm->isnull, typByVal, typLen));
126  }
127 
128  return sz;
129 }
130 
131 /*
132  * Serialize a paramListInfo structure into caller-provided storage.
133  *
134  * We write the number of parameters first, as a 4-byte integer, and then
135  * write details for each parameter in turn. The details for each parameter
136  * consist of a 4-byte type OID, 2 bytes of flags, and then the datum as
137  * serialized by datumSerialize(). The caller is responsible for ensuring
138  * that there is enough storage to store the number of bytes that will be
139  * written; use EstimateParamListSpace to find out how many will be needed.
140  * *start_address is updated to point to the byte immediately following those
141  * written.
142  *
143  * RestoreParamList can be used to recreate a ParamListInfo based on the
144  * serialized representation; this will be a static, self-contained copy
145  * just as copyParamList would create.
146  */
147 void
148 SerializeParamList(ParamListInfo paramLI, char **start_address)
149 {
150  int nparams;
151  int i;
152 
153  /* Write number of parameters. */
154  if (paramLI == NULL || paramLI->numParams <= 0)
155  nparams = 0;
156  else
157  nparams = paramLI->numParams;
158  memcpy(*start_address, &nparams, sizeof(int));
159  *start_address += sizeof(int);
160 
161  /* Write each parameter in turn. */
162  for (i = 0; i < nparams; i++)
163  {
164  ParamExternData *prm;
165  ParamExternData prmdata;
166  Oid typeOid;
167  int16 typLen;
168  bool typByVal;
169 
170  /* give hook a chance in case parameter is dynamic */
171  if (paramLI->paramFetch != NULL)
172  prm = paramLI->paramFetch(paramLI, i + 1, false, &prmdata);
173  else
174  prm = &paramLI->params[i];
175 
176  typeOid = prm->ptype;
177 
178  /* Write type OID. */
179  memcpy(*start_address, &typeOid, sizeof(Oid));
180  *start_address += sizeof(Oid);
181 
182  /* Write flags. */
183  memcpy(*start_address, &prm->pflags, sizeof(uint16));
184  *start_address += sizeof(uint16);
185 
186  /* Write datum/isnull. */
187  if (OidIsValid(typeOid))
188  get_typlenbyval(typeOid, &typLen, &typByVal);
189  else
190  {
191  /* If no type OID, assume by-value, like copyParamList does. */
192  typLen = sizeof(Datum);
193  typByVal = true;
194  }
195  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
196  start_address);
197  }
198 }
199 
200 /*
201  * Copy a ParamListInfo structure.
202  *
203  * The result is allocated in CurrentMemoryContext.
204  *
205  * Note: the intent of this function is to make a static, self-contained
206  * set of parameter values. If dynamic parameter hooks are present, we
207  * intentionally do not copy them into the result. Rather, we forcibly
208  * instantiate all available parameter values and copy the datum values.
209  */
211 RestoreParamList(char **start_address)
212 {
213  ParamListInfo paramLI;
214  Size size;
215  int i;
216  int nparams;
217 
218  memcpy(&nparams, *start_address, sizeof(int));
219  *start_address += sizeof(int);
220 
221  size = offsetof(ParamListInfoData, params) +
222  nparams * sizeof(ParamExternData);
223 
224  paramLI = (ParamListInfo) palloc(size);
225  paramLI->paramFetch = NULL;
226  paramLI->paramFetchArg = NULL;
227  paramLI->paramCompile = NULL;
228  paramLI->paramCompileArg = NULL;
229  paramLI->parserSetup = NULL;
230  paramLI->parserSetupArg = NULL;
231  paramLI->numParams = nparams;
232 
233  for (i = 0; i < nparams; i++)
234  {
235  ParamExternData *prm = &paramLI->params[i];
236 
237  /* Read type OID. */
238  memcpy(&prm->ptype, *start_address, sizeof(Oid));
239  *start_address += sizeof(Oid);
240 
241  /* Read flags. */
242  memcpy(&prm->pflags, *start_address, sizeof(uint16));
243  *start_address += sizeof(uint16);
244 
245  /* Read datum/isnull. */
246  prm->value = datumRestore(start_address, &prm->isnull);
247  }
248 
249  return paramLI;
250 }
signed short int16
Definition: c.h:312
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:124
ParamListInfo copyParamList(ParamListInfo from)
Definition: params.c:36
Datum value
Definition: params.h:92
void * parserSetupArg
Definition: params.h:117
ParserSetupHook parserSetup
Definition: params.h:116
struct ParamListInfoData * ParamListInfo
Definition: params.h:98
unsigned int Oid
Definition: postgres_ext.h:31
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:88
#define OidIsValid(objectId)
Definition: c.h:605
void * paramFetchArg
Definition: params.h:113
ParamCompileHook paramCompile
Definition: params.h:114
ParamFetchHook paramFetch
Definition: params.h:112
unsigned short uint16
Definition: c.h:324
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:211
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:128
struct ParamExternData ParamExternData
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:262
uintptr_t Datum
Definition: postgres.h:365
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:360
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:148
uint16 pflags
Definition: params.h:94
size_t Size
Definition: c.h:433
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2005
void * paramCompileArg
Definition: params.h:115
void * palloc(Size size)
Definition: mcxt.c:924
int i
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:309
bool isnull
Definition: params.h:93
#define offsetof(type, field)
Definition: c.h:622