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