1 /*-------------------------------------------------------------------------
4 * Primary include file for PostgreSQL server .c files
6 * This should be the first file included by PostgreSQL backend modules.
7 * Client-side code should include postgres_fe.h instead.
10 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * src/include/postgres.h
15 *-------------------------------------------------------------------------
18 *----------------------------------------------------------------
21 * When adding stuff to this file, please try to put stuff
22 * into the relevant section, or add new sections as appropriate.
25 * ------- ------------------------------------------------
26 * 1) variable-length datatypes (TOAST support)
27 * 2) Datum type + support macros
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.
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.
41 *----------------------------------------------------------------
47 #include "utils/elog.h"
48 #include "utils/palloc.h"
50 /* ----------------------------------------------------------------
51 * Section 1: variable-length datatypes (TOAST support)
52 * ----------------------------------------------------------------
56 * struct varatt_external is a traditional "TOAST pointer", that is, the
57 * information needed to fetch a Datum stored out-of-line in a TOAST table.
58 * The data is compressed if and only if the external size stored in
59 * va_extinfo is less than va_rawsize - VARHDRSZ.
61 * This struct must not contain any padding, because we sometimes compare
62 * these pointers using memcmp.
64 * Note that this information is stored unaligned within actual tuples, so
65 * you need to memcpy from the tuple into a local struct variable before
66 * you can look at these fields! (The reason we use memcmp is to avoid
67 * having to do that just to detect equality of two TOAST pointers...)
69 typedef struct varatt_external
71 int32 va_rawsize
; /* Original data size (includes header) */
72 uint32 va_extinfo
; /* External saved size (without header) and
73 * compression method */
74 Oid va_valueid
; /* Unique ID of value within TOAST table */
75 Oid va_toastrelid
; /* RelID of TOAST table containing it */
79 * These macros define the "saved size" portion of va_extinfo. Its remaining
80 * two high-order bits identify the compression method.
82 #define VARLENA_EXTSIZE_BITS 30
83 #define VARLENA_EXTSIZE_MASK ((1U << VARLENA_EXTSIZE_BITS) - 1)
86 * struct varatt_indirect is a "TOAST pointer" representing an out-of-line
87 * Datum that's stored in memory, not in an external toast relation.
88 * The creator of such a Datum is entirely responsible that the referenced
89 * storage survives for as long as referencing pointer Datums can exist.
91 * Note that just as for struct varatt_external, this struct is stored
92 * unaligned within any containing tuple.
94 typedef struct varatt_indirect
96 struct varlena
*pointer
; /* Pointer to in-memory varlena */
100 * struct varatt_expanded is a "TOAST pointer" representing an out-of-line
101 * Datum that is stored in memory, in some type-specific, not necessarily
102 * physically contiguous format that is convenient for computation not
103 * storage. APIs for this, in particular the definition of struct
104 * ExpandedObjectHeader, are in src/include/utils/expandeddatum.h.
106 * Note that just as for struct varatt_external, this struct is stored
107 * unaligned within any containing tuple.
109 typedef struct ExpandedObjectHeader ExpandedObjectHeader
;
111 typedef struct varatt_expanded
113 ExpandedObjectHeader
*eohptr
;
117 * Type tag for the various sorts of "TOAST pointer" datums. The peculiar
118 * value for VARTAG_ONDISK comes from a requirement for on-disk compatibility
119 * with a previous notion that the tag field was the pointer datum's length.
121 typedef enum vartag_external
124 VARTAG_EXPANDED_RO
= 2,
125 VARTAG_EXPANDED_RW
= 3,
129 /* this test relies on the specific tag values above */
130 #define VARTAG_IS_EXPANDED(tag) \
131 (((tag) & ~1) == VARTAG_EXPANDED_RO)
133 #define VARTAG_SIZE(tag) \
134 ((tag) == VARTAG_INDIRECT ? sizeof(varatt_indirect) : \
135 VARTAG_IS_EXPANDED(tag) ? sizeof(varatt_expanded) : \
136 (tag) == VARTAG_ONDISK ? sizeof(varatt_external) : \
137 TrapMacro(true, "unrecognized TOAST vartag"))
140 * These structs describe the header of a varlena object that may have been
141 * TOASTed. Generally, don't reference these structs directly, but use the
144 * We use separate structs for the aligned and unaligned cases because the
145 * compiler might otherwise think it could generate code that assumes
146 * alignment while touching fields of a 1-byte-header varlena.
150 struct /* Normal varlena (4-byte length) */
153 char va_data
[FLEXIBLE_ARRAY_MEMBER
];
155 struct /* Compressed-in-line format */
158 uint32 va_tcinfo
; /* Original data size (excludes header) and
159 * compression method; see va_extinfo */
160 char va_data
[FLEXIBLE_ARRAY_MEMBER
]; /* Compressed data */
167 char va_data
[FLEXIBLE_ARRAY_MEMBER
]; /* Data begins here */
170 /* TOAST pointers are a subset of varattrib_1b with an identifying tag byte */
173 uint8 va_header
; /* Always 0x80 or 0x01 */
174 uint8 va_tag
; /* Type of datum */
175 char va_data
[FLEXIBLE_ARRAY_MEMBER
]; /* Type-specific data */
179 * Bit layouts for varlena headers on big-endian machines:
181 * 00xxxxxx 4-byte length word, aligned, uncompressed data (up to 1G)
182 * 01xxxxxx 4-byte length word, aligned, *compressed* data (up to 1G)
183 * 10000000 1-byte length word, unaligned, TOAST pointer
184 * 1xxxxxxx 1-byte length word, unaligned, uncompressed data (up to 126b)
186 * Bit layouts for varlena headers on little-endian machines:
188 * xxxxxx00 4-byte length word, aligned, uncompressed data (up to 1G)
189 * xxxxxx10 4-byte length word, aligned, *compressed* data (up to 1G)
190 * 00000001 1-byte length word, unaligned, TOAST pointer
191 * xxxxxxx1 1-byte length word, unaligned, uncompressed data (up to 126b)
193 * The "xxx" bits are the length field (which includes itself in all cases).
194 * In the big-endian case we mask to extract the length, in the little-endian
195 * case we shift. Note that in both cases the flag bits are in the physically
196 * first byte. Also, it is not possible for a 1-byte length word to be zero;
197 * this lets us disambiguate alignment padding bytes from the start of an
198 * unaligned datum. (We now *require* pad bytes to be filled with zero!)
200 * In TOAST pointers the va_tag field (see varattrib_1b_e) is used to discern
201 * the specific type and length of the pointer datum.
205 * Endian-dependent macros. These are considered internal --- use the
206 * external macros below instead of using these directly.
208 * Note: IS_1B is true for external toast records but VARSIZE_1B will return 0
209 * for such records. Hence you should usually check for IS_EXTERNAL before
210 * checking for IS_1B.
213 #ifdef WORDS_BIGENDIAN
215 #define VARATT_IS_4B(PTR) \
216 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
217 #define VARATT_IS_4B_U(PTR) \
218 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
219 #define VARATT_IS_4B_C(PTR) \
220 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
221 #define VARATT_IS_1B(PTR) \
222 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
223 #define VARATT_IS_1B_E(PTR) \
224 ((((varattrib_1b *) (PTR))->va_header) == 0x80)
225 #define VARATT_NOT_PAD_BYTE(PTR) \
226 (*((uint8 *) (PTR)) != 0)
228 /* VARSIZE_4B() should only be used on known-aligned data */
229 #define VARSIZE_4B(PTR) \
230 (((varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
231 #define VARSIZE_1B(PTR) \
232 (((varattrib_1b *) (PTR))->va_header & 0x7F)
233 #define VARTAG_1B_E(PTR) \
234 (((varattrib_1b_e *) (PTR))->va_tag)
236 #define SET_VARSIZE_4B(PTR,len) \
237 (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
238 #define SET_VARSIZE_4B_C(PTR,len) \
239 (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 0x40000000)
240 #define SET_VARSIZE_1B(PTR,len) \
241 (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
242 #define SET_VARTAG_1B_E(PTR,tag) \
243 (((varattrib_1b_e *) (PTR))->va_header = 0x80, \
244 ((varattrib_1b_e *) (PTR))->va_tag = (tag))
246 #else /* !WORDS_BIGENDIAN */
248 #define VARATT_IS_4B(PTR) \
249 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
250 #define VARATT_IS_4B_U(PTR) \
251 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
252 #define VARATT_IS_4B_C(PTR) \
253 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
254 #define VARATT_IS_1B(PTR) \
255 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
256 #define VARATT_IS_1B_E(PTR) \
257 ((((varattrib_1b *) (PTR))->va_header) == 0x01)
258 #define VARATT_NOT_PAD_BYTE(PTR) \
259 (*((uint8 *) (PTR)) != 0)
261 /* VARSIZE_4B() should only be used on known-aligned data */
262 #define VARSIZE_4B(PTR) \
263 ((((varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
264 #define VARSIZE_1B(PTR) \
265 ((((varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
266 #define VARTAG_1B_E(PTR) \
267 (((varattrib_1b_e *) (PTR))->va_tag)
269 #define SET_VARSIZE_4B(PTR,len) \
270 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
271 #define SET_VARSIZE_4B_C(PTR,len) \
272 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) | 0x02)
273 #define SET_VARSIZE_1B(PTR,len) \
274 (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
275 #define SET_VARTAG_1B_E(PTR,tag) \
276 (((varattrib_1b_e *) (PTR))->va_header = 0x01, \
277 ((varattrib_1b_e *) (PTR))->va_tag = (tag))
279 #endif /* WORDS_BIGENDIAN */
281 #define VARDATA_4B(PTR) (((varattrib_4b *) (PTR))->va_4byte.va_data)
282 #define VARDATA_4B_C(PTR) (((varattrib_4b *) (PTR))->va_compressed.va_data)
283 #define VARDATA_1B(PTR) (((varattrib_1b *) (PTR))->va_data)
284 #define VARDATA_1B_E(PTR) (((varattrib_1b_e *) (PTR))->va_data)
287 * Externally visible TOAST macros begin here.
290 #define VARHDRSZ_EXTERNAL offsetof(varattrib_1b_e, va_data)
291 #define VARHDRSZ_COMPRESSED offsetof(varattrib_4b, va_compressed.va_data)
292 #define VARHDRSZ_SHORT offsetof(varattrib_1b, va_data)
294 #define VARATT_SHORT_MAX 0x7F
295 #define VARATT_CAN_MAKE_SHORT(PTR) \
296 (VARATT_IS_4B_U(PTR) && \
297 (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT) <= VARATT_SHORT_MAX)
298 #define VARATT_CONVERTED_SHORT_SIZE(PTR) \
299 (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT)
302 * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(),
303 * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call
304 * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16,
305 * int32 or wider field in the struct representing the datum layout requires
306 * aligned data. memcpy() is alignment-oblivious, as are most operations on
307 * datatypes, such as text, whose layout struct contains only char fields.
309 * Code assembling a new datum should call VARDATA() and SET_VARSIZE().
310 * (Datums begin life untoasted.)
312 * Other macros here should usually be used only by tuple assembly/disassembly
313 * code and code that specifically wants to work with still-toasted Datums.
315 #define VARDATA(PTR) VARDATA_4B(PTR)
316 #define VARSIZE(PTR) VARSIZE_4B(PTR)
318 #define VARSIZE_SHORT(PTR) VARSIZE_1B(PTR)
319 #define VARDATA_SHORT(PTR) VARDATA_1B(PTR)
321 #define VARTAG_EXTERNAL(PTR) VARTAG_1B_E(PTR)
322 #define VARSIZE_EXTERNAL(PTR) (VARHDRSZ_EXTERNAL + VARTAG_SIZE(VARTAG_EXTERNAL(PTR)))
323 #define VARDATA_EXTERNAL(PTR) VARDATA_1B_E(PTR)
325 #define VARATT_IS_COMPRESSED(PTR) VARATT_IS_4B_C(PTR)
326 #define VARATT_IS_EXTERNAL(PTR) VARATT_IS_1B_E(PTR)
327 #define VARATT_IS_EXTERNAL_ONDISK(PTR) \
328 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_ONDISK)
329 #define VARATT_IS_EXTERNAL_INDIRECT(PTR) \
330 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_INDIRECT)
331 #define VARATT_IS_EXTERNAL_EXPANDED_RO(PTR) \
332 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RO)
333 #define VARATT_IS_EXTERNAL_EXPANDED_RW(PTR) \
334 (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RW)
335 #define VARATT_IS_EXTERNAL_EXPANDED(PTR) \
336 (VARATT_IS_EXTERNAL(PTR) && VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR)))
337 #define VARATT_IS_EXTERNAL_NON_EXPANDED(PTR) \
338 (VARATT_IS_EXTERNAL(PTR) && !VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR)))
339 #define VARATT_IS_SHORT(PTR) VARATT_IS_1B(PTR)
340 #define VARATT_IS_EXTENDED(PTR) (!VARATT_IS_4B_U(PTR))
342 #define SET_VARSIZE(PTR, len) SET_VARSIZE_4B(PTR, len)
343 #define SET_VARSIZE_SHORT(PTR, len) SET_VARSIZE_1B(PTR, len)
344 #define SET_VARSIZE_COMPRESSED(PTR, len) SET_VARSIZE_4B_C(PTR, len)
346 #define SET_VARTAG_EXTERNAL(PTR, tag) SET_VARTAG_1B_E(PTR, tag)
348 #define VARSIZE_ANY(PTR) \
349 (VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR) : \
350 (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR) : \
353 /* Size of a varlena data, excluding header */
354 #define VARSIZE_ANY_EXHDR(PTR) \
355 (VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR)-VARHDRSZ_EXTERNAL : \
356 (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR)-VARHDRSZ_SHORT : \
357 VARSIZE_4B(PTR)-VARHDRSZ))
359 /* caution: this will not work on an external or compressed-in-line Datum */
360 /* caution: this will return a possibly unaligned pointer */
361 #define VARDATA_ANY(PTR) \
362 (VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR))
364 /* Decompressed size and compression method of an external compressed Datum */
365 #define VARDATA_COMPRESSED_GET_EXTSIZE(PTR) \
366 (((varattrib_4b *) (PTR))->va_compressed.va_tcinfo & VARLENA_EXTSIZE_MASK)
367 #define VARDATA_COMPRESSED_GET_COMPRESS_METHOD(PTR) \
368 (((varattrib_4b *) (PTR))->va_compressed.va_tcinfo >> VARLENA_EXTSIZE_BITS)
370 /* Same, when working directly with a struct varatt_external */
371 #define VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) \
372 ((toast_pointer).va_extinfo & VARLENA_EXTSIZE_MASK)
373 #define VARATT_EXTERNAL_GET_COMPRESS_METHOD(toast_pointer) \
374 ((toast_pointer).va_extinfo >> VARLENA_EXTSIZE_BITS)
376 #define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm) \
378 Assert((cm) == TOAST_PGLZ_COMPRESSION_ID || \
379 (cm) == TOAST_LZ4_COMPRESSION_ID); \
380 ((toast_pointer).va_extinfo = \
381 (len) | ((uint32) (cm) << VARLENA_EXTSIZE_BITS)); \
385 * Testing whether an externally-stored value is compressed now requires
386 * comparing size stored in va_extinfo (the actual length of the external data)
387 * to rawsize (the original uncompressed datum's size). The latter includes
388 * VARHDRSZ overhead, the former doesn't. We never use compression unless it
389 * actually saves space, so we expect either equality or less-than.
391 #define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer) \
392 (VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) < \
393 (toast_pointer).va_rawsize - VARHDRSZ)
396 /* ----------------------------------------------------------------
397 * Section 2: Datum type + support macros
398 * ----------------------------------------------------------------
402 * A Datum contains either a value of a pass-by-value type or a pointer to a
403 * value of a pass-by-reference type. Therefore, we require:
405 * sizeof(Datum) == sizeof(void *) == 4 or 8
407 * The macros below and the analogous macros for other types should be used to
408 * convert between a Datum and the appropriate C type.
411 typedef uintptr_t Datum
;
414 * A NullableDatum is used in places where both a Datum and its nullness needs
415 * to be stored. This can be more efficient than storing datums and nullness
416 * in separate arrays, due to better spatial locality, even if more space may
417 * be wasted due to padding.
419 typedef struct NullableDatum
421 #define FIELDNO_NULLABLE_DATUM_DATUM 0
423 #define FIELDNO_NULLABLE_DATUM_ISNULL 1
425 /* due to alignment padding this could be used for flags for free */
428 #define SIZEOF_DATUM SIZEOF_VOID_P
432 * Returns boolean value of a datum.
434 * Note: any nonzero value will be considered true.
437 #define DatumGetBool(X) ((bool) ((X) != 0))
441 * Returns datum representation for a boolean.
443 * Note: any nonzero value will be considered true.
446 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
450 * Returns character value of a datum.
453 #define DatumGetChar(X) ((char) (X))
457 * Returns datum representation for a character.
460 #define CharGetDatum(X) ((Datum) (X))
464 * Returns datum representation for an 8-bit integer.
467 #define Int8GetDatum(X) ((Datum) (X))
471 * Returns 8-bit unsigned integer value of a datum.
474 #define DatumGetUInt8(X) ((uint8) (X))
478 * Returns datum representation for an 8-bit unsigned integer.
481 #define UInt8GetDatum(X) ((Datum) (X))
485 * Returns 16-bit integer value of a datum.
488 #define DatumGetInt16(X) ((int16) (X))
492 * Returns datum representation for a 16-bit integer.
495 #define Int16GetDatum(X) ((Datum) (X))
499 * Returns 16-bit unsigned integer value of a datum.
502 #define DatumGetUInt16(X) ((uint16) (X))
506 * Returns datum representation for a 16-bit unsigned integer.
509 #define UInt16GetDatum(X) ((Datum) (X))
513 * Returns 32-bit integer value of a datum.
516 #define DatumGetInt32(X) ((int32) (X))
520 * Returns datum representation for a 32-bit integer.
523 #define Int32GetDatum(X) ((Datum) (X))
527 * Returns 32-bit unsigned integer value of a datum.
530 #define DatumGetUInt32(X) ((uint32) (X))
534 * Returns datum representation for a 32-bit unsigned integer.
537 #define UInt32GetDatum(X) ((Datum) (X))
541 * Returns object identifier value of a datum.
544 #define DatumGetObjectId(X) ((Oid) (X))
548 * Returns datum representation for an object identifier.
551 #define ObjectIdGetDatum(X) ((Datum) (X))
554 * DatumGetTransactionId
555 * Returns transaction identifier value of a datum.
558 #define DatumGetTransactionId(X) ((TransactionId) (X))
561 * TransactionIdGetDatum
562 * Returns datum representation for a transaction identifier.
565 #define TransactionIdGetDatum(X) ((Datum) (X))
568 * MultiXactIdGetDatum
569 * Returns datum representation for a multixact identifier.
572 #define MultiXactIdGetDatum(X) ((Datum) (X))
576 * Returns command identifier value of a datum.
579 #define DatumGetCommandId(X) ((CommandId) (X))
583 * Returns datum representation for a command identifier.
586 #define CommandIdGetDatum(X) ((Datum) (X))
590 * Returns pointer value of a datum.
593 #define DatumGetPointer(X) ((Pointer) (X))
597 * Returns datum representation for a pointer.
600 #define PointerGetDatum(X) ((Datum) (X))
604 * Returns C string (null-terminated string) value of a datum.
606 * Note: C string is not a full-fledged Postgres type at present,
607 * but type input functions use this conversion for their inputs.
610 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
614 * Returns datum representation for a C string (null-terminated string).
616 * Note: C string is not a full-fledged Postgres type at present,
617 * but type output functions use this conversion for their outputs.
618 * Note: CString is pass-by-reference; caller must ensure the pointed-to
619 * value has adequate lifetime.
622 #define CStringGetDatum(X) PointerGetDatum(X)
626 * Returns name value of a datum.
629 #define DatumGetName(X) ((Name) DatumGetPointer(X))
633 * Returns datum representation for a name.
635 * Note: Name is pass-by-reference; caller must ensure the pointed-to
636 * value has adequate lifetime.
639 #define NameGetDatum(X) CStringGetDatum(NameStr(*(X)))
643 * Returns 64-bit integer value of a datum.
645 * Note: this macro hides whether int64 is pass by value or by reference.
648 #ifdef USE_FLOAT8_BYVAL
649 #define DatumGetInt64(X) ((int64) (X))
651 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
656 * Returns datum representation for a 64-bit integer.
658 * Note: if int64 is pass by reference, this function returns a reference
662 #ifdef USE_FLOAT8_BYVAL
663 #define Int64GetDatum(X) ((Datum) (X))
665 extern Datum
Int64GetDatum(int64 X
);
670 * Returns 64-bit unsigned integer value of a datum.
672 * Note: this macro hides whether int64 is pass by value or by reference.
675 #ifdef USE_FLOAT8_BYVAL
676 #define DatumGetUInt64(X) ((uint64) (X))
678 #define DatumGetUInt64(X) (* ((uint64 *) DatumGetPointer(X)))
683 * Returns datum representation for a 64-bit unsigned integer.
685 * Note: if int64 is pass by reference, this function returns a reference
689 #ifdef USE_FLOAT8_BYVAL
690 #define UInt64GetDatum(X) ((Datum) (X))
692 #define UInt64GetDatum(X) Int64GetDatum((int64) (X))
696 * Float <-> Datum conversions
698 * These have to be implemented as inline functions rather than macros, when
699 * passing by value, because many machines pass int and float function
700 * parameters/results differently; so we need to play weird games with unions.
705 * Returns 4-byte floating point value of a datum.
708 DatumGetFloat4(Datum X
)
716 myunion
.value
= DatumGetInt32(X
);
717 return myunion
.retval
;
722 * Returns datum representation for a 4-byte floating point number.
725 Float4GetDatum(float4 X
)
734 return Int32GetDatum(myunion
.retval
);
739 * Returns 8-byte floating point value of a datum.
741 * Note: this macro hides whether float8 is pass by value or by reference.
744 #ifdef USE_FLOAT8_BYVAL
746 DatumGetFloat8(Datum X
)
754 myunion
.value
= DatumGetInt64(X
);
755 return myunion
.retval
;
758 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
763 * Returns datum representation for an 8-byte floating point number.
765 * Note: if float8 is pass by reference, this function returns a reference
769 #ifdef USE_FLOAT8_BYVAL
771 Float8GetDatum(float8 X
)
780 return Int64GetDatum(myunion
.retval
);
783 extern Datum
Float8GetDatum(float8 X
);
791 * These macros are intended to allow writing code that does not depend on
792 * whether int64 and float8 are pass-by-reference types, while not
793 * sacrificing performance when they are. The argument must be a variable
794 * that will exist and have the same value for as long as the Datum is needed.
795 * In the pass-by-ref case, the address of the variable is taken to use as
796 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
800 #ifdef USE_FLOAT8_BYVAL
801 #define Int64GetDatumFast(X) Int64GetDatum(X)
802 #define Float8GetDatumFast(X) Float8GetDatum(X)
804 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
805 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
808 #endif /* POSTGRES_H */