]> granicus.if.org Git - postgresql/blob - src/include/postgres.h
make sure the $Id tags are converted to $PostgreSQL as well ...
[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-2003, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1995, Regents of the University of California
12  *
13  * $PostgreSQL: pgsql/src/include/postgres.h,v 1.66 2003/11/29 22:40:53 pgsql 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
134 #define SIZEOF_DATUM SIZEOF_UNSIGNED_LONG
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  * DatumGetTransactionId
269  *              Returns transaction identifier value of a datum.
270  */
271
272 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
273
274 /*
275  * TransactionIdGetDatum
276  *              Returns datum representation for a transaction identifier.
277  */
278
279 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
280
281 /*
282  * DatumGetCommandId
283  *              Returns command identifier value of a datum.
284  */
285
286 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
287
288 /*
289  * CommandIdGetDatum
290  *              Returns datum representation for a command identifier.
291  */
292
293 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
294
295 /*
296  * DatumGetPointer
297  *              Returns pointer value of a datum.
298  */
299
300 #define DatumGetPointer(X) ((Pointer) (X))
301
302 /*
303  * PointerGetDatum
304  *              Returns datum representation for a pointer.
305  */
306
307 #define PointerGetDatum(X) ((Datum) (X))
308
309 /*
310  * DatumGetCString
311  *              Returns C string (null-terminated string) value of a datum.
312  *
313  * Note: C string is not a full-fledged Postgres type at present,
314  * but type input functions use this conversion for their inputs.
315  */
316
317 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
318
319 /*
320  * CStringGetDatum
321  *              Returns datum representation for a C string (null-terminated string).
322  *
323  * Note: C string is not a full-fledged Postgres type at present,
324  * but type output functions use this conversion for their outputs.
325  * Note: CString is pass-by-reference; caller must ensure the pointed-to
326  * value has adequate lifetime.
327  */
328
329 #define CStringGetDatum(X) PointerGetDatum(X)
330
331 /*
332  * DatumGetName
333  *              Returns name value of a datum.
334  */
335
336 #define DatumGetName(X) ((Name) DatumGetPointer(X))
337
338 /*
339  * NameGetDatum
340  *              Returns datum representation for a name.
341  *
342  * Note: Name is pass-by-reference; caller must ensure the pointed-to
343  * value has adequate lifetime.
344  */
345
346 #define NameGetDatum(X) PointerGetDatum(X)
347
348 /*
349  * DatumGetInt64
350  *              Returns 64-bit integer value of a datum.
351  *
352  * Note: this macro hides the fact that int64 is currently a
353  * pass-by-reference type.      Someday it may be pass-by-value,
354  * at least on some platforms.
355  */
356
357 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
358
359 /*
360  * Int64GetDatum
361  *              Returns datum representation for a 64-bit integer.
362  *
363  * Note: this routine returns a reference to palloc'd space.
364  */
365
366 extern Datum Int64GetDatum(int64 X);
367
368 /*
369  * DatumGetFloat4
370  *              Returns 4-byte floating point value of a datum.
371  *
372  * Note: this macro hides the fact that float4 is currently a
373  * pass-by-reference type.      Someday it may be pass-by-value.
374  */
375
376 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
377
378 /*
379  * Float4GetDatum
380  *              Returns datum representation for a 4-byte floating point number.
381  *
382  * Note: this routine returns a reference to palloc'd space.
383  */
384
385 extern Datum Float4GetDatum(float4 X);
386
387 /*
388  * DatumGetFloat8
389  *              Returns 8-byte floating point value of a datum.
390  *
391  * Note: this macro hides the fact that float8 is currently a
392  * pass-by-reference type.      Someday it may be pass-by-value,
393  * at least on some platforms.
394  */
395
396 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
397
398 /*
399  * Float8GetDatum
400  *              Returns datum representation for an 8-byte floating point number.
401  *
402  * Note: this routine returns a reference to palloc'd space.
403  */
404
405 extern Datum Float8GetDatum(float8 X);
406
407
408 /*
409  * DatumGetFloat32
410  *              Returns 32-bit floating point value of a datum.
411  *              This is really a pointer, of course.
412  *
413  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
414  * It will eventually go away.
415  */
416
417 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
418
419 /*
420  * Float32GetDatum
421  *              Returns datum representation for a 32-bit floating point number.
422  *              This is really a pointer, of course.
423  *
424  * XXX: this macro is now deprecated in favor of Float4GetDatum.
425  * It will eventually go away.
426  */
427
428 #define Float32GetDatum(X) PointerGetDatum(X)
429
430 /*
431  * DatumGetFloat64
432  *              Returns 64-bit floating point value of a datum.
433  *              This is really a pointer, of course.
434  *
435  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
436  * It will eventually go away.
437  */
438
439 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
440
441 /*
442  * Float64GetDatum
443  *              Returns datum representation for a 64-bit floating point number.
444  *              This is really a pointer, of course.
445  *
446  * XXX: this macro is now deprecated in favor of Float8GetDatum.
447  * It will eventually go away.
448  */
449
450 #define Float64GetDatum(X) PointerGetDatum(X)
451
452 /*
453  * Int64GetDatumFast
454  * Float4GetDatumFast
455  * Float8GetDatumFast
456  *
457  * These macros are intended to allow writing code that does not depend on
458  * whether int64, float4, float8 are pass-by-reference types, while not
459  * sacrificing performance when they are.  The argument must be a variable
460  * that will exist and have the same value for as long as the Datum is needed.
461  * In the pass-by-ref case, the address of the variable is taken to use as
462  * the Datum.  In the pass-by-val case, these will be the same as the non-Fast
463  * macros.
464  */
465
466 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
467 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
468 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
469
470
471 /* ----------------------------------------------------------------
472  *                              Section 3:      exception handling definitions
473  *                                                      Assert, Trap, etc macros
474  * ----------------------------------------------------------------
475  */
476
477 extern DLLIMPORT bool assert_enabled;
478
479 /*
480  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
481  * - plai  9/5/90
482  *
483  * It should _NOT_ be defined in releases or in benchmark copies
484  */
485
486 /*
487  * Trap
488  *              Generates an exception if the given condition is true.
489  *
490  */
491 #define Trap(condition, errorType) \
492                 do { \
493                         if ((assert_enabled) && (condition)) \
494                                 ExceptionalCondition(CppAsString(condition), (errorType), \
495                                                                          __FILE__, __LINE__); \
496                 } while (0)
497
498 /*
499  *      TrapMacro is the same as Trap but it's intended for use in macros:
500  *
501  *              #define foo(x) (AssertMacro(x != 0) && bar(x))
502  *
503  *      Isn't CPP fun?
504  */
505 #define TrapMacro(condition, errorType) \
506         ((bool) ((! assert_enabled) || ! (condition) || \
507                          (ExceptionalCondition(CppAsString(condition), (errorType), \
508                                                                    __FILE__, __LINE__))))
509
510 #ifndef USE_ASSERT_CHECKING
511 #define Assert(condition)
512 #define AssertMacro(condition)  ((void)true)
513 #define AssertArg(condition)
514 #define AssertState(condition)
515 #define assert_enabled 0
516 #else
517 #define Assert(condition) \
518                 Trap(!(condition), "FailedAssertion")
519
520 #define AssertMacro(condition) \
521                 ((void) TrapMacro(!(condition), "FailedAssertion"))
522
523 #define AssertArg(condition) \
524                 Trap(!(condition), "BadArgument")
525
526 #define AssertState(condition) \
527                 Trap(!(condition), "BadState")
528 #endif   /* USE_ASSERT_CHECKING */
529
530 extern int ExceptionalCondition(char *conditionName, char *errorType,
531                                          char *fileName, int lineNumber);
532
533 /* ----------------------------------------------------------------
534  *                              Section 4: genbki macros used by catalog/pg_xxx.h files
535  * ----------------------------------------------------------------
536  */
537 #define CATALOG(x)      typedef struct CppConcat(FormData_,x)
538
539 #define BOOTSTRAP
540 #define BKI_SHARED_RELATION
541 #define BKI_WITHOUT_OIDS
542
543 /* these need to expand into some harmless, repeatable declaration */
544 #define DATA(x)   extern int no_such_variable
545 #define DESCR(x)  extern int no_such_variable
546
547 #define BKI_BEGIN
548 #define BKI_END
549
550 typedef int4 aclitem;                   /* PHONY definition for catalog use only */
551
552 #endif   /* POSTGRES_H */