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-2001, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * $Id: postgres.h,v 1.47 2001/03/22 04:00:25 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 *----------------------------------------------------------------
47 #include "postgres_ext.h"
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 TOASTed.
61 #define TUPLE_TOASTER_ACTIVE
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 Oid va_toastidxid; /* Main tables row Oid */
82 Oid va_rowid; /* Referencing row Oid */
83 int16 va_attno; /* Main tables attno */
84 } va_external;/* External stored attribute */
86 char va_data[1]; /* Plain stored attribute */
90 #define VARATT_FLAG_EXTERNAL 0x80000000
91 #define VARATT_FLAG_COMPRESSED 0x40000000
92 #define VARATT_MASK_FLAGS 0xc0000000
93 #define VARATT_MASK_SIZE 0x3fffffff
95 #define VARATT_SIZEP(_PTR) (((varattrib *)(_PTR))->va_header)
96 #define VARATT_SIZE(PTR) (VARATT_SIZEP(PTR) & VARATT_MASK_SIZE)
97 #define VARATT_DATA(PTR) (((varattrib *)(PTR))->va_content.va_data)
98 #define VARATT_CDATA(PTR) (((varattrib *)(PTR))->va_content.va_compressed.va_data)
100 #define VARSIZE(__PTR) VARATT_SIZE(__PTR)
101 #define VARDATA(__PTR) VARATT_DATA(__PTR)
103 #define VARATT_IS_EXTENDED(PTR) \
104 ((VARATT_SIZEP(PTR) & VARATT_MASK_FLAGS) != 0)
105 #define VARATT_IS_EXTERNAL(PTR) \
106 ((VARATT_SIZEP(PTR) & VARATT_FLAG_EXTERNAL) != 0)
107 #define VARATT_IS_COMPRESSED(PTR) \
108 ((VARATT_SIZEP(PTR) & VARATT_FLAG_COMPRESSED) != 0)
111 /* ----------------------------------------------------------------
112 * Section 2: datum type + support macros
113 * ----------------------------------------------------------------
118 * Postgres makes the following assumption about machines:
120 * sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
122 * Postgres also assumes that
130 * If your machine meets these requirements, Datums should also be checked
131 * to see if the positioning is correct.
134 typedef unsigned long Datum; /* XXX sizeof(long) >= sizeof(void *) */
135 typedef Datum *DatumPtr;
137 #define GET_1_BYTE(datum) (((Datum) (datum)) & 0x000000ff)
138 #define GET_2_BYTES(datum) (((Datum) (datum)) & 0x0000ffff)
139 #define GET_4_BYTES(datum) (((Datum) (datum)) & 0xffffffff)
140 #define SET_1_BYTE(value) (((Datum) (value)) & 0x000000ff)
141 #define SET_2_BYTES(value) (((Datum) (value)) & 0x0000ffff)
142 #define SET_4_BYTES(value) (((Datum) (value)) & 0xffffffff)
146 * Returns boolean value of a datum.
148 * Note: any nonzero value will be considered TRUE.
151 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
155 * Returns datum representation for a boolean.
157 * Note: any nonzero value will be considered TRUE.
160 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
164 * Returns character value of a datum.
167 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
171 * Returns datum representation for a character.
174 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
178 * Returns datum representation for an 8-bit integer.
181 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
185 * Returns 8-bit unsigned integer value of a datum.
188 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
192 * Returns datum representation for an 8-bit unsigned integer.
195 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
199 * Returns 16-bit integer value of a datum.
202 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
206 * Returns datum representation for a 16-bit integer.
209 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
213 * Returns 16-bit unsigned integer value of a datum.
216 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
220 * Returns datum representation for a 16-bit unsigned integer.
223 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
227 * Returns 32-bit integer value of a datum.
230 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
234 * Returns datum representation for a 32-bit integer.
237 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
241 * Returns 32-bit unsigned integer value of a datum.
244 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
248 * Returns datum representation for a 32-bit unsigned integer.
251 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
255 * Returns object identifier value of a datum.
258 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
262 * Returns datum representation for an object identifier.
265 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
269 * Returns pointer value of a datum.
272 #define DatumGetPointer(X) ((Pointer) (X))
276 * Returns datum representation for a pointer.
279 #define PointerGetDatum(X) ((Datum) (X))
283 * Returns C string (null-terminated string) value of a datum.
285 * Note: C string is not a full-fledged Postgres type at present,
286 * but type input functions use this conversion for their inputs.
289 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
293 * Returns datum representation for a C string (null-terminated string).
295 * Note: C string is not a full-fledged Postgres type at present,
296 * but type output functions use this conversion for their outputs.
297 * Note: CString is pass-by-reference; caller must ensure the pointed-to
298 * value has adequate lifetime.
301 #define CStringGetDatum(X) PointerGetDatum(X)
305 * Returns name value of a datum.
308 #define DatumGetName(X) ((Name) DatumGetPointer(X))
312 * Returns datum representation for a name.
314 * Note: Name is pass-by-reference; caller must ensure the pointed-to
315 * value has adequate lifetime.
318 #define NameGetDatum(X) PointerGetDatum(X)
322 * Returns 64-bit integer value of a datum.
324 * Note: this macro hides the fact that int64 is currently a
325 * pass-by-reference type. Someday it may be pass-by-value,
326 * at least on some platforms.
329 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
333 * Returns datum representation for a 64-bit integer.
335 * Note: this routine returns a reference to palloc'd space.
338 extern Datum Int64GetDatum(int64 X);
342 * Returns 4-byte floating point value of a datum.
344 * Note: this macro hides the fact that float4 is currently a
345 * pass-by-reference type. Someday it may be pass-by-value.
348 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
352 * Returns datum representation for a 4-byte floating point number.
354 * Note: this routine returns a reference to palloc'd space.
357 extern Datum Float4GetDatum(float4 X);
361 * Returns 8-byte floating point value of a datum.
363 * Note: this macro hides the fact that float8 is currently a
364 * pass-by-reference type. Someday it may be pass-by-value,
365 * at least on some platforms.
368 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
372 * Returns datum representation for an 8-byte floating point number.
374 * Note: this routine returns a reference to palloc'd space.
377 extern Datum Float8GetDatum(float8 X);
382 * Returns 32-bit floating point value of a datum.
383 * This is really a pointer, of course.
385 * XXX: this macro is now deprecated in favor of DatumGetFloat4.
386 * It will eventually go away.
389 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
393 * Returns datum representation for a 32-bit floating point number.
394 * This is really a pointer, of course.
396 * XXX: this macro is now deprecated in favor of Float4GetDatum.
397 * It will eventually go away.
400 #define Float32GetDatum(X) PointerGetDatum(X)
404 * Returns 64-bit floating point value of a datum.
405 * This is really a pointer, of course.
407 * XXX: this macro is now deprecated in favor of DatumGetFloat8.
408 * It will eventually go away.
411 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
415 * Returns datum representation for a 64-bit floating point number.
416 * This is really a pointer, of course.
418 * XXX: this macro is now deprecated in favor of Float8GetDatum.
419 * It will eventually go away.
422 #define Float64GetDatum(X) PointerGetDatum(X)
429 * These macros are intended to allow writing code that does not depend on
430 * whether int64, float4, float8 are pass-by-reference types, while not
431 * sacrificing performance when they are. The argument must be a variable
432 * that will exist and have the same value for as long as the Datum is needed.
433 * In the pass-by-ref case, the address of the variable is taken to use as
434 * the Datum. In the pass-by-val case, these will be the same as the non-Fast
438 #define Int64GetDatumFast(X) PointerGetDatum(&(X))
439 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
440 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
443 /* ----------------------------------------------------------------
444 * Section 3: exception handling definitions
445 * Assert, Trap, etc macros
446 * ----------------------------------------------------------------
449 typedef char *ExcMessage;
451 typedef struct Exception
456 extern Exception FailedAssertion;
457 extern Exception BadArg;
458 extern Exception BadState;
460 extern bool assert_enabled;
463 * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
466 * It should _NOT_ be defined in releases or in benchmark copies
471 * Generates an exception if the given condition is true.
474 #define Trap(condition, exception) \
476 if ((assert_enabled) && (condition)) \
477 ExceptionalCondition(CppAsString(condition), &(exception), \
478 (char*)NULL, __FILE__, __LINE__); \
482 * TrapMacro is the same as Trap but it's intended for use in macros:
484 * #define foo(x) (AssertM(x != 0) && bar(x))
488 #define TrapMacro(condition, exception) \
489 ((bool) ((! assert_enabled) || ! (condition) || \
490 (ExceptionalCondition(CppAsString(condition), \
492 (char*) NULL, __FILE__, __LINE__))))
494 #ifndef USE_ASSERT_CHECKING
495 #define Assert(condition)
496 #define AssertMacro(condition) ((void)true)
497 #define AssertArg(condition)
498 #define AssertState(condition)
499 #define assert_enabled 0
501 #define Assert(condition) \
502 Trap(!(condition), FailedAssertion)
504 #define AssertMacro(condition) \
505 ((void) TrapMacro(!(condition), FailedAssertion))
507 #define AssertArg(condition) \
508 Trap(!(condition), BadArg)
510 #define AssertState(condition) \
511 Trap(!(condition), BadState)
513 #endif /* USE_ASSERT_CHECKING */
517 * Generates an exception with a message if the given condition is true.
520 #define LogTrap(condition, exception, printArgs) \
522 if ((assert_enabled) && (condition)) \
523 ExceptionalCondition(CppAsString(condition), &(exception), \
524 vararg_format printArgs, __FILE__, __LINE__); \
528 * LogTrapMacro is the same as LogTrap but it's intended for use in macros:
530 * #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
532 #define LogTrapMacro(condition, exception, printArgs) \
533 ((bool) ((! assert_enabled) || ! (condition) || \
534 (ExceptionalCondition(CppAsString(condition), \
536 vararg_format printArgs, __FILE__, __LINE__))))
538 extern int ExceptionalCondition(char *conditionName,
539 Exception *exceptionP, char *details,
540 char *fileName, int lineNumber);
541 extern char *vararg_format(const char *fmt,...);
543 #ifndef USE_ASSERT_CHECKING
544 #define LogAssert(condition, printArgs)
545 #define LogAssertMacro(condition, printArgs) true
546 #define LogAssertArg(condition, printArgs)
547 #define LogAssertState(condition, printArgs)
549 #define LogAssert(condition, printArgs) \
550 LogTrap(!(condition), FailedAssertion, printArgs)
552 #define LogAssertMacro(condition, printArgs) \
553 LogTrapMacro(!(condition), FailedAssertion, printArgs)
555 #define LogAssertArg(condition, printArgs) \
556 LogTrap(!(condition), BadArg, printArgs)
558 #define LogAssertState(condition, printArgs) \
559 LogTrap(!(condition), BadState, printArgs)
561 #ifdef ASSERT_CHECKING_TEST
562 extern int assertTest(int val);
566 #endif /* USE_ASSERT_CHECKING */
568 /* ----------------------------------------------------------------
569 * Section 4: genbki macros used by catalog/pg_xxx.h files
570 * ----------------------------------------------------------------
573 typedef struct CppConcat(FormData_,x)
576 #define DATA(x) extern int errno
577 #define DESCR(x) extern int errno
578 #define DECLARE_INDEX(x) extern int errno
579 #define DECLARE_UNIQUE_INDEX(x) extern int errno
581 #define BUILD_INDICES
587 typedef int4 aclitem; /* PHONY definition for catalog use only */
590 #endif /* POSTGRES_H */