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