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-2005, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * $PostgreSQL: pgsql/src/include/postgres.h,v 1.73 2006/02/12 03:22:19 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 varattrib is the header of a varlena object that may have been
62 typedef struct varattrib
64 int32 va_header; /* External/compressed storage */
65 /* flags and item size */
70 int32 va_rawsize; /* Plain data size */
71 char va_data[1]; /* Compressed data */
72 } va_compressed; /* Compressed stored attribute */
76 int32 va_rawsize; /* Plain data size */
77 int32 va_extsize; /* External saved size */
78 Oid va_valueid; /* Unique identifier of value */
79 Oid va_toastrelid; /* RelID where to find chunks */
80 } va_external; /* External stored attribute */
82 char va_data[1]; /* Plain stored attribute */
86 #define VARATT_FLAG_EXTERNAL 0x80000000
87 #define VARATT_FLAG_COMPRESSED 0x40000000
88 #define VARATT_MASK_FLAGS 0xc0000000
89 #define VARATT_MASK_SIZE 0x3fffffff
91 #define VARATT_SIZEP(_PTR) (((varattrib *)(_PTR))->va_header)
92 #define VARATT_SIZE(PTR) (VARATT_SIZEP(PTR) & VARATT_MASK_SIZE)
93 #define VARATT_DATA(PTR) (((varattrib *)(PTR))->va_content.va_data)
94 #define VARATT_CDATA(PTR) (((varattrib *)(PTR))->va_content.va_compressed.va_data)
96 #define VARSIZE(__PTR) VARATT_SIZE(__PTR)
97 #define VARDATA(__PTR) VARATT_DATA(__PTR)
99 #define VARATT_IS_EXTENDED(PTR) \
100 ((VARATT_SIZEP(PTR) & VARATT_MASK_FLAGS) != 0)
101 #define VARATT_IS_EXTERNAL(PTR) \
102 ((VARATT_SIZEP(PTR) & VARATT_FLAG_EXTERNAL) != 0)
103 #define VARATT_IS_COMPRESSED(PTR) \
104 ((VARATT_SIZEP(PTR) & VARATT_FLAG_COMPRESSED) != 0)
107 /* ----------------------------------------------------------------
108 * Section 2: datum type + support macros
109 * ----------------------------------------------------------------
114 * Postgres makes the following assumption about machines:
116 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
118 * Postgres also assumes that
126 * If your machine meets these requirements, Datums should also be checked
127 * to see if the positioning is correct.
130 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
132 #define SIZEOF_DATUM SIZEOF_UNSIGNED_LONG
133 typedef Datum *DatumPtr;
135 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
136 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
137 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
138 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
139 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
140 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
144 * Returns boolean value of a datum.
146 * Note: any nonzero value will be considered TRUE.
149 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
153 * Returns datum representation for a boolean.
155 * Note: any nonzero value will be considered TRUE.
158 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
162 * Returns character value of a datum.
165 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
169 * Returns datum representation for a character.
172 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
176 * Returns datum representation for an 8-bit integer.
179 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
183 * Returns 8-bit unsigned integer value of a datum.
186 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
190 * Returns datum representation for an 8-bit unsigned integer.
193 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
197 * Returns 16-bit integer value of a datum.
200 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
204 * Returns datum representation for a 16-bit integer.
207 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
211 * Returns 16-bit unsigned integer value of a datum.
214 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
218 * Returns datum representation for a 16-bit unsigned integer.
221 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
225 * Returns 32-bit integer value of a datum.
228 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
232 * Returns datum representation for a 32-bit integer.
235 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
239 * Returns 32-bit unsigned integer value of a datum.
242 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
246 * Returns datum representation for a 32-bit unsigned integer.
249 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
253 * Returns object identifier value of a datum.
256 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
260 * Returns datum representation for an object identifier.
263 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
266 * DatumGetTransactionId
267 * Returns transaction identifier value of a datum.
270 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
273 * TransactionIdGetDatum
274 * Returns datum representation for a transaction identifier.
277 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
281 * Returns command identifier value of a datum.
284 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
288 * Returns datum representation for a command identifier.
291 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
295 * Returns pointer value of a datum.
298 #define DatumGetPointer(X) ((Pointer) (X))
302 * Returns datum representation for a pointer.
305 #define PointerGetDatum(X) ((Datum) (X))
309 * Returns C string (null-terminated string) value of a datum.
311 * Note: C string is not a full-fledged Postgres type at present,
312 * but type input functions use this conversion for their inputs.
315 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
319 * Returns datum representation for a C string (null-terminated string).
321 * Note: C string is not a full-fledged Postgres type at present,
322 * but type output functions use this conversion for their outputs.
323 * Note: CString is pass-by-reference; caller must ensure the pointed-to
324 * value has adequate lifetime.
327 #define CStringGetDatum(X) PointerGetDatum(X)
331 * Returns name value of a datum.
334 #define DatumGetName(X) ((Name) DatumGetPointer(X))
338 * Returns datum representation for a name.
340 * Note: Name is pass-by-reference; caller must ensure the pointed-to
341 * value has adequate lifetime.
344 #define NameGetDatum(X) PointerGetDatum(X)
348 * Returns 64-bit integer value of a datum.
350 * Note: this macro hides the fact that int64 is currently a
351 * pass-by-reference type. Someday it may be pass-by-value,
352 * at least on some platforms.
355 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
359 * Returns datum representation for a 64-bit integer.
361 * Note: this routine returns a reference to palloc'd space.
364 extern Datum Int64GetDatum(int64 X);
368 * Returns 4-byte floating point value of a datum.
370 * Note: this macro hides the fact that float4 is currently a
371 * pass-by-reference type. Someday it may be pass-by-value.
374 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
378 * Returns datum representation for a 4-byte floating point number.
380 * Note: this routine returns a reference to palloc'd space.
383 extern Datum Float4GetDatum(float4 X);
387 * Returns 8-byte floating point value of a datum.
389 * Note: this macro hides the fact that float8 is currently a
390 * pass-by-reference type. Someday it may be pass-by-value,
391 * at least on some platforms.
394 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
398 * Returns datum representation for an 8-byte floating point number.
400 * Note: this routine returns a reference to palloc'd space.
403 extern Datum Float8GetDatum(float8 X);
408 * Returns 32-bit floating point value of a datum.
409 * This is really a pointer, of course.
411 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
412 * It will eventually go away.
415 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
419 * Returns datum representation for a 32-bit floating point number.
420 * This is really a pointer, of course.
422 * XXX: this macro is now deprecated in favor of Float4GetDatum.
423 * It will eventually go away.
426 #define Float32GetDatum(X) PointerGetDatum(X)
430 * Returns 64-bit floating point value of a datum.
431 * This is really a pointer, of course.
433 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
434 * It will eventually go away.
437 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
441 * Returns datum representation for a 64-bit floating point number.
442 * This is really a pointer, of course.
444 * XXX: this macro is now deprecated in favor of Float8GetDatum.
445 * It will eventually go away.
448 #define Float64GetDatum(X) PointerGetDatum(X)
455 * These macros are intended to allow writing code that does not depend on
456 * whether int64, float4, float8 are pass-by-reference types, while not
457 * sacrificing performance when they are. The argument must be a variable
458 * that will exist and have the same value for as long as the Datum is needed.
459 * In the pass-by-ref case, the address of the variable is taken to use as
460 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
464 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
465 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
466 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
469 /* ----------------------------------------------------------------
470 * Section 3: exception handling definitions
471 * Assert, Trap, etc macros
472 * ----------------------------------------------------------------
475 extern DLLIMPORT bool assert_enabled;
478 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
481 * It should _NOT_ be defined in releases or in benchmark copies
486 * Generates an exception if the given condition is true.
488 #define Trap(condition, errorType) \
490 if ((assert_enabled) && (condition)) \
491 ExceptionalCondition(CppAsString(condition), (errorType), \
492 __FILE__, __LINE__); \
496 * TrapMacro is the same as Trap but it's intended for use in macros:
498 * #define foo(x) (AssertMacro(x != 0) && bar(x))
502 #define TrapMacro(condition, errorType) \
503 ((bool) ((! assert_enabled) || ! (condition) || \
504 (ExceptionalCondition(CppAsString(condition), (errorType), \
505 __FILE__, __LINE__))))
507 #ifndef USE_ASSERT_CHECKING
508 #define Assert(condition)
509 #define AssertMacro(condition) ((void)true)
510 #define AssertArg(condition)
511 #define AssertState(condition)
513 #define Assert(condition) \
514 Trap(!(condition), "FailedAssertion")
516 #define AssertMacro(condition) \
517 ((void) TrapMacro(!(condition), "FailedAssertion"))
519 #define AssertArg(condition) \
520 Trap(!(condition), "BadArgument")
522 #define AssertState(condition) \
523 Trap(!(condition), "BadState")
524 #endif /* USE_ASSERT_CHECKING */
526 extern int ExceptionalCondition(char *conditionName, char *errorType,
527 char *fileName, int lineNumber);
529 /* ----------------------------------------------------------------
530 * Section 4: genbki macros used by catalog/pg_xxx.h files
531 * ----------------------------------------------------------------
533 #define CATALOG(name,oid) typedef struct CppConcat(FormData_,name)
535 #define BKI_BOOTSTRAP
536 #define BKI_SHARED_RELATION
537 #define BKI_WITHOUT_OIDS
539 /* these need to expand into some harmless, repeatable declaration */
540 #define DATA(x) extern int no_such_variable
541 #define DESCR(x) extern int no_such_variable
542 #define SHDESCR(x) extern int no_such_variable
544 typedef int4 aclitem; /* PHONY definition for catalog use only */
546 #endif /* POSTGRES_H */