]> granicus.if.org Git - postgresql/blob - src/include/postgres.h
Another pgindent run. Fixes enum indenting, and improves #endif
[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.55 2001/10/28 06:25:59 momjian 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 "c.h"
48 #include "utils/elog.h"
49 #include "utils/palloc.h"
50
51 /* ----------------------------------------------------------------
52  *                              Section 1:      variable-length datatypes (TOAST support)
53  * ----------------------------------------------------------------
54  */
55
56 /* ----------------
57  * struct varattrib is the header of a varlena object that may have been TOASTed.
58  * ----------------
59  */
60 #define TUPLE_TOASTER_ACTIVE
61
62 typedef struct varattrib
63 {
64         int32           va_header;              /* External/compressed storage */
65         /* flags and item size */
66         union
67         {
68                 struct
69                 {
70                         int32           va_rawsize;             /* Plain data size */
71                         char            va_data[1];             /* Compressed data */
72                 }                       va_compressed;          /* Compressed stored attribute */
73
74                 struct
75                 {
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 */
81
82                 char            va_data[1]; /* Plain stored attribute */
83         }                       va_content;
84 } varattrib;
85
86 #define VARATT_FLAG_EXTERNAL    0x80000000
87 #define VARATT_FLAG_COMPRESSED  0x40000000
88 #define VARATT_MASK_FLAGS               0xc0000000
89 #define VARATT_MASK_SIZE                0x3fffffff
90
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)
95
96 #define VARSIZE(__PTR)          VARATT_SIZE(__PTR)
97 #define VARDATA(__PTR)          VARATT_DATA(__PTR)
98
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)
105
106
107 /* ----------------------------------------------------------------
108  *                              Section 2:      datum type + support macros
109  * ----------------------------------------------------------------
110  */
111
112 /*
113  * Port Notes:
114  *      Postgres makes the following assumption about machines:
115  *
116  *      sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
117  *
118  *      Postgres also assumes that
119  *
120  *      sizeof(char) == 1
121  *
122  *      and that
123  *
124  *      sizeof(short) == 2
125  *
126  *      If your machine meets these requirements, Datums should also be checked
127  *      to see if the positioning is correct.
128  */
129
130 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
131 typedef Datum *DatumPtr;
132
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)
139
140 /*
141  * DatumGetBool
142  *              Returns boolean value of a datum.
143  *
144  * Note: any nonzero value will be considered TRUE.
145  */
146
147 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
148
149 /*
150  * BoolGetDatum
151  *              Returns datum representation for a boolean.
152  *
153  * Note: any nonzero value will be considered TRUE.
154  */
155
156 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
157
158 /*
159  * DatumGetChar
160  *              Returns character value of a datum.
161  */
162
163 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
164
165 /*
166  * CharGetDatum
167  *              Returns datum representation for a character.
168  */
169
170 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
171
172 /*
173  * Int8GetDatum
174  *              Returns datum representation for an 8-bit integer.
175  */
176
177 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
178
179 /*
180  * DatumGetUInt8
181  *              Returns 8-bit unsigned integer value of a datum.
182  */
183
184 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
185
186 /*
187  * UInt8GetDatum
188  *              Returns datum representation for an 8-bit unsigned integer.
189  */
190
191 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
192
193 /*
194  * DatumGetInt16
195  *              Returns 16-bit integer value of a datum.
196  */
197
198 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
199
200 /*
201  * Int16GetDatum
202  *              Returns datum representation for a 16-bit integer.
203  */
204
205 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
206
207 /*
208  * DatumGetUInt16
209  *              Returns 16-bit unsigned integer value of a datum.
210  */
211
212 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
213
214 /*
215  * UInt16GetDatum
216  *              Returns datum representation for a 16-bit unsigned integer.
217  */
218
219 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
220
221 /*
222  * DatumGetInt32
223  *              Returns 32-bit integer value of a datum.
224  */
225
226 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
227
228 /*
229  * Int32GetDatum
230  *              Returns datum representation for a 32-bit integer.
231  */
232
233 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
234
235 /*
236  * DatumGetUInt32
237  *              Returns 32-bit unsigned integer value of a datum.
238  */
239
240 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
241
242 /*
243  * UInt32GetDatum
244  *              Returns datum representation for a 32-bit unsigned integer.
245  */
246
247 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
248
249 /*
250  * DatumGetObjectId
251  *              Returns object identifier value of a datum.
252  */
253
254 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
255
256 /*
257  * ObjectIdGetDatum
258  *              Returns datum representation for an object identifier.
259  */
260
261 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
262
263 /*
264  * DatumGetTransactionId
265  *              Returns transaction identifier value of a datum.
266  */
267
268 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
269
270 /*
271  * TransactionIdGetDatum
272  *              Returns datum representation for a transaction identifier.
273  */
274
275 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
276
277 /*
278  * DatumGetCommandId
279  *              Returns command identifier value of a datum.
280  */
281
282 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
283
284 /*
285  * CommandIdGetDatum
286  *              Returns datum representation for a command identifier.
287  */
288
289 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
290
291 /*
292  * DatumGetPointer
293  *              Returns pointer value of a datum.
294  */
295
296 #define DatumGetPointer(X) ((Pointer) (X))
297
298 /*
299  * PointerGetDatum
300  *              Returns datum representation for a pointer.
301  */
302
303 #define PointerGetDatum(X) ((Datum) (X))
304
305 /*
306  * DatumGetCString
307  *              Returns C string (null-terminated string) value of a datum.
308  *
309  * Note: C string is not a full-fledged Postgres type at present,
310  * but type input functions use this conversion for their inputs.
311  */
312
313 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
314
315 /*
316  * CStringGetDatum
317  *              Returns datum representation for a C string (null-terminated string).
318  *
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.
323  */
324
325 #define CStringGetDatum(X) PointerGetDatum(X)
326
327 /*
328  * DatumGetName
329  *              Returns name value of a datum.
330  */
331
332 #define DatumGetName(X) ((Name) DatumGetPointer(X))
333
334 /*
335  * NameGetDatum
336  *              Returns datum representation for a name.
337  *
338  * Note: Name is pass-by-reference; caller must ensure the pointed-to
339  * value has adequate lifetime.
340  */
341
342 #define NameGetDatum(X) PointerGetDatum(X)
343
344 /*
345  * DatumGetInt64
346  *              Returns 64-bit integer value of a datum.
347  *
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.
351  */
352
353 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
354
355 /*
356  * Int64GetDatum
357  *              Returns datum representation for a 64-bit integer.
358  *
359  * Note: this routine returns a reference to palloc'd space.
360  */
361
362 extern Datum Int64GetDatum(int64 X);
363
364 /*
365  * DatumGetFloat4
366  *              Returns 4-byte floating point value of a datum.
367  *
368  * Note: this macro hides the fact that float4 is currently a
369  * pass-by-reference type.      Someday it may be pass-by-value.
370  */
371
372 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
373
374 /*
375  * Float4GetDatum
376  *              Returns datum representation for a 4-byte floating point number.
377  *
378  * Note: this routine returns a reference to palloc'd space.
379  */
380
381 extern Datum Float4GetDatum(float4 X);
382
383 /*
384  * DatumGetFloat8
385  *              Returns 8-byte floating point value of a datum.
386  *
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.
390  */
391
392 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
393
394 /*
395  * Float8GetDatum
396  *              Returns datum representation for an 8-byte floating point number.
397  *
398  * Note: this routine returns a reference to palloc'd space.
399  */
400
401 extern Datum Float8GetDatum(float8 X);
402
403
404 /*
405  * DatumGetFloat32
406  *              Returns 32-bit floating point value of a datum.
407  *              This is really a pointer, of course.
408  *
409  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
410  * It will eventually go away.
411  */
412
413 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
414
415 /*
416  * Float32GetDatum
417  *              Returns datum representation for a 32-bit floating point number.
418  *              This is really a pointer, of course.
419  *
420  * XXX: this macro is now deprecated in favor of Float4GetDatum.
421  * It will eventually go away.
422  */
423
424 #define Float32GetDatum(X) PointerGetDatum(X)
425
426 /*
427  * DatumGetFloat64
428  *              Returns 64-bit floating point value of a datum.
429  *              This is really a pointer, of course.
430  *
431  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
432  * It will eventually go away.
433  */
434
435 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
436
437 /*
438  * Float64GetDatum
439  *              Returns datum representation for a 64-bit floating point number.
440  *              This is really a pointer, of course.
441  *
442  * XXX: this macro is now deprecated in favor of Float8GetDatum.
443  * It will eventually go away.
444  */
445
446 #define Float64GetDatum(X) PointerGetDatum(X)
447
448 /*
449  * Int64GetDatumFast
450  * Float4GetDatumFast
451  * Float8GetDatumFast
452  *
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
459  * macros.
460  */
461
462 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
463 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
464 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
465
466
467 /* ----------------------------------------------------------------
468  *                              Section 3:      exception handling definitions
469  *                                                      Assert, Trap, etc macros
470  * ----------------------------------------------------------------
471  */
472
473 typedef char *ExcMessage;
474
475 typedef struct Exception
476 {
477         ExcMessage      message;
478 } Exception;
479
480 extern DLLIMPORT Exception FailedAssertion;
481 extern DLLIMPORT Exception BadArg;
482 extern DLLIMPORT Exception BadState;
483
484 extern DLLIMPORT bool assert_enabled;
485
486 /*
487  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
488  * - plai  9/5/90
489  *
490  * It should _NOT_ be defined in releases or in benchmark copies
491  */
492
493 /*
494  * Trap
495  *              Generates an exception if the given condition is true.
496  *
497  */
498 #define Trap(condition, exception) \
499                 do { \
500                         if ((assert_enabled) && (condition)) \
501                                 ExceptionalCondition(CppAsString(condition), &(exception), \
502                                                 (char*)NULL, __FILE__, __LINE__); \
503                 } while (0)
504
505 /*
506  *      TrapMacro is the same as Trap but it's intended for use in macros:
507  *
508  *              #define foo(x) (AssertM(x != 0) && bar(x))
509  *
510  *      Isn't CPP fun?
511  */
512 #define TrapMacro(condition, exception) \
513         ((bool) ((! assert_enabled) || ! (condition) || \
514                          (ExceptionalCondition(CppAsString(condition), \
515                                                                   &(exception), \
516                                                                   (char*) NULL, __FILE__, __LINE__))))
517
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
524 #else
525 #define Assert(condition) \
526                 Trap(!(condition), FailedAssertion)
527
528 #define AssertMacro(condition) \
529                 ((void) TrapMacro(!(condition), FailedAssertion))
530
531 #define AssertArg(condition) \
532                 Trap(!(condition), BadArg)
533
534 #define AssertState(condition) \
535                 Trap(!(condition), BadState)
536 #endif   /* USE_ASSERT_CHECKING */
537
538 /*
539  * LogTrap
540  *              Generates an exception with a message if the given condition is true.
541  *
542  */
543 #define LogTrap(condition, exception, printArgs) \
544                 do { \
545                         if ((assert_enabled) && (condition)) \
546                                 ExceptionalCondition(CppAsString(condition), &(exception), \
547                                                 vararg_format printArgs, __FILE__, __LINE__); \
548                 } while (0)
549
550 /*
551  *      LogTrapMacro is the same as LogTrap but it's intended for use in macros:
552  *
553  *              #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
554  */
555 #define LogTrapMacro(condition, exception, printArgs) \
556         ((bool) ((! assert_enabled) || ! (condition) || \
557                          (ExceptionalCondition(CppAsString(condition), \
558                                                                    &(exception), \
559                                                                    vararg_format printArgs, __FILE__, __LINE__))))
560
561 extern int ExceptionalCondition(char *conditionName,
562                                          Exception *exceptionP, char *details,
563                                          char *fileName, int lineNumber);
564
565 extern char *
566 vararg_format(const char *fmt,...)
567 /* This lets gcc check the format string for consistency. */
568 __attribute__((format(printf, 1, 2)));
569
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)
575 #else
576 #define LogAssert(condition, printArgs) \
577                 LogTrap(!(condition), FailedAssertion, printArgs)
578
579 #define LogAssertMacro(condition, printArgs) \
580                 LogTrapMacro(!(condition), FailedAssertion, printArgs)
581
582 #define LogAssertArg(condition, printArgs) \
583                 LogTrap(!(condition), BadArg, printArgs)
584
585 #define LogAssertState(condition, printArgs) \
586                 LogTrap(!(condition), BadState, printArgs)
587
588 #ifdef ASSERT_CHECKING_TEST
589 extern int      assertTest(int val);
590 #endif
591 #endif   /* USE_ASSERT_CHECKING */
592
593 /* ----------------------------------------------------------------
594  *                              Section 4: genbki macros used by catalog/pg_xxx.h files
595  * ----------------------------------------------------------------
596  */
597 #define CATALOG(x)      typedef struct CppConcat(FormData_,x)
598
599 #define BOOTSTRAP
600
601 #define BKI_WITHOUT_OIDS
602
603 /* these need to expand into some harmless, repeatable declaration */
604 #define DATA(x)   extern int errno
605 #define DESCR(x)  extern int errno
606
607 #define BKI_BEGIN
608 #define BKI_END
609
610 typedef int4 aclitem;                   /* PHONY definition for catalog use only */
611
612 #endif   /* POSTGRES_H */