1 /*-------------------------------------------------------------------------
4 * Primary include file for PostgreSQL server .c files
6 * This should be the first file included by PostgreSQL backend modules.
7 * Client-side code should include postgres_fe.h instead.
10 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * $PostgreSQL: pgsql/src/include/postgres.h,v 1.88 2008/01/01 19:45:56 momjian Exp $
15 *-------------------------------------------------------------------------
18 *----------------------------------------------------------------
21 * When adding stuff to this file, please try to put stuff
22 * into the relevant section, or add new sections as appropriate.
25 * ------- ------------------------------------------------
26 * 1) variable-length datatypes (TOAST support)
27 * 2) datum type + support macros
28 * 3) exception handling definitions
29 * 4) genbki macros used by catalog/pg_xxx.h files
33 * In general, this file should contain declarations that are widely needed
34 * in the backend environment, but are of no interest outside the backend.
36 * Simple type definitions live in c.h, where they are shared with
37 * postgres_fe.h. We do that since those type definitions are needed by
38 * frontend modules that want to deal with binary data transmission to or
39 * from the backend. Type definitions in this file should be for
40 * representations that never escape the backend, such as Datum or
41 * TOASTed varlena objects.
43 *----------------------------------------------------------------
49 #include "utils/elog.h"
50 #include "utils/palloc.h"
52 /* ----------------------------------------------------------------
53 * Section 1: variable-length datatypes (TOAST support)
54 * ----------------------------------------------------------------
58 * struct varatt_external is a "TOAST pointer", that is, the information
59 * needed to fetch a stored-out-of-line Datum. The data is compressed
60 * if and only if va_extsize < va_rawsize - VARHDRSZ. This struct must not
61 * contain any padding, because we sometimes compare pointers using memcmp.
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...)
68 struct varatt_external
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 */
77 * These structs describe the header of a varlena object that may have been
78 * TOASTed. Generally, don't reference these structs directly, but use the
81 * We use separate structs for the aligned and unaligned cases because the
82 * compiler might otherwise think it could generate code that assumes
83 * alignment while touching fields of a 1-byte-header varlena.
87 struct /* Normal varlena (4-byte length) */
92 struct /* Compressed-in-line format */
95 uint32 va_rawsize; /* Original data size (excludes header) */
96 char va_data[1]; /* Compressed data */
103 char va_data[1]; /* Data begins here */
108 uint8 va_header; /* Always 0x80 or 0x01 */
109 uint8 va_len_1be; /* Physical length of datum */
110 char va_data[1]; /* Data (for now always a TOAST pointer) */
114 * Bit layouts for varlena headers on big-endian machines:
116 * 00xxxxxx 4-byte length word, aligned, uncompressed data (up to 1G)
117 * 01xxxxxx 4-byte length word, aligned, *compressed* data (up to 1G)
118 * 10000000 1-byte length word, unaligned, TOAST pointer
119 * 1xxxxxxx 1-byte length word, unaligned, uncompressed data (up to 126b)
121 * Bit layouts for varlena headers on little-endian machines:
123 * xxxxxx00 4-byte length word, aligned, uncompressed data (up to 1G)
124 * xxxxxx10 4-byte length word, aligned, *compressed* data (up to 1G)
125 * 00000001 1-byte length word, unaligned, TOAST pointer
126 * xxxxxxx1 1-byte length word, unaligned, uncompressed data (up to 126b)
128 * The "xxx" bits are the length field (which includes itself in all cases).
129 * In the big-endian case we mask to extract the length, in the little-endian
130 * case we shift. Note that in both cases the flag bits are in the physically
131 * first byte. Also, it is not possible for a 1-byte length word to be zero;
132 * this lets us disambiguate alignment padding bytes from the start of an
133 * unaligned datum. (We now *require* pad bytes to be filled with zero!)
137 * Endian-dependent macros. These are considered internal --- use the
138 * external macros below instead of using these directly.
140 * Note: IS_1B is true for external toast records but VARSIZE_1B will return 0
141 * for such records. Hence you should usually check for IS_EXTERNAL before
142 * checking for IS_1B.
145 #ifdef WORDS_BIGENDIAN
147 #define VARATT_IS_4B(PTR) \
148 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
149 #define VARATT_IS_4B_U(PTR) \
150 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
151 #define VARATT_IS_4B_C(PTR) \
152 ((((varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
153 #define VARATT_IS_1B(PTR) \
154 ((((varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
155 #define VARATT_IS_1B_E(PTR) \
156 ((((varattrib_1b *) (PTR))->va_header) == 0x80)
157 #define VARATT_NOT_PAD_BYTE(PTR) \
158 (*((uint8 *) (PTR)) != 0)
160 /* VARSIZE_4B() should only be used on known-aligned data */
161 #define VARSIZE_4B(PTR) \
162 (((varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
163 #define VARSIZE_1B(PTR) \
164 (((varattrib_1b *) (PTR))->va_header & 0x7F)
165 #define VARSIZE_1B_E(PTR) \
166 (((varattrib_1b_e *) (PTR))->va_len_1be)
168 #define SET_VARSIZE_4B(PTR,len) \
169 (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
170 #define SET_VARSIZE_4B_C(PTR,len) \
171 (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 0x40000000)
172 #define SET_VARSIZE_1B(PTR,len) \
173 (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
174 #define SET_VARSIZE_1B_E(PTR,len) \
175 (((varattrib_1b_e *) (PTR))->va_header = 0x80, \
176 ((varattrib_1b_e *) (PTR))->va_len_1be = (len))
177 #else /* !WORDS_BIGENDIAN */
179 #define VARATT_IS_4B(PTR) \
180 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
181 #define VARATT_IS_4B_U(PTR) \
182 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
183 #define VARATT_IS_4B_C(PTR) \
184 ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
185 #define VARATT_IS_1B(PTR) \
186 ((((varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
187 #define VARATT_IS_1B_E(PTR) \
188 ((((varattrib_1b *) (PTR))->va_header) == 0x01)
189 #define VARATT_NOT_PAD_BYTE(PTR) \
190 (*((uint8 *) (PTR)) != 0)
192 /* VARSIZE_4B() should only be used on known-aligned data */
193 #define VARSIZE_4B(PTR) \
194 ((((varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
195 #define VARSIZE_1B(PTR) \
196 ((((varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
197 #define VARSIZE_1B_E(PTR) \
198 (((varattrib_1b_e *) (PTR))->va_len_1be)
200 #define SET_VARSIZE_4B(PTR,len) \
201 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
202 #define SET_VARSIZE_4B_C(PTR,len) \
203 (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) | 0x02)
204 #define SET_VARSIZE_1B(PTR,len) \
205 (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
206 #define SET_VARSIZE_1B_E(PTR,len) \
207 (((varattrib_1b_e *) (PTR))->va_header = 0x01, \
208 ((varattrib_1b_e *) (PTR))->va_len_1be = (len))
209 #endif /* WORDS_BIGENDIAN */
211 #define VARHDRSZ_SHORT 1
212 #define VARATT_SHORT_MAX 0x7F
213 #define VARATT_CAN_MAKE_SHORT(PTR) \
214 (VARATT_IS_4B_U(PTR) && \
215 (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT) <= VARATT_SHORT_MAX)
216 #define VARATT_CONVERTED_SHORT_SIZE(PTR) \
217 (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT)
219 #define VARHDRSZ_EXTERNAL 2
221 #define VARDATA_4B(PTR) (((varattrib_4b *) (PTR))->va_4byte.va_data)
222 #define VARDATA_4B_C(PTR) (((varattrib_4b *) (PTR))->va_compressed.va_data)
223 #define VARDATA_1B(PTR) (((varattrib_1b *) (PTR))->va_data)
224 #define VARDATA_1B_E(PTR) (((varattrib_1b_e *) (PTR))->va_data)
226 #define VARRAWSIZE_4B_C(PTR) \
227 (((varattrib_4b *) (PTR))->va_compressed.va_rawsize)
229 /* Externally visible macros */
232 * VARDATA, VARSIZE, and SET_VARSIZE are the recommended API for most code
233 * for varlena datatypes. Note that they only work on untoasted,
234 * 4-byte-header Datums!
236 * Code that wants to use 1-byte-header values without detoasting should
237 * use VARSIZE_ANY/VARSIZE_ANY_EXHDR/VARDATA_ANY. The other macros here
238 * should usually be used only by tuple assembly/disassembly code and
239 * code that specifically wants to work with still-toasted Datums.
241 * WARNING: It is only safe to use VARDATA_ANY() -- typically with
242 * PG_DETOAST_DATUM_PACKED() -- if you really don't care about the alignment.
243 * Either because you're working with something like text where the alignment
244 * doesn't matter or because you're not going to access its constituent parts
245 * and just use things like memcpy on it anyways.
247 #define VARDATA(PTR) VARDATA_4B(PTR)
248 #define VARSIZE(PTR) VARSIZE_4B(PTR)
250 #define VARSIZE_SHORT(PTR) VARSIZE_1B(PTR)
251 #define VARDATA_SHORT(PTR) VARDATA_1B(PTR)
253 #define VARSIZE_EXTERNAL(PTR) VARSIZE_1B_E(PTR)
254 #define VARDATA_EXTERNAL(PTR) VARDATA_1B_E(PTR)
256 #define VARATT_IS_COMPRESSED(PTR) VARATT_IS_4B_C(PTR)
257 #define VARATT_IS_EXTERNAL(PTR) VARATT_IS_1B_E(PTR)
258 #define VARATT_IS_SHORT(PTR) VARATT_IS_1B(PTR)
259 #define VARATT_IS_EXTENDED(PTR) (!VARATT_IS_4B_U(PTR))
261 #define SET_VARSIZE(PTR, len) SET_VARSIZE_4B(PTR, len)
262 #define SET_VARSIZE_SHORT(PTR, len) SET_VARSIZE_1B(PTR, len)
263 #define SET_VARSIZE_COMPRESSED(PTR, len) SET_VARSIZE_4B_C(PTR, len)
264 #define SET_VARSIZE_EXTERNAL(PTR, len) SET_VARSIZE_1B_E(PTR, len)
266 #define VARSIZE_ANY(PTR) \
267 (VARATT_IS_1B_E(PTR) ? VARSIZE_1B_E(PTR) : \
268 (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR) : \
271 #define VARSIZE_ANY_EXHDR(PTR) \
272 (VARATT_IS_1B_E(PTR) ? VARSIZE_1B_E(PTR)-VARHDRSZ_EXTERNAL : \
273 (VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR)-VARHDRSZ_SHORT : \
274 VARSIZE_4B(PTR)-VARHDRSZ))
276 /* caution: this will not work on an external or compressed-in-line Datum */
277 /* caution: this will return a possibly unaligned pointer */
278 #define VARDATA_ANY(PTR) \
279 (VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR))
282 /* ----------------------------------------------------------------
283 * Section 2: datum type + support macros
284 * ----------------------------------------------------------------
289 * Postgres makes the following assumption about machines:
291 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
293 * Postgres also assumes that
301 * When a type narrower than Datum is stored in a Datum, we place it in the
302 * low-order bits and are careful that the DatumGetXXX macro for it discards
303 * the unused high-order bits (as opposed to, say, assuming they are zero).
304 * This is needed to support old-style user-defined functions, since depending
305 * on architecture and compiler, the return value of a function returning char
306 * or short may contain garbage when called as if it returned Datum.
309 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
311 #define SIZEOF_DATUM SIZEOF_UNSIGNED_LONG
313 typedef Datum *DatumPtr;
315 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
316 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
317 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
318 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
319 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
320 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
324 * Returns boolean value of a datum.
326 * Note: any nonzero value will be considered TRUE, but we ignore bits to
327 * the left of the width of bool, per comment above.
330 #define DatumGetBool(X) ((bool) (((bool) (X)) != 0))
334 * Returns datum representation for a boolean.
336 * Note: any nonzero value will be considered TRUE.
339 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
343 * Returns character value of a datum.
346 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
350 * Returns datum representation for a character.
353 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
357 * Returns datum representation for an 8-bit integer.
360 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
364 * Returns 8-bit unsigned integer value of a datum.
367 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
371 * Returns datum representation for an 8-bit unsigned integer.
374 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
378 * Returns 16-bit integer value of a datum.
381 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
385 * Returns datum representation for a 16-bit integer.
388 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
392 * Returns 16-bit unsigned integer value of a datum.
395 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
399 * Returns datum representation for a 16-bit unsigned integer.
402 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
406 * Returns 32-bit integer value of a datum.
409 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
413 * Returns datum representation for a 32-bit integer.
416 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
420 * Returns 32-bit unsigned integer value of a datum.
423 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
427 * Returns datum representation for a 32-bit unsigned integer.
430 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
434 * Returns object identifier value of a datum.
437 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
441 * Returns datum representation for an object identifier.
444 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
447 * DatumGetTransactionId
448 * Returns transaction identifier value of a datum.
451 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
454 * TransactionIdGetDatum
455 * Returns datum representation for a transaction identifier.
458 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
462 * Returns command identifier value of a datum.
465 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
469 * Returns datum representation for a command identifier.
472 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
476 * Returns pointer value of a datum.
479 #define DatumGetPointer(X) ((Pointer) (X))
483 * Returns datum representation for a pointer.
486 #define PointerGetDatum(X) ((Datum) (X))
490 * Returns C string (null-terminated string) value of a datum.
492 * Note: C string is not a full-fledged Postgres type at present,
493 * but type input functions use this conversion for their inputs.
496 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
500 * Returns datum representation for a C string (null-terminated string).
502 * Note: C string is not a full-fledged Postgres type at present,
503 * but type output functions use this conversion for their outputs.
504 * Note: CString is pass-by-reference; caller must ensure the pointed-to
505 * value has adequate lifetime.
508 #define CStringGetDatum(X) PointerGetDatum(X)
512 * Returns name value of a datum.
515 #define DatumGetName(X) ((Name) DatumGetPointer(X))
519 * Returns datum representation for a name.
521 * Note: Name is pass-by-reference; caller must ensure the pointed-to
522 * value has adequate lifetime.
525 #define NameGetDatum(X) PointerGetDatum(X)
529 * Returns 64-bit integer value of a datum.
531 * Note: this macro hides the fact that int64 is currently a
532 * pass-by-reference type. Someday it may be pass-by-value,
533 * at least on some platforms.
536 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
540 * Returns datum representation for a 64-bit integer.
542 * Note: this routine returns a reference to palloc'd space.
545 extern Datum Int64GetDatum(int64 X);
549 * Returns 4-byte floating point value of a datum.
551 * Note: this macro hides the fact that float4 is currently a
552 * pass-by-reference type. Someday it may be pass-by-value.
555 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
559 * Returns datum representation for a 4-byte floating point number.
561 * Note: this routine returns a reference to palloc'd space.
564 extern Datum Float4GetDatum(float4 X);
568 * Returns 8-byte floating point value of a datum.
570 * Note: this macro hides the fact that float8 is currently a
571 * pass-by-reference type. Someday it may be pass-by-value,
572 * at least on some platforms.
575 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
579 * Returns datum representation for an 8-byte floating point number.
581 * Note: this routine returns a reference to palloc'd space.
584 extern Datum Float8GetDatum(float8 X);
589 * Returns 32-bit floating point value of a datum.
590 * This is really a pointer, of course.
592 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
593 * It will eventually go away.
596 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
600 * Returns datum representation for a 32-bit floating point number.
601 * This is really a pointer, of course.
603 * XXX: this macro is now deprecated in favor of Float4GetDatum.
604 * It will eventually go away.
607 #define Float32GetDatum(X) PointerGetDatum(X)
611 * Returns 64-bit floating point value of a datum.
612 * This is really a pointer, of course.
614 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
615 * It will eventually go away.
618 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
622 * Returns datum representation for a 64-bit floating point number.
623 * This is really a pointer, of course.
625 * XXX: this macro is now deprecated in favor of Float8GetDatum.
626 * It will eventually go away.
629 #define Float64GetDatum(X) PointerGetDatum(X)
636 * These macros are intended to allow writing code that does not depend on
637 * whether int64, float4, float8 are pass-by-reference types, while not
638 * sacrificing performance when they are. The argument must be a variable
639 * that will exist and have the same value for as long as the Datum is needed.
640 * In the pass-by-ref case, the address of the variable is taken to use as
641 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
645 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
646 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
647 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
650 /* ----------------------------------------------------------------
651 * Section 3: exception handling definitions
652 * Assert, Trap, etc macros
653 * ----------------------------------------------------------------
656 extern PGDLLIMPORT bool assert_enabled;
659 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
662 * It should _NOT_ be defined in releases or in benchmark copies
667 * Generates an exception if the given condition is true.
669 #define Trap(condition, errorType) \
671 if ((assert_enabled) && (condition)) \
672 ExceptionalCondition(CppAsString(condition), (errorType), \
673 __FILE__, __LINE__); \
677 * TrapMacro is the same as Trap but it's intended for use in macros:
679 * #define foo(x) (AssertMacro(x != 0) && bar(x))
683 #define TrapMacro(condition, errorType) \
684 ((bool) ((! assert_enabled) || ! (condition) || \
685 (ExceptionalCondition(CppAsString(condition), (errorType), \
686 __FILE__, __LINE__))))
688 #ifndef USE_ASSERT_CHECKING
689 #define Assert(condition)
690 #define AssertMacro(condition) ((void)true)
691 #define AssertArg(condition)
692 #define AssertState(condition)
694 #define Assert(condition) \
695 Trap(!(condition), "FailedAssertion")
697 #define AssertMacro(condition) \
698 ((void) TrapMacro(!(condition), "FailedAssertion"))
700 #define AssertArg(condition) \
701 Trap(!(condition), "BadArgument")
703 #define AssertState(condition) \
704 Trap(!(condition), "BadState")
705 #endif /* USE_ASSERT_CHECKING */
707 extern int ExceptionalCondition(const char *conditionName,
708 const char *errorType,
709 const char *fileName, int lineNumber);
711 /* ----------------------------------------------------------------
712 * Section 4: genbki macros used by catalog/pg_xxx.h files
713 * ----------------------------------------------------------------
715 #define CATALOG(name,oid) typedef struct CppConcat(FormData_,name)
717 #define BKI_BOOTSTRAP
718 #define BKI_SHARED_RELATION
719 #define BKI_WITHOUT_OIDS
721 /* these need to expand into some harmless, repeatable declaration */
722 #define DATA(x) extern int no_such_variable
723 #define DESCR(x) extern int no_such_variable
724 #define SHDESCR(x) extern int no_such_variable
726 typedef int4 aclitem; /* PHONY definition for catalog use only */
728 #endif /* POSTGRES_H */