59#include "pg_config_os.h"
76#if defined(WIN32) || defined(__CYGWIN__)
117#if defined(__arm__) || defined(__arm)
121#elif defined(__aarch64__) || defined(_M_ARM64)
125#elif defined(__loongarch64__) || defined(__loongarch64)
126#ifndef __loongarch64__
127#define __loongarch64__ 1
129#elif defined(__mips__)
131#elif defined(__mips64__)
133#elif defined(__powerpc__) || defined(__ppc__)
137#elif defined(__powerpc64__) || defined(__ppc64__)
139#define __powerpc64__ 1
141#elif defined(__riscv)
143#if SIZEOF_VOID_P == 8
148#elif defined(__s390__)
150#elif defined(__s390x__)
152#elif defined(__sparc__) || defined(__sparc)
156#elif defined(__i386__) || defined (__i386) || defined(_M_IX86)
160#elif defined(__x86_64__) || defined(__x86_64) || defined (__amd64) || defined(_M_AMD64)
165#error "cannot identify target architecture"
173#ifdef PG_FORCE_DISABLE_INLINE
196#ifndef __has_attribute
197#define __has_attribute(attribute) 0
203#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
204#define pg_attribute_unused() [[maybe_unused]]
205#elif defined(__GNUC__)
206#define pg_attribute_unused() __attribute__((unused))
208#define pg_attribute_unused()
215#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
216#define pg_fallthrough [[fallthrough]]
217#elif __has_attribute(fallthrough)
218#define pg_fallthrough __attribute__((fallthrough))
220#define pg_fallthrough
227#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
228#define pg_nodiscard [[nodiscard]]
229#elif defined(__GNUC__)
230#define pg_nodiscard __attribute__((warn_unused_result))
246#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || defined(__cplusplus)
247#define pg_noreturn [[noreturn]]
249#define pg_noreturn _Noreturn
256#if defined(__clang__) || __GNUC__ >= 8
257#define pg_attribute_no_sanitize_address() __attribute__((no_sanitize("address")))
258#elif __has_attribute(no_sanitize_address)
260#define pg_attribute_no_sanitize_address() __attribute__((no_sanitize_address))
262#define pg_attribute_no_sanitize_address()
271#if __clang_major__ >= 7 || __GNUC__ >= 8
272#define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
274#define pg_attribute_no_sanitize_alignment()
282#if __has_attribute (nonnull)
283#define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
285#define pg_attribute_nonnull(...)
294#if __has_attribute (target)
295#define pg_attribute_target(...) __attribute__((target(__VA_ARGS__)))
297#define pg_attribute_target(...)
305#ifdef USE_ASSERT_CHECKING
306#define PG_USED_FOR_ASSERTS_ONLY
308#define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
316#define PG_PRINTF_ATTRIBUTE PG_C_PRINTF_ATTRIBUTE
318#define PG_PRINTF_ATTRIBUTE PG_CXX_PRINTF_ATTRIBUTE
323#define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
324#define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
326#define pg_attribute_format_arg(a)
327#define pg_attribute_printf(f,a)
332#define pg_attribute_aligned(a) __attribute__((aligned(a)))
333#define pg_attribute_packed() __attribute__((packed))
334#elif defined(_MSC_VER)
341#define pg_attribute_aligned(a) __declspec(align(a))
356#if defined(__GNUC__) && defined(__OPTIMIZE__)
358#define pg_attribute_always_inline __attribute__((always_inline)) inline
359#elif defined(_MSC_VER)
361#define pg_attribute_always_inline __forceinline
364#define pg_attribute_always_inline inline
375#define pg_noinline __attribute__((noinline))
377#elif defined(_MSC_VER)
378#define pg_noinline __declspec(noinline)
392#if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
394#define pg_attribute_cold
395#define pg_attribute_hot
402#if __has_attribute (cold)
403#define pg_attribute_cold __attribute__((cold))
405#define pg_attribute_cold
408#if __has_attribute (hot)
409#define pg_attribute_hot __attribute__((hot))
411#define pg_attribute_hot
421#if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
422#define pg_unreachable() __builtin_unreachable()
423#elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
424#define pg_unreachable() __assume(0)
426#define pg_unreachable() abort()
436#if defined(HAVE__BUILTIN_CONSTANT_P)
439#define pg_integer_constant_p(x) __builtin_constant_p(x)
440#define HAVE_PG_INTEGER_CONSTANT_P
441#elif defined(_MSC_VER) && defined(__STDC_VERSION__)
450#define pg_integer_constant_p(x) \
451 _Generic((1 ? ((void *) ((x) * (uintptr_t) 0)) : &(int) {1}), int *: 1, void *: 0)
452#define HAVE_PG_INTEGER_CONSTANT_P
471#if defined(USE_ASSERT_CHECKING)
472#define pg_assume(expr) Assert(expr)
473#elif defined(HAVE__BUILTIN_UNREACHABLE)
474#define pg_assume(expr) \
477 __builtin_unreachable(); \
479#elif defined(_MSC_VER)
480#define pg_assume(expr) __assume(expr)
482#define pg_assume(expr) ((void) 0)
493#define likely(x) __builtin_expect((x) != 0, 1)
494#define unlikely(x) __builtin_expect((x) != 0, 0)
496#define likely(x) ((x) != 0)
497#define unlikely(x) ((x) != 0)
509#if defined(__clang__)
510#if __clang_major__ < 19
511#undef HAVE_TYPEOF_UNQUAL
514#define typeof_unqual __typeof_unqual__
528#if defined(__cplusplus)
531#define typeof(x) pg_cxx_typeof(x)
532#elif !defined(HAVE_CXX_TYPEOF)
533#define typeof(x) std::remove_reference<decltype(x)>::type
542#ifdef pg_cxx_typeof_unqual
543#define typeof_unqual(x) pg_cxx_typeof_unqual(x)
544#elif !defined(HAVE_CXX_TYPEOF_UNQUAL)
545#define typeof_unqual(x) std::remove_cv<std::remove_reference<decltype(x)>::type>::type
547#ifndef HAVE_TYPEOF_UNQUAL
548#define HAVE_TYPEOF_UNQUAL 1
564#define CppAsString(identifier) #identifier
565#define CppAsString2(x) CppAsString(x)
566#define CppConcat(x, y) x##y
582#define VA_ARGS_NARGS(...) \
583 VA_ARGS_NARGS_(__VA_ARGS__, \
585 59,58,57,56,55,54,53,52,51,50, \
586 49,48,47,46,45,44,43,42,41,40, \
587 39,38,37,36,35,34,33,32,31,30, \
588 29,28,27,26,25,24,23,22,21,20, \
589 19,18,17,16,15,14,13,12,11,10, \
590 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
592#define VA_ARGS_NARGS_( \
593 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
594 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
595 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
596 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
597 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
598 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
599 _61,_62,_63, N, ...) \
617#define FLEXIBLE_ARRAY_MEMBER
644#define HAVE_PRAGMA_GCC_SYSTEM_HEADER 1
689#define INT64CONST(x) INT64_C(x)
690#define UINT64CONST(x) UINT64_C(x)
693#define INT64_FORMAT "%" PRId64
694#define UINT64_FORMAT "%" PRIu64
695#define OID8_FORMAT "%" PRIu64
705#if defined(PG_INT128_TYPE)
706#if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
710#if defined(pg_attribute_aligned)
716#if defined(pg_attribute_aligned)
725#define PG_INT8_MIN INT8_MIN
726#define PG_INT8_MAX INT8_MAX
727#define PG_UINT8_MAX UINT8_MAX
728#define PG_INT16_MIN INT16_MIN
729#define PG_INT16_MAX INT16_MAX
730#define PG_UINT16_MAX UINT16_MAX
731#define PG_INT32_MIN INT32_MIN
732#define PG_INT32_MAX INT32_MAX
733#define PG_UINT32_MAX UINT32_MAX
734#define PG_INT64_MIN INT64_MIN
735#define PG_INT64_MAX INT64_MAX
736#define PG_UINT64_MAX UINT64_MAX
742#define HAVE_INT64_TIMESTAMP
779#define FLOAT8PASSBYVAL true
801#define InvalidSubTransactionId ((SubTransactionId) 0)
802#define TopSubTransactionId ((SubTransactionId) 1)
811#define FirstCommandId ((CommandId) 0)
812#define InvalidCommandId (~(CommandId)0)
817#define InvalidOid8 ((Oid8) 0)
818#define OID8_MAX UINT64_MAX
840#define VARHDRSZ ((int32) sizeof(int32))
894#define NameStr(name) ((name).data)
905#define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
911#define PointerIsAligned(pointer, type) \
912 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
914#define OffsetToPointer(base, offset) \
915 ((void *)((char *) base + offset))
917#define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
919#define Oid8IsValid(objectId) ((bool) ((objectId) != InvalidOid8))
921#define RegProcedureIsValid(p) OidIsValid(p)
932#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
948#define TYPEALIGN(ALIGNVAL,LEN) \
949 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
951#define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
952#define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
953#define INT64ALIGN(LEN) TYPEALIGN(ALIGNOF_INT64_T, (LEN))
954#define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
955#define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
957#define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
958#define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
960#define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
961 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
963#define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
964#define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
965#define INT64ALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT64_T, (LEN))
966#define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
967#define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
968#define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
976#define TYPEALIGN64(ALIGNVAL,LEN) \
977 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
980#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
1000#ifndef USE_ASSERT_CHECKING
1002#define Assert(condition) ((void)true)
1003#define AssertMacro(condition) ((void)true)
1005#elif defined(FRONTEND)
1007#define Assert(p) assert(p)
1008#define AssertMacro(p) ((void) assert(p))
1016#define Assert(condition) \
1019 ExceptionalCondition(#condition, __FILE__, __LINE__); \
1028#define AssertMacro(condition) \
1029 ((void) ((condition) || \
1030 (ExceptionalCondition(#condition, __FILE__, __LINE__), 0)))
1037#define AssertPointerAlignment(ptr, bndr) \
1038 Assert(TYPEALIGN(bndr, (uintptr_t)(ptr)) == (uintptr_t)(ptr))
1067#define StaticAssertDecl(condition, errmessage) \
1068 static_assert(condition, errmessage)
1074#define StaticAssertStmt(condition, errmessage) \
1075 do { static_assert(condition, errmessage); } while(0)
1091#if !defined(_MSC_VER) || _MSC_VER >= 1933
1092#define StaticAssertExpr(condition, errmessage) \
1093 ((void) sizeof(struct {static_assert(condition, errmessage); char a;}))
1099#define StaticAssertExpr(condition, errmessage) \
1100 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
1103#define StaticAssertExpr(condition, errmessage) \
1104 ([]{static_assert(condition, errmessage);})
1119#ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
1120#define StaticAssertVariableIsOfType(varname, typename) \
1121 StaticAssertDecl(__builtin_types_compatible_p(typeof(varname), typename), \
1122 CppAsString(varname) " does not have type " CppAsString(typename))
1123#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1124 (StaticAssertExpr(__builtin_types_compatible_p(typeof(varname), typename), \
1125 CppAsString(varname) " does not have type " CppAsString(typename)))
1127#define StaticAssertVariableIsOfType(varname, typename) \
1128 StaticAssertDecl(sizeof(varname) == sizeof(typename), \
1129 CppAsString(varname) " does not have type " CppAsString(typename))
1130#define StaticAssertVariableIsOfTypeMacro(varname, typename) \
1131 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
1132 CppAsString(varname) " does not have type " CppAsString(typename)))
1144#define Max(x, y) ((x) > (y) ? (x) : (y))
1150#define Min(x, y) ((x) < (y) ? (x) : (y))
1154#define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1)
1166#define MemSet(start, val, len) \
1170 void *_vstart = (void *) (start); \
1172 Size _len = (len); \
1174 if ((((uintptr_t) _vstart) & SIZE_T_ALIGN_MASK) == 0 && \
1175 (_len & SIZE_T_ALIGN_MASK) == 0 && \
1177 _len <= MEMSET_LOOP_LIMIT && \
1182 MEMSET_LOOP_LIMIT != 0) \
1184 size_t *_start = (size_t *) _vstart; \
1185 size_t *_stop = (size_t *) ((char *) _start + _len); \
1186 while (_start < _stop) \
1190 memset(_vstart, _val, _len); \
1199#define MemSetAligned(start, val, len) \
1202 size_t *_start = (size_t *) (start); \
1204 Size _len = (len); \
1206 if ((_len & SIZE_T_ALIGN_MASK) == 0 && \
1208 _len <= MEMSET_LOOP_LIMIT && \
1209 MEMSET_LOOP_LIMIT != 0) \
1211 size_t *_stop = (size_t *) ((char *) _start + _len); \
1212 while (_start < _stop) \
1216 memset(_start, _val, _len); \
1231#define FLOAT4_FITS_IN_INT16(num) \
1232 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1233#define FLOAT4_FITS_IN_INT32(num) \
1234 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1235#define FLOAT4_FITS_IN_INT64(num) \
1236 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1237#define FLOAT8_FITS_IN_INT16(num) \
1238 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1239#define FLOAT8_FITS_IN_INT32(num) \
1240 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1241#define FLOAT8_FITS_IN_INT64(num) \
1242 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1255#define INVERT_COMPARE_RESULT(var) \
1256 ((var) = ((var) < 0) ? 1 : -(var))
1275#if !(defined(__cplusplus) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9)
1305#define HIGHBIT (0x80)
1306#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1314#define SQL_STR_DOUBLE(ch, escape_backslash) \
1315 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1317#define ESCAPE_STRING_SYNTAX 'E'
1320#define STATUS_OK (0)
1321#define STATUS_ERROR (-1)
1322#define STATUS_EOF (-2)
1330#define gettext(x) (x)
1331#define dgettext(d,x) (x)
1332#define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1333#define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1336#define _(x) gettext(x)
1347#define gettext_noop(x) (x)
1362#ifdef SO_MAJOR_VERSION
1363#define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1365#define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1383#if defined(__cplusplus)
1384#define unconstify(underlying_type, expr) const_cast<underlying_type>(expr)
1385#define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr)
1387#define unconstify(underlying_type, expr) \
1388 (StaticAssertVariableIsOfTypeMacro(expr, const underlying_type), \
1389 (underlying_type) (expr))
1390#define unvolatize(underlying_type, expr) \
1391 (StaticAssertVariableIsOfTypeMacro(expr, volatile underlying_type), \
1392 (underlying_type) (expr))
1399#if defined(__x86_64__)
1409#undef USE_AVX2_WITH_RUNTIME_CHECK
1410#undef USE_AVX512_CRC32C_WITH_RUNTIME_CHECK
1411#undef USE_AVX512_POPCNT_WITH_RUNTIME_CHECK
1421#if defined(__aarch64__) && defined(__ARM_NEON)
1442#if defined(WIN32) || defined(__CYGWIN__)
1443#define PG_BINARY O_BINARY
1444#define PG_BINARY_A "ab"
1445#define PG_BINARY_R "rb"
1446#define PG_BINARY_W "wb"
1449#define PG_BINARY_A "a"
1450#define PG_BINARY_R "r"
1451#define PG_BINARY_W "w"
1459#if !HAVE_DECL_FDATASYNC
1469#define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1470#define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1471#elif SIZEOF_LONG_LONG == 8
1472#define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1473#define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1475#error "cannot find integer type of the same size as int64_t"
1482#define i64abs(i) ((int64) labs(i))
1483#elif SIZEOF_LONG_LONG == 8
1484#define i64abs(i) ((int64) llabs(i))
1486#error "cannot find integer type of the same size as int64_t"
1507#ifdef HAVE_VISIBILITY_ATTRIBUTE
1508#define PGDLLEXPORT __attribute__((visibility("default")))
1536#define SIGNAL_ARGS int postgres_signal_arg, const pg_signal_info *pg_siginfo
1547#define sigsetjmp(x,y) __builtin_setjmp(x)
1548#define siglongjmp __builtin_longjmp
1550#define sigjmp_buf jmp_buf
1551#define sigsetjmp(x,y) setjmp(x)
1552#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]