postgres.h 23 KB

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