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 #include "utils/elog.h"
48 #include "utils/palloc.h"
49 
50 /* ----------------------------------------------------------------
51  *				Section 1:	variable-length datatypes (TOAST support)
52  * ----------------------------------------------------------------
53  */
54 
55 /*
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.
60  *
61  * This struct must not contain any padding, because we sometimes compare
62  * these pointers using memcmp.
63  *
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...)
68  */
69 typedef struct varatt_external
70 {
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 */
76 }			varatt_external;
77 
78 /*
79  * These macros define the "saved size" portion of va_extinfo.  Its remaining
80  * two high-order bits identify the compression method.
81  */
82 #define VARLENA_EXTSIZE_BITS	30
83 #define VARLENA_EXTSIZE_MASK	((1U << VARLENA_EXTSIZE_BITS) - 1)
84 
85 /*
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.
90  *
91  * Note that just as for struct varatt_external, this struct is stored
92  * unaligned within any containing tuple.
93  */
94 typedef struct varatt_indirect
95 {
96 	struct varlena *pointer;	/* Pointer to in-memory varlena */
97 }			varatt_indirect;
98 
99 /*
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.
105  *
106  * Note that just as for struct varatt_external, this struct is stored
107  * unaligned within any containing tuple.
108  */
109 typedef struct ExpandedObjectHeader ExpandedObjectHeader;
110 
111 typedef struct varatt_expanded
112 {
113 	ExpandedObjectHeader *eohptr;
114 } varatt_expanded;
115 
116 /*
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.
120  */
121 typedef enum vartag_external
122 {
123 	VARTAG_INDIRECT = 1,
124 	VARTAG_EXPANDED_RO = 2,
125 	VARTAG_EXPANDED_RW = 3,
126 	VARTAG_ONDISK = 18
127 } vartag_external;
128 
129 /* this test relies on the specific tag values above */
130 #define VARTAG_IS_EXPANDED(tag) \
131 	(((tag) & ~1) == VARTAG_EXPANDED_RO)
132 
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"))
138 
139 /*
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
142  * macros below.
143  *
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.
147  */
148 typedef union
149 {
150 	struct						/* Normal varlena (4-byte length) */
151 	{
152 		uint32		va_header;
153 		char		va_data[FLEXIBLE_ARRAY_MEMBER];
154 	}			va_4byte;
155 	struct						/* Compressed-in-line format */
156 	{
157 		uint32		va_header;
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 */
161 	}			va_compressed;
162 } varattrib_4b;
163 
164 typedef struct
165 {
166 	uint8		va_header;
167 	char		va_data[FLEXIBLE_ARRAY_MEMBER]; /* Data begins here */
168 } varattrib_1b;
169 
170 /* TOAST pointers are a subset of varattrib_1b with an identifying tag byte */
171 typedef struct
172 {
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 */
176 } varattrib_1b_e;
177 
178 /*
179  * Bit layouts for varlena headers on big-endian machines:
180  *
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)
185  *
186  * Bit layouts for varlena headers on little-endian machines:
187  *
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)
192  *
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!)
199  *
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.
202  */
203 
204 /*
205  * Endian-dependent macros.  These are considered internal --- use the
206  * external macros below instead of using these directly.
207  *
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.
211  */
212 
213 #ifdef WORDS_BIGENDIAN
214 
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)
227 
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)
235 
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))
245 
246 #else							/* !WORDS_BIGENDIAN */
247 
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)
260 
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)
268 
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))
278 
279 #endif							/* WORDS_BIGENDIAN */
280 
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)
285 
286 /*
287  * Externally visible TOAST macros begin here.
288  */
289 
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)
293 
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)
300 
301 /*
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.
308  *
309  * Code assembling a new datum should call VARDATA() and SET_VARSIZE().
310  * (Datums begin life untoasted.)
311  *
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.
314  */
315 #define VARDATA(PTR)						VARDATA_4B(PTR)
316 #define VARSIZE(PTR)						VARSIZE_4B(PTR)
317 
318 #define VARSIZE_SHORT(PTR)					VARSIZE_1B(PTR)
319 #define VARDATA_SHORT(PTR)					VARDATA_1B(PTR)
320 
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)
324 
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))
341 
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)
345 
346 #define SET_VARTAG_EXTERNAL(PTR, tag)		SET_VARTAG_1B_E(PTR, tag)
347 
348 #define VARSIZE_ANY(PTR) \
349 	(VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR) : \
350 	 (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR) : \
351 	  VARSIZE_4B(PTR)))
352 
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))
358 
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))
363 
364 /* Decompressed size and compression method of a compressed-in-line 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)
369 
370 /* Same for external Datums; but note argument is 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)
375 
376 #define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm) \
377 	do { \
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)); \
382 	} while (0)
383 
384 /*
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.
390  */
391 #define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer) \
392 	(VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) < \
393 	 (toast_pointer).va_rawsize - VARHDRSZ)
394 
395 
396 /* ----------------------------------------------------------------
397  *				Section 2:	Datum type + support macros
398  * ----------------------------------------------------------------
399  */
400 
401 /*
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:
404  *
405  * sizeof(Datum) == sizeof(void *) == 4 or 8
406  *
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.
409  */
410 
411 typedef uintptr_t Datum;
412 
413 /*
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.
418  */
419 typedef struct NullableDatum
420 {
421 #define FIELDNO_NULLABLE_DATUM_DATUM 0
422 	Datum		value;
423 #define FIELDNO_NULLABLE_DATUM_ISNULL 1
424 	bool		isnull;
425 	/* due to alignment padding this could be used for flags for free */
426 } NullableDatum;
427 
428 #define SIZEOF_DATUM SIZEOF_VOID_P
429 
430 /*
431  * DatumGetBool
432  *		Returns boolean value of a datum.
433  *
434  * Note: any nonzero value will be considered true.
435  */
436 
437 #define DatumGetBool(X) ((bool) ((X) != 0))
438 
439 /*
440  * BoolGetDatum
441  *		Returns datum representation for a boolean.
442  *
443  * Note: any nonzero value will be considered true.
444  */
445 
446 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
447 
448 /*
449  * DatumGetChar
450  *		Returns character value of a datum.
451  */
452 
453 #define DatumGetChar(X) ((char) (X))
454 
455 /*
456  * CharGetDatum
457  *		Returns datum representation for a character.
458  */
459 
460 #define CharGetDatum(X) ((Datum) (X))
461 
462 /*
463  * Int8GetDatum
464  *		Returns datum representation for an 8-bit integer.
465  */
466 
467 #define Int8GetDatum(X) ((Datum) (X))
468 
469 /*
470  * DatumGetUInt8
471  *		Returns 8-bit unsigned integer value of a datum.
472  */
473 
474 #define DatumGetUInt8(X) ((uint8) (X))
475 
476 /*
477  * UInt8GetDatum
478  *		Returns datum representation for an 8-bit unsigned integer.
479  */
480 
481 #define UInt8GetDatum(X) ((Datum) (X))
482 
483 /*
484  * DatumGetInt16
485  *		Returns 16-bit integer value of a datum.
486  */
487 
488 #define DatumGetInt16(X) ((int16) (X))
489 
490 /*
491  * Int16GetDatum
492  *		Returns datum representation for a 16-bit integer.
493  */
494 
495 #define Int16GetDatum(X) ((Datum) (X))
496 
497 /*
498  * DatumGetUInt16
499  *		Returns 16-bit unsigned integer value of a datum.
500  */
501 
502 #define DatumGetUInt16(X) ((uint16) (X))
503 
504 /*
505  * UInt16GetDatum
506  *		Returns datum representation for a 16-bit unsigned integer.
507  */
508 
509 #define UInt16GetDatum(X) ((Datum) (X))
510 
511 /*
512  * DatumGetInt32
513  *		Returns 32-bit integer value of a datum.
514  */
515 
516 #define DatumGetInt32(X) ((int32) (X))
517 
518 /*
519  * Int32GetDatum
520  *		Returns datum representation for a 32-bit integer.
521  */
522 
523 #define Int32GetDatum(X) ((Datum) (X))
524 
525 /*
526  * DatumGetUInt32
527  *		Returns 32-bit unsigned integer value of a datum.
528  */
529 
530 #define DatumGetUInt32(X) ((uint32) (X))
531 
532 /*
533  * UInt32GetDatum
534  *		Returns datum representation for a 32-bit unsigned integer.
535  */
536 
537 #define UInt32GetDatum(X) ((Datum) (X))
538 
539 /*
540  * DatumGetObjectId
541  *		Returns object identifier value of a datum.
542  */
543 
544 #define DatumGetObjectId(X) ((Oid) (X))
545 
546 /*
547  * ObjectIdGetDatum
548  *		Returns datum representation for an object identifier.
549  */
550 
551 #define ObjectIdGetDatum(X) ((Datum) (X))
552 
553 /*
554  * DatumGetTransactionId
555  *		Returns transaction identifier value of a datum.
556  */
557 
558 #define DatumGetTransactionId(X) ((TransactionId) (X))
559 
560 /*
561  * TransactionIdGetDatum
562  *		Returns datum representation for a transaction identifier.
563  */
564 
565 #define TransactionIdGetDatum(X) ((Datum) (X))
566 
567 /*
568  * MultiXactIdGetDatum
569  *		Returns datum representation for a multixact identifier.
570  */
571 
572 #define MultiXactIdGetDatum(X) ((Datum) (X))
573 
574 /*
575  * DatumGetCommandId
576  *		Returns command identifier value of a datum.
577  */
578 
579 #define DatumGetCommandId(X) ((CommandId) (X))
580 
581 /*
582  * CommandIdGetDatum
583  *		Returns datum representation for a command identifier.
584  */
585 
586 #define CommandIdGetDatum(X) ((Datum) (X))
587 
588 /*
589  * DatumGetPointer
590  *		Returns pointer value of a datum.
591  */
592 
593 #define DatumGetPointer(X) ((Pointer) (X))
594 
595 /*
596  * PointerGetDatum
597  *		Returns datum representation for a pointer.
598  */
599 
600 #define PointerGetDatum(X) ((Datum) (X))
601 
602 /*
603  * DatumGetCString
604  *		Returns C string (null-terminated string) value of a datum.
605  *
606  * Note: C string is not a full-fledged Postgres type at present,
607  * but type input functions use this conversion for their inputs.
608  */
609 
610 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
611 
612 /*
613  * CStringGetDatum
614  *		Returns datum representation for a C string (null-terminated string).
615  *
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.
620  */
621 
622 #define CStringGetDatum(X) PointerGetDatum(X)
623 
624 /*
625  * DatumGetName
626  *		Returns name value of a datum.
627  */
628 
629 #define DatumGetName(X) ((Name) DatumGetPointer(X))
630 
631 /*
632  * NameGetDatum
633  *		Returns datum representation for a name.
634  *
635  * Note: Name is pass-by-reference; caller must ensure the pointed-to
636  * value has adequate lifetime.
637  */
638 
639 #define NameGetDatum(X) CStringGetDatum(NameStr(*(X)))
640 
641 /*
642  * DatumGetInt64
643  *		Returns 64-bit integer value of a datum.
644  *
645  * Note: this macro hides whether int64 is pass by value or by reference.
646  */
647 
648 #ifdef USE_FLOAT8_BYVAL
649 #define DatumGetInt64(X) ((int64) (X))
650 #else
651 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
652 #endif
653 
654 /*
655  * Int64GetDatum
656  *		Returns datum representation for a 64-bit integer.
657  *
658  * Note: if int64 is pass by reference, this function returns a reference
659  * to palloc'd space.
660  */
661 
662 #ifdef USE_FLOAT8_BYVAL
663 #define Int64GetDatum(X) ((Datum) (X))
664 #else
665 extern Datum Int64GetDatum(int64 X);
666 #endif
667 
668 /*
669  * DatumGetUInt64
670  *		Returns 64-bit unsigned integer value of a datum.
671  *
672  * Note: this macro hides whether int64 is pass by value or by reference.
673  */
674 
675 #ifdef USE_FLOAT8_BYVAL
676 #define DatumGetUInt64(X) ((uint64) (X))
677 #else
678 #define DatumGetUInt64(X) (* ((uint64 *) DatumGetPointer(X)))
679 #endif
680 
681 /*
682  * UInt64GetDatum
683  *		Returns datum representation for a 64-bit unsigned integer.
684  *
685  * Note: if int64 is pass by reference, this function returns a reference
686  * to palloc'd space.
687  */
688 
689 #ifdef USE_FLOAT8_BYVAL
690 #define UInt64GetDatum(X) ((Datum) (X))
691 #else
692 #define UInt64GetDatum(X) Int64GetDatum((int64) (X))
693 #endif
694 
695 /*
696  * Float <-> Datum conversions
697  *
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.
701  */
702 
703 /*
704  * DatumGetFloat4
705  *		Returns 4-byte floating point value of a datum.
706  */
707 static inline float4
DatumGetFloat4(Datum X)708 DatumGetFloat4(Datum X)
709 {
710 	union
711 	{
712 		int32		value;
713 		float4		retval;
714 	}			myunion;
715 
716 	myunion.value = DatumGetInt32(X);
717 	return myunion.retval;
718 }
719 
720 /*
721  * Float4GetDatum
722  *		Returns datum representation for a 4-byte floating point number.
723  */
724 static inline Datum
Float4GetDatum(float4 X)725 Float4GetDatum(float4 X)
726 {
727 	union
728 	{
729 		float4		value;
730 		int32		retval;
731 	}			myunion;
732 
733 	myunion.value = X;
734 	return Int32GetDatum(myunion.retval);
735 }
736 
737 /*
738  * DatumGetFloat8
739  *		Returns 8-byte floating point value of a datum.
740  *
741  * Note: this macro hides whether float8 is pass by value or by reference.
742  */
743 
744 #ifdef USE_FLOAT8_BYVAL
745 static inline float8
DatumGetFloat8(Datum X)746 DatumGetFloat8(Datum X)
747 {
748 	union
749 	{
750 		int64		value;
751 		float8		retval;
752 	}			myunion;
753 
754 	myunion.value = DatumGetInt64(X);
755 	return myunion.retval;
756 }
757 #else
758 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
759 #endif
760 
761 /*
762  * Float8GetDatum
763  *		Returns datum representation for an 8-byte floating point number.
764  *
765  * Note: if float8 is pass by reference, this function returns a reference
766  * to palloc'd space.
767  */
768 
769 #ifdef USE_FLOAT8_BYVAL
770 static inline Datum
Float8GetDatum(float8 X)771 Float8GetDatum(float8 X)
772 {
773 	union
774 	{
775 		float8		value;
776 		int64		retval;
777 	}			myunion;
778 
779 	myunion.value = X;
780 	return Int64GetDatum(myunion.retval);
781 }
782 #else
783 extern Datum Float8GetDatum(float8 X);
784 #endif
785 
786 
787 /*
788  * Int64GetDatumFast
789  * Float8GetDatumFast
790  *
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
797  * macros.
798  */
799 
800 #ifdef USE_FLOAT8_BYVAL
801 #define Int64GetDatumFast(X)  Int64GetDatum(X)
802 #define Float8GetDatumFast(X) Float8GetDatum(X)
803 #else
804 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
805 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
806 #endif
807 
808 #endif							/* POSTGRES_H */
809