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-2007, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * $PostgreSQL: pgsql/src/include/postgres.h,v 1.78 2007/03/23 20:24:41 tgl 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 varattrib is the header of a varlena object that may have been
59 * TOASTed. Generally, only the code closely associated with TOAST logic
60 * should mess directly with struct varattrib or use the VARATT_FOO macros.
63 typedef struct varattrib
65 int32 va_header_; /* External/compressed storage */
66 /* flags and item size */
71 int32 va_rawsize; /* Plain data size */
72 char va_data[1]; /* Compressed data */
73 } va_compressed; /* Compressed stored attribute */
77 int32 va_rawsize; /* Plain data size */
78 int32 va_extsize; /* External saved size */
79 Oid va_valueid; /* Unique identifier of value */
80 Oid va_toastrelid; /* RelID where to find chunks */
81 } va_external; /* External stored attribute */
83 char va_data[1]; /* Plain stored attribute */
87 #define VARATT_FLAG_EXTERNAL 0x80000000
88 #define VARATT_FLAG_COMPRESSED 0x40000000
89 #define VARATT_MASK_FLAGS 0xc0000000
90 #define VARATT_MASK_SIZE 0x3fffffff
92 #define VARATT_SIZEP_DEPRECATED(PTR) (((varattrib *) (PTR))->va_header_)
94 #define VARATT_IS_EXTENDED(PTR) \
95 ((VARATT_SIZEP_DEPRECATED(PTR) & VARATT_MASK_FLAGS) != 0)
96 #define VARATT_IS_EXTERNAL(PTR) \
97 ((VARATT_SIZEP_DEPRECATED(PTR) & VARATT_FLAG_EXTERNAL) != 0)
98 #define VARATT_IS_COMPRESSED(PTR) \
99 ((VARATT_SIZEP_DEPRECATED(PTR) & VARATT_FLAG_COMPRESSED) != 0)
101 /* These macros are the ones for non-TOAST code to use */
103 #define VARSIZE(PTR) (VARATT_SIZEP_DEPRECATED(PTR) & VARATT_MASK_SIZE)
104 #define VARDATA(PTR) (((varattrib *) (PTR))->va_content.va_data)
106 #define SET_VARSIZE(PTR,SIZE) (VARATT_SIZEP_DEPRECATED(PTR) = (SIZE))
109 /* ----------------------------------------------------------------
110 * Section 2: datum type + support macros
111 * ----------------------------------------------------------------
116 * Postgres makes the following assumption about machines:
118 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
120 * Postgres also assumes that
128 * When a type narrower than Datum is stored in a Datum, we place it in the
129 * low-order bits and are careful that the DatumGetXXX macro for it discards
130 * the unused high-order bits (as opposed to, say, assuming they are zero).
131 * This is needed to support old-style user-defined functions, since depending
132 * on architecture and compiler, the return value of a function returning char
133 * or short may contain garbage when called as if it returned Datum.
136 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
138 #define SIZEOF_DATUM SIZEOF_UNSIGNED_LONG
140 typedef Datum *DatumPtr;
142 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
143 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
144 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
145 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
146 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
147 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
151 * Returns boolean value of a datum.
153 * Note: any nonzero value will be considered TRUE, but we ignore bits to
154 * the left of the width of bool, per comment above.
157 #define DatumGetBool(X) ((bool) (((bool) (X)) != 0))
161 * Returns datum representation for a boolean.
163 * Note: any nonzero value will be considered TRUE.
166 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
170 * Returns character value of a datum.
173 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
177 * Returns datum representation for a character.
180 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
184 * Returns datum representation for an 8-bit integer.
187 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
191 * Returns 8-bit unsigned integer value of a datum.
194 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
198 * Returns datum representation for an 8-bit unsigned integer.
201 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
205 * Returns 16-bit integer value of a datum.
208 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
212 * Returns datum representation for a 16-bit integer.
215 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
219 * Returns 16-bit unsigned integer value of a datum.
222 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
226 * Returns datum representation for a 16-bit unsigned integer.
229 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
233 * Returns 32-bit integer value of a datum.
236 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
240 * Returns datum representation for a 32-bit integer.
243 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
247 * Returns 32-bit unsigned integer value of a datum.
250 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
254 * Returns datum representation for a 32-bit unsigned integer.
257 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
261 * Returns object identifier value of a datum.
264 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
268 * Returns datum representation for an object identifier.
271 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
274 * DatumGetTransactionId
275 * Returns transaction identifier value of a datum.
278 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
281 * TransactionIdGetDatum
282 * Returns datum representation for a transaction identifier.
285 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
289 * Returns command identifier value of a datum.
292 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
296 * Returns datum representation for a command identifier.
299 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
303 * Returns pointer value of a datum.
306 #define DatumGetPointer(X) ((Pointer) (X))
310 * Returns datum representation for a pointer.
313 #define PointerGetDatum(X) ((Datum) (X))
317 * Returns C string (null-terminated string) value of a datum.
319 * Note: C string is not a full-fledged Postgres type at present,
320 * but type input functions use this conversion for their inputs.
323 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
327 * Returns datum representation for a C string (null-terminated string).
329 * Note: C string is not a full-fledged Postgres type at present,
330 * but type output functions use this conversion for their outputs.
331 * Note: CString is pass-by-reference; caller must ensure the pointed-to
332 * value has adequate lifetime.
335 #define CStringGetDatum(X) PointerGetDatum(X)
339 * Returns name value of a datum.
342 #define DatumGetName(X) ((Name) DatumGetPointer(X))
346 * Returns datum representation for a name.
348 * Note: Name is pass-by-reference; caller must ensure the pointed-to
349 * value has adequate lifetime.
352 #define NameGetDatum(X) PointerGetDatum(X)
356 * Returns 64-bit integer value of a datum.
358 * Note: this macro hides the fact that int64 is currently a
359 * pass-by-reference type. Someday it may be pass-by-value,
360 * at least on some platforms.
363 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
367 * Returns datum representation for a 64-bit integer.
369 * Note: this routine returns a reference to palloc'd space.
372 extern Datum Int64GetDatum(int64 X);
376 * Returns 4-byte floating point value of a datum.
378 * Note: this macro hides the fact that float4 is currently a
379 * pass-by-reference type. Someday it may be pass-by-value.
382 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
386 * Returns datum representation for a 4-byte floating point number.
388 * Note: this routine returns a reference to palloc'd space.
391 extern Datum Float4GetDatum(float4 X);
395 * Returns 8-byte floating point value of a datum.
397 * Note: this macro hides the fact that float8 is currently a
398 * pass-by-reference type. Someday it may be pass-by-value,
399 * at least on some platforms.
402 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
406 * Returns datum representation for an 8-byte floating point number.
408 * Note: this routine returns a reference to palloc'd space.
411 extern Datum Float8GetDatum(float8 X);
416 * Returns 32-bit floating point value of a datum.
417 * This is really a pointer, of course.
419 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
420 * It will eventually go away.
423 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
427 * Returns datum representation for a 32-bit floating point number.
428 * This is really a pointer, of course.
430 * XXX: this macro is now deprecated in favor of Float4GetDatum.
431 * It will eventually go away.
434 #define Float32GetDatum(X) PointerGetDatum(X)
438 * Returns 64-bit floating point value of a datum.
439 * This is really a pointer, of course.
441 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
442 * It will eventually go away.
445 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
449 * Returns datum representation for a 64-bit floating point number.
450 * This is really a pointer, of course.
452 * XXX: this macro is now deprecated in favor of Float8GetDatum.
453 * It will eventually go away.
456 #define Float64GetDatum(X) PointerGetDatum(X)
463 * These macros are intended to allow writing code that does not depend on
464 * whether int64, float4, float8 are pass-by-reference types, while not
465 * sacrificing performance when they are. The argument must be a variable
466 * that will exist and have the same value for as long as the Datum is needed.
467 * In the pass-by-ref case, the address of the variable is taken to use as
468 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
472 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
473 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
474 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
477 /* ----------------------------------------------------------------
478 * Section 3: exception handling definitions
479 * Assert, Trap, etc macros
480 * ----------------------------------------------------------------
483 extern DLLIMPORT bool assert_enabled;
486 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
489 * It should _NOT_ be defined in releases or in benchmark copies
494 * Generates an exception if the given condition is true.
496 #define Trap(condition, errorType) \
498 if ((assert_enabled) && (condition)) \
499 ExceptionalCondition(CppAsString(condition), (errorType), \
500 __FILE__, __LINE__); \
504 * TrapMacro is the same as Trap but it's intended for use in macros:
506 * #define foo(x) (AssertMacro(x != 0) && bar(x))
510 #define TrapMacro(condition, errorType) \
511 ((bool) ((! assert_enabled) || ! (condition) || \
512 (ExceptionalCondition(CppAsString(condition), (errorType), \
513 __FILE__, __LINE__))))
515 #ifndef USE_ASSERT_CHECKING
516 #define Assert(condition)
517 #define AssertMacro(condition) ((void)true)
518 #define AssertArg(condition)
519 #define AssertState(condition)
521 #define Assert(condition) \
522 Trap(!(condition), "FailedAssertion")
524 #define AssertMacro(condition) \
525 ((void) TrapMacro(!(condition), "FailedAssertion"))
527 #define AssertArg(condition) \
528 Trap(!(condition), "BadArgument")
530 #define AssertState(condition) \
531 Trap(!(condition), "BadState")
532 #endif /* USE_ASSERT_CHECKING */
534 extern int ExceptionalCondition(char *conditionName, char *errorType,
535 char *fileName, int lineNumber);
537 /* ----------------------------------------------------------------
538 * Section 4: genbki macros used by catalog/pg_xxx.h files
539 * ----------------------------------------------------------------
541 #define CATALOG(name,oid) typedef struct CppConcat(FormData_,name)
543 #define BKI_BOOTSTRAP
544 #define BKI_SHARED_RELATION
545 #define BKI_WITHOUT_OIDS
547 /* these need to expand into some harmless, repeatable declaration */
548 #define DATA(x) extern int no_such_variable
549 #define DESCR(x) extern int no_such_variable
550 #define SHDESCR(x) extern int no_such_variable
552 typedef int4 aclitem; /* PHONY definition for catalog use only */
554 #endif /* POSTGRES_H */