]> granicus.if.org Git - postgresql/blob - src/include/postgres.h
Restructure the key include files per recent pghackers discussion: there
[postgresql] / src / include / postgres.h
1 /*-------------------------------------------------------------------------
2  *
3  * postgres.h
4  *        Primary include file for PostgreSQL server .c files
5  *
6  * This should be the first file included by PostgreSQL backend modules.
7  * Client-side code should include postgres_fe.h instead.
8  *
9  *
10  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1995, Regents of the University of California
12  *
13  * $Id: postgres.h,v 1.46 2001/02/10 02:31:28 tgl Exp $
14  *
15  *-------------------------------------------------------------------------
16  */
17 /*
18  *----------------------------------------------------------------
19  *       TABLE OF CONTENTS
20  *
21  *              When adding stuff to this file, please try to put stuff
22  *              into the relevant section, or add new sections as appropriate.
23  *
24  *        section       description
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
30  *
31  *       NOTES
32  *
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.
35  *
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.
41  *
42  *----------------------------------------------------------------
43  */
44 #ifndef POSTGRES_H
45 #define POSTGRES_H
46
47 #include "postgres_ext.h"
48 #include "c.h"
49 #include "utils/elog.h"
50 #include "utils/palloc.h"
51
52 /* ----------------------------------------------------------------
53  *                              Section 1:      variable-length datatypes (TOAST support)
54  * ----------------------------------------------------------------
55  */
56
57 /* ----------------
58  *              struct varattrib is the header of a varlena object that may have been TOASTed.
59  * ----------------
60  */
61 #define TUPLE_TOASTER_ACTIVE
62
63 typedef struct varattrib
64 {
65         int32           va_header;              /* External/compressed storage */
66         /* flags and item size */
67         union
68         {
69                 struct
70                 {
71                         int32           va_rawsize;             /* Plain data size */
72                         char            va_data[1];             /* Compressed data */
73                 }                       va_compressed;          /* Compressed stored attribute */
74
75                 struct
76                 {
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 */
85
86                 char            va_data[1]; /* Plain stored attribute */
87         }                       va_content;
88 }                       varattrib;
89
90 #define VARATT_FLAG_EXTERNAL    0x80000000
91 #define VARATT_FLAG_COMPRESSED  0x40000000
92 #define VARATT_MASK_FLAGS               0xc0000000
93 #define VARATT_MASK_SIZE                0x3fffffff
94
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)
99
100 #define VARSIZE(__PTR)          VARATT_SIZE(__PTR)
101 #define VARDATA(__PTR)          VARATT_DATA(__PTR)
102
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)
109
110
111 /* ----------------------------------------------------------------
112  *                              Section 2:      datum type + support macros
113  * ----------------------------------------------------------------
114  */
115
116 /*
117  * Port Notes:
118  *      Postgres makes the following assumption about machines:
119  *
120  *      sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
121  *
122  *      Postgres also assumes that
123  *
124  *      sizeof(char) == 1
125  *
126  *      and that
127  *
128  *      sizeof(short) == 2
129  *
130  *      If your machine meets these requirements, Datums should also be checked
131  *      to see if the positioning is correct.
132  */
133
134 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
135 typedef Datum *DatumPtr;
136
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)
143
144 /*
145  * DatumGetBool
146  *              Returns boolean value of a datum.
147  *
148  * Note: any nonzero value will be considered TRUE.
149  */
150
151 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
152
153 /*
154  * BoolGetDatum
155  *              Returns datum representation for a boolean.
156  *
157  * Note: any nonzero value will be considered TRUE.
158  */
159
160 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
161
162 /*
163  * DatumGetChar
164  *              Returns character value of a datum.
165  */
166
167 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
168
169 /*
170  * CharGetDatum
171  *              Returns datum representation for a character.
172  */
173
174 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
175
176 /*
177  * Int8GetDatum
178  *              Returns datum representation for an 8-bit integer.
179  */
180
181 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
182
183 /*
184  * DatumGetUInt8
185  *              Returns 8-bit unsigned integer value of a datum.
186  */
187
188 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
189
190 /*
191  * UInt8GetDatum
192  *              Returns datum representation for an 8-bit unsigned integer.
193  */
194
195 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
196
197 /*
198  * DatumGetInt16
199  *              Returns 16-bit integer value of a datum.
200  */
201
202 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
203
204 /*
205  * Int16GetDatum
206  *              Returns datum representation for a 16-bit integer.
207  */
208
209 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
210
211 /*
212  * DatumGetUInt16
213  *              Returns 16-bit unsigned integer value of a datum.
214  */
215
216 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
217
218 /*
219  * UInt16GetDatum
220  *              Returns datum representation for a 16-bit unsigned integer.
221  */
222
223 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
224
225 /*
226  * DatumGetInt32
227  *              Returns 32-bit integer value of a datum.
228  */
229
230 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
231
232 /*
233  * Int32GetDatum
234  *              Returns datum representation for a 32-bit integer.
235  */
236
237 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
238
239 /*
240  * DatumGetUInt32
241  *              Returns 32-bit unsigned integer value of a datum.
242  */
243
244 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
245
246 /*
247  * UInt32GetDatum
248  *              Returns datum representation for a 32-bit unsigned integer.
249  */
250
251 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
252
253 /*
254  * DatumGetObjectId
255  *              Returns object identifier value of a datum.
256  */
257
258 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
259
260 /*
261  * ObjectIdGetDatum
262  *              Returns datum representation for an object identifier.
263  */
264
265 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
266
267 /*
268  * DatumGetPointer
269  *              Returns pointer value of a datum.
270  */
271
272 #define DatumGetPointer(X) ((Pointer) (X))
273
274 /*
275  * PointerGetDatum
276  *              Returns datum representation for a pointer.
277  */
278
279 #define PointerGetDatum(X) ((Datum) (X))
280
281 /*
282  * DatumGetCString
283  *              Returns C string (null-terminated string) value of a datum.
284  *
285  * Note: C string is not a full-fledged Postgres type at present,
286  * but type input functions use this conversion for their inputs.
287  */
288
289 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
290
291 /*
292  * CStringGetDatum
293  *              Returns datum representation for a C string (null-terminated string).
294  *
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.
299  */
300
301 #define CStringGetDatum(X) PointerGetDatum(X)
302
303 /*
304  * DatumGetName
305  *              Returns name value of a datum.
306  */
307
308 #define DatumGetName(X) ((Name) DatumGetPointer(X))
309
310 /*
311  * NameGetDatum
312  *              Returns datum representation for a name.
313  *
314  * Note: Name is pass-by-reference; caller must ensure the pointed-to
315  * value has adequate lifetime.
316  */
317
318 #define NameGetDatum(X) PointerGetDatum(X)
319
320 /*
321  * DatumGetInt64
322  *              Returns 64-bit integer value of a datum.
323  *
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.
327  */
328
329 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
330
331 /*
332  * Int64GetDatum
333  *              Returns datum representation for a 64-bit integer.
334  *
335  * Note: this routine returns a reference to palloc'd space.
336  */
337
338 extern Datum Int64GetDatum(int64 X);
339
340 /*
341  * DatumGetFloat4
342  *              Returns 4-byte floating point value of a datum.
343  *
344  * Note: this macro hides the fact that float4 is currently a
345  * pass-by-reference type.  Someday it may be pass-by-value.
346  */
347
348 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
349
350 /*
351  * Float4GetDatum
352  *              Returns datum representation for a 4-byte floating point number.
353  *
354  * Note: this routine returns a reference to palloc'd space.
355  */
356
357 extern Datum Float4GetDatum(float4 X);
358
359 /*
360  * DatumGetFloat8
361  *              Returns 8-byte floating point value of a datum.
362  *
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.
366  */
367
368 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
369
370 /*
371  * Float8GetDatum
372  *              Returns datum representation for an 8-byte floating point number.
373  *
374  * Note: this routine returns a reference to palloc'd space.
375  */
376
377 extern Datum Float8GetDatum(float8 X);
378
379
380 /*
381  * DatumGetFloat32
382  *              Returns 32-bit floating point value of a datum.
383  *              This is really a pointer, of course.
384  *
385  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
386  * It will eventually go away.
387  */
388
389 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
390
391 /*
392  * Float32GetDatum
393  *              Returns datum representation for a 32-bit floating point number.
394  *              This is really a pointer, of course.
395  *
396  * XXX: this macro is now deprecated in favor of Float4GetDatum.
397  * It will eventually go away.
398  */
399
400 #define Float32GetDatum(X) PointerGetDatum(X)
401
402 /*
403  * DatumGetFloat64
404  *              Returns 64-bit floating point value of a datum.
405  *              This is really a pointer, of course.
406  *
407  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
408  * It will eventually go away.
409  */
410
411 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
412
413 /*
414  * Float64GetDatum
415  *              Returns datum representation for a 64-bit floating point number.
416  *              This is really a pointer, of course.
417  *
418  * XXX: this macro is now deprecated in favor of Float8GetDatum.
419  * It will eventually go away.
420  */
421
422 #define Float64GetDatum(X) PointerGetDatum(X)
423
424 /*
425  * Int64GetDatumFast
426  * Float4GetDatumFast
427  * Float8GetDatumFast
428  *
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
435  * macros.
436  */
437
438 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
439 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
440 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
441
442
443 /* ----------------------------------------------------------------
444  *                              Section 3:      exception handling definitions
445  *                                                      Assert, Trap, etc macros
446  * ----------------------------------------------------------------
447  */
448
449 typedef char *ExcMessage;
450
451 typedef struct Exception
452 {
453         ExcMessage      message;
454 } Exception;
455
456 extern Exception FailedAssertion;
457 extern Exception BadArg;
458 extern Exception BadState;
459
460 extern bool     assert_enabled;
461
462 /*
463  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
464  * - plai  9/5/90
465  *
466  * It should _NOT_ be defined in releases or in benchmark copies
467  */
468
469 /*
470  * Trap
471  *              Generates an exception if the given condition is true.
472  *
473  */
474 #define Trap(condition, exception) \
475                 do { \
476                         if ((assert_enabled) && (condition)) \
477                                 ExceptionalCondition(CppAsString(condition), &(exception), \
478                                                 (char*)NULL, __FILE__, __LINE__); \
479                 } while (0)
480
481 /*
482  *      TrapMacro is the same as Trap but it's intended for use in macros:
483  *
484  *              #define foo(x) (AssertM(x != 0) && bar(x))
485  *
486  *      Isn't CPP fun?
487  */
488 #define TrapMacro(condition, exception) \
489         ((bool) ((! assert_enabled) || ! (condition) || \
490                          (ExceptionalCondition(CppAsString(condition), \
491                                                                   &(exception), \
492                                                                   (char*) NULL, __FILE__, __LINE__))))
493
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
500 #else
501 #define Assert(condition) \
502                 Trap(!(condition), FailedAssertion)
503
504 #define AssertMacro(condition) \
505                 ((void) TrapMacro(!(condition), FailedAssertion))
506
507 #define AssertArg(condition) \
508                 Trap(!(condition), BadArg)
509
510 #define AssertState(condition) \
511                 Trap(!(condition), BadState)
512
513 #endif   /* USE_ASSERT_CHECKING */
514
515 /*
516  * LogTrap
517  *              Generates an exception with a message if the given condition is true.
518  *
519  */
520 #define LogTrap(condition, exception, printArgs) \
521                 do { \
522                         if ((assert_enabled) && (condition)) \
523                                 ExceptionalCondition(CppAsString(condition), &(exception), \
524                                                 vararg_format printArgs, __FILE__, __LINE__); \
525                 } while (0)
526
527 /*
528  *      LogTrapMacro is the same as LogTrap but it's intended for use in macros:
529  *
530  *              #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
531  */
532 #define LogTrapMacro(condition, exception, printArgs) \
533         ((bool) ((! assert_enabled) || ! (condition) || \
534                          (ExceptionalCondition(CppAsString(condition), \
535                                                                    &(exception), \
536                                                                    vararg_format printArgs, __FILE__, __LINE__))))
537
538 extern int ExceptionalCondition(char *conditionName,
539                                          Exception *exceptionP, char *details,
540                                          char *fileName, int lineNumber);
541 extern char *vararg_format(const char *fmt, ...);
542
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)
548 #else
549 #define LogAssert(condition, printArgs) \
550                 LogTrap(!(condition), FailedAssertion, printArgs)
551
552 #define LogAssertMacro(condition, printArgs) \
553                 LogTrapMacro(!(condition), FailedAssertion, printArgs)
554
555 #define LogAssertArg(condition, printArgs) \
556                 LogTrap(!(condition), BadArg, printArgs)
557
558 #define LogAssertState(condition, printArgs) \
559                 LogTrap(!(condition), BadState, printArgs)
560
561 #ifdef ASSERT_CHECKING_TEST
562 extern int      assertTest(int val);
563
564 #endif
565
566 #endif   /* USE_ASSERT_CHECKING */
567
568 /* ----------------------------------------------------------------
569  *                              Section 4: genbki macros used by catalog/pg_xxx.h files
570  * ----------------------------------------------------------------
571  */
572 #define CATALOG(x) \
573         typedef struct CppConcat(FormData_,x)
574
575 /* Huh? */
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
580
581 #define BUILD_INDICES
582 #define BOOTSTRAP
583
584 #define BKI_BEGIN
585 #define BKI_END
586
587 typedef int4 aclitem;                   /* PHONY definition for catalog use only */
588
589
590 #endif   /* POSTGRES_H */