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-2002, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * $Id: postgres.h,v 1.58 2002/06/20 20:29:42 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 in the
34 * backend environment, but are of no interest outside the backend.
36 * Simple type definitions live in c.h, where they are shared with postgres_fe.h.
37 * We do that since those type definitions are needed by frontend modules that want
38 * to deal with binary data transmission to or from the backend. Type definitions
39 * in this file should be for representations that never escape the backend, such
40 * as Datum or TOASTed varlena objects.
42 *----------------------------------------------------------------
48 #include "utils/elog.h"
49 #include "utils/palloc.h"
51 /* ----------------------------------------------------------------
52 * Section 1: variable-length datatypes (TOAST support)
53 * ----------------------------------------------------------------
57 * struct varattrib is the header of a varlena object that may have been TOASTed.
60 #define TUPLE_TOASTER_ACTIVE
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 *) */
131 typedef Datum *DatumPtr;
133 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
134 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
135 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
136 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
137 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
138 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
142 * Returns boolean value of a datum.
144 * Note: any nonzero value will be considered TRUE.
147 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
151 * Returns datum representation for a boolean.
153 * Note: any nonzero value will be considered TRUE.
156 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
160 * Returns character value of a datum.
163 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
167 * Returns datum representation for a character.
170 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
174 * Returns datum representation for an 8-bit integer.
177 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
181 * Returns 8-bit unsigned integer value of a datum.
184 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
188 * Returns datum representation for an 8-bit unsigned integer.
191 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
195 * Returns 16-bit integer value of a datum.
198 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
202 * Returns datum representation for a 16-bit integer.
205 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
209 * Returns 16-bit unsigned integer value of a datum.
212 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
216 * Returns datum representation for a 16-bit unsigned integer.
219 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
223 * Returns 32-bit integer value of a datum.
226 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
230 * Returns datum representation for a 32-bit integer.
233 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
237 * Returns 32-bit unsigned integer value of a datum.
240 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
244 * Returns datum representation for a 32-bit unsigned integer.
247 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
251 * Returns object identifier value of a datum.
254 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
258 * Returns datum representation for an object identifier.
261 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
264 * DatumGetTransactionId
265 * Returns transaction identifier value of a datum.
268 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
271 * TransactionIdGetDatum
272 * Returns datum representation for a transaction identifier.
275 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
279 * Returns command identifier value of a datum.
282 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
286 * Returns datum representation for a command identifier.
289 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
293 * Returns pointer value of a datum.
296 #define DatumGetPointer(X) ((Pointer) (X))
300 * Returns datum representation for a pointer.
303 #define PointerGetDatum(X) ((Datum) (X))
307 * Returns C string (null-terminated string) value of a datum.
309 * Note: C string is not a full-fledged Postgres type at present,
310 * but type input functions use this conversion for their inputs.
313 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
317 * Returns datum representation for a C string (null-terminated string).
319 * Note: C string is not a full-fledged Postgres type at present,
320 * but type output functions use this conversion for their outputs.
321 * Note: CString is pass-by-reference; caller must ensure the pointed-to
322 * value has adequate lifetime.
325 #define CStringGetDatum(X) PointerGetDatum(X)
329 * Returns name value of a datum.
332 #define DatumGetName(X) ((Name) DatumGetPointer(X))
336 * Returns datum representation for a name.
338 * Note: Name is pass-by-reference; caller must ensure the pointed-to
339 * value has adequate lifetime.
342 #define NameGetDatum(X) PointerGetDatum(X)
346 * Returns 64-bit integer value of a datum.
348 * Note: this macro hides the fact that int64 is currently a
349 * pass-by-reference type. Someday it may be pass-by-value,
350 * at least on some platforms.
353 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
357 * Returns datum representation for a 64-bit integer.
359 * Note: this routine returns a reference to palloc'd space.
362 extern Datum Int64GetDatum(int64 X);
366 * Returns 4-byte floating point value of a datum.
368 * Note: this macro hides the fact that float4 is currently a
369 * pass-by-reference type. Someday it may be pass-by-value.
372 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
376 * Returns datum representation for a 4-byte floating point number.
378 * Note: this routine returns a reference to palloc'd space.
381 extern Datum Float4GetDatum(float4 X);
385 * Returns 8-byte floating point value of a datum.
387 * Note: this macro hides the fact that float8 is currently a
388 * pass-by-reference type. Someday it may be pass-by-value,
389 * at least on some platforms.
392 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
396 * Returns datum representation for an 8-byte floating point number.
398 * Note: this routine returns a reference to palloc'd space.
401 extern Datum Float8GetDatum(float8 X);
406 * Returns 32-bit floating point value of a datum.
407 * This is really a pointer, of course.
409 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
410 * It will eventually go away.
413 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
417 * Returns datum representation for a 32-bit floating point number.
418 * This is really a pointer, of course.
420 * XXX: this macro is now deprecated in favor of Float4GetDatum.
421 * It will eventually go away.
424 #define Float32GetDatum(X) PointerGetDatum(X)
428 * Returns 64-bit floating point value of a datum.
429 * This is really a pointer, of course.
431 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
432 * It will eventually go away.
435 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
439 * Returns datum representation for a 64-bit floating point number.
440 * This is really a pointer, of course.
442 * XXX: this macro is now deprecated in favor of Float8GetDatum.
443 * It will eventually go away.
446 #define Float64GetDatum(X) PointerGetDatum(X)
453 * These macros are intended to allow writing code that does not depend on
454 * whether int64, float4, float8 are pass-by-reference types, while not
455 * sacrificing performance when they are. The argument must be a variable
456 * that will exist and have the same value for as long as the Datum is needed.
457 * In the pass-by-ref case, the address of the variable is taken to use as
458 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
462 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
463 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
464 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
467 /* ----------------------------------------------------------------
468 * Section 3: exception handling definitions
469 * Assert, Trap, etc macros
470 * ----------------------------------------------------------------
473 typedef char *ExcMessage;
475 typedef struct Exception
480 extern DLLIMPORT Exception FailedAssertion;
481 extern DLLIMPORT Exception BadArg;
482 extern DLLIMPORT Exception BadState;
484 extern DLLIMPORT bool assert_enabled;
487 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
490 * It should _NOT_ be defined in releases or in benchmark copies
495 * Generates an exception if the given condition is true.
498 #define Trap(condition, exception) \
500 if ((assert_enabled) && (condition)) \
501 ExceptionalCondition(CppAsString(condition), &(exception), \
502 (char*)NULL, __FILE__, __LINE__); \
506 * TrapMacro is the same as Trap but it's intended for use in macros:
508 * #define foo(x) (AssertM(x != 0) && bar(x))
512 #define TrapMacro(condition, exception) \
513 ((bool) ((! assert_enabled) || ! (condition) || \
514 (ExceptionalCondition(CppAsString(condition), \
516 (char*) NULL, __FILE__, __LINE__))))
518 #ifndef USE_ASSERT_CHECKING
519 #define Assert(condition)
520 #define AssertMacro(condition) ((void)true)
521 #define AssertArg(condition)
522 #define AssertState(condition)
523 #define assert_enabled 0
525 #define Assert(condition) \
526 Trap(!(condition), FailedAssertion)
528 #define AssertMacro(condition) \
529 ((void) TrapMacro(!(condition), FailedAssertion))
531 #define AssertArg(condition) \
532 Trap(!(condition), BadArg)
534 #define AssertState(condition) \
535 Trap(!(condition), BadState)
536 #endif /* USE_ASSERT_CHECKING */
540 * Generates an exception with a message if the given condition is true.
543 #define LogTrap(condition, exception, printArgs) \
545 if ((assert_enabled) && (condition)) \
546 ExceptionalCondition(CppAsString(condition), &(exception), \
547 vararg_format printArgs, __FILE__, __LINE__); \
551 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
553 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
555 #define LogTrapMacro(condition, exception, printArgs) \
556 ((bool) ((! assert_enabled) || ! (condition) || \
557 (ExceptionalCondition(CppAsString(condition), \
559 vararg_format printArgs, __FILE__, __LINE__))))
561 extern int ExceptionalCondition(char *conditionName,
562 Exception *exceptionP, char *details,
563 char *fileName, int lineNumber);
566 vararg_format(const char *fmt,...)
567 /* This lets gcc check the format string for consistency. */
568 __attribute__((format(printf, 1, 2)));
570 #ifndef USE_ASSERT_CHECKING
571 #define LogAssert(condition, printArgs)
572 #define LogAssertMacro(condition, printArgs) true
573 #define LogAssertArg(condition, printArgs)
574 #define LogAssertState(condition, printArgs)
576 #define LogAssert(condition, printArgs) \
577 LogTrap(!(condition), FailedAssertion, printArgs)
579 #define LogAssertMacro(condition, printArgs) \
580 LogTrapMacro(!(condition), FailedAssertion, printArgs)
582 #define LogAssertArg(condition, printArgs) \
583 LogTrap(!(condition), BadArg, printArgs)
585 #define LogAssertState(condition, printArgs) \
586 LogTrap(!(condition), BadState, printArgs)
588 #ifdef ASSERT_CHECKING_TEST
589 extern int assertTest(int val);
591 #endif /* USE_ASSERT_CHECKING */
593 /* ----------------------------------------------------------------
594 * Section 4: genbki macros used by catalog/pg_xxx.h files
595 * ----------------------------------------------------------------
597 #define CATALOG(x) typedef struct CppConcat(FormData_,x)
600 #define BKI_SHARED_RELATION
601 #define BKI_WITHOUT_OIDS
603 /* these need to expand into some harmless, repeatable declaration */
604 #define DATA(x) extern int errno
605 #define DESCR(x) extern int errno
610 typedef int4 aclitem; /* PHONY definition for catalog use only */
612 #endif /* POSTGRES_H */