postgres.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 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. #include "c.h"
  46. #include "utils/elog.h"
  47. #include "utils/palloc.h"
  48. /* ----------------------------------------------------------------
  49. * Section 1: variable-length datatypes (TOAST support)
  50. * ----------------------------------------------------------------
  51. */
  52. /*
  53. * struct varatt_external is a traditional "TOAST pointer", that is, the
  54. * information needed to fetch a Datum stored out-of-line in a TOAST table.
  55. * The data is compressed if and only if the external size stored in
  56. * va_extinfo is less than va_rawsize - VARHDRSZ.
  57. *
  58. * This struct must not contain any padding, because we sometimes compare
  59. * these pointers using memcmp.
  60. *
  61. * Note that this information is stored unaligned within actual tuples, so
  62. * you need to memcpy from the tuple into a local struct variable before
  63. * you can look at these fields! (The reason we use memcmp is to avoid
  64. * having to do that just to detect equality of two TOAST pointers...)
  65. */
  66. typedef struct varatt_external
  67. {
  68. int32 va_rawsize; /* Original data size (includes header) */
  69. uint32 va_extinfo; /* External saved size (without header) and
  70. * compression method */
  71. Oid va_valueid; /* Unique ID of value within TOAST table */
  72. Oid va_toastrelid; /* RelID of TOAST table containing it */
  73. } varatt_external;
  74. /*
  75. * These macros define the "saved size" portion of va_extinfo. Its remaining
  76. * two high-order bits identify the compression method.
  77. */
  78. #define VARLENA_EXTSIZE_BITS 30
  79. #define VARLENA_EXTSIZE_MASK ((1U << VARLENA_EXTSIZE_BITS) - 1)
  80. /*
  81. * struct varatt_indirect is a "TOAST pointer" representing an out-of-line
  82. * Datum that's stored in memory, not in an external toast relation.
  83. * The creator of such a Datum is entirely responsible that the referenced
  84. * storage survives for as long as referencing pointer Datums can exist.
  85. *
  86. * Note that just as for struct varatt_external, this struct is stored
  87. * unaligned within any containing tuple.
  88. */
  89. typedef struct varatt_indirect
  90. {
  91. struct varlena *pointer; /* Pointer to in-memory varlena */
  92. } varatt_indirect;
  93. /*
  94. * struct varatt_expanded is a "TOAST pointer" representing an out-of-line
  95. * Datum that is stored in memory, in some type-specific, not necessarily
  96. * physically contiguous format that is convenient for computation not
  97. * storage. APIs for this, in particular the definition of struct
  98. * ExpandedObjectHeader, are in src/include/utils/expandeddatum.h.
  99. *
  100. * Note that just as for struct varatt_external, this struct is stored
  101. * unaligned within any containing tuple.
  102. */
  103. typedef struct ExpandedObjectHeader ExpandedObjectHeader;
  104. typedef struct varatt_expanded
  105. {
  106. ExpandedObjectHeader *eohptr;
  107. } varatt_expanded;
  108. /*
  109. * Type tag for the various sorts of "TOAST pointer" datums. The peculiar
  110. * value for VARTAG_ONDISK comes from a requirement for on-disk compatibility
  111. * with a previous notion that the tag field was the pointer datum's length.
  112. */
  113. typedef enum vartag_external
  114. {
  115. VARTAG_INDIRECT = 1,
  116. VARTAG_EXPANDED_RO = 2,
  117. VARTAG_EXPANDED_RW = 3,
  118. VARTAG_ONDISK = 18
  119. } vartag_external;
  120. /* this test relies on the specific tag values above */
  121. #define VARTAG_IS_EXPANDED(tag) \
  122. (((tag) & ~1) == VARTAG_EXPANDED_RO)
  123. #define VARTAG_SIZE(tag) \
  124. ((tag) == VARTAG_INDIRECT ? sizeof(varatt_indirect) : \
  125. VARTAG_IS_EXPANDED(tag) ? sizeof(varatt_expanded) : \
  126. (tag) == VARTAG_ONDISK ? sizeof(varatt_external) : \
  127. TrapMacro(true, "unrecognized TOAST vartag"))
  128. /*
  129. * These structs describe the header of a varlena object that may have been
  130. * TOASTed. Generally, don't reference these structs directly, but use the
  131. * macros below.
  132. *
  133. * We use separate structs for the aligned and unaligned cases because the
  134. * compiler might otherwise think it could generate code that assumes
  135. * alignment while touching fields of a 1-byte-header varlena.
  136. */
  137. typedef union
  138. {
  139. struct /* Normal varlena (4-byte length) */
  140. {
  141. uint32 va_header;
  142. char va_data[FLEXIBLE_ARRAY_MEMBER];
  143. } va_4byte;
  144. struct /* Compressed-in-line format */
  145. {
  146. uint32 va_header;
  147. uint32 va_tcinfo; /* Original data size (excludes header) and
  148. * compression method; see va_extinfo */
  149. char va_data[FLEXIBLE_ARRAY_MEMBER]; /* Compressed data */
  150. } va_compressed;
  151. } varattrib_4b;
  152. typedef struct
  153. {
  154. uint8 va_header;
  155. char va_data[FLEXIBLE_ARRAY_MEMBER]; /* Data begins here */
  156. } varattrib_1b;
  157. /* TOAST pointers are a subset of varattrib_1b with an identifying tag byte */
  158. typedef struct
  159. {
  160. uint8 va_header; /* Always 0x80 or 0x01 */
  161. uint8 va_tag; /* Type of datum */
  162. char va_data[FLEXIBLE_ARRAY_MEMBER]; /* Type-specific data */
  163. } varattrib_1b_e;
  164. /*
  165. * Bit layouts for varlena headers on big-endian machines:
  166. *
  167. * 00xxxxxx 4-byte length word, aligned, uncompressed data (up to 1G)
  168. * 01xxxxxx 4-byte length word, aligned, *compressed* data (up to 1G)
  169. * 10000000 1-byte length word, unaligned, TOAST pointer
  170. * 1xxxxxxx 1-byte length word, unaligned, uncompressed data (up to 126b)
  171. *
  172. * Bit layouts for varlena headers on little-endian machines:
  173. *
  174. * xxxxxx00 4-byte length word, aligned, uncompressed data (up to 1G)
  175. * xxxxxx10 4-byte length word, aligned, *compressed* data (up to 1G)
  176. * 00000001 1-byte length word, unaligned, TOAST pointer
  177. * xxxxxxx1 1-byte length word, unaligned, uncompressed data (up to 126b)
  178. *
  179. * The "xxx" bits are the length field (which includes itself in all cases).
  180. * In the big-endian case we mask to extract the length, in the little-endian
  181. * case we shift. Note that in both cases the flag bits are in the physically
  182. * first byte. Also, it is not possible for a 1-byte length word to be zero;
  183. * this lets us disambiguate alignment padding bytes from the start of an
  184. * unaligned datum. (We now *require* pad bytes to be filled with zero!)
  185. *
  186. * In TOAST pointers the va_tag field (see varattrib_1b_e) is used to discern
  187. * the specific type and length of the pointer datum.
  188. */
  189. /*
  190. * Endian-dependent macros. These are considered internal --- use the
  191. * external macros below instead of using these directly.
  192. *
  193. * Note: IS_1B is true for external toast records but VARSIZE_1B will return 0
  194. * for such records. Hence you should usually check for IS_EXTERNAL before
  195. * checking for IS_1B.
  196. */
  197. #ifdef WORDS_BIGENDIAN
  198. #define VARATT_IS_4B(PTR) \
  199. ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
  200. #define VARATT_IS_4B_U(PTR) \
  201. ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
  202. #define VARATT_IS_4B_C(PTR) \
  203. ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
  204. #define VARATT_IS_1B(PTR) \
  205. ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
  206. #define VARATT_IS_1B_E(PTR) \
  207. ((((varattrib_1b *) (PTR))->va_header) == 0x80)
  208. #define VARATT_NOT_PAD_BYTE(PTR) \
  209. (*((uint8 *) (PTR)) != 0)
  210. /* VARSIZE_4B() should only be used on known-aligned data */
  211. #define VARSIZE_4B(PTR) \
  212. (((varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
  213. #define VARSIZE_1B(PTR) \
  214. (((varattrib_1b *) (PTR))->va_header & 0x7F)
  215. #define VARTAG_1B_E(PTR) \
  216. (((varattrib_1b_e *) (PTR))->va_tag)
  217. #define SET_VARSIZE_4B(PTR,len) \
  218. (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
  219. #define SET_VARSIZE_4B_C(PTR,len) \
  220. (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 0x40000000)
  221. #define SET_VARSIZE_1B(PTR,len) \
  222. (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
  223. #define SET_VARTAG_1B_E(PTR,tag) \
  224. (((varattrib_1b_e *) (PTR))->va_header = 0x80, \
  225. ((varattrib_1b_e *) (PTR))->va_tag = (tag))
  226. #else /* !WORDS_BIGENDIAN */
  227. #define VARATT_IS_4B(PTR) \
  228. ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
  229. #define VARATT_IS_4B_U(PTR) \
  230. ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
  231. #define VARATT_IS_4B_C(PTR) \
  232. ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
  233. #define VARATT_IS_1B(PTR) \
  234. ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
  235. #define VARATT_IS_1B_E(PTR) \
  236. ((((varattrib_1b *) (PTR))->va_header) == 0x01)
  237. #define VARATT_NOT_PAD_BYTE(PTR) \
  238. (*((uint8 *) (PTR)) != 0)
  239. /* VARSIZE_4B() should only be used on known-aligned data */
  240. #define VARSIZE_4B(PTR) \
  241. ((((varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
  242. #define VARSIZE_1B(PTR) \
  243. ((((varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
  244. #define VARTAG_1B_E(PTR) \
  245. (((varattrib_1b_e *) (PTR))->va_tag)
  246. #define SET_VARSIZE_4B(PTR,len) \
  247. (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
  248. #define SET_VARSIZE_4B_C(PTR,len) \
  249. (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) | 0x02)
  250. #define SET_VARSIZE_1B(PTR,len) \
  251. (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
  252. #define SET_VARTAG_1B_E(PTR,tag) \
  253. (((varattrib_1b_e *) (PTR))->va_header = 0x01, \
  254. ((varattrib_1b_e *) (PTR))->va_tag = (tag))
  255. #endif /* WORDS_BIGENDIAN */
  256. #define VARDATA_4B(PTR) (((varattrib_4b *) (PTR))->va_4byte.va_data)
  257. #define VARDATA_4B_C(PTR) (((varattrib_4b *) (PTR))->va_compressed.va_data)
  258. #define VARDATA_1B(PTR) (((varattrib_1b *) (PTR))->va_data)
  259. #define VARDATA_1B_E(PTR) (((varattrib_1b_e *) (PTR))->va_data)
  260. /*
  261. * Externally visible TOAST macros begin here.
  262. */
  263. #define VARHDRSZ_EXTERNAL offsetof(varattrib_1b_e, va_data)
  264. #define VARHDRSZ_COMPRESSED offsetof(varattrib_4b, va_compressed.va_data)
  265. #define VARHDRSZ_SHORT offsetof(varattrib_1b, va_data)
  266. #define VARATT_SHORT_MAX 0x7F
  267. #define VARATT_CAN_MAKE_SHORT(PTR) \
  268. (VARATT_IS_4B_U(PTR) && \
  269. (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT) <= VARATT_SHORT_MAX)
  270. #define VARATT_CONVERTED_SHORT_SIZE(PTR) \
  271. (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT)
  272. /*
  273. * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(),
  274. * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call
  275. * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16,
  276. * int32 or wider field in the struct representing the datum layout requires
  277. * aligned data. memcpy() is alignment-oblivious, as are most operations on
  278. * datatypes, such as text, whose layout struct contains only char fields.
  279. *
  280. * Code assembling a new datum should call VARDATA() and SET_VARSIZE().
  281. * (Datums begin life untoasted.)
  282. *
  283. * Other macros here should usually be used only by tuple assembly/disassembly
  284. * code and code that specifically wants to work with still-toasted Datums.
  285. */
  286. #define VARDATA(PTR) VARDATA_4B(PTR)
  287. #define VARSIZE(PTR) VARSIZE_4B(PTR)
  288. #define VARSIZE_SHORT(PTR) VARSIZE_1B(PTR)
  289. #define VARDATA_SHORT(PTR) VARDATA_1B(PTR)
  290. #define VARTAG_EXTERNAL(PTR) VARTAG_1B_E(PTR)
  291. #define VARSIZE_EXTERNAL(PTR) (VARHDRSZ_EXTERNAL + VARTAG_SIZE(VARTAG_EXTERNAL(PTR)))
  292. #define VARDATA_EXTERNAL(PTR) VARDATA_1B_E(PTR)
  293. #define VARATT_IS_COMPRESSED(PTR) VARATT_IS_4B_C(PTR)
  294. #define VARATT_IS_EXTERNAL(PTR) VARATT_IS_1B_E(PTR)
  295. #define VARATT_IS_EXTERNAL_ONDISK(PTR) \
  296. (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_ONDISK)
  297. #define VARATT_IS_EXTERNAL_INDIRECT(PTR) \
  298. (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_INDIRECT)
  299. #define VARATT_IS_EXTERNAL_EXPANDED_RO(PTR) \
  300. (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RO)
  301. #define VARATT_IS_EXTERNAL_EXPANDED_RW(PTR) \
  302. (VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RW)
  303. #define VARATT_IS_EXTERNAL_EXPANDED(PTR) \
  304. (VARATT_IS_EXTERNAL(PTR) && VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR)))
  305. #define VARATT_IS_EXTERNAL_NON_EXPANDED(PTR) \
  306. (VARATT_IS_EXTERNAL(PTR) && !VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR)))
  307. #define VARATT_IS_SHORT(PTR) VARATT_IS_1B(PTR)
  308. #define VARATT_IS_EXTENDED(PTR) (!VARATT_IS_4B_U(PTR))
  309. #define SET_VARSIZE(PTR, len) SET_VARSIZE_4B(PTR, len)
  310. #define SET_VARSIZE_SHORT(PTR, len) SET_VARSIZE_1B(PTR, len)
  311. #define SET_VARSIZE_COMPRESSED(PTR, len) SET_VARSIZE_4B_C(PTR, len)
  312. #define SET_VARTAG_EXTERNAL(PTR, tag) SET_VARTAG_1B_E(PTR, tag)
  313. #define VARSIZE_ANY(PTR) \
  314. (VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR) : \
  315. (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR) : \
  316. VARSIZE_4B(PTR)))
  317. /* Size of a varlena data, excluding header */
  318. #define VARSIZE_ANY_EXHDR(PTR) \
  319. (VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR)-VARHDRSZ_EXTERNAL : \
  320. (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR)-VARHDRSZ_SHORT : \
  321. VARSIZE_4B(PTR)-VARHDRSZ))
  322. /* caution: this will not work on an external or compressed-in-line Datum */
  323. /* caution: this will return a possibly unaligned pointer */
  324. #define VARDATA_ANY(PTR) \
  325. (VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR))
  326. /* Decompressed size and compression method of a compressed-in-line Datum */
  327. #define VARDATA_COMPRESSED_GET_EXTSIZE(PTR) \
  328. (((varattrib_4b *) (PTR))->va_compressed.va_tcinfo & VARLENA_EXTSIZE_MASK)
  329. #define VARDATA_COMPRESSED_GET_COMPRESS_METHOD(PTR) \
  330. (((varattrib_4b *) (PTR))->va_compressed.va_tcinfo >> VARLENA_EXTSIZE_BITS)
  331. /* Same for external Datums; but note argument is a struct varatt_external */
  332. #define VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) \
  333. ((toast_pointer).va_extinfo & VARLENA_EXTSIZE_MASK)
  334. #define VARATT_EXTERNAL_GET_COMPRESS_METHOD(toast_pointer) \
  335. ((toast_pointer).va_extinfo >> VARLENA_EXTSIZE_BITS)
  336. #define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm) \
  337. do { \
  338. Assert((cm) == TOAST_PGLZ_COMPRESSION_ID || \
  339. (cm) == TOAST_LZ4_COMPRESSION_ID); \
  340. ((toast_pointer).va_extinfo = \
  341. (len) | ((uint32) (cm) << VARLENA_EXTSIZE_BITS)); \
  342. } while (0)
  343. /*
  344. * Testing whether an externally-stored value is compressed now requires
  345. * comparing size stored in va_extinfo (the actual length of the external data)
  346. * to rawsize (the original uncompressed datum's size). The latter includes
  347. * VARHDRSZ overhead, the former doesn't. We never use compression unless it
  348. * actually saves space, so we expect either equality or less-than.
  349. */
  350. #define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer) \
  351. (VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer) < \
  352. (toast_pointer).va_rawsize - VARHDRSZ)
  353. /* ----------------------------------------------------------------
  354. * Section 2: Datum type + support macros
  355. * ----------------------------------------------------------------
  356. */
  357. /*
  358. * A Datum contains either a value of a pass-by-value type or a pointer to a
  359. * value of a pass-by-reference type. Therefore, we require:
  360. *
  361. * sizeof(Datum) == sizeof(void *) == 4 or 8
  362. *
  363. * The macros below and the analogous macros for other types should be used to
  364. * convert between a Datum and the appropriate C type.
  365. */
  366. typedef uintptr_t Datum;
  367. /*
  368. * A NullableDatum is used in places where both a Datum and its nullness needs
  369. * to be stored. This can be more efficient than storing datums and nullness
  370. * in separate arrays, due to better spatial locality, even if more space may
  371. * be wasted due to padding.
  372. */
  373. typedef struct NullableDatum
  374. {
  375. #define FIELDNO_NULLABLE_DATUM_DATUM 0
  376. Datum value;
  377. #define FIELDNO_NULLABLE_DATUM_ISNULL 1
  378. bool isnull;
  379. /* due to alignment padding this could be used for flags for free */
  380. } NullableDatum;
  381. #define SIZEOF_DATUM SIZEOF_VOID_P
  382. /*
  383. * DatumGetBool
  384. * Returns boolean value of a datum.
  385. *
  386. * Note: any nonzero value will be considered true.
  387. */
  388. #define DatumGetBool(X) ((bool) ((X) != 0))
  389. /*
  390. * BoolGetDatum
  391. * Returns datum representation for a boolean.
  392. *
  393. * Note: any nonzero value will be considered true.
  394. */
  395. #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
  396. /*
  397. * DatumGetChar
  398. * Returns character value of a datum.
  399. */
  400. #define DatumGetChar(X) ((char) (X))
  401. /*
  402. * CharGetDatum
  403. * Returns datum representation for a character.
  404. */
  405. #define CharGetDatum(X) ((Datum) (X))
  406. /*
  407. * Int8GetDatum
  408. * Returns datum representation for an 8-bit integer.
  409. */
  410. #define Int8GetDatum(X) ((Datum) (X))
  411. /*
  412. * DatumGetUInt8
  413. * Returns 8-bit unsigned integer value of a datum.
  414. */
  415. #define DatumGetUInt8(X) ((uint8) (X))
  416. /*
  417. * UInt8GetDatum
  418. * Returns datum representation for an 8-bit unsigned integer.
  419. */
  420. #define UInt8GetDatum(X) ((Datum) (X))
  421. /*
  422. * DatumGetInt16
  423. * Returns 16-bit integer value of a datum.
  424. */
  425. #define DatumGetInt16(X) ((int16) (X))
  426. /*
  427. * Int16GetDatum
  428. * Returns datum representation for a 16-bit integer.
  429. */
  430. #define Int16GetDatum(X) ((Datum) (X))
  431. /*
  432. * DatumGetUInt16
  433. * Returns 16-bit unsigned integer value of a datum.
  434. */
  435. #define DatumGetUInt16(X) ((uint16) (X))
  436. /*
  437. * UInt16GetDatum
  438. * Returns datum representation for a 16-bit unsigned integer.
  439. */
  440. #define UInt16GetDatum(X) ((Datum) (X))
  441. /*
  442. * DatumGetInt32
  443. * Returns 32-bit integer value of a datum.
  444. */
  445. #define DatumGetInt32(X) ((int32) (X))
  446. /*
  447. * Int32GetDatum
  448. * Returns datum representation for a 32-bit integer.
  449. */
  450. #define Int32GetDatum(X) ((Datum) (X))
  451. /*
  452. * DatumGetUInt32
  453. * Returns 32-bit unsigned integer value of a datum.
  454. */
  455. #define DatumGetUInt32(X) ((uint32) (X))
  456. /*
  457. * UInt32GetDatum
  458. * Returns datum representation for a 32-bit unsigned integer.
  459. */
  460. #define UInt32GetDatum(X) ((Datum) (X))
  461. /*
  462. * DatumGetObjectId
  463. * Returns object identifier value of a datum.
  464. */
  465. #define DatumGetObjectId(X) ((Oid) (X))
  466. /*
  467. * ObjectIdGetDatum
  468. * Returns datum representation for an object identifier.
  469. */
  470. #define ObjectIdGetDatum(X) ((Datum) (X))
  471. /*
  472. * DatumGetTransactionId
  473. * Returns transaction identifier value of a datum.
  474. */
  475. #define DatumGetTransactionId(X) ((TransactionId) (X))
  476. /*
  477. * TransactionIdGetDatum
  478. * Returns datum representation for a transaction identifier.
  479. */
  480. #define TransactionIdGetDatum(X) ((Datum) (X))
  481. /*
  482. * MultiXactIdGetDatum
  483. * Returns datum representation for a multixact identifier.
  484. */
  485. #define MultiXactIdGetDatum(X) ((Datum) (X))
  486. /*
  487. * DatumGetCommandId
  488. * Returns command identifier value of a datum.
  489. */
  490. #define DatumGetCommandId(X) ((CommandId) (X))
  491. /*
  492. * CommandIdGetDatum
  493. * Returns datum representation for a command identifier.
  494. */
  495. #define CommandIdGetDatum(X) ((Datum) (X))
  496. /*
  497. * DatumGetPointer
  498. * Returns pointer value of a datum.
  499. */
  500. #define DatumGetPointer(X) ((Pointer) (X))
  501. /*
  502. * PointerGetDatum
  503. * Returns datum representation for a pointer.
  504. */
  505. #define PointerGetDatum(X) ((Datum) (X))
  506. /*
  507. * DatumGetCString
  508. * Returns C string (null-terminated string) value of a datum.
  509. *
  510. * Note: C string is not a full-fledged Postgres type at present,
  511. * but type input functions use this conversion for their inputs.
  512. */
  513. #define DatumGetCString(X) ((char *) DatumGetPointer(X))
  514. /*
  515. * CStringGetDatum
  516. * Returns datum representation for a C string (null-terminated string).
  517. *
  518. * Note: C string is not a full-fledged Postgres type at present,
  519. * but type output functions use this conversion for their outputs.
  520. * Note: CString is pass-by-reference; caller must ensure the pointed-to
  521. * value has adequate lifetime.
  522. */
  523. #define CStringGetDatum(X) PointerGetDatum(X)
  524. /*
  525. * DatumGetName
  526. * Returns name value of a datum.
  527. */
  528. #define DatumGetName(X) ((Name) DatumGetPointer(X))
  529. /*
  530. * NameGetDatum
  531. * Returns datum representation for a name.
  532. *
  533. * Note: Name is pass-by-reference; caller must ensure the pointed-to
  534. * value has adequate lifetime.
  535. */
  536. #define NameGetDatum(X) CStringGetDatum(NameStr(*(X)))
  537. /*
  538. * DatumGetInt64
  539. * Returns 64-bit integer value of a datum.
  540. *
  541. * Note: this macro hides whether int64 is pass by value or by reference.
  542. */
  543. #ifdef USE_FLOAT8_BYVAL
  544. #define DatumGetInt64(X) ((int64) (X))
  545. #else
  546. #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
  547. #endif
  548. /*
  549. * Int64GetDatum
  550. * Returns datum representation for a 64-bit integer.
  551. *
  552. * Note: if int64 is pass by reference, this function returns a reference
  553. * to palloc'd space.
  554. */
  555. #ifdef USE_FLOAT8_BYVAL
  556. #define Int64GetDatum(X) ((Datum) (X))
  557. #else
  558. extern Datum Int64GetDatum(int64 X);
  559. #endif
  560. /*
  561. * DatumGetUInt64
  562. * Returns 64-bit unsigned integer value of a datum.
  563. *
  564. * Note: this macro hides whether int64 is pass by value or by reference.
  565. */
  566. #ifdef USE_FLOAT8_BYVAL
  567. #define DatumGetUInt64(X) ((uint64) (X))
  568. #else
  569. #define DatumGetUInt64(X) (* ((uint64 *) DatumGetPointer(X)))
  570. #endif
  571. /*
  572. * UInt64GetDatum
  573. * Returns datum representation for a 64-bit unsigned integer.
  574. *
  575. * Note: if int64 is pass by reference, this function returns a reference
  576. * to palloc'd space.
  577. */
  578. #ifdef USE_FLOAT8_BYVAL
  579. #define UInt64GetDatum(X) ((Datum) (X))
  580. #else
  581. #define UInt64GetDatum(X) Int64GetDatum((int64) (X))
  582. #endif
  583. /*
  584. * Float <-> Datum conversions
  585. *
  586. * These have to be implemented as inline functions rather than macros, when
  587. * passing by value, because many machines pass int and float function
  588. * parameters/results differently; so we need to play weird games with unions.
  589. */
  590. /*
  591. * DatumGetFloat4
  592. * Returns 4-byte floating point value of a datum.
  593. */
  594. static inline float4
  595. DatumGetFloat4(Datum X)
  596. {
  597. union
  598. {
  599. int32 value;
  600. float4 retval;
  601. } myunion;
  602. myunion.value = DatumGetInt32(X);
  603. return myunion.retval;
  604. }
  605. /*
  606. * Float4GetDatum
  607. * Returns datum representation for a 4-byte floating point number.
  608. */
  609. static inline Datum
  610. Float4GetDatum(float4 X)
  611. {
  612. union
  613. {
  614. float4 value;
  615. int32 retval;
  616. } myunion;
  617. myunion.value = X;
  618. return Int32GetDatum(myunion.retval);
  619. }
  620. /*
  621. * DatumGetFloat8
  622. * Returns 8-byte floating point value of a datum.
  623. *
  624. * Note: this macro hides whether float8 is pass by value or by reference.
  625. */
  626. #ifdef USE_FLOAT8_BYVAL
  627. static inline float8
  628. DatumGetFloat8(Datum X)
  629. {
  630. union
  631. {
  632. int64 value;
  633. float8 retval;
  634. } myunion;
  635. myunion.value = DatumGetInt64(X);
  636. return myunion.retval;
  637. }
  638. #else
  639. #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
  640. #endif
  641. /*
  642. * Float8GetDatum
  643. * Returns datum representation for an 8-byte floating point number.
  644. *
  645. * Note: if float8 is pass by reference, this function returns a reference
  646. * to palloc'd space.
  647. */
  648. #ifdef USE_FLOAT8_BYVAL
  649. static inline Datum
  650. Float8GetDatum(float8 X)
  651. {
  652. union
  653. {
  654. float8 value;
  655. int64 retval;
  656. } myunion;
  657. myunion.value = X;
  658. return Int64GetDatum(myunion.retval);
  659. }
  660. #else
  661. extern Datum Float8GetDatum(float8 X);
  662. #endif
  663. /*
  664. * Int64GetDatumFast
  665. * Float8GetDatumFast
  666. *
  667. * These macros are intended to allow writing code that does not depend on
  668. * whether int64 and float8 are pass-by-reference types, while not
  669. * sacrificing performance when they are. The argument must be a variable
  670. * that will exist and have the same value for as long as the Datum is needed.
  671. * In the pass-by-ref case, the address of the variable is taken to use as
  672. * the Datum. In the pass-by-val case, these will be the same as the non-Fast
  673. * macros.
  674. */
  675. #ifdef USE_FLOAT8_BYVAL
  676. #define Int64GetDatumFast(X) Int64GetDatum(X)
  677. #define Float8GetDatumFast(X) Float8GetDatum(X)
  678. #else
  679. #define Int64GetDatumFast(X) PointerGetDatum(&(X))
  680. #define Float8GetDatumFast(X) PointerGetDatum(&(X))
  681. #endif
  682. #endif /* POSTGRES_H */