]> granicus.if.org Git - postgresql/blob - src/include/postgres.h
Adjust DatumGetBool macro so that it isn't fooled by garbage in the Datum
[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-2007, 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.78 2007/03/23 20:24:41 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
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.  Generally, only the code closely associated with TOAST logic
60  * should mess directly with struct varattrib or use the VARATT_FOO macros.
61  * ----------------
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                 }                       va_external;    /* External stored attribute */
82
83                 char            va_data[1]; /* Plain stored attribute */
84         }                       va_content;
85 } varattrib;
86
87 #define VARATT_FLAG_EXTERNAL    0x80000000
88 #define VARATT_FLAG_COMPRESSED  0x40000000
89 #define VARATT_MASK_FLAGS               0xc0000000
90 #define VARATT_MASK_SIZE                0x3fffffff
91
92 #define VARATT_SIZEP_DEPRECATED(PTR)    (((varattrib *) (PTR))->va_header_)
93
94 #define VARATT_IS_EXTENDED(PTR)         \
95                                 ((VARATT_SIZEP_DEPRECATED(PTR) & VARATT_MASK_FLAGS) != 0)
96 #define VARATT_IS_EXTERNAL(PTR)         \
97                                 ((VARATT_SIZEP_DEPRECATED(PTR) & VARATT_FLAG_EXTERNAL) != 0)
98 #define VARATT_IS_COMPRESSED(PTR)       \
99                                 ((VARATT_SIZEP_DEPRECATED(PTR) & VARATT_FLAG_COMPRESSED) != 0)
100
101 /* These macros are the ones for non-TOAST code to use */
102
103 #define VARSIZE(PTR)    (VARATT_SIZEP_DEPRECATED(PTR) & VARATT_MASK_SIZE)
104 #define VARDATA(PTR)    (((varattrib *) (PTR))->va_content.va_data)
105
106 #define SET_VARSIZE(PTR,SIZE)   (VARATT_SIZEP_DEPRECATED(PTR) = (SIZE))
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  * When a type narrower than Datum is stored in a Datum, we place it in the
129  * low-order bits and are careful that the DatumGetXXX macro for it discards
130  * the unused high-order bits (as opposed to, say, assuming they are zero).
131  * This is needed to support old-style user-defined functions, since depending
132  * on architecture and compiler, the return value of a function returning char
133  * or short may contain garbage when called as if it returned Datum.
134  */
135
136 typedef unsigned long Datum;    /* XXX sizeof(long) >= sizeof(void *) */
137
138 #define SIZEOF_DATUM SIZEOF_UNSIGNED_LONG
139
140 typedef Datum *DatumPtr;
141
142 #define GET_1_BYTE(datum)       (((Datum) (datum)) & 0x000000ff)
143 #define GET_2_BYTES(datum)      (((Datum) (datum)) & 0x0000ffff)
144 #define GET_4_BYTES(datum)      (((Datum) (datum)) & 0xffffffff)
145 #define SET_1_BYTE(value)       (((Datum) (value)) & 0x000000ff)
146 #define SET_2_BYTES(value)      (((Datum) (value)) & 0x0000ffff)
147 #define SET_4_BYTES(value)      (((Datum) (value)) & 0xffffffff)
148
149 /*
150  * DatumGetBool
151  *              Returns boolean value of a datum.
152  *
153  * Note: any nonzero value will be considered TRUE, but we ignore bits to
154  * the left of the width of bool, per comment above.
155  */
156
157 #define DatumGetBool(X) ((bool) (((bool) (X)) != 0))
158
159 /*
160  * BoolGetDatum
161  *              Returns datum representation for a boolean.
162  *
163  * Note: any nonzero value will be considered TRUE.
164  */
165
166 #define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0))
167
168 /*
169  * DatumGetChar
170  *              Returns character value of a datum.
171  */
172
173 #define DatumGetChar(X) ((char) GET_1_BYTE(X))
174
175 /*
176  * CharGetDatum
177  *              Returns datum representation for a character.
178  */
179
180 #define CharGetDatum(X) ((Datum) SET_1_BYTE(X))
181
182 /*
183  * Int8GetDatum
184  *              Returns datum representation for an 8-bit integer.
185  */
186
187 #define Int8GetDatum(X) ((Datum) SET_1_BYTE(X))
188
189 /*
190  * DatumGetUInt8
191  *              Returns 8-bit unsigned integer value of a datum.
192  */
193
194 #define DatumGetUInt8(X) ((uint8) GET_1_BYTE(X))
195
196 /*
197  * UInt8GetDatum
198  *              Returns datum representation for an 8-bit unsigned integer.
199  */
200
201 #define UInt8GetDatum(X) ((Datum) SET_1_BYTE(X))
202
203 /*
204  * DatumGetInt16
205  *              Returns 16-bit integer value of a datum.
206  */
207
208 #define DatumGetInt16(X) ((int16) GET_2_BYTES(X))
209
210 /*
211  * Int16GetDatum
212  *              Returns datum representation for a 16-bit integer.
213  */
214
215 #define Int16GetDatum(X) ((Datum) SET_2_BYTES(X))
216
217 /*
218  * DatumGetUInt16
219  *              Returns 16-bit unsigned integer value of a datum.
220  */
221
222 #define DatumGetUInt16(X) ((uint16) GET_2_BYTES(X))
223
224 /*
225  * UInt16GetDatum
226  *              Returns datum representation for a 16-bit unsigned integer.
227  */
228
229 #define UInt16GetDatum(X) ((Datum) SET_2_BYTES(X))
230
231 /*
232  * DatumGetInt32
233  *              Returns 32-bit integer value of a datum.
234  */
235
236 #define DatumGetInt32(X) ((int32) GET_4_BYTES(X))
237
238 /*
239  * Int32GetDatum
240  *              Returns datum representation for a 32-bit integer.
241  */
242
243 #define Int32GetDatum(X) ((Datum) SET_4_BYTES(X))
244
245 /*
246  * DatumGetUInt32
247  *              Returns 32-bit unsigned integer value of a datum.
248  */
249
250 #define DatumGetUInt32(X) ((uint32) GET_4_BYTES(X))
251
252 /*
253  * UInt32GetDatum
254  *              Returns datum representation for a 32-bit unsigned integer.
255  */
256
257 #define UInt32GetDatum(X) ((Datum) SET_4_BYTES(X))
258
259 /*
260  * DatumGetObjectId
261  *              Returns object identifier value of a datum.
262  */
263
264 #define DatumGetObjectId(X) ((Oid) GET_4_BYTES(X))
265
266 /*
267  * ObjectIdGetDatum
268  *              Returns datum representation for an object identifier.
269  */
270
271 #define ObjectIdGetDatum(X) ((Datum) SET_4_BYTES(X))
272
273 /*
274  * DatumGetTransactionId
275  *              Returns transaction identifier value of a datum.
276  */
277
278 #define DatumGetTransactionId(X) ((TransactionId) GET_4_BYTES(X))
279
280 /*
281  * TransactionIdGetDatum
282  *              Returns datum representation for a transaction identifier.
283  */
284
285 #define TransactionIdGetDatum(X) ((Datum) SET_4_BYTES((X)))
286
287 /*
288  * DatumGetCommandId
289  *              Returns command identifier value of a datum.
290  */
291
292 #define DatumGetCommandId(X) ((CommandId) GET_4_BYTES(X))
293
294 /*
295  * CommandIdGetDatum
296  *              Returns datum representation for a command identifier.
297  */
298
299 #define CommandIdGetDatum(X) ((Datum) SET_4_BYTES(X))
300
301 /*
302  * DatumGetPointer
303  *              Returns pointer value of a datum.
304  */
305
306 #define DatumGetPointer(X) ((Pointer) (X))
307
308 /*
309  * PointerGetDatum
310  *              Returns datum representation for a pointer.
311  */
312
313 #define PointerGetDatum(X) ((Datum) (X))
314
315 /*
316  * DatumGetCString
317  *              Returns C string (null-terminated string) value of a datum.
318  *
319  * Note: C string is not a full-fledged Postgres type at present,
320  * but type input functions use this conversion for their inputs.
321  */
322
323 #define DatumGetCString(X) ((char *) DatumGetPointer(X))
324
325 /*
326  * CStringGetDatum
327  *              Returns datum representation for a C string (null-terminated string).
328  *
329  * Note: C string is not a full-fledged Postgres type at present,
330  * but type output functions use this conversion for their outputs.
331  * Note: CString is pass-by-reference; caller must ensure the pointed-to
332  * value has adequate lifetime.
333  */
334
335 #define CStringGetDatum(X) PointerGetDatum(X)
336
337 /*
338  * DatumGetName
339  *              Returns name value of a datum.
340  */
341
342 #define DatumGetName(X) ((Name) DatumGetPointer(X))
343
344 /*
345  * NameGetDatum
346  *              Returns datum representation for a name.
347  *
348  * Note: Name is pass-by-reference; caller must ensure the pointed-to
349  * value has adequate lifetime.
350  */
351
352 #define NameGetDatum(X) PointerGetDatum(X)
353
354 /*
355  * DatumGetInt64
356  *              Returns 64-bit integer value of a datum.
357  *
358  * Note: this macro hides the fact that int64 is currently a
359  * pass-by-reference type.      Someday it may be pass-by-value,
360  * at least on some platforms.
361  */
362
363 #define DatumGetInt64(X) (* ((int64 *) DatumGetPointer(X)))
364
365 /*
366  * Int64GetDatum
367  *              Returns datum representation for a 64-bit integer.
368  *
369  * Note: this routine returns a reference to palloc'd space.
370  */
371
372 extern Datum Int64GetDatum(int64 X);
373
374 /*
375  * DatumGetFloat4
376  *              Returns 4-byte floating point value of a datum.
377  *
378  * Note: this macro hides the fact that float4 is currently a
379  * pass-by-reference type.      Someday it may be pass-by-value.
380  */
381
382 #define DatumGetFloat4(X) (* ((float4 *) DatumGetPointer(X)))
383
384 /*
385  * Float4GetDatum
386  *              Returns datum representation for a 4-byte floating point number.
387  *
388  * Note: this routine returns a reference to palloc'd space.
389  */
390
391 extern Datum Float4GetDatum(float4 X);
392
393 /*
394  * DatumGetFloat8
395  *              Returns 8-byte floating point value of a datum.
396  *
397  * Note: this macro hides the fact that float8 is currently a
398  * pass-by-reference type.      Someday it may be pass-by-value,
399  * at least on some platforms.
400  */
401
402 #define DatumGetFloat8(X) (* ((float8 *) DatumGetPointer(X)))
403
404 /*
405  * Float8GetDatum
406  *              Returns datum representation for an 8-byte floating point number.
407  *
408  * Note: this routine returns a reference to palloc'd space.
409  */
410
411 extern Datum Float8GetDatum(float8 X);
412
413
414 /*
415  * DatumGetFloat32
416  *              Returns 32-bit floating point value of a datum.
417  *              This is really a pointer, of course.
418  *
419  * XXX: this macro is now deprecated in favor of DatumGetFloat4.
420  * It will eventually go away.
421  */
422
423 #define DatumGetFloat32(X) ((float32) DatumGetPointer(X))
424
425 /*
426  * Float32GetDatum
427  *              Returns datum representation for a 32-bit floating point number.
428  *              This is really a pointer, of course.
429  *
430  * XXX: this macro is now deprecated in favor of Float4GetDatum.
431  * It will eventually go away.
432  */
433
434 #define Float32GetDatum(X) PointerGetDatum(X)
435
436 /*
437  * DatumGetFloat64
438  *              Returns 64-bit floating point value of a datum.
439  *              This is really a pointer, of course.
440  *
441  * XXX: this macro is now deprecated in favor of DatumGetFloat8.
442  * It will eventually go away.
443  */
444
445 #define DatumGetFloat64(X) ((float64) DatumGetPointer(X))
446
447 /*
448  * Float64GetDatum
449  *              Returns datum representation for a 64-bit floating point number.
450  *              This is really a pointer, of course.
451  *
452  * XXX: this macro is now deprecated in favor of Float8GetDatum.
453  * It will eventually go away.
454  */
455
456 #define Float64GetDatum(X) PointerGetDatum(X)
457
458 /*
459  * Int64GetDatumFast
460  * Float4GetDatumFast
461  * Float8GetDatumFast
462  *
463  * These macros are intended to allow writing code that does not depend on
464  * whether int64, float4, float8 are pass-by-reference types, while not
465  * sacrificing performance when they are.  The argument must be a variable
466  * that will exist and have the same value for as long as the Datum is needed.
467  * In the pass-by-ref case, the address of the variable is taken to use as
468  * the Datum.  In the pass-by-val case, these will be the same as the non-Fast
469  * macros.
470  */
471
472 #define Int64GetDatumFast(X)  PointerGetDatum(&(X))
473 #define Float4GetDatumFast(X) PointerGetDatum(&(X))
474 #define Float8GetDatumFast(X) PointerGetDatum(&(X))
475
476
477 /* ----------------------------------------------------------------
478  *                              Section 3:      exception handling definitions
479  *                                                      Assert, Trap, etc macros
480  * ----------------------------------------------------------------
481  */
482
483 extern DLLIMPORT bool assert_enabled;
484
485 /*
486  * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
487  * - plai  9/5/90
488  *
489  * It should _NOT_ be defined in releases or in benchmark copies
490  */
491
492 /*
493  * Trap
494  *              Generates an exception if the given condition is true.
495  */
496 #define Trap(condition, errorType) \
497         do { \
498                 if ((assert_enabled) && (condition)) \
499                         ExceptionalCondition(CppAsString(condition), (errorType), \
500                                                                  __FILE__, __LINE__); \
501         } while (0)
502
503 /*
504  *      TrapMacro is the same as Trap but it's intended for use in macros:
505  *
506  *              #define foo(x) (AssertMacro(x != 0) && bar(x))
507  *
508  *      Isn't CPP fun?
509  */
510 #define TrapMacro(condition, errorType) \
511         ((bool) ((! assert_enabled) || ! (condition) || \
512                          (ExceptionalCondition(CppAsString(condition), (errorType), \
513                                                                    __FILE__, __LINE__))))
514
515 #ifndef USE_ASSERT_CHECKING
516 #define Assert(condition)
517 #define AssertMacro(condition)  ((void)true)
518 #define AssertArg(condition)
519 #define AssertState(condition)
520 #else
521 #define Assert(condition) \
522                 Trap(!(condition), "FailedAssertion")
523
524 #define AssertMacro(condition) \
525                 ((void) TrapMacro(!(condition), "FailedAssertion"))
526
527 #define AssertArg(condition) \
528                 Trap(!(condition), "BadArgument")
529
530 #define AssertState(condition) \
531                 Trap(!(condition), "BadState")
532 #endif   /* USE_ASSERT_CHECKING */
533
534 extern int ExceptionalCondition(char *conditionName, char *errorType,
535                                          char *fileName, int lineNumber);
536
537 /* ----------------------------------------------------------------
538  *                              Section 4: genbki macros used by catalog/pg_xxx.h files
539  * ----------------------------------------------------------------
540  */
541 #define CATALOG(name,oid)       typedef struct CppConcat(FormData_,name)
542
543 #define BKI_BOOTSTRAP
544 #define BKI_SHARED_RELATION
545 #define BKI_WITHOUT_OIDS
546
547 /* these need to expand into some harmless, repeatable declaration */
548 #define DATA(x)   extern int no_such_variable
549 #define DESCR(x)  extern int no_such_variable
550 #define SHDESCR(x) extern int no_such_variable
551
552 typedef int4 aclitem;                   /* PHONY definition for catalog use only */
553
554 #endif   /* POSTGRES_H */