PostgreSQL Source Code git master
Loading...
Searching...
No Matches
datum.h File Reference
This graph shows which files directly or indirectly include this file:

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)
 
bool datum_image_eq (Datum value1, Datum value2, bool typByVal, int typLen)
 
uint32 datum_image_hash (Datum value, 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_image_eq()

bool datum_image_eq ( Datum  value1,
Datum  value2,
bool  typByVal,
int  typLen 
)
extern

Definition at line 266 of file datum.c.

267{
268 Size len1,
269 len2;
270 bool result = true;
271
272 if (typByVal)
273 {
274 result = (value1 == value2);
275 }
276 else if (typLen > 0)
277 {
278 result = (memcmp(DatumGetPointer(value1),
280 typLen) == 0);
281 }
282 else if (typLen == -1)
283 {
286 /* No need to de-toast if lengths don't match. */
287 if (len1 != len2)
288 result = false;
289 else
290 {
291 struct varlena *arg1val;
292 struct varlena *arg2val;
293
296
297 result = (memcmp(VARDATA_ANY(arg1val),
299 len1 - VARHDRSZ) == 0);
300
301 /* Only free memory if it's a copy made here. */
303 pfree(arg1val);
305 pfree(arg2val);
306 }
307 }
308 else if (typLen == -2)
309 {
310 char *s1,
311 *s2;
312
313 /* Compare cstring datums */
316 len1 = strlen(s1) + 1;
317 len2 = strlen(s2) + 1;
318 if (len1 != len2)
319 return false;
320 result = (memcmp(s1, s2, len1) == 0);
321 }
322 else
323 elog(ERROR, "unexpected typLen: %d", typLen);
324
325 return result;
326}
#define VARHDRSZ
Definition c.h:711
size_t Size
Definition c.h:619
Size toast_raw_datum_size(Datum value)
Definition detoast.c:545
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define PG_DETOAST_DATUM_PACKED(datum)
Definition fmgr.h:248
void pfree(void *pointer)
Definition mcxt.c:1616
static char * DatumGetCString(Datum X)
Definition postgres.h:365
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static int fb(int x)
char * s1
char * s2
Definition c.h:706
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

References DatumGetCString(), DatumGetPointer(), elog, ERROR, fb(), pfree(), PG_DETOAST_DATUM_PACKED, s1, s2, toast_raw_datum_size(), VARDATA_ANY(), and VARHDRSZ.

Referenced by _bt_keep_natts_fast(), mcvs_equal(), MemoizeHash_equal(), record_image_eq(), and ri_KeysEqual().

◆ datum_image_hash()

uint32 datum_image_hash ( Datum  value,
bool  typByVal,
int  typLen 
)
extern

Definition at line 338 of file datum.c.

339{
340 Size len;
341 uint32 result;
342
343 if (typByVal)
344 result = hash_bytes((unsigned char *) &value, sizeof(Datum));
345 else if (typLen > 0)
346 result = hash_bytes((unsigned char *) DatumGetPointer(value), typLen);
347 else if (typLen == -1)
348 {
349 struct varlena *val;
350
352
354
355 result = hash_bytes((unsigned char *) VARDATA_ANY(val), len - VARHDRSZ);
356
357 /* Only free memory if it's a copy made here. */
358 if (val != DatumGetPointer(value))
359 pfree(val);
360 }
361 else if (typLen == -2)
362 {
363 char *s;
364
366 len = strlen(s) + 1;
367
368 result = hash_bytes((unsigned char *) s, len);
369 }
370 else
371 {
372 elog(ERROR, "unexpected typLen: %d", typLen);
373 result = 0; /* keep compiler quiet */
374 }
375
376 return result;
377}
uint32_t uint32
Definition c.h:546
uint32 hash_bytes(const unsigned char *k, int keylen)
Definition hashfn.c:146
long val
Definition informix.c:689
static struct @172 value
const void size_t len
uint64_t Datum
Definition postgres.h:70

References DatumGetCString(), DatumGetPointer(), elog, ERROR, fb(), hash_bytes(), len, pfree(), PG_DETOAST_DATUM_PACKED, toast_raw_datum_size(), val, value, VARDATA_ANY(), and VARHDRSZ.

Referenced by MemoizeHash_hash().

◆ datumCopy()

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

Definition at line 132 of file datum.c.

133{
134 Datum res;
135
136 if (typByVal)
137 res = value;
138 else if (typLen == -1)
139 {
140 /* It is a varlena datatype */
141 struct varlena *vl = (struct varlena *) DatumGetPointer(value);
142
144 {
145 /* Flatten into the caller's memory context */
148 char *resultptr;
149
151 resultptr = (char *) palloc(resultsize);
154 }
155 else
156 {
157 /* Otherwise, just copy the varlena datum verbatim */
159 char *resultptr;
160
162 resultptr = (char *) palloc(realSize);
165 }
166 }
167 else
168 {
169 /* Pass by reference, but not varlena, so not toasted */
171 char *resultptr;
172
173 realSize = datumGetSize(value, typByVal, typLen);
174
175 resultptr = (char *) palloc(realSize);
178 }
179 return res;
180}
Size datumGetSize(Datum value, bool typByVal, int typLen)
Definition datum.c:65
ExpandedObjectHeader * DatumGetEOHP(Datum d)
void EOH_flatten_into(ExpandedObjectHeader *eohptr, void *result, Size allocated_size)
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
void * palloc(Size size)
Definition mcxt.c:1387
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Size VARSIZE_ANY(const void *PTR)
Definition varatt.h:460
static bool VARATT_IS_EXTERNAL_EXPANDED(const void *PTR)
Definition varatt.h:389

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

Referenced by _bt_array_decrement(), _bt_array_increment(), _bt_skiparray_set_element(), _copyConst(), accumArrayResult(), advance_transition_function(), advance_windowaggregate(), advance_windowaggregate_base(), array_agg_combine(), array_set_element_expanded(), assign_simple_var(), brin_deform_tuple(), brin_inclusion_add_value(), brin_inclusion_union(), brin_minmax_add_value(), brin_minmax_union(), calculate_frame_offsets(), CatCacheCopyKeys(), collectMatchBitmap(), compute_array_stats(), compute_distinct_stats(), compute_expr_stats(), compute_index_stats(), compute_scalar_stats(), CopyIndexAttOptions(), copyParamList(), copyScalarSubstructure(), create_list_bounds(), create_range_bounds(), CreateTupleDescCopyConstr(), datumTransfer(), eval_const_expressions_mutator(), eval_windowaggregates(), eval_windowfunction(), evaluate_expr(), exec_eval_using_params(), ExecAggCopyTransValue(), ExecAggInitGroup(), ExecComputeStoredGenerated(), ExecEvalPreOrderedDistinctSingle(), expanded_record_set_field_internal(), expanded_record_set_fields(), get_actual_variable_endpoint(), get_attoptions(), get_qual_for_list(), get_stats_slot_range(), get_variable_range(), getDatumCopy(), getmissingattr(), GinBufferStoreTuple(), initialize_aggregate(), initialize_windowaggregate(), partition_bounds_copy(), pattern_fixed_prefix(), postquel_get_single_result(), process_ordered_aggregate_single(), range_add_value(), RelationBuildTupleDesc(), reorderqueue_push(), spg_range_quad_inner_consistent(), spg_text_inner_consistent(), spgist_name_inner_consistent(), spgMakeInnerItem(), spgNewHeapItem(), tuplesort_getdatum(), tuplesort_putdatum(), and union_tuples().

◆ datumEstimateSpace()

Size datumEstimateSpace ( Datum  value,
bool  isnull,
bool  typByVal,
int  typLen 
)
extern

Definition at line 414 of file datum.c.

415{
416 Size sz = sizeof(int);
417
418 if (!isnull)
419 {
420 /* no need to use add_size, can't overflow */
421 if (typByVal)
422 sz += sizeof(Datum);
423 else if (typLen == -1 &&
425 {
426 /* Expanded objects need to be flattened, see comment below */
428 }
429 else
430 sz += datumGetSize(value, typByVal, typLen);
431 }
432
433 return sz;
434}

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

Referenced by btestimateparallelscan(), EstimateParamExecSpace(), and EstimateParamListSpace().

◆ datumGetSize()

Size datumGetSize ( Datum  value,
bool  typByVal,
int  typLen 
)
extern

Definition at line 65 of file datum.c.

66{
67 Size size;
68
69 if (typByVal)
70 {
71 /* Pass-by-value types are always fixed-length */
72 Assert(typLen > 0 && typLen <= sizeof(Datum));
73 size = (Size) typLen;
74 }
75 else
76 {
77 if (typLen > 0)
78 {
79 /* Fixed-length pass-by-ref type */
80 size = (Size) typLen;
81 }
82 else if (typLen == -1)
83 {
84 /* It is a varlena datatype */
85 struct varlena *s = (struct varlena *) DatumGetPointer(value);
86
87 if (!s)
90 errmsg("invalid Datum pointer")));
91
92 size = (Size) VARSIZE_ANY(s);
93 }
94 else if (typLen == -2)
95 {
96 /* It is a cstring datatype */
97 char *s = (char *) DatumGetPointer(value);
98
99 if (!s)
102 errmsg("invalid Datum pointer")));
103
104 size = (Size) (strlen(s) + 1);
105 }
106 else
107 {
108 elog(ERROR, "invalid typLen: %d", typLen);
109 size = 0; /* keep compiler quiet */
110 }
111 }
112
113 return size;
114}
#define Assert(condition)
Definition c.h:873
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ereport(elevel,...)
Definition elog.h:150

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

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

◆ datumIsEqual()

bool datumIsEqual ( Datum  value1,
Datum  value2,
bool  typByVal,
int  typLen 
)
extern

Definition at line 223 of file datum.c.

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

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

Referenced by _equalConst(), coerce_type(), equalTupleDescs(), find_compatible_trans(), heap_attr_equals(), and partition_bounds_equal().

◆ datumRestore()

Datum datumRestore ( char **  start_address,
bool isnull 
)
extern

Definition at line 523 of file datum.c.

524{
525 int header;
526 void *d;
527
528 /* Read header word. */
529 memcpy(&header, *start_address, sizeof(int));
530 *start_address += sizeof(int);
531
532 /* If this datum is NULL, we can stop here. */
533 if (header == -2)
534 {
535 *isnull = true;
536 return (Datum) 0;
537 }
538
539 /* OK, datum is not null. */
540 *isnull = false;
541
542 /* If this datum is pass-by-value, sizeof(Datum) bytes follow. */
543 if (header == -1)
544 {
545 Datum val;
546
547 memcpy(&val, *start_address, sizeof(Datum));
548 *start_address += sizeof(Datum);
549 return val;
550 }
551
552 /* Pass-by-reference case; copy indicated number of bytes. */
553 Assert(header > 0);
554 d = palloc(header);
555 memcpy(d, *start_address, header);
556 *start_address += header;
557 return PointerGetDatum(d);
558}

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

Referenced by _bt_parallel_restore_arrays(), RestoreParamExecParams(), and RestoreParamList().

◆ datumSerialize()

void datumSerialize ( Datum  value,
bool  isnull,
bool  typByVal,
int  typLen,
char **  start_address 
)
extern

Definition at line 461 of file datum.c.

463{
465 int header;
466
467 /* Write header word. */
468 if (isnull)
469 header = -2;
470 else if (typByVal)
471 header = -1;
472 else if (typLen == -1 &&
474 {
476 header = EOH_get_flat_size(eoh);
477 }
478 else
479 header = datumGetSize(value, typByVal, typLen);
480 memcpy(*start_address, &header, sizeof(int));
481 *start_address += sizeof(int);
482
483 /* If not null, write payload bytes. */
484 if (!isnull)
485 {
486 if (typByVal)
487 {
488 memcpy(*start_address, &value, sizeof(Datum));
489 *start_address += sizeof(Datum);
490 }
491 else if (eoh)
492 {
493 char *tmp;
494
495 /*
496 * EOH_flatten_into expects the target address to be maxaligned,
497 * so we can't store directly to *start_address.
498 */
499 tmp = (char *) palloc(header);
500 EOH_flatten_into(eoh, tmp, header);
501 memcpy(*start_address, tmp, header);
502 *start_address += header;
503
504 /* be tidy. */
505 pfree(tmp);
506 }
507 else
508 {
510 *start_address += header;
511 }
512 }
513}

References DatumGetEOHP(), DatumGetPointer(), datumGetSize(), EOH_flatten_into(), EOH_get_flat_size(), fb(), palloc(), pfree(), value, and VARATT_IS_EXTERNAL_EXPANDED().

Referenced by _bt_parallel_serialize_arrays(), SerializeParamExecParams(), and SerializeParamList().

◆ datumTransfer()

Datum datumTransfer ( Datum  value,
bool  typByVal,
int  typLen 
)
extern

Definition at line 194 of file datum.c.

195{
196 if (!typByVal && typLen == -1 &&
199 else
200 value = datumCopy(value, typByVal, typLen);
201 return value;
202}
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition datum.c:132
Datum TransferExpandedObject(Datum d, MemoryContext new_parent)
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
static bool VARATT_IS_EXTERNAL_EXPANDED_RW(const void *PTR)
Definition varatt.h:382

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

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