1 /*-------------------------------------------------------------------------
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-2024, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
13 * src/include/postgres.h
15 *-------------------------------------------------------------------------
18 *----------------------------------------------------------------
21 * When adding stuff to this file, please try to put stuff
22 * into the relevant section, or add new sections as appropriate.
25 * ------- ------------------------------------------------
26 * 1) Datum type + support functions
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 *----------------------------------------------------------------
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
76 #define FIELDNO_NULLABLE_DATUM_ISNULL 1
78 /* due to alignment padding this could be used for flags for free */
81 #define SIZEOF_DATUM SIZEOF_VOID_P
85 * Returns boolean value of a datum.
87 * Note: any nonzero value will be considered true.
97 * Returns datum representation for a boolean.
99 * Note: any nonzero value will be considered true.
104 return (Datum
) (X
? 1 : 0);
109 * Returns character value of a datum.
112 DatumGetChar(Datum X
)
119 * Returns datum representation for a character.
129 * Returns datum representation for an 8-bit integer.
139 * Returns 8-bit unsigned integer value of a datum.
142 DatumGetUInt8(Datum X
)
149 * Returns datum representation for an 8-bit unsigned integer.
152 UInt8GetDatum(uint8 X
)
159 * Returns 16-bit integer value of a datum.
162 DatumGetInt16(Datum X
)
169 * Returns datum representation for a 16-bit integer.
172 Int16GetDatum(int16 X
)
179 * Returns 16-bit unsigned integer value of a datum.
182 DatumGetUInt16(Datum X
)
189 * Returns datum representation for a 16-bit unsigned integer.
192 UInt16GetDatum(uint16 X
)
199 * Returns 32-bit integer value of a datum.
202 DatumGetInt32(Datum X
)
209 * Returns datum representation for a 32-bit integer.
212 Int32GetDatum(int32 X
)
219 * Returns 32-bit unsigned integer value of a datum.
222 DatumGetUInt32(Datum X
)
229 * Returns datum representation for a 32-bit unsigned integer.
232 UInt32GetDatum(uint32 X
)
239 * Returns object identifier value of a datum.
242 DatumGetObjectId(Datum X
)
249 * Returns datum representation for an object identifier.
252 ObjectIdGetDatum(Oid 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.
272 TransactionIdGetDatum(TransactionId X
)
278 * MultiXactIdGetDatum
279 * Returns datum representation for a multixact identifier.
282 MultiXactIdGetDatum(MultiXactId X
)
289 * Returns command identifier value of a datum.
291 static inline CommandId
292 DatumGetCommandId(Datum X
)
294 return (CommandId
) X
;
299 * Returns datum representation for a command identifier.
302 CommandIdGetDatum(CommandId X
)
309 * Returns pointer value of a datum.
311 static inline Pointer
312 DatumGetPointer(Datum X
)
319 * Returns datum representation for a pointer.
322 PointerGetDatum(const void *X
)
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.
335 DatumGetCString(Datum X
)
337 return (char *) DatumGetPointer(X
);
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.
350 CStringGetDatum(const char *X
)
352 return PointerGetDatum(X
);
357 * Returns name value of a datum.
360 DatumGetName(Datum X
)
362 return (Name
) DatumGetPointer(X
);
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.
373 NameGetDatum(const NameData
*X
)
375 return CStringGetDatum(NameStr(*X
));
380 * Returns 64-bit integer value of a datum.
382 * Note: this function hides whether int64 is pass by value or by reference.
385 DatumGetInt64(Datum X
)
387 #ifdef USE_FLOAT8_BYVAL
390 return *((int64
*) DatumGetPointer(X
));
396 * Returns datum representation for a 64-bit integer.
398 * Note: if int64 is pass by reference, this function returns a reference
401 #ifdef USE_FLOAT8_BYVAL
403 Int64GetDatum(int64 X
)
408 extern Datum
Int64GetDatum(int64 X
);
414 * Returns 64-bit unsigned integer value of a datum.
416 * Note: this function hides whether int64 is pass by value or by reference.
419 DatumGetUInt64(Datum X
)
421 #ifdef USE_FLOAT8_BYVAL
424 return *((uint64
*) DatumGetPointer(X
));
430 * Returns datum representation for a 64-bit unsigned integer.
432 * Note: if int64 is pass by reference, this function returns a reference
436 UInt64GetDatum(uint64 X
)
438 #ifdef USE_FLOAT8_BYVAL
441 return Int64GetDatum((int64
) X
);
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.
455 * Returns 4-byte floating point value of a datum.
458 DatumGetFloat4(Datum X
)
466 myunion
.value
= DatumGetInt32(X
);
467 return myunion
.retval
;
472 * Returns datum representation for a 4-byte floating point number.
475 Float4GetDatum(float4 X
)
484 return Int32GetDatum(myunion
.retval
);
489 * Returns 8-byte floating point value of a datum.
491 * Note: this function hides whether float8 is pass by value or by reference.
494 DatumGetFloat8(Datum X
)
496 #ifdef USE_FLOAT8_BYVAL
503 myunion
.value
= DatumGetInt64(X
);
504 return myunion
.retval
;
506 return *((float8
*) DatumGetPointer(X
));
512 * Returns datum representation for an 8-byte floating point number.
514 * Note: if float8 is pass by reference, this function returns a reference
517 #ifdef USE_FLOAT8_BYVAL
519 Float8GetDatum(float8 X
)
528 return Int64GetDatum(myunion
.retval
);
531 extern Datum
Float8GetDatum(float8 X
);
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))
554 #define Int64GetDatumFast(X) \
555 (AssertVariableIsOfTypeMacro(X, int64), PointerGetDatum(&(X)))
556 #define Float8GetDatumFast(X) \
557 (AssertVariableIsOfTypeMacro(X, double), PointerGetDatum(&(X)))
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.
574 #define NON_EXEC_STATIC
576 #define NON_EXEC_STATIC static
579 #endif /* POSTGRES_H */