doc: gin_page_opaque_info() must be a _compressed_ GIN page
[pgsql.git] / src / include / postgres.h
blob8a028ff789bc857b086a3a17b02c20ffb76844c0
1 /*-------------------------------------------------------------------------
3 * postgres.h
4 * Primary include file for PostgreSQL server .c files
6 * This should be the first file included by PostgreSQL backend modules.
7 * Client-side code should include postgres_fe.h instead.
10 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * src/include/postgres.h
15 *-------------------------------------------------------------------------
18 *----------------------------------------------------------------
19 * TABLE OF CONTENTS
21 * When adding stuff to this file, please try to put stuff
22 * into the relevant section, or add new sections as appropriate.
24 * section description
25 * ------- ------------------------------------------------
26 * 1) Datum type + support functions
27 * 2) miscellaneous
29 * NOTES
31 * In general, this file should contain declarations that are widely needed
32 * in the backend environment, but are of no interest outside the backend.
34 * Simple type definitions live in c.h, where they are shared with
35 * postgres_fe.h. We do that since those type definitions are needed by
36 * frontend modules that want to deal with binary data transmission to or
37 * from the backend. Type definitions in this file should be for
38 * representations that never escape the backend, such as Datum.
40 *----------------------------------------------------------------
42 #ifndef POSTGRES_H
43 #define POSTGRES_H
45 #include "c.h"
46 #include "utils/elog.h"
47 #include "utils/palloc.h"
49 /* ----------------------------------------------------------------
50 * Section 1: Datum type + support functions
51 * ----------------------------------------------------------------
55 * A Datum contains either a value of a pass-by-value type or a pointer to a
56 * value of a pass-by-reference type. Therefore, we require:
58 * sizeof(Datum) == sizeof(void *) == 4 or 8
60 * The functions below and the analogous functions for other types should be used to
61 * convert between a Datum and the appropriate C type.
64 typedef uintptr_t Datum;
67 * A NullableDatum is used in places where both a Datum and its nullness needs
68 * to be stored. This can be more efficient than storing datums and nullness
69 * in separate arrays, due to better spatial locality, even if more space may
70 * be wasted due to padding.
72 typedef struct NullableDatum
74 #define FIELDNO_NULLABLE_DATUM_DATUM 0
75 Datum value;
76 #define FIELDNO_NULLABLE_DATUM_ISNULL 1
77 bool isnull;
78 /* due to alignment padding this could be used for flags for free */
79 } NullableDatum;
81 #define SIZEOF_DATUM SIZEOF_VOID_P
84 * DatumGetBool
85 * Returns boolean value of a datum.
87 * Note: any nonzero value will be considered true.
89 static inline bool
90 DatumGetBool(Datum X)
92 return (X != 0);
96 * BoolGetDatum
97 * Returns datum representation for a boolean.
99 * Note: any nonzero value will be considered true.
101 static inline Datum
102 BoolGetDatum(bool X)
104 return (Datum) (X ? 1 : 0);
108 * DatumGetChar
109 * Returns character value of a datum.
111 static inline char
112 DatumGetChar(Datum X)
114 return (char) X;
118 * CharGetDatum
119 * Returns datum representation for a character.
121 static inline Datum
122 CharGetDatum(char X)
124 return (Datum) X;
128 * Int8GetDatum
129 * Returns datum representation for an 8-bit integer.
131 static inline Datum
132 Int8GetDatum(int8 X)
134 return (Datum) X;
138 * DatumGetUInt8
139 * Returns 8-bit unsigned integer value of a datum.
141 static inline uint8
142 DatumGetUInt8(Datum X)
144 return (uint8) X;
148 * UInt8GetDatum
149 * Returns datum representation for an 8-bit unsigned integer.
151 static inline Datum
152 UInt8GetDatum(uint8 X)
154 return (Datum) X;
158 * DatumGetInt16
159 * Returns 16-bit integer value of a datum.
161 static inline int16
162 DatumGetInt16(Datum X)
164 return (int16) X;
168 * Int16GetDatum
169 * Returns datum representation for a 16-bit integer.
171 static inline Datum
172 Int16GetDatum(int16 X)
174 return (Datum) X;
178 * DatumGetUInt16
179 * Returns 16-bit unsigned integer value of a datum.
181 static inline uint16
182 DatumGetUInt16(Datum X)
184 return (uint16) X;
188 * UInt16GetDatum
189 * Returns datum representation for a 16-bit unsigned integer.
191 static inline Datum
192 UInt16GetDatum(uint16 X)
194 return (Datum) X;
198 * DatumGetInt32
199 * Returns 32-bit integer value of a datum.
201 static inline int32
202 DatumGetInt32(Datum X)
204 return (int32) X;
208 * Int32GetDatum
209 * Returns datum representation for a 32-bit integer.
211 static inline Datum
212 Int32GetDatum(int32 X)
214 return (Datum) X;
218 * DatumGetUInt32
219 * Returns 32-bit unsigned integer value of a datum.
221 static inline uint32
222 DatumGetUInt32(Datum X)
224 return (uint32) X;
228 * UInt32GetDatum
229 * Returns datum representation for a 32-bit unsigned integer.
231 static inline Datum
232 UInt32GetDatum(uint32 X)
234 return (Datum) X;
238 * DatumGetObjectId
239 * Returns object identifier value of a datum.
241 static inline Oid
242 DatumGetObjectId(Datum X)
244 return (Oid) X;
248 * ObjectIdGetDatum
249 * Returns datum representation for an object identifier.
251 static inline Datum
252 ObjectIdGetDatum(Oid X)
254 return (Datum) X;
258 * DatumGetTransactionId
259 * Returns transaction identifier value of a datum.
261 static inline TransactionId
262 DatumGetTransactionId(Datum X)
264 return (TransactionId) X;
268 * TransactionIdGetDatum
269 * Returns datum representation for a transaction identifier.
271 static inline Datum
272 TransactionIdGetDatum(TransactionId X)
274 return (Datum) X;
278 * MultiXactIdGetDatum
279 * Returns datum representation for a multixact identifier.
281 static inline Datum
282 MultiXactIdGetDatum(MultiXactId X)
284 return (Datum) X;
288 * DatumGetCommandId
289 * Returns command identifier value of a datum.
291 static inline CommandId
292 DatumGetCommandId(Datum X)
294 return (CommandId) X;
298 * CommandIdGetDatum
299 * Returns datum representation for a command identifier.
301 static inline Datum
302 CommandIdGetDatum(CommandId X)
304 return (Datum) X;
308 * DatumGetPointer
309 * Returns pointer value of a datum.
311 static inline Pointer
312 DatumGetPointer(Datum X)
314 return (Pointer) X;
318 * PointerGetDatum
319 * Returns datum representation for a pointer.
321 static inline Datum
322 PointerGetDatum(const void *X)
324 return (Datum) X;
328 * DatumGetCString
329 * Returns C string (null-terminated string) value of a datum.
331 * Note: C string is not a full-fledged Postgres type at present,
332 * but type input functions use this conversion for their inputs.
334 static inline char *
335 DatumGetCString(Datum X)
337 return (char *) DatumGetPointer(X);
341 * CStringGetDatum
342 * Returns datum representation for a C string (null-terminated string).
344 * Note: C string is not a full-fledged Postgres type at present,
345 * but type output functions use this conversion for their outputs.
346 * Note: CString is pass-by-reference; caller must ensure the pointed-to
347 * value has adequate lifetime.
349 static inline Datum
350 CStringGetDatum(const char *X)
352 return PointerGetDatum(X);
356 * DatumGetName
357 * Returns name value of a datum.
359 static inline Name
360 DatumGetName(Datum X)
362 return (Name) DatumGetPointer(X);
366 * NameGetDatum
367 * Returns datum representation for a name.
369 * Note: Name is pass-by-reference; caller must ensure the pointed-to
370 * value has adequate lifetime.
372 static inline Datum
373 NameGetDatum(const NameData *X)
375 return CStringGetDatum(NameStr(*X));
379 * DatumGetInt64
380 * Returns 64-bit integer value of a datum.
382 * Note: this function hides whether int64 is pass by value or by reference.
384 static inline int64
385 DatumGetInt64(Datum X)
387 #ifdef USE_FLOAT8_BYVAL
388 return (int64) X;
389 #else
390 return *((int64 *) DatumGetPointer(X));
391 #endif
395 * Int64GetDatum
396 * Returns datum representation for a 64-bit integer.
398 * Note: if int64 is pass by reference, this function returns a reference
399 * to palloc'd space.
401 #ifdef USE_FLOAT8_BYVAL
402 static inline Datum
403 Int64GetDatum(int64 X)
405 return (Datum) X;
407 #else
408 extern Datum Int64GetDatum(int64 X);
409 #endif
413 * DatumGetUInt64
414 * Returns 64-bit unsigned integer value of a datum.
416 * Note: this function hides whether int64 is pass by value or by reference.
418 static inline uint64
419 DatumGetUInt64(Datum X)
421 #ifdef USE_FLOAT8_BYVAL
422 return (uint64) X;
423 #else
424 return *((uint64 *) DatumGetPointer(X));
425 #endif
429 * UInt64GetDatum
430 * Returns datum representation for a 64-bit unsigned integer.
432 * Note: if int64 is pass by reference, this function returns a reference
433 * to palloc'd space.
435 static inline Datum
436 UInt64GetDatum(uint64 X)
438 #ifdef USE_FLOAT8_BYVAL
439 return (Datum) X;
440 #else
441 return Int64GetDatum((int64) X);
442 #endif
446 * Float <-> Datum conversions
448 * These have to be implemented as inline functions rather than macros, when
449 * passing by value, because many machines pass int and float function
450 * parameters/results differently; so we need to play weird games with unions.
454 * DatumGetFloat4
455 * Returns 4-byte floating point value of a datum.
457 static inline float4
458 DatumGetFloat4(Datum X)
460 union
462 int32 value;
463 float4 retval;
464 } myunion;
466 myunion.value = DatumGetInt32(X);
467 return myunion.retval;
471 * Float4GetDatum
472 * Returns datum representation for a 4-byte floating point number.
474 static inline Datum
475 Float4GetDatum(float4 X)
477 union
479 float4 value;
480 int32 retval;
481 } myunion;
483 myunion.value = X;
484 return Int32GetDatum(myunion.retval);
488 * DatumGetFloat8
489 * Returns 8-byte floating point value of a datum.
491 * Note: this function hides whether float8 is pass by value or by reference.
493 static inline float8
494 DatumGetFloat8(Datum X)
496 #ifdef USE_FLOAT8_BYVAL
497 union
499 int64 value;
500 float8 retval;
501 } myunion;
503 myunion.value = DatumGetInt64(X);
504 return myunion.retval;
505 #else
506 return *((float8 *) DatumGetPointer(X));
507 #endif
511 * Float8GetDatum
512 * Returns datum representation for an 8-byte floating point number.
514 * Note: if float8 is pass by reference, this function returns a reference
515 * to palloc'd space.
517 #ifdef USE_FLOAT8_BYVAL
518 static inline Datum
519 Float8GetDatum(float8 X)
521 union
523 float8 value;
524 int64 retval;
525 } myunion;
527 myunion.value = X;
528 return Int64GetDatum(myunion.retval);
530 #else
531 extern Datum Float8GetDatum(float8 X);
532 #endif
536 * Int64GetDatumFast
537 * Float8GetDatumFast
539 * These macros are intended to allow writing code that does not depend on
540 * whether int64 and float8 are pass-by-reference types, while not
541 * sacrificing performance when they are. The argument must be a variable
542 * that will exist and have the same value for as long as the Datum is needed.
543 * In the pass-by-ref case, the address of the variable is taken to use as
544 * the Datum. In the pass-by-val case, these are the same as the non-Fast
545 * functions, except for asserting that the variable is of the correct type.
548 #ifdef USE_FLOAT8_BYVAL
549 #define Int64GetDatumFast(X) \
550 (AssertVariableIsOfTypeMacro(X, int64), Int64GetDatum(X))
551 #define Float8GetDatumFast(X) \
552 (AssertVariableIsOfTypeMacro(X, double), Float8GetDatum(X))
553 #else
554 #define Int64GetDatumFast(X) \
555 (AssertVariableIsOfTypeMacro(X, int64), PointerGetDatum(&(X)))
556 #define Float8GetDatumFast(X) \
557 (AssertVariableIsOfTypeMacro(X, double), PointerGetDatum(&(X)))
558 #endif
561 /* ----------------------------------------------------------------
562 * Section 2: miscellaneous
563 * ----------------------------------------------------------------
567 * NON_EXEC_STATIC: It's sometimes useful to define a variable or function
568 * that is normally static but extern when using EXEC_BACKEND (see
569 * pg_config_manual.h). There would then typically be some code in
570 * postmaster.c that uses those extern symbols to transfer state between
571 * processes or do whatever other things it needs to do in EXEC_BACKEND mode.
573 #ifdef EXEC_BACKEND
574 #define NON_EXEC_STATIC
575 #else
576 #define NON_EXEC_STATIC static
577 #endif
579 #endif /* POSTGRES_H */