codekingpro/portable-devtools
116k
1/*-------------------------------------------------------------------------2 *3 * postgres.h4 * Primary include file for PostgreSQL server .c files5 *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-2023, PostgreSQL Global Development Group11 * Portions Copyright (c) 1995, Regents of the University of California12 *13 * src/include/postgres.h14 *15 *-------------------------------------------------------------------------16 */17/*18 *----------------------------------------------------------------19 * TABLE OF CONTENTS20 *21 * When adding stuff to this file, please try to put stuff22 * into the relevant section, or add new sections as appropriate.23 *24 * section description25 * ------- ------------------------------------------------26 * 1) Datum type + support functions27 * 2) miscellaneous28 *29 * NOTES30 *31 * In general, this file should contain declarations that are widely needed32 * in the backend environment, but are of no interest outside the backend.33 *34 * Simple type definitions live in c.h, where they are shared with35 * postgres_fe.h. We do that since those type definitions are needed by36 * frontend modules that want to deal with binary data transmission to or37 * from the backend. Type definitions in this file should be for38 * representations that never escape the backend, such as Datum.39 *40 *----------------------------------------------------------------41 */42#ifndef POSTGRES_H43#define POSTGRES_H44 45#include "c.h"46#include "utils/elog.h"47#include "utils/palloc.h"48 49/* ----------------------------------------------------------------50 * Section 1: Datum type + support functions51 * ----------------------------------------------------------------52 */53 54/*55 * A Datum contains either a value of a pass-by-value type or a pointer to a56 * value of a pass-by-reference type. Therefore, we require:57 *58 * sizeof(Datum) == sizeof(void *) == 4 or 859 *60 * The functions below and the analogous functions for other types should be used to61 * convert between a Datum and the appropriate C type.62 */63 64typedef uintptr_t Datum;65 66/*67 * A NullableDatum is used in places where both a Datum and its nullness needs68 * to be stored. This can be more efficient than storing datums and nullness69 * in separate arrays, due to better spatial locality, even if more space may70 * be wasted due to padding.71 */72typedef struct NullableDatum73{74#define FIELDNO_NULLABLE_DATUM_DATUM 075 Datum value;76#define FIELDNO_NULLABLE_DATUM_ISNULL 177 bool isnull;78 /* due to alignment padding this could be used for flags for free */79} NullableDatum;80 81#define SIZEOF_DATUM SIZEOF_VOID_P82 83/*84 * DatumGetBool85 * Returns boolean value of a datum.86 *87 * Note: any nonzero value will be considered true.88 */89static inline bool90DatumGetBool(Datum X)91{92 return (X != 0);93}94 95/*96 * BoolGetDatum97 * Returns datum representation for a boolean.98 *99 * Note: any nonzero value will be considered true.100 */101static inline Datum102BoolGetDatum(bool X)103{104 return (Datum) (X ? 1 : 0);105}106 107/*108 * DatumGetChar109 * Returns character value of a datum.110 */111static inline char112DatumGetChar(Datum X)113{114 return (char) X;115}116 117/*118 * CharGetDatum119 * Returns datum representation for a character.120 */121static inline Datum122CharGetDatum(char X)123{124 return (Datum) X;125}126 127/*128 * Int8GetDatum129 * Returns datum representation for an 8-bit integer.130 */131static inline Datum132Int8GetDatum(int8 X)133{134 return (Datum) X;135}136 137/*138 * DatumGetUInt8139 * Returns 8-bit unsigned integer value of a datum.140 */141static inline uint8142DatumGetUInt8(Datum X)143{144 return (uint8) X;145}146 147/*148 * UInt8GetDatum149 * Returns datum representation for an 8-bit unsigned integer.150 */151static inline Datum152UInt8GetDatum(uint8 X)153{154 return (Datum) X;155}156 157/*158 * DatumGetInt16159 * Returns 16-bit integer value of a datum.160 */161static inline int16162DatumGetInt16(Datum X)163{164 return (int16) X;165}166 167/*168 * Int16GetDatum169 * Returns datum representation for a 16-bit integer.170 */171static inline Datum172Int16GetDatum(int16 X)173{174 return (Datum) X;175}176 177/*178 * DatumGetUInt16179 * Returns 16-bit unsigned integer value of a datum.180 */181static inline uint16182DatumGetUInt16(Datum X)183{184 return (uint16) X;185}186 187/*188 * UInt16GetDatum189 * Returns datum representation for a 16-bit unsigned integer.190 */191static inline Datum192UInt16GetDatum(uint16 X)193{194 return (Datum) X;195}196 197/*198 * DatumGetInt32199 * Returns 32-bit integer value of a datum.200 */201static inline int32202DatumGetInt32(Datum X)203{204 return (int32) X;205}206 207/*208 * Int32GetDatum209 * Returns datum representation for a 32-bit integer.210 */211static inline Datum212Int32GetDatum(int32 X)213{214 return (Datum) X;215}216 217/*218 * DatumGetUInt32219 * Returns 32-bit unsigned integer value of a datum.220 */221static inline uint32222DatumGetUInt32(Datum X)223{224 return (uint32) X;225}226 227/*228 * UInt32GetDatum229 * Returns datum representation for a 32-bit unsigned integer.230 */231static inline Datum232UInt32GetDatum(uint32 X)233{234 return (Datum) X;235}236 237/*238 * DatumGetObjectId239 * Returns object identifier value of a datum.240 */241static inline Oid242DatumGetObjectId(Datum X)243{244 return (Oid) X;245}246 247/*248 * ObjectIdGetDatum249 * Returns datum representation for an object identifier.250 */251static inline Datum252ObjectIdGetDatum(Oid X)253{254 return (Datum) X;255}256 257/*258 * DatumGetTransactionId259 * Returns transaction identifier value of a datum.260 */261static inline TransactionId262DatumGetTransactionId(Datum X)263{264 return (TransactionId) X;265}266 267/*268 * TransactionIdGetDatum269 * Returns datum representation for a transaction identifier.270 */271static inline Datum272TransactionIdGetDatum(TransactionId X)273{274 return (Datum) X;275}276 277/*278 * MultiXactIdGetDatum279 * Returns datum representation for a multixact identifier.280 */281static inline Datum282MultiXactIdGetDatum(MultiXactId X)283{284 return (Datum) X;285}286 287/*288 * DatumGetCommandId289 * Returns command identifier value of a datum.290 */291static inline CommandId292DatumGetCommandId(Datum X)293{294 return (CommandId) X;295}296 297/*298 * CommandIdGetDatum299 * Returns datum representation for a command identifier.300 */301static inline Datum302CommandIdGetDatum(CommandId X)303{304 return (Datum) X;305}306 307/*308 * DatumGetPointer309 * Returns pointer value of a datum.310 */311static inline Pointer312DatumGetPointer(Datum X)313{314 return (Pointer) X;315}316 317/*318 * PointerGetDatum319 * Returns datum representation for a pointer.320 */321static inline Datum322PointerGetDatum(const void *X)323{324 return (Datum) X;325}326 327/*328 * DatumGetCString329 * Returns C string (null-terminated string) value of a datum.330 *331 * Note: C string is not a full-fledged Postgres type at present,332 * but type input functions use this conversion for their inputs.333 */334static inline char *335DatumGetCString(Datum X)336{337 return (char *) DatumGetPointer(X);338}339 340/*341 * CStringGetDatum342 * Returns datum representation for a C string (null-terminated string).343 *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-to347 * value has adequate lifetime.348 */349static inline Datum350CStringGetDatum(const char *X)351{352 return PointerGetDatum(X);353}354 355/*356 * DatumGetName357 * Returns name value of a datum.358 */359static inline Name360DatumGetName(Datum X)361{362 return (Name) DatumGetPointer(X);363}364 365/*366 * NameGetDatum367 * Returns datum representation for a name.368 *369 * Note: Name is pass-by-reference; caller must ensure the pointed-to370 * value has adequate lifetime.371 */372static inline Datum373NameGetDatum(const NameData *X)374{375 return CStringGetDatum(NameStr(*X));376}377 378/*379 * DatumGetInt64380 * Returns 64-bit integer value of a datum.381 *382 * Note: this function hides whether int64 is pass by value or by reference.383 */384static inline int64385DatumGetInt64(Datum X)386{387#ifdef USE_FLOAT8_BYVAL388 return (int64) X;389#else390 return *((int64 *) DatumGetPointer(X));391#endif392}393 394/*395 * Int64GetDatum396 * Returns datum representation for a 64-bit integer.397 *398 * Note: if int64 is pass by reference, this function returns a reference399 * to palloc'd space.400 */401#ifdef USE_FLOAT8_BYVAL402static inline Datum403Int64GetDatum(int64 X)404{405 return (Datum) X;406}407#else408extern Datum Int64GetDatum(int64 X);409#endif410 411 412/*413 * DatumGetUInt64414 * Returns 64-bit unsigned integer value of a datum.415 *416 * Note: this function hides whether int64 is pass by value or by reference.417 */418static inline uint64419DatumGetUInt64(Datum X)420{421#ifdef USE_FLOAT8_BYVAL422 return (uint64) X;423#else424 return *((uint64 *) DatumGetPointer(X));425#endif426}427 428/*429 * UInt64GetDatum430 * Returns datum representation for a 64-bit unsigned integer.431 *432 * Note: if int64 is pass by reference, this function returns a reference433 * to palloc'd space.434 */435static inline Datum436UInt64GetDatum(uint64 X)437{438#ifdef USE_FLOAT8_BYVAL439 return (Datum) X;440#else441 return Int64GetDatum((int64) X);442#endif443}444 445/*446 * Float <-> Datum conversions447 *448 * These have to be implemented as inline functions rather than macros, when449 * passing by value, because many machines pass int and float function450 * parameters/results differently; so we need to play weird games with unions.451 */452 453/*454 * DatumGetFloat4455 * Returns 4-byte floating point value of a datum.456 */457static inline float4458DatumGetFloat4(Datum X)459{460 union461 {462 int32 value;463 float4 retval;464 } myunion;465 466 myunion.value = DatumGetInt32(X);467 return myunion.retval;468}469 470/*471 * Float4GetDatum472 * Returns datum representation for a 4-byte floating point number.473 */474static inline Datum475Float4GetDatum(float4 X)476{477 union478 {479 float4 value;480 int32 retval;481 } myunion;482 483 myunion.value = X;484 return Int32GetDatum(myunion.retval);485}486 487/*488 * DatumGetFloat8489 * Returns 8-byte floating point value of a datum.490 *491 * Note: this function hides whether float8 is pass by value or by reference.492 */493static inline float8494DatumGetFloat8(Datum X)495{496#ifdef USE_FLOAT8_BYVAL497 union498 {499 int64 value;500 float8 retval;501 } myunion;502 503 myunion.value = DatumGetInt64(X);504 return myunion.retval;505#else506 return *((float8 *) DatumGetPointer(X));507#endif508}509 510/*511 * Float8GetDatum512 * Returns datum representation for an 8-byte floating point number.513 *514 * Note: if float8 is pass by reference, this function returns a reference515 * to palloc'd space.516 */517#ifdef USE_FLOAT8_BYVAL518static inline Datum519Float8GetDatum(float8 X)520{521 union522 {523 float8 value;524 int64 retval;525 } myunion;526 527 myunion.value = X;528 return Int64GetDatum(myunion.retval);529}530#else531extern Datum Float8GetDatum(float8 X);532#endif533 534 535/*536 * Int64GetDatumFast537 * Float8GetDatumFast538 *539 * These macros are intended to allow writing code that does not depend on540 * whether int64 and float8 are pass-by-reference types, while not541 * sacrificing performance when they are. The argument must be a variable542 * 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 as544 * the Datum. In the pass-by-val case, these are the same as the non-Fast545 * functions, except for asserting that the variable is of the correct type.546 */547 548#ifdef USE_FLOAT8_BYVAL549#define Int64GetDatumFast(X) \550 (AssertVariableIsOfTypeMacro(X, int64), Int64GetDatum(X))551#define Float8GetDatumFast(X) \552 (AssertVariableIsOfTypeMacro(X, double), Float8GetDatum(X))553#else554#define Int64GetDatumFast(X) \555 (AssertVariableIsOfTypeMacro(X, int64), PointerGetDatum(&(X)))556#define Float8GetDatumFast(X) \557 (AssertVariableIsOfTypeMacro(X, double), PointerGetDatum(&(X)))558#endif559 560 561/* ----------------------------------------------------------------562 * Section 2: miscellaneous563 * ----------------------------------------------------------------564 */565 566/*567 * NON_EXEC_STATIC: It's sometimes useful to define a variable or function568 * that is normally static but extern when using EXEC_BACKEND (see569 * pg_config_manual.h). There would then typically be some code in570 * postmaster.c that uses those extern symbols to transfer state between571 * processes or do whatever other things it needs to do in EXEC_BACKEND mode.572 */573#ifdef EXEC_BACKEND574#define NON_EXEC_STATIC575#else576#define NON_EXEC_STATIC static577#endif578 579#endif /* POSTGRES_H */580 