gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

786 lines
22KB

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