MobilityDB 1.1
postgres.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * postgres.h
4 * Primary include file for PostgreSQL server .c files
5 *
6 * This should be the first file included by PostgreSQL backend modules.
7 * Client-side code should include postgres_fe.h instead.
8 *
9 *
10 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
12 *
13 * src/include/postgres.h
14 *
15 *-------------------------------------------------------------------------
16 */
17/*
18 *----------------------------------------------------------------
19 * TABLE OF CONTENTS
20 *
21 * When adding stuff to this file, please try to put stuff
22 * into the relevant section, or add new sections as appropriate.
23 *
24 * section description
25 * ------- ------------------------------------------------
26 * 1) variable-length datatypes (TOAST support)
27 * 2) Datum type + support macros
28 *
29 * NOTES
30 *
31 * In general, this file should contain declarations that are widely needed
32 * in the backend environment, but are of no interest outside the backend.
33 *
34 * Simple type definitions live in c.h, where they are shared with
35 * postgres_fe.h. We do that since those type definitions are needed by
36 * frontend modules that want to deal with binary data transmission to or
37 * from the backend. Type definitions in this file should be for
38 * representations that never escape the backend, such as Datum or
39 * TOASTed varlena objects.
40 *
41 *----------------------------------------------------------------
42 */
43#ifndef POSTGRES_H
44#define POSTGRES_H
45
46#include "c.h"
47
48/* MobilityDB: redefining elog */
49#define NOTICE 18 /* Helpful messages to users about query
50 * operation; sent to client and not to server
51 * log by default. */
52#define WARNING 19 /* Warnings. NOTICE is for expected messages
53 * like implicit sequence creation by SERIAL.
54 * WARNING is for unexpected messages. */
55#define ERROR 21 /* user error - abort transaction; return to
56 * known state */
57#define EXIT_FAILURE 1
58#define elog(error, ...) \
59 do { \
60 fprintf (stderr, __VA_ARGS__); \
61 if (error == ERROR) \
62 exit(EXIT_FAILURE); \
63 } while(0);
65/* MEOS: redefining palloc0, palloc, and pfree */
66#if MEOS
67#define palloc0(X) (calloc(1, X))
68#define palloc malloc
69#define repalloc realloc
70#define pfree free
71#define pstrdup strdup
72#endif /* MEOS */
73
74/* ----------------------------------------------------------------
75 * Section 1: variable-length datatypes (TOAST support)
76 * ----------------------------------------------------------------
77 */
78
79/*
80 * struct varatt_external is a traditional "TOAST pointer", that is, the
81 * information needed to fetch a Datum stored out-of-line in a TOAST table.
82 * The data is compressed if and only if the external size stored in
83 * va_extinfo is less than va_rawsize - VARHDRSZ.
84 *
85 * This struct must not contain any padding, because we sometimes compare
86 * these pointers using memcmp.
87 *
88 * Note that this information is stored unaligned within actual tuples, so
89 * you need to memcpy from the tuple into a local struct variable before
90 * you can look at these fields! (The reason we use memcmp is to avoid
91 * having to do that just to detect equality of two TOAST pointers...)
92 */
93typedef struct varatt_external
95 int32 va_rawsize; /* Original data size (includes header) */
96 uint32 va_extinfo; /* External saved size (without header) and
97 * compression method */
98 Oid va_valueid; /* Unique ID of value within TOAST table */
99 Oid va_toastrelid; /* RelID of TOAST table containing it */
103 * These macros define the "saved size" portion of va_extinfo. Its remaining
104 * two high-order bits identify the compression method.
105 */
106#define VARLENA_EXTSIZE_BITS 30
107#define VARLENA_EXTSIZE_MASK ((1U << VARLENA_EXTSIZE_BITS) - 1)
108
109/*
110 * struct varatt_indirect is a "TOAST pointer" representing an out-of-line
111 * Datum that's stored in memory, not in an external toast relation.
112 * The creator of such a Datum is entirely responsible that the referenced
113 * storage survives for as long as referencing pointer Datums can exist.
114 *
115 * Note that just as for struct varatt_external, this struct is stored
116 * unaligned within any containing tuple.
117 */
118typedef struct varatt_indirect
119{
120 struct varlena *pointer; /* Pointer to in-memory varlena */
122
123/*
124 * struct varatt_expanded is a "TOAST pointer" representing an out-of-line
125 * Datum that is stored in memory, in some type-specific, not necessarily
126 * physically contiguous format that is convenient for computation not
127 * storage. APIs for this, in particular the definition of struct
128 * ExpandedObjectHeader, are in src/include/utils/expandeddatum.h.
129 *
130 * Note that just as for struct varatt_external, this struct is stored
131 * unaligned within any containing tuple.
132 */
134
135typedef struct varatt_expanded
136{
139
141 * Type tag for the various sorts of "TOAST pointer" datums. The peculiar
142 * value for VARTAG_ONDISK comes from a requirement for on-disk compatibility
143 * with a previous notion that the tag field was the pointer datum's length.
144 */
145typedef enum vartag_external
147 VARTAG_INDIRECT = 1,
150 VARTAG_ONDISK = 18
153/* this test relies on the specific tag values above */
154#define VARTAG_IS_EXPANDED(tag) \
155 (((tag) & ~1) == VARTAG_EXPANDED_RO)
156
157#define VARTAG_SIZE(tag) \
158 ((tag) == VARTAG_INDIRECT ? sizeof(varatt_indirect) : \
159 VARTAG_IS_EXPANDED(tag) ? sizeof(varatt_expanded) : \
160 (tag) == VARTAG_ONDISK ? sizeof(varatt_external) : \
161 TrapMacro(true, "unrecognized TOAST vartag"))
162
163/*
164 * These structs describe the header of a varlena object that may have been
165 * TOASTed. Generally, don't reference these structs directly, but use the
166 * macros below.
168 * We use separate structs for the aligned and unaligned cases because the
169 * compiler might otherwise think it could generate code that assumes
170 * alignment while touching fields of a 1-byte-header varlena.
171 */
172typedef union
174 struct /* Normal varlena (4-byte length) */
175 {
176 uint32 va_header;
178 } va_4byte;
179 struct /* Compressed-in-line format */
181 uint32 va_header;
182 uint32 va_tcinfo; /* Original data size (excludes header) and
183 * compression method; see va_extinfo */
184 char va_data[FLEXIBLE_ARRAY_MEMBER]; /* Compressed data */
185 } va_compressed;
187
188typedef struct
189{
190 uint8 va_header;
191 char va_data[FLEXIBLE_ARRAY_MEMBER]; /* Data begins here */
194/* TOAST pointers are a subset of varattrib_1b with an identifying tag byte */
195typedef struct
196{
197 uint8 va_header; /* Always 0x80 or 0x01 */
198 uint8 va_tag; /* Type of datum */
199 char va_data[FLEXIBLE_ARRAY_MEMBER]; /* Type-specific data */
201
202/*
203 * Bit layouts for varlena headers on big-endian machines:
204 *
205 * 00xxxxxx 4-byte length word, aligned, uncompressed data (up to 1G)
206 * 01xxxxxx 4-byte length word, aligned, *compressed* data (up to 1G)
207 * 10000000 1-byte length word, unaligned, TOAST pointer
208 * 1xxxxxxx 1-byte length word, unaligned, uncompressed data (up to 126b)
209 *
210 * Bit layouts for varlena headers on little-endian machines:
211 *
212 * xxxxxx00 4-byte length word, aligned, uncompressed data (up to 1G)
213 * xxxxxx10 4-byte length word, aligned, *compressed* data (up to 1G)
214 * 00000001 1-byte length word, unaligned, TOAST pointer
215 * xxxxxxx1 1-byte length word, unaligned, uncompressed data (up to 126b)
216 *
217 * The "xxx" bits are the length field (which includes itself in all cases).
218 * In the big-endian case we mask to extract the length, in the little-endian
219 * case we shift. Note that in both cases the flag bits are in the physically
220 * first byte. Also, it is not possible for a 1-byte length word to be zero;
221 * this lets us disambiguate alignment padding bytes from the start of an
222 * unaligned datum. (We now *require* pad bytes to be filled with zero!)
223 *
224 * In TOAST pointers the va_tag field (see varattrib_1b_e) is used to discern
225 * the specific type and length of the pointer datum.
226 */
227
228/*
229 * Endian-dependent macros. These are considered internal --- use the
230 * external macros below instead of using these directly.
231 *
232 * Note: IS_1B is true for external toast records but VARSIZE_1B will return 0
233 * for such records. Hence you should usually check for IS_EXTERNAL before
234 * checking for IS_1B.
235 */
236
237#ifdef WORDS_BIGENDIAN
238
239#define VARATT_IS_4B(PTR) \
240 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
241#define VARATT_IS_4B_U(PTR) \
242 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
243#define VARATT_IS_4B_C(PTR) \
244 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
245#define VARATT_IS_1B(PTR) \
246 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
247#define VARATT_IS_1B_E(PTR) \
248 ((((varattrib_1b *) (PTR))->va_header) == 0x80)
249#define VARATT_NOT_PAD_BYTE(PTR) \
250 (*((uint8 *) (PTR)) != 0)
251
252/* VARSIZE_4B() should only be used on known-aligned data */
253#define VARSIZE_4B(PTR) \
254 (((varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
255#define VARSIZE_1B(PTR) \
256 (((varattrib_1b *) (PTR))->va_header & 0x7F)
257#define VARTAG_1B_E(PTR) \
258 (((varattrib_1b_e *) (PTR))->va_tag)
259
260#define SET_VARSIZE_4B(PTR,len) \
261 (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
262#define SET_VARSIZE_4B_C(PTR,len) \
263 (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 0x40000000)
264#define SET_VARSIZE_1B(PTR,len) \
265 (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
266#define SET_VARTAG_1B_E(PTR,tag) \
267 (((varattrib_1b_e *) (PTR))->va_header = 0x80, \
268 ((varattrib_1b_e *) (PTR))->va_tag = (tag))
270#else /* !WORDS_BIGENDIAN */
272#define VARATT_IS_4B(PTR) \
273 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
274#define VARATT_IS_4B_U(PTR) \
275 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
276#define VARATT_IS_4B_C(PTR) \
277 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
278#define VARATT_IS_1B(PTR) \
279 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
280#define VARATT_IS_1B_E(PTR) \
281 ((((varattrib_1b *) (PTR))->va_header) == 0x01)
282#define VARATT_NOT_PAD_BYTE(PTR) \
283 (*((uint8 *) (PTR)) != 0)
284
285/* VARSIZE_4B() should only be used on known-aligned data */
286#define VARSIZE_4B(PTR) \
287 ((((varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
288#define VARSIZE_1B(PTR) \
289 ((((varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
290#define VARTAG_1B_E(PTR) \
291 (((varattrib_1b_e *) (PTR))->va_tag)
293#define SET_VARSIZE_4B(PTR,len) \
294 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
295#define SET_VARSIZE_4B_C(PTR,len) \
296 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) | 0x02)
297#define SET_VARSIZE_1B(PTR,len) \
298 (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
299#define SET_VARTAG_1B_E(PTR,tag) \
300 (((varattrib_1b_e *) (PTR))->va_header = 0x01, \
301 ((varattrib_1b_e *) (PTR))->va_tag = (tag))
303#endif /* WORDS_BIGENDIAN */
304
305#define VARDATA_4B(PTR) (((varattrib_4b *) (PTR))->va_4byte.va_data)
306#define VARDATA_4B_C(PTR) (((varattrib_4b *) (PTR))->va_compressed.va_data)
307#define VARDATA_1B(PTR) (((varattrib_1b *) (PTR))->va_data)
308#define VARDATA_1B_E(PTR) (((varattrib_1b_e *) (PTR))->va_data)
311 * Externally visible TOAST macros begin here.
312 */
314#define VARHDRSZ_EXTERNAL offsetof(varattrib_1b_e, va_data)
315#define VARHDRSZ_COMPRESSED offsetof(varattrib_4b, va_compressed.va_data)
316#define VARHDRSZ_SHORT offsetof(varattrib_1b, va_data)
318#define VARATT_SHORT_MAX 0x7F
319#define VARATT_CAN_MAKE_SHORT(PTR) \
320 (VARATT_IS_4B_U(PTR) && \
321 (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT) <= VARATT_SHORT_MAX)
322#define VARATT_CONVERTED_SHORT_SIZE(PTR) \
323 (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT)
324
325/*
326 * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(),
327 * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call
328 * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16,
329 * int32 or wider field in the struct representing the datum layout requires
330 * aligned data. memcpy() is alignment-oblivious, as are most operations on
331 * datatypes, such as text, whose layout struct contains only char fields.
332 *
333 * Code assembling a new datum should call VARDATA() and SET_VARSIZE().
334 * (Datums begin life untoasted.)
336 * Other macros here should usually be used only by tuple assembly/disassembly
337 * code and code that specifically wants to work with still-toasted Datums.
338 */
339#define VARDATA(PTR) VARDATA_4B(PTR)
340#define VARSIZE(PTR) VARSIZE_4B(PTR)
342#define VARSIZE_SHORT(PTR) VARSIZE_1B(PTR)
343#define VARDATA_SHORT(PTR) VARDATA_1B(PTR)
345#define VARTAG_EXTERNAL(PTR) VARTAG_1B_E(PTR)
346#define VARSIZE_EXTERNAL(PTR) (VARHDRSZ_EXTERNAL + VARTAG_SIZE(VARTAG_EXTERNAL(PTR)))
347#define VARDATA_EXTERNAL(PTR) VARDATA_1B_E(PTR)
349#define VARATT_IS_COMPRESSED(PTR) VARATT_IS_4B_C(PTR)
350#define VARATT_IS_EXTERNAL(PTR) VARATT_IS_1B_E(PTR)
351#define VARATT_IS_EXTERNAL_ONDISK(PTR) \
352 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_ONDISK)
353#define VARATT_IS_EXTERNAL_INDIRECT(PTR) \
354 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_INDIRECT)
355#define VARATT_IS_EXTERNAL_EXPANDED_RO(PTR) \
356 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RO)
357#define VARATT_IS_EXTERNAL_EXPANDED_RW(PTR) \
358 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RW)
359#define VARATT_IS_EXTERNAL_EXPANDED(PTR) \
360 (VARATT_IS_EXTERNAL(PTR) && VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR)))
361#define VARATT_IS_EXTERNAL_NON_EXPANDED(PTR) \
362 (VARATT_IS_EXTERNAL(PTR) && !VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR)))
363#define VARATT_IS_SHORT(PTR) VARATT_IS_1B(PTR)
364#define VARATT_IS_EXTENDED(PTR) (!VARATT_IS_4B_U(PTR))
366#define SET_VARSIZE(PTR, len) SET_VARSIZE_4B(PTR, len)
367#define SET_VARSIZE_SHORT(PTR, len) SET_VARSIZE_1B(PTR, len)
368#define SET_VARSIZE_COMPRESSED(PTR, len) SET_VARSIZE_4B_C(PTR, len)
369
370#define SET_VARTAG_EXTERNAL(PTR, tag) SET_VARTAG_1B_E(PTR, tag)
371
372#define VARSIZE_ANY(PTR) \
373 (VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR) : \
374 (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR) : \
375 VARSIZE_4B(PTR)))
376
377/* Size of a varlena data, excluding header */
378#define VARSIZE_ANY_EXHDR(PTR) \
379 (VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR)-VARHDRSZ_EXTERNAL : \
380 (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR)-VARHDRSZ_SHORT : \
381 VARSIZE_4B(PTR)-VARHDRSZ))
382
383/* caution: this will not work on an external or compressed-in-line Datum */
384/* caution: this will return a possibly unaligned pointer */
385#define VARDATA_ANY(PTR) \
386 (VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR))
387
388/* Decompressed size and compression method of a compressed-in-line Datum */
389#define VARDATA_COMPRESSED_GET_EXTSIZE(PTR) \
390 (((varattrib_4b *) (PTR))->va_compressed.va_tcinfo & VARLENA_EXTSIZE_MASK)
391#define VARDATA_COMPRESSED_GET_COMPRESS_METHOD(PTR) \
392 (((varattrib_4b *) (PTR))->va_compressed.va_tcinfo >> VARLENA_EXTSIZE_BITS)
393
394/* Same for external Datums; but note argument is a struct varatt_external */
395#define VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) \
396 ((toast_pointer).va_extinfo & VARLENA_EXTSIZE_MASK)
397#define VARATT_EXTERNAL_GET_COMPRESS_METHOD(toast_pointer) \
398 ((toast_pointer).va_extinfo >> VARLENA_EXTSIZE_BITS)
399
400#define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm) \
401 do { \
402 Assert((cm) == TOAST_PGLZ_COMPRESSION_ID || \
403 (cm) == TOAST_LZ4_COMPRESSION_ID); \
404 ((toast_pointer).va_extinfo = \
405 (len) | ((uint32) (cm) << VARLENA_EXTSIZE_BITS)); \
406 } while (0)
407
408/*
409 * Testing whether an externally-stored value is compressed now requires
410 * comparing size stored in va_extinfo (the actual length of the external data)
411 * to rawsize (the original uncompressed datum's size). The latter includes
412 * VARHDRSZ overhead, the former doesn't. We never use compression unless it
413 * actually saves space, so we expect either equality or less-than.
414 */
415#define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer) \
416 (VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) < \
417 (toast_pointer).va_rawsize - VARHDRSZ)
418
419
420/* ----------------------------------------------------------------
421 * Section 2: Datum type + support macros
422 * ----------------------------------------------------------------
423 */
424
425/*
426 * A Datum contains either a value of a pass-by-value type or a pointer to a
427 * value of a pass-by-reference type. Therefore, we require:
428 *
429 * sizeof(Datum) == sizeof(void *) == 4 or 8
431 * The macros below and the analogous macros for other types should be used to
432 * convert between a Datum and the appropriate C type.
433 */
434
435typedef uintptr_t Datum;
436
437/*
438 * A NullableDatum is used in places where both a Datum and its nullness needs
439 * to be stored. This can be more efficient than storing datums and nullness
440 * in separate arrays, due to better spatial locality, even if more space may
441 * be wasted due to padding.
442 */
443typedef struct NullableDatum
444{
445#define FIELDNO_NULLABLE_DATUM_DATUM 0
446 Datum value;
447#define FIELDNO_NULLABLE_DATUM_ISNULL 1
448 bool isnull;
449 /* due to alignment padding this could be used for flags for free */
451
452#define SIZEOF_DATUM SIZEOF_VOID_P
453
454/*
455 * DatumGetBool
456 * Returns boolean value of a datum.
457 *
458 * Note: any nonzero value will be considered true.
459 */
460
461#define DatumGetBool(X) ((bool) ((X) != 0))
462
463/*
464 * BoolGetDatum
465 * Returns datum representation for a boolean.
466 *
467 * Note: any nonzero value will be considered true.
468 */
469
470#define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
471
473 * DatumGetChar
474 * Returns character value of a datum.
475 */
476
477#define DatumGetChar(X) ((char) (X))
478
480 * CharGetDatum
481 * Returns datum representation for a character.
482 */
483
484#define CharGetDatum(X) ((Datum) (X))
485
487 * Int8GetDatum
488 * Returns datum representation for an 8-bit integer.
489 */
490
491#define Int8GetDatum(X) ((Datum) (X))
492
494 * DatumGetUInt8
495 * Returns 8-bit unsigned integer value of a datum.
496 */
497
498#define DatumGetUInt8(X) ((uint8) (X))
499
501 * UInt8GetDatum
502 * Returns datum representation for an 8-bit unsigned integer.
503 */
504
505#define UInt8GetDatum(X) ((Datum) (X))
506
508 * DatumGetInt16
509 * Returns 16-bit integer value of a datum.
510 */
511
512#define DatumGetInt16(X) ((int16) (X))
513
515 * Int16GetDatum
516 * Returns datum representation for a 16-bit integer.
517 */
518
519#define Int16GetDatum(X) ((Datum) (X))
520
522 * DatumGetUInt16
523 * Returns 16-bit unsigned integer value of a datum.
524 */
525
526#define DatumGetUInt16(X) ((uint16) (X))
527
529 * UInt16GetDatum
530 * Returns datum representation for a 16-bit unsigned integer.
531 */
532
533#define UInt16GetDatum(X) ((Datum) (X))
534
536 * DatumGetInt32
537 * Returns 32-bit integer value of a datum.
538 */
539
540#define DatumGetInt32(X) ((int32) (X))
541
543 * Int32GetDatum
544 * Returns datum representation for a 32-bit integer.
545 */
546
547#define Int32GetDatum(X) ((Datum) (X))
548
550 * DatumGetUInt32
551 * Returns 32-bit unsigned integer value of a datum.
552 */
553
554#define DatumGetUInt32(X) ((uint32) (X))
555
557 * UInt32GetDatum
558 * Returns datum representation for a 32-bit unsigned integer.
559 */
560
561#define UInt32GetDatum(X) ((Datum) (X))
562
564 * DatumGetObjectId
565 * Returns object identifier value of a datum.
566 */
567
568#define DatumGetObjectId(X) ((Oid) (X))
569
571 * ObjectIdGetDatum
572 * Returns datum representation for an object identifier.
573 */
574
575#define ObjectIdGetDatum(X) ((Datum) (X))
576
578 * DatumGetTransactionId
579 * Returns transaction identifier value of a datum.
580 */
581
582#define DatumGetTransactionId(X) ((TransactionId) (X))
583
585 * TransactionIdGetDatum
586 * Returns datum representation for a transaction identifier.
587 */
588
589#define TransactionIdGetDatum(X) ((Datum) (X))
590
592 * MultiXactIdGetDatum
593 * Returns datum representation for a multixact identifier.
594 */
595
596#define MultiXactIdGetDatum(X) ((Datum) (X))
597
599 * DatumGetCommandId
600 * Returns command identifier value of a datum.
601 */
602
603#define DatumGetCommandId(X) ((CommandId) (X))
604
606 * CommandIdGetDatum
607 * Returns datum representation for a command identifier.
608 */
609
610#define CommandIdGetDatum(X) ((Datum) (X))
611
613 * DatumGetPointer
614 * Returns pointer value of a datum.
615 */
616
617#define DatumGetPointer(X) ((Pointer) (X))
618
620 * PointerGetDatum
621 * Returns datum representation for a pointer.
622 */
623
624#define PointerGetDatum(X) ((Datum) (X))
625
626/*
627 * DatumGetCString
628 * Returns C string (null-terminated string) value of a datum.
630 * Note: C string is not a full-fledged Postgres type at present,
631 * but type input functions use this conversion for their inputs.
632 */
633
634#define DatumGetCString(X) ((char *) DatumGetPointer(X))
635
636/*
637 * CStringGetDatum
638 * Returns datum representation for a C string (null-terminated string).
639 *
640 * Note: C string is not a full-fledged Postgres type at present,
641 * but type output functions use this conversion for their outputs.
642 * Note: CString is pass-by-reference; caller must ensure the pointed-to
643 * value has adequate lifetime.
644 */
645
646#define CStringGetDatum(X) PointerGetDatum(X)
647
649 * DatumGetName
650 * Returns name value of a datum.
651 */
652
653#define DatumGetName(X) ((Name) DatumGetPointer(X))
654
655/*
656 * NameGetDatum
657 * Returns datum representation for a name.
659 * Note: Name is pass-by-reference; caller must ensure the pointed-to
660 * value has adequate lifetime.
661 */
662
663#define NameGetDatum(X) CStringGetDatum(NameStr(*(X)))
664
665/*
666 * DatumGetInt64
667 * Returns 64-bit integer value of a datum.
669 * Note: this macro hides whether int64 is pass by value or by reference.
670 */
671
672#ifdef USE_FLOAT8_BYVAL
673#define DatumGetInt64(X) ((int64) (X))
674#else
675#define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
676#endif
677
678/*
679 * Int64GetDatum
680 * Returns datum representation for a 64-bit integer.
681 *
682 * Note: if int64 is pass by reference, this function returns a reference
683 * to palloc'd space.
684 */
685
686#ifdef USE_FLOAT8_BYVAL
687#define Int64GetDatum(X) ((Datum) (X))
688#else
689extern Datum Int64GetDatum(int64 X);
690#endif
691
692/*
693 * DatumGetUInt64
694 * Returns 64-bit unsigned integer value of a datum.
696 * Note: this macro hides whether int64 is pass by value or by reference.
697 */
698
699#ifdef USE_FLOAT8_BYVAL
700#define DatumGetUInt64(X) ((uint64) (X))
701#else
702#define DatumGetUInt64(X) (* ((uint64 *) DatumGetPointer(X)))
703#endif
704
705/*
706 * UInt64GetDatum
707 * Returns datum representation for a 64-bit unsigned integer.
708 *
709 * Note: if int64 is pass by reference, this function returns a reference
710 * to palloc'd space.
711 */
712
713#ifdef USE_FLOAT8_BYVAL
714#define UInt64GetDatum(X) ((Datum) (X))
715#else
716#define UInt64GetDatum(X) Int64GetDatum((int64) (X))
717#endif
718
719/*
720 * Float <-> Datum conversions
721 *
722 * These have to be implemented as inline functions rather than macros, when
723 * passing by value, because many machines pass int and float function
724 * parameters/results differently; so we need to play weird games with unions.
725 */
726
728 * DatumGetFloat4
729 * Returns 4-byte floating point value of a datum.
730 */
731static inline float4
733{
734 union
735 {
736 int32 value;
737 float4 retval;
738 } myunion;
739
740 myunion.value = DatumGetInt32(X);
741 return myunion.retval;
742}
743
745 * Float4GetDatum
746 * Returns datum representation for a 4-byte floating point number.
747 */
748static inline Datum
750{
751 union
752 {
753 float4 value;
754 int32 retval;
755 } myunion;
756
757 myunion.value = X;
758 return Int32GetDatum(myunion.retval);
759}
760
761/*
762 * DatumGetFloat8
763 * Returns 8-byte floating point value of a datum.
764 *
765 * Note: this macro hides whether float8 is pass by value or by reference.
766 */
767
768#ifdef USE_FLOAT8_BYVAL
769static inline float8
771{
772 union
773 {
774 int64 value;
775 float8 retval;
776 } myunion;
777
778 myunion.value = DatumGetInt64(X);
779 return myunion.retval;
780}
781#else
782#define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
783#endif
784
785/*
786 * Float8GetDatum
787 * Returns datum representation for an 8-byte floating point number.
788 *
789 * Note: if float8 is pass by reference, this function returns a reference
790 * to palloc'd space.
791 */
792
793#ifdef USE_FLOAT8_BYVAL
794static inline Datum
796{
797 union
798 {
799 float8 value;
800 int64 retval;
801 } myunion;
802
803 myunion.value = X;
804 return Int64GetDatum(myunion.retval);
805}
806#else
807extern Datum Float8GetDatum(float8 X);
808#endif
809
810
811/*
812 * Int64GetDatumFast
813 * Float8GetDatumFast
814 *
815 * These macros are intended to allow writing code that does not depend on
816 * whether int64 and float8 are pass-by-reference types, while not
817 * sacrificing performance when they are. The argument must be a variable
818 * that will exist and have the same value for as long as the Datum is needed.
819 * In the pass-by-ref case, the address of the variable is taken to use as
820 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
821 * macros.
822 */
823
824#ifdef USE_FLOAT8_BYVAL
825#define Int64GetDatumFast(X) Int64GetDatum(X)
826#define Float8GetDatumFast(X) Float8GetDatum(X)
827#else
828#define Int64GetDatumFast(X) PointerGetDatum(&(X))
829#define Float8GetDatumFast(X) PointerGetDatum(&(X))
830#endif
831
832#endif /* POSTGRES_H */
double float8
Definition: c.h:570
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:351
float float4
Definition: c.h:569
unsigned int uint32
Definition: pg_ext_defs.in.h:13
uintptr_t Datum
Definition: pg_ext_defs.in.h:4
signed int int32
Definition: pg_ext_defs.in.h:8
unsigned char uint8
Definition: pg_ext_defs.in.h:11
long int int64
Definition: pg_ext_defs.in.h:9
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:744
uintptr_t Datum
Definition: postgres.h:430
static float4 DatumGetFloat4(Datum X)
Definition: postgres.h:727
static float8 DatumGetFloat8(Datum X)
Definition: postgres.h:765
struct ExpandedObjectHeader ExpandedObjectHeader
Definition: postgres.h:128
struct varatt_external varatt_external
static Datum Float8GetDatum(float8 X)
Definition: postgres.h:790
#define DatumGetInt32(X)
Definition: postgres.h:535
#define Int32GetDatum(X)
Definition: postgres.h:542
#define Int64GetDatum(X)
Definition: postgres.h:682
struct varatt_indirect varatt_indirect
struct varatt_expanded varatt_expanded
struct NullableDatum NullableDatum
vartag_external
Definition: postgres.h:141
@ VARTAG_ONDISK
Definition: postgres.h:145
@ VARTAG_EXPANDED_RW
Definition: postgres.h:144
@ VARTAG_INDIRECT
Definition: postgres.h:142
@ VARTAG_EXPANDED_RO
Definition: postgres.h:143
#define DatumGetInt64(X)
Definition: postgres.h:668
unsigned int Oid
Definition: postgres_ext.h:31
Datum value
Definition: postgres.h:441
bool isnull
Definition: postgres.h:443
Definition: postgres.h:439
ExpandedObjectHeader * eohptr
Definition: postgres.h:132
Definition: postgres.h:131
uint32 va_extinfo
Definition: postgres.h:91
int32 va_rawsize
Definition: postgres.h:90
Oid va_valueid
Definition: postgres.h:93
Oid va_toastrelid
Definition: postgres.h:94
Definition: postgres.h:89
struct varlena * pointer
Definition: postgres.h:115
Definition: postgres.h:114
Definition: postgres.h:191
Definition: postgres.h:184
Definition: pg_ext_defs.in.h:31
Definition: postgres.h:168