59#include "pg_config_os.h"
76#if defined(WIN32) || defined(__CYGWIN__)
114#ifdef PG_FORCE_DISABLE_INLINE
132#ifndef __has_attribute
133#define __has_attribute(attribute) 0
138#define pg_attribute_unused() __attribute__((unused))
140#define pg_attribute_unused()
147#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
148#define pg_fallthrough [[fallthrough]]
149#elif __has_attribute(fallthrough)
150#define pg_fallthrough __attribute__((fallthrough))
152#define pg_fallthrough
162#define pg_nodiscard __attribute__((warn_unused_result))
177#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) && !defined(__cplusplus)
178#define pg_noreturn _Noreturn
179#elif defined(__GNUC__)
180#define pg_noreturn __attribute__((noreturn))
181#elif defined(_MSC_VER)
182#define pg_noreturn __declspec(noreturn)
191#if defined(__clang__) || __GNUC__ >= 8
192#define pg_attribute_no_sanitize_address() __attribute__((no_sanitize("address")))
193#elif __has_attribute(no_sanitize_address)
195#define pg_attribute_no_sanitize_address() __attribute__((no_sanitize_address))
197#define pg_attribute_no_sanitize_address()
206#if __clang_major__ >= 7 || __GNUC__ >= 8
207#define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
209#define pg_attribute_no_sanitize_alignment()
217#if __has_attribute (nonnull)
218#define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
220#define pg_attribute_nonnull(...)
229#if __has_attribute (target)
230#define pg_attribute_target(...) __attribute__((target(__VA_ARGS__)))
232#define pg_attribute_target(...)
240#ifdef USE_ASSERT_CHECKING
241#define PG_USED_FOR_ASSERTS_ONLY
243#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
251#define PG_PRINTF_ATTRIBUTE PG_C_PRINTF_ATTRIBUTE
253#define PG_PRINTF_ATTRIBUTE PG_CXX_PRINTF_ATTRIBUTE
258#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
259#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
261#define pg_attribute_format_arg(a)
262#define pg_attribute_printf(f,a)
267#define pg_attribute_aligned(a) __attribute__((aligned(a)))
268#define pg_attribute_packed() __attribute__((packed))
269#elif defined(_MSC_VER)
276#define pg_attribute_aligned(a) __declspec(align(a))
291#if defined(__GNUC__) && defined(__OPTIMIZE__)
293#define pg_attribute_always_inline __attribute__((always_inline)) inline
294#elif defined(_MSC_VER)
296#define pg_attribute_always_inline __forceinline
299#define pg_attribute_always_inline inline
310#define pg_noinline __attribute__((noinline))
312#elif defined(_MSC_VER)
313#define pg_noinline __declspec(noinline)
327#if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
329#define pg_attribute_cold
330#define pg_attribute_hot
337#if __has_attribute (cold)
338#define pg_attribute_cold __attribute__((cold))
340#define pg_attribute_cold
343#if __has_attribute (hot)
344#define pg_attribute_hot __attribute__((hot))
346#define pg_attribute_hot
356#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
357#define pg_unreachable() __builtin_unreachable()
358#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
359#define pg_unreachable() __assume(0)
361#define pg_unreachable() abort()
371#if defined(HAVE__BUILTIN_CONSTANT_P)
374#define pg_integer_constant_p(x) __builtin_constant_p(x)
375#define HAVE_PG_INTEGER_CONSTANT_P
376#elif defined(_MSC_VER) && defined(__STDC_VERSION__)
385#define pg_integer_constant_p(x) \
386 _Generic((1 ? ((void *) ((x) * (uintptr_t) 0)) : &(int) {1}), int *: 1, void *: 0)
387#define HAVE_PG_INTEGER_CONSTANT_P
406#if defined(USE_ASSERT_CHECKING)
407#define pg_assume(expr) Assert(expr)
408#elif defined(HAVE__BUILTIN_UNREACHABLE)
409#define pg_assume(expr) \
412 __builtin_unreachable(); \
414#elif defined(_MSC_VER)
415#define pg_assume(expr) __assume(expr)
417#define pg_assume(expr) ((void) 0)
428#define likely(x) __builtin_expect((x) != 0, 1)
429#define unlikely(x) __builtin_expect((x) != 0, 0)
431#define likely(x) ((x) != 0)
432#define unlikely(x) ((x) != 0)
444#if defined(__clang__)
445#if __clang_major__ < 19
446#undef HAVE_TYPEOF_UNQUAL
449#define typeof_unqual __typeof_unqual__
463#if defined(__cplusplus)
466#define typeof(x) pg_cxx_typeof(x)
467#elif !defined(HAVE_CXX_TYPEOF)
468#define typeof(x) std::remove_reference<decltype(x)>::type
477#ifdef pg_cxx_typeof_unqual
478#define typeof_unqual(x) pg_cxx_typeof_unqual(x)
479#elif !defined(HAVE_CXX_TYPEOF_UNQUAL)
480#define typeof_unqual(x) std::remove_cv<std::remove_reference<decltype(x)>::type>::type
482#ifndef HAVE_TYPEOF_UNQUAL
483#define HAVE_TYPEOF_UNQUAL 1
499#define CppAsString(identifier) #identifier
500#define CppAsString2(x) CppAsString(x)
501#define CppConcat(x, y) x##y
517#define VA_ARGS_NARGS(...) \
518 VA_ARGS_NARGS_(__VA_ARGS__, \
520 59,58,57,56,55,54,53,52,51,50, \
521 49,48,47,46,45,44,43,42,41,40, \
522 39,38,37,36,35,34,33,32,31,30, \
523 29,28,27,26,25,24,23,22,21,20, \
524 19,18,17,16,15,14,13,12,11,10, \
525 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
527#define VA_ARGS_NARGS_( \
528 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
529 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
530 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
531 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
532 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
533 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
534 _61,_62,_63, N, ...) \
552#define FLEXIBLE_ARRAY_MEMBER
579#define HAVE_PRAGMA_GCC_SYSTEM_HEADER 1
632#define INT64CONST(x) INT64_C(x)
633#define UINT64CONST(x) UINT64_C(x)
636#define INT64_FORMAT "%" PRId64
637#define UINT64_FORMAT "%" PRIu64
638#define OID8_FORMAT "%" PRIu64
648#if defined(PG_INT128_TYPE)
649#if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
653#if defined(pg_attribute_aligned)
659#if defined(pg_attribute_aligned)
668#define PG_INT8_MIN INT8_MIN
669#define PG_INT8_MAX INT8_MAX
670#define PG_UINT8_MAX UINT8_MAX
671#define PG_INT16_MIN INT16_MIN
672#define PG_INT16_MAX INT16_MAX
673#define PG_UINT16_MAX UINT16_MAX
674#define PG_INT32_MIN INT32_MIN
675#define PG_INT32_MAX INT32_MAX
676#define PG_UINT32_MAX UINT32_MAX
677#define PG_INT64_MIN INT64_MIN
678#define PG_INT64_MAX INT64_MAX
679#define PG_UINT64_MAX UINT64_MAX
685#define HAVE_INT64_TIMESTAMP
722#define FLOAT8PASSBYVAL true
744#define InvalidSubTransactionId ((SubTransactionId) 0)
745#define TopSubTransactionId ((SubTransactionId) 1)
754#define FirstCommandId ((CommandId) 0)
755#define InvalidCommandId (~(CommandId)0)
760#define InvalidOid8 ((Oid8) 0)
761#define OID8_MAX UINT64_MAX
783#define VARHDRSZ ((int32) sizeof(int32))
837#define NameStr(name) ((name).data)
848#define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
854#define PointerIsAligned(pointer, type) \
855 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
857#define OffsetToPointer(base, offset) \
858 ((void *)((char *) base + offset))
860#define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
862#define Oid8IsValid(objectId) ((bool) ((objectId) != InvalidOid8))
864#define RegProcedureIsValid(p) OidIsValid(p)
875#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
891#define TYPEALIGN(ALIGNVAL,LEN) \
892 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
894#define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
895#define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
896#define INT64ALIGN(LEN) TYPEALIGN(ALIGNOF_INT64_T, (LEN))
897#define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
898#define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
900#define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
901#define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
903#define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
904 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
906#define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
907#define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
908#define INT64ALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT64_T, (LEN))
909#define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
910#define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
911#define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
919#define TYPEALIGN64(ALIGNVAL,LEN) \
920 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
923#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
943#ifndef USE_ASSERT_CHECKING
945#define Assert(condition) ((void)true)
946#define AssertMacro(condition) ((void)true)
948#elif defined(FRONTEND)
950#define Assert(p) assert(p)
951#define AssertMacro(p) ((void) assert(p))
959#define Assert(condition) \
962 ExceptionalCondition(#condition, __FILE__, __LINE__); \
971#define AssertMacro(condition) \
972 ((void) ((condition) || \
973 (ExceptionalCondition(#condition, __FILE__, __LINE__), 0)))
980#define AssertPointerAlignment(ptr, bndr) \
981 Assert(TYPEALIGN(bndr, (uintptr_t)(ptr)) == (uintptr_t)(ptr))
1010#define StaticAssertDecl(condition, errmessage) \
1011 static_assert(condition, errmessage)
1017#define StaticAssertStmt(condition, errmessage) \
1018 do { static_assert(condition, errmessage); } while(0)
1034#if !defined(_MSC_VER) || _MSC_VER >= 1933
1035#define StaticAssertExpr(condition, errmessage) \
1036 ((void) sizeof(struct {static_assert(condition, errmessage); char a;}))
1042#define StaticAssertExpr(condition, errmessage) \
1043 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
1046#define StaticAssertExpr(condition, errmessage) \
1047 ([]{static_assert(condition, errmessage);})
1062#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
1063#define StaticAssertVariableIsOfType(varname, typename) \
1064 StaticAssertDecl(__builtin_types_compatible_p(typeof(varname), typename), \
1065 CppAsString(varname) " does not have type " CppAsString(typename))
1066#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1067 (StaticAssertExpr(__builtin_types_compatible_p(typeof(varname), typename), \
1068 CppAsString(varname) " does not have type " CppAsString(typename)))
1070#define StaticAssertVariableIsOfType(varname, typename) \
1071 StaticAssertDecl(sizeof(varname) == sizeof(typename), \
1072 CppAsString(varname) " does not have type " CppAsString(typename))
1073#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1074 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
1075 CppAsString(varname) " does not have type " CppAsString(typename)))
1087#define Max(x, y) ((x) > (y) ? (x) : (y))
1093#define Min(x, y) ((x) < (y) ? (x) : (y))
1097#define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1)
1109#define MemSet(start, val, len) \
1113 void *_vstart = (void *) (start); \
1115 Size _len = (len); \
1117 if ((((uintptr_t) _vstart) & SIZE_T_ALIGN_MASK) == 0 && \
1118 (_len & SIZE_T_ALIGN_MASK) == 0 && \
1120 _len <= MEMSET_LOOP_LIMIT && \
1125 MEMSET_LOOP_LIMIT != 0) \
1127 size_t *_start = (size_t *) _vstart; \
1128 size_t *_stop = (size_t *) ((char *) _start + _len); \
1129 while (_start < _stop) \
1133 memset(_vstart, _val, _len); \
1142#define MemSetAligned(start, val, len) \
1145 size_t *_start = (size_t *) (start); \
1147 Size _len = (len); \
1149 if ((_len & SIZE_T_ALIGN_MASK) == 0 && \
1151 _len <= MEMSET_LOOP_LIMIT && \
1152 MEMSET_LOOP_LIMIT != 0) \
1154 size_t *_stop = (size_t *) ((char *) _start + _len); \
1155 while (_start < _stop) \
1159 memset(_start, _val, _len); \
1174#define FLOAT4_FITS_IN_INT16(num) \
1175 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1176#define FLOAT4_FITS_IN_INT32(num) \
1177 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1178#define FLOAT4_FITS_IN_INT64(num) \
1179 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1180#define FLOAT8_FITS_IN_INT16(num) \
1181 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1182#define FLOAT8_FITS_IN_INT32(num) \
1183 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1184#define FLOAT8_FITS_IN_INT64(num) \
1185 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1198#define INVERT_COMPARE_RESULT(var) \
1199 ((var) = ((var) < 0) ? 1 : -(var))
1218#if !(defined(__cplusplus) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9)
1248#define HIGHBIT (0x80)
1249#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1257#define SQL_STR_DOUBLE(ch, escape_backslash) \
1258 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1260#define ESCAPE_STRING_SYNTAX 'E'
1263#define STATUS_OK (0)
1264#define STATUS_ERROR (-1)
1265#define STATUS_EOF (-2)
1273#define gettext(x) (x)
1274#define dgettext(d,x) (x)
1275#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1276#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1279#define _(x) gettext(x)
1290#define gettext_noop(x) (x)
1305#ifdef SO_MAJOR_VERSION
1306#define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1308#define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1326#if defined(__cplusplus)
1327#define unconstify(underlying_type, expr) const_cast<underlying_type>(expr)
1328#define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr)
1330#define unconstify(underlying_type, expr) \
1331 (StaticAssertVariableIsOfTypeMacro(expr, const underlying_type), \
1332 (underlying_type) (expr))
1333#define unvolatize(underlying_type, expr) \
1334 (StaticAssertVariableIsOfTypeMacro(expr, volatile underlying_type), \
1335 (underlying_type) (expr))
1342#if (defined(__x86_64__) || defined(_M_AMD64))
1353#elif defined(__aarch64__) && defined(__ARM_NEON)
1373#if defined(WIN32) || defined(__CYGWIN__)
1374#define PG_BINARY O_BINARY
1375#define PG_BINARY_A "ab"
1376#define PG_BINARY_R "rb"
1377#define PG_BINARY_W "wb"
1380#define PG_BINARY_A "a"
1381#define PG_BINARY_R "r"
1382#define PG_BINARY_W "w"
1390#if !HAVE_DECL_FDATASYNC
1400#define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1401#define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1402#elif SIZEOF_LONG_LONG == 8
1403#define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1404#define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1406#error "cannot find integer type of the same size as int64_t"
1413#define i64abs(i) ((int64) labs(i))
1414#elif SIZEOF_LONG_LONG == 8
1415#define i64abs(i) ((int64) llabs(i))
1417#error "cannot find integer type of the same size as int64_t"
1438#ifdef HAVE_VISIBILITY_ATTRIBUTE
1439#define PGDLLEXPORT __attribute__((visibility("default")))
1455#define SIGNAL_ARGS int postgres_signal_arg
1467#define sigsetjmp(x,y) __builtin_setjmp(x)
1468#define siglongjmp __builtin_longjmp
1470#define sigjmp_buf jmp_buf
1471#define sigsetjmp(x,y) setjmp(x)
1472#define siglongjmp longjmp
static Datum values[MAXATTR]
pg_noreturn void ExceptionalCondition(const char *conditionName, const char *fileName, int lineNumber)
TransactionId MultiXactId
#define FLEXIBLE_ARRAY_MEMBER
uint32 LocalTransactionId
void(* pg_funcptr_t)(void)
static int fd(const char *x, int i)
char vl_dat[FLEXIBLE_ARRAY_MEMBER]