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