PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
datum.c File Reference
#include "postgres.h"
#include "utils/datum.h"
#include "utils/expandeddatum.h"
Include dependency graph for datum.c:

Go to the source code of this file.

Functions

Size datumGetSize (Datum value, bool typByVal, int typLen)
 
Datum datumCopy (Datum value, bool typByVal, int typLen)
 
Datum datumTransfer (Datum value, bool typByVal, int typLen)
 
bool datumIsEqual (Datum value1, Datum value2, bool typByVal, int typLen)
 
Size datumEstimateSpace (Datum value, bool isnull, bool typByVal, int typLen)
 
void datumSerialize (Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
 
Datum datumRestore (char **start_address, bool *isnull)
 

Function Documentation

Datum datumCopy ( Datum  value,
bool  typByVal,
int  typLen 
)

Definition at line 128 of file datum.c.

References DatumGetEOHP(), DatumGetPointer, datumGetSize(), EOH_flatten_into(), EOH_get_flat_size(), palloc(), PointerGetDatum, value, VARATT_IS_EXTERNAL_EXPANDED, and VARSIZE_ANY.

Referenced by _copyConst(), accumArrayResult(), advance_combine_function(), advance_transition_function(), advance_windowaggregate(), advance_windowaggregate_base(), array_set_element_expanded(), brin_deform_tuple(), brin_inclusion_add_value(), brin_inclusion_union(), brin_minmax_add_value(), brin_minmax_union(), CatCacheCopyKeys(), collectMatchBitmap(), compute_array_stats(), compute_distinct_stats(), compute_index_stats(), compute_scalar_stats(), copyParamList(), datumTransfer(), eval_const_expressions_mutator(), eval_windowaggregates(), eval_windowfunction(), evaluate_expr(), exec_eval_using_params(), ExecWindowAgg(), finalize_aggregate(), finalize_partialaggregate(), finalize_windowaggregate(), get_actual_variable_range(), get_qual_for_list(), get_variable_range(), getDatumCopy(), initialize_aggregate(), initialize_windowaggregate(), partition_bounds_copy(), postquel_get_single_result(), RelationBuildPartitionDesc(), reorderqueue_push(), spg_range_quad_inner_consistent(), spg_text_inner_consistent(), spgWalk(), tuplesort_getdatum(), and tuplesort_putdatum().

129 {
130  Datum res;
131 
132  if (typByVal)
133  res = value;
134  else if (typLen == -1)
135  {
136  /* It is a varlena datatype */
137  struct varlena *vl = (struct varlena *) DatumGetPointer(value);
138 
140  {
141  /* Flatten into the caller's memory context */
143  Size resultsize;
144  char *resultptr;
145 
146  resultsize = EOH_get_flat_size(eoh);
147  resultptr = (char *) palloc(resultsize);
148  EOH_flatten_into(eoh, (void *) resultptr, resultsize);
149  res = PointerGetDatum(resultptr);
150  }
151  else
152  {
153  /* Otherwise, just copy the varlena datum verbatim */
154  Size realSize;
155  char *resultptr;
156 
157  realSize = (Size) VARSIZE_ANY(vl);
158  resultptr = (char *) palloc(realSize);
159  memcpy(resultptr, vl, realSize);
160  res = PointerGetDatum(resultptr);
161  }
162  }
163  else
164  {
165  /* Pass by reference, but not varlena, so not toasted */
166  Size realSize;
167  char *resultptr;
168 
169  realSize = datumGetSize(value, typByVal, typLen);
170 
171  resultptr = (char *) palloc(realSize);
172  memcpy(resultptr, DatumGetPointer(value), realSize);
173  res = PointerGetDatum(resultptr);
174  }
175  return res;
176 }
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:323
#define PointerGetDatum(X)
Definition: postgres.h:562
static struct @121 value
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
uintptr_t Datum
Definition: postgres.h:372
void EOH_flatten_into(ExpandedObjectHeader *eohptr, void *result, Size allocated_size)
Definition: expandeddatum.c:81
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
size_t Size
Definition: c.h:350
#define DatumGetPointer(X)
Definition: postgres.h:555
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
Size datumGetSize(Datum value, bool typByVal, int typLen)
Definition: datum.c:61
Size datumEstimateSpace ( Datum  value,
bool  isnull,
bool  typByVal,
int  typLen 
)

Definition at line 262 of file datum.c.

References DatumGetEOHP(), DatumGetPointer, datumGetSize(), EOH_get_flat_size(), and VARATT_IS_EXTERNAL_EXPANDED.

Referenced by EstimateParamListSpace().

263 {
264  Size sz = sizeof(int);
265 
266  if (!isnull)
267  {
268  /* no need to use add_size, can't overflow */
269  if (typByVal)
270  sz += sizeof(Datum);
271  else if (typLen == -1 &&
273  {
274  /* Expanded objects need to be flattened, see comment below */
276  }
277  else
278  sz += datumGetSize(value, typByVal, typLen);
279  }
280 
281  return sz;
282 }
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:323
static struct @121 value
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
uintptr_t Datum
Definition: postgres.h:372
size_t Size
Definition: c.h:350
#define DatumGetPointer(X)
Definition: postgres.h:555
Size datumGetSize(Datum value, bool typByVal, int typLen)
Definition: datum.c:61
Size datumGetSize ( Datum  value,
bool  typByVal,
int  typLen 
)

Definition at line 61 of file datum.c.

References Assert, DatumGetPointer, elog, ereport, errcode(), errmsg(), ERROR, PointerIsValid, and VARSIZE_ANY.

Referenced by datumCopy(), datumEstimateSpace(), datumIsEqual(), datumSerialize(), outDatum(), and writetup_datum().

62 {
63  Size size;
64 
65  if (typByVal)
66  {
67  /* Pass-by-value types are always fixed-length */
68  Assert(typLen > 0 && typLen <= sizeof(Datum));
69  size = (Size) typLen;
70  }
71  else
72  {
73  if (typLen > 0)
74  {
75  /* Fixed-length pass-by-ref type */
76  size = (Size) typLen;
77  }
78  else if (typLen == -1)
79  {
80  /* It is a varlena datatype */
81  struct varlena *s = (struct varlena *) DatumGetPointer(value);
82 
83  if (!PointerIsValid(s))
84  ereport(ERROR,
85  (errcode(ERRCODE_DATA_EXCEPTION),
86  errmsg("invalid Datum pointer")));
87 
88  size = (Size) VARSIZE_ANY(s);
89  }
90  else if (typLen == -2)
91  {
92  /* It is a cstring datatype */
93  char *s = (char *) DatumGetPointer(value);
94 
95  if (!PointerIsValid(s))
96  ereport(ERROR,
97  (errcode(ERRCODE_DATA_EXCEPTION),
98  errmsg("invalid Datum pointer")));
99 
100  size = (Size) (strlen(s) + 1);
101  }
102  else
103  {
104  elog(ERROR, "invalid typLen: %d", typLen);
105  size = 0; /* keep compiler quiet */
106  }
107  }
108 
109  return size;
110 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
static struct @121 value
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
#define Assert(condition)
Definition: c.h:681
size_t Size
Definition: c.h:350
#define DatumGetPointer(X)
Definition: postgres.h:555
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
#define elog
Definition: elog.h:219
#define PointerIsValid(pointer)
Definition: c.h:520
bool datumIsEqual ( Datum  value1,
Datum  value2,
bool  typByVal,
int  typLen 
)

Definition at line 219 of file datum.c.

References DatumGetPointer, datumGetSize(), s1, and s2.

Referenced by _equalConst(), coerce_type(), find_compatible_pertrans(), heap_tuple_attr_equals(), and partition_bounds_equal().

220 {
221  bool res;
222 
223  if (typByVal)
224  {
225  /*
226  * just compare the two datums. NOTE: just comparing "len" bytes will
227  * not do the work, because we do not know how these bytes are aligned
228  * inside the "Datum". We assume instead that any given datatype is
229  * consistent about how it fills extraneous bits in the Datum.
230  */
231  res = (value1 == value2);
232  }
233  else
234  {
235  Size size1,
236  size2;
237  char *s1,
238  *s2;
239 
240  /*
241  * Compare the bytes pointed by the pointers stored in the datums.
242  */
243  size1 = datumGetSize(value1, typByVal, typLen);
244  size2 = datumGetSize(value2, typByVal, typLen);
245  if (size1 != size2)
246  return false;
247  s1 = (char *) DatumGetPointer(value1);
248  s2 = (char *) DatumGetPointer(value2);
249  res = (memcmp(s1, s2, size1) == 0);
250  }
251  return res;
252 }
char * s1
char * s2
size_t Size
Definition: c.h:350
#define DatumGetPointer(X)
Definition: postgres.h:555
Size datumGetSize(Datum value, bool typByVal, int typLen)
Definition: datum.c:61
Datum datumRestore ( char **  start_address,
bool isnull 
)

Definition at line 360 of file datum.c.

References Assert, header(), palloc(), PointerGetDatum, and val.

Referenced by RestoreParamList().

361 {
362  int header;
363  void *d;
364 
365  /* Read header word. */
366  memcpy(&header, *start_address, sizeof(int));
367  *start_address += sizeof(int);
368 
369  /* If this datum is NULL, we can stop here. */
370  if (header == -2)
371  {
372  *isnull = true;
373  return (Datum) 0;
374  }
375 
376  /* OK, datum is not null. */
377  *isnull = false;
378 
379  /* If this datum is pass-by-value, sizeof(Datum) bytes follow. */
380  if (header == -1)
381  {
382  Datum val;
383 
384  memcpy(&val, *start_address, sizeof(Datum));
385  *start_address += sizeof(Datum);
386  return val;
387  }
388 
389  /* Pass-by-reference case; copy indicated number of bytes. */
390  Assert(header > 0);
391  d = palloc(header);
392  memcpy(d, *start_address, header);
393  *start_address += header;
394  return PointerGetDatum(d);
395 }
#define PointerGetDatum(X)
Definition: postgres.h:562
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:681
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
void * palloc(Size size)
Definition: mcxt.c:848
long val
Definition: informix.c:689
void datumSerialize ( Datum  value,
bool  isnull,
bool  typByVal,
int  typLen,
char **  start_address 
)

Definition at line 309 of file datum.c.

References DatumGetEOHP(), DatumGetPointer, datumGetSize(), EOH_flatten_into(), EOH_get_flat_size(), header(), and VARATT_IS_EXTERNAL_EXPANDED.

Referenced by SerializeParamList().

311 {
312  ExpandedObjectHeader *eoh = NULL;
313  int header;
314 
315  /* Write header word. */
316  if (isnull)
317  header = -2;
318  else if (typByVal)
319  header = -1;
320  else if (typLen == -1 &&
322  {
323  eoh = DatumGetEOHP(value);
324  header = EOH_get_flat_size(eoh);
325  }
326  else
327  header = datumGetSize(value, typByVal, typLen);
328  memcpy(*start_address, &header, sizeof(int));
329  *start_address += sizeof(int);
330 
331  /* If not null, write payload bytes. */
332  if (!isnull)
333  {
334  if (typByVal)
335  {
336  memcpy(*start_address, &value, sizeof(Datum));
337  *start_address += sizeof(Datum);
338  }
339  else if (eoh)
340  {
341  EOH_flatten_into(eoh, (void *) *start_address, header);
342  *start_address += header;
343  }
344  else
345  {
346  memcpy(*start_address, DatumGetPointer(value), header);
347  *start_address += header;
348  }
349  }
350 }
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:323
static struct @121 value
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
uintptr_t Datum
Definition: postgres.h:372
void EOH_flatten_into(ExpandedObjectHeader *eohptr, void *result, Size allocated_size)
Definition: expandeddatum.c:81
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
#define DatumGetPointer(X)
Definition: postgres.h:555
Size datumGetSize(Datum value, bool typByVal, int typLen)
Definition: datum.c:61
Datum datumTransfer ( Datum  value,
bool  typByVal,
int  typLen 
)

Definition at line 190 of file datum.c.

References CurrentMemoryContext, datumCopy(), DatumGetPointer, TransferExpandedObject(), value, and VARATT_IS_EXTERNAL_EXPANDED_RW.

Referenced by exec_assign_value(), exec_stmt_block(), and SPI_datumTransfer().

191 {
192  if (!typByVal && typLen == -1 &&
195  else
196  value = datumCopy(value, typByVal, typLen);
197  return value;
198 }
Datum TransferExpandedObject(Datum d, MemoryContext new_parent)
static struct @121 value
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:128
#define DatumGetPointer(X)
Definition: postgres.h:555
#define VARATT_IS_EXTERNAL_EXPANDED_RW(PTR)
Definition: postgres.h:321