]> granicus.if.org Git - postgresql/blob - src/include/postgres.h
pgindent run.
[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-2002, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1995, Regents of the University of California
12  *
13  * $Id: postgres.h,v 1.61 2002/09/04 20:31:36 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
34  *      in the backend environment, but are of no interest outside the backend.
35  *
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.
42  *
43  *----------------------------------------------------------------
44  */
45 #ifndef POSTGRES_H
46 #define POSTGRES_H
47
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
59  * TOASTed.
60  * ----------------
61  */
62 #define TUPLE_TOASTER_ACTIVE
63
64 typedef struct varattrib
65 {
66         int32           va_header;              /* External/compressed storage */
67         /* flags and item size */
68         union
69         {
70                 struct
71                 {
72                         int32           va_rawsize;             /* Plain data size */
73                         char            va_data[1];             /* Compressed data */
74                 }                       va_compressed;          /* Compressed stored attribute */
75
76                 struct
77                 {
78                         int32           va_rawsize;             /* Plain data size */
79                         int32           va_extsize;             /* External saved size */
80                         Oid                     va_valueid;             /* Unique identifier of value */
81                         Oid                     va_toastrelid;  /* RelID where to find chunks */
82                 }                       va_external;    /* External stored attribute */
83
84                 char            va_data[1]; /* Plain stored attribute */
85         }                       va_content;
86 } varattrib;
87
88 #define VARATT_FLAG_EXTERNAL    0x80000000
89 #define VARATT_FLAG_COMPRESSED  0x40000000
90 #define VARATT_MASK_FLAGS               0xc0000000
91 #define VARATT_MASK_SIZE                0x3fffffff
92
93 #define VARATT_SIZEP(_PTR)      (((varattrib *)(_PTR))->va_header)
94 #define VARATT_SIZE(PTR)        (VARATT_SIZEP(PTR) & VARATT_MASK_SIZE)
95 #define VARATT_DATA(PTR)        (((varattrib *)(PTR))->va_content.va_data)
96 #define VARATT_CDATA(PTR)       (((varattrib *)(PTR))->va_content.va_compressed.va_data)
97
98 #define VARSIZE(__PTR)          VARATT_SIZE(__PTR)
99 #define VARDATA(__PTR)          VARATT_DATA(__PTR)
100
101 #define VARATT_IS_EXTENDED(PTR)         \
102                                 ((VARATT_SIZEP(PTR) & VARATT_MASK_FLAGS) != 0)
103 #define VARATT_IS_EXTERNAL(PTR)         \
104                                 ((VARATT_SIZEP(PTR) & VARATT_FLAG_EXTERNAL) != 0)
105 #define VARATT_IS_COMPRESSED(PTR)       \
106                                 ((VARATT_SIZEP(PTR) & VARATT_FLAG_COMPRESSED) != 0)
107
108
109 /* ----------------------------------------------------------------
110  *                              Section 2:      datum type + support macros
111  * ----------------------------------------------------------------
112  */
113
114 /*
115  * Port Notes:
116  *      Postgres makes the following assumption about machines:
117  *
118  *      sizeof(Datum) == sizeof(long) >= sizeof(void *) >= 4
119  *
120  *      Postgres also assumes that
121  *
122  *      sizeof(char) == 1
123  *
124  *      and that
125  *
126  *      sizeof(short) == 2
127  *
128  *      If your machine meets these requirements, Datums should also be checked
129  *      to see if the positioning is correct.
130  */
131
132 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
133 typedef Datum *DatumPtr;
134
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)
141
142 /*
143  * DatumGetBool
144  *              Returns boolean value of a datum.
145  *
146  * Note: any nonzero value will be considered TRUE.
147  */
148
149 #define DatumGetBool(X) ((bool) (((Datum) (X)) != 0))
150
151 /*
152  * BoolGetDatum
153  *              Returns datum representation for a boolean.
154  *
155  * Note: any nonzero value will be considered TRUE.
156  */
157
158 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
159
160 /*
161  * DatumGetChar
162  *              Returns character value of a datum.
163  */
164
165 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
166
167 /*
168  * CharGetDatum
169  *              Returns datum representation for a character.
170  */
171
172 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
173
174 /*
175  * Int8GetDatum
176  *              Returns datum representation for an 8-bit integer.
177  */
178
179 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
180
181 /*
182  * DatumGetUInt8
183  *              Returns 8-bit unsigned integer value of a datum.
184  */
185
186 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
187
188 /*
189  * UInt8GetDatum
190  *              Returns datum representation for an 8-bit unsigned integer.
191  */
192
193 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
194
195 /*
196  * DatumGetInt16
197  *              Returns 16-bit integer value of a datum.
198  */
199
200 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
201
202 /*
203  * Int16GetDatum
204  *              Returns datum representation for a 16-bit integer.
205  */
206
207 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
208
209 /*
210  * DatumGetUInt16
211  *              Returns 16-bit unsigned integer value of a datum.
212  */
213
214 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
215
216 /*
217  * UInt16GetDatum
218  *              Returns datum representation for a 16-bit unsigned integer.
219  */
220
221 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
222
223 /*
224  * DatumGetInt32
225  *              Returns 32-bit integer value of a datum.
226  */
227
228 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
229
230 /*
231  * Int32GetDatum
232  *              Returns datum representation for a 32-bit integer.
233  */
234
235 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
236
237 /*
238  * DatumGetUInt32
239  *              Returns 32-bit unsigned integer value of a datum.
240  */
241
242 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
243
244 /*
245  * UInt32GetDatum
246  *              Returns datum representation for a 32-bit unsigned integer.
247  */
248
249 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
250
251 /*
252  * DatumGetObjectId
253  *              Returns object identifier value of a datum.
254  */
255
256 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
257
258 /*
259  * ObjectIdGetDatum
260  *              Returns datum representation for an object identifier.
261  */
262
263 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
264
265 /*
266  * DatumGetTransactionId
267  *              Returns transaction identifier value of a datum.
268  */
269
270 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
271
272 /*
273  * TransactionIdGetDatum
274  *              Returns datum representation for a transaction identifier.
275  */
276
277 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
278
279 /*
280  * DatumGetCommandId
281  *              Returns command identifier value of a datum.
282  */
283
284 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
285
286 /*
287  * CommandIdGetDatum
288  *              Returns datum representation for a command identifier.
289  */
290
291 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
292
293 /*
294  * DatumGetPointer
295  *              Returns pointer value of a datum.
296  */
297
298 #define DatumGetPointer(X) ((Pointer) (X))
299
300 /*
301  * PointerGetDatum
302  *              Returns datum representation for a pointer.
303  */
304
305 #define PointerGetDatum(X) ((Datum) (X))
306
307 /*
308  * DatumGetCString
309  *              Returns C string (null-terminated string) value of a datum.
310  *
311  * Note: C string is not a full-fledged Postgres type at present,
312  * but type input functions use this conversion for their inputs.
313  */
314
315 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
316
317 /*
318  * CStringGetDatum
319  *              Returns datum representation for a C string (null-terminated string).
320  *
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.
325  */
326
327 #define CStringGetDatum(X) PointerGetDatum(X)
328
329 /*
330  * DatumGetName
331  *              Returns name value of a datum.
332  */
333
334 #define DatumGetName(X) ((Name) DatumGetPointer(X))
335
336 /*
337  * NameGetDatum
338  *              Returns datum representation for a name.
339  *
340  * Note: Name is pass-by-reference; caller must ensure the pointed-to
341  * value has adequate lifetime.
342  */
343
344 #define NameGetDatum(X) PointerGetDatum(X)
345
346 /*
347  * DatumGetInt64
348  *              Returns 64-bit integer value of a datum.
349  *
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.
353  */
354
355 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
356
357 /*
358  * Int64GetDatum
359  *              Returns datum representation for a 64-bit integer.
360  *
361  * Note: this routine returns a reference to palloc'd space.
362  */
363
364 extern Datum Int64GetDatum(int64 X);
365
366 /*
367  * DatumGetFloat4
368  *              Returns 4-byte floating point value of a datum.
369  *
370  * Note: this macro hides the fact that float4 is currently a
371  * pass-by-reference type.      Someday it may be pass-by-value.
372  */
373
374 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
375
376 /*
377  * Float4GetDatum
378  *              Returns datum representation for a 4-byte floating point number.
379  *
380  * Note: this routine returns a reference to palloc'd space.
381  */
382
383 extern Datum Float4GetDatum(float4 X);
384
385 /*
386  * DatumGetFloat8
387  *              Returns 8-byte floating point value of a datum.
388  *
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.
392  */
393
394 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
395
396 /*
397  * Float8GetDatum
398  *              Returns datum representation for an 8-byte floating point number.
399  *
400  * Note: this routine returns a reference to palloc'd space.
401  */
402
403 extern Datum Float8GetDatum(float8 X);
404
405
406 /*
407  * DatumGetFloat32
408  *              Returns 32-bit floating point value of a datum.
409  *              This is really a pointer, of course.
410  *
411  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
412  * It will eventually go away.
413  */
414
415 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
416
417 /*
418  * Float32GetDatum
419  *              Returns datum representation for a 32-bit floating point number.
420  *              This is really a pointer, of course.
421  *
422  * XXX: this macro is now deprecated in favor of Float4GetDatum.
423  * It will eventually go away.
424  */
425
426 #define Float32GetDatum(X) PointerGetDatum(X)
427
428 /*
429  * DatumGetFloat64
430  *              Returns 64-bit floating point value of a datum.
431  *              This is really a pointer, of course.
432  *
433  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
434  * It will eventually go away.
435  */
436
437 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
438
439 /*
440  * Float64GetDatum
441  *              Returns datum representation for a 64-bit floating point number.
442  *              This is really a pointer, of course.
443  *
444  * XXX: this macro is now deprecated in favor of Float8GetDatum.
445  * It will eventually go away.
446  */
447
448 #define Float64GetDatum(X) PointerGetDatum(X)
449
450 /*
451  * Int64GetDatumFast
452  * Float4GetDatumFast
453  * Float8GetDatumFast
454  *
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
461  * macros.
462  */
463
464 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
465 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
466 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
467
468
469 /* ----------------------------------------------------------------
470  *                              Section 3:      exception handling definitions
471  *                                                      Assert, Trap, etc macros
472  * ----------------------------------------------------------------
473  */
474
475 extern DLLIMPORT bool assert_enabled;
476
477 /*
478  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
479  * - plai  9/5/90
480  *
481  * It should _NOT_ be defined in releases or in benchmark copies
482  */
483
484 /*
485  * Trap
486  *              Generates an exception if the given condition is true.
487  *
488  */
489 #define Trap(condition, errorType) \
490                 do { \
491                         if ((assert_enabled) && (condition)) \
492                                 ExceptionalCondition(CppAsString(condition), (errorType), \
493                                                                          __FILE__, __LINE__); \
494                 } while (0)
495
496 /*
497  *      TrapMacro is the same as Trap but it's intended for use in macros:
498  *
499  *              #define foo(x) (AssertMacro(x != 0) && bar(x))
500  *
501  *      Isn't CPP fun?
502  */
503 #define TrapMacro(condition, errorType) \
504         ((bool) ((! assert_enabled) || ! (condition) || \
505                          (ExceptionalCondition(CppAsString(condition), (errorType), \
506                                                                    __FILE__, __LINE__))))
507
508 #ifndef USE_ASSERT_CHECKING
509 #define Assert(condition)
510 #define AssertMacro(condition)  ((void)true)
511 #define AssertArg(condition)
512 #define AssertState(condition)
513 #define assert_enabled 0
514 #else
515 #define Assert(condition) \
516                 Trap(!(condition), "FailedAssertion")
517
518 #define AssertMacro(condition) \
519                 ((void) TrapMacro(!(condition), "FailedAssertion"))
520
521 #define AssertArg(condition) \
522                 Trap(!(condition), "BadArgument")
523
524 #define AssertState(condition) \
525                 Trap(!(condition), "BadState")
526 #endif   /* USE_ASSERT_CHECKING */
527
528 extern int ExceptionalCondition(char *conditionName, char *errorType,
529                                          char *fileName, int lineNumber);
530
531 /* ----------------------------------------------------------------
532  *                              Section 4: genbki macros used by catalog/pg_xxx.h files
533  * ----------------------------------------------------------------
534  */
535 #define CATALOG(x)      typedef struct CppConcat(FormData_,x)
536
537 #define BOOTSTRAP
538 #define BKI_SHARED_RELATION
539 #define BKI_WITHOUT_OIDS
540
541 /* these need to expand into some harmless, repeatable declaration */
542 #define DATA(x)   extern int errno
543 #define DESCR(x)  extern int errno
544
545 #define BKI_BEGIN
546 #define BKI_END
547
548 typedef int4 aclitem;                   /* PHONY definition for catalog use only */
549
550 #endif   /* POSTGRES_H */