From 32ab1c5a8da0aa416c7d5d73595fd532be6d07d6 Mon Sep 17 00:00:00 2001 From: Dmitrii Dolgov <9erthalion6@gmail.com> Date: Wed, 26 Nov 2025 20:05:20 +0100 Subject: [PATCH v8 2/3] Add core versions to pg_system_versions Populate pg_system_versions with a set of core versions: host system architecture, ICU version, glibc version, PostgreSQL itself and compiler which was used to build everything. Register the core versions at the backend startup. select * from pg_system_versions; name | version | type ----------+--------------+-------------- Arch | x86_64-linux | Compile Time ICU | 15.1 | Run Time Core | 18devel | Compile Time Compiler | gcc-14.0.1 | Compile Time Glibc | 2.40 | Run Time --- configure | 12 +++++ configure.ac | 4 ++ meson.build | 4 ++ src/backend/tcop/postgres.c | 12 +++++ src/backend/utils/misc/system_version.c | 72 +++++++++++++++++++++++++ src/include/pg_config.h.in | 4 ++ src/include/utils/system_version.h | 1 + src/test/regress/expected/sysviews.out | 10 ++++ src/test/regress/sql/sysviews.sql | 6 +++ 9 files changed, 125 insertions(+) diff --git a/configure b/configure index d42a7a794ff..5e834f5938c 100755 --- a/configure +++ b/configure @@ -19722,6 +19722,18 @@ else fi +cat >>confdefs.h <<_ACEOF +#define PG_CC_STR "$cc_string" +_ACEOF + + + +cat >>confdefs.h <<_ACEOF +#define PG_ARCH_STR "$host" +_ACEOF + + + cat >>confdefs.h <<_ACEOF #define PG_VERSION_STR "PostgreSQL $PG_VERSION on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit" _ACEOF diff --git a/configure.ac b/configure.ac index a331749fcb5..05fd396c89d 100644 --- a/configure.ac +++ b/configure.ac @@ -2541,6 +2541,10 @@ else cc_string=$CC fi +AC_DEFINE_UNQUOTED(PG_CC_STR, ["$cc_string"], [C compiler version]) + +AC_DEFINE_UNQUOTED(PG_ARCH_STR, ["$host"], [Platform]) + AC_DEFINE_UNQUOTED(PG_VERSION_STR, ["PostgreSQL $PG_VERSION on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit"], [A string containing the version number, platform, and C compiler]) diff --git a/meson.build b/meson.build index f4cde249242..a1d8f141f00 100644 --- a/meson.build +++ b/meson.build @@ -3292,6 +3292,10 @@ cdata.set('USE_@0@_SEMAPHORES'.format(sema_kind.to_upper()), 1) cdata.set('MEMSET_LOOP_LIMIT', memset_loop_limit) cdata.set_quoted('DLSUFFIX', dlsuffix) +cdata.set_quoted('PG_CC_STR', '@0@-@1@'.format(cc.get_id(), cc.version())) + +cdata.set_quoted('PG_ARCH_STR', '@0@-@1@'.format( + host_machine.cpu_family(), host_system)) # built later than the rest of the version metadata, we need SIZEOF_VOID_P cdata.set_quoted('PG_VERSION_STR', diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index b6bdfe213fe..eec5b035a2e 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -86,6 +86,7 @@ #include "utils/timeout.h" #include "utils/timestamp.h" #include "utils/varlena.h" +#include "utils/system_version.h" /* ---------------- * global variables @@ -199,6 +200,7 @@ static void report_recovery_conflict(RecoveryConflictReason reason); static void log_disconnections(int code, Datum arg); static void enable_statement_timeout(void); static void disable_statement_timeout(void); +static void register_system_versions(void); /* ---------------------------------------------------------------- @@ -4489,6 +4491,9 @@ PostgresMain(const char *dbname, const char *username) */ BeginReportingGUCOptions(); + /* Prepare information for reporting versions and libraries. */ + register_system_versions(); + /* * Also set up handler to log session end; we have to wait till now to be * sure Log_disconnections has its final value. @@ -5408,3 +5413,10 @@ disable_statement_timeout(void) if (get_timeout_active(STATEMENT_TIMEOUT)) disable_timeout(STATEMENT_TIMEOUT, false); } + +static void +register_system_versions(void) +{ + /* Set up reporting of core versions. */ + register_core_versions(); +} diff --git a/src/backend/utils/misc/system_version.c b/src/backend/utils/misc/system_version.c index d80c53fcc7b..3bfd1318320 100644 --- a/src/backend/utils/misc/system_version.c +++ b/src/backend/utils/misc/system_version.c @@ -25,6 +25,14 @@ */ #include "postgres.h" +#ifdef USE_ICU +#include +#endif + +#ifdef __GLIBC__ +#include +#endif + #include "funcapi.h" #include "utils/builtins.h" #include "utils/hsearch.h" @@ -66,6 +74,70 @@ add_system_version(const char *name, SystemVersionCB cb, VersionType type) hentry->type = type; } +static const char * +core_get_version(bool *available) +{ + *available = true; + return (const char *) psprintf("%s", PG_VERSION); +} + +static const char * +core_get_arch(bool *available) +{ + *available = true; + return (const char *) psprintf("%s", PG_ARCH_STR); +} + +static const char * +core_get_compiler(bool *available) +{ + *available = true; + return (const char *) psprintf("%s", PG_CC_STR); +} + +#ifdef USE_ICU +static const char * +icu_get_version(bool *available) +{ + UVersionInfo UCDVersion; + char *version = palloc0(U_MAX_VERSION_STRING_LENGTH); + + *available = true; + u_getUnicodeVersion(UCDVersion); + u_versionToString(UCDVersion, version); + return (const char *) version; +} +#endif + +#ifdef __GLIBC__ +static const char * +glibc_get_version(bool *available) +{ + *available = true; + return (const char *) gnu_get_libc_version(); +} +#endif + +/* + * Register versions that describe core components and do not correspond to any + * individual component. + */ +void +register_core_versions() +{ + add_system_version("Core", core_get_version, CompileTime); + add_system_version("Arch", core_get_arch, CompileTime); + add_system_version("Compiler", core_get_compiler, CompileTime); + +#ifdef USE_ICU + add_system_version("ICU", icu_get_version, RunTime); +#endif + +#ifdef __GLIBC__ + add_system_version("Glibc", glibc_get_version, RunTime); +#endif +} + /* * pg_get_system_versions * diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index 661c4a9b168..1bea53c05bd 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -617,6 +617,10 @@ /* PostgreSQL version as a number */ #undef PG_VERSION_NUM +#undef PG_CC_STR + +#undef PG_ARCH_STR + /* A string containing the version number, platform, and C compiler */ #undef PG_VERSION_STR diff --git a/src/include/utils/system_version.h b/src/include/utils/system_version.h index 18cb673d4ca..49685b6809f 100644 --- a/src/include/utils/system_version.h +++ b/src/include/utils/system_version.h @@ -34,5 +34,6 @@ typedef struct SystemVersion } SystemVersion; void add_system_version(const char *name, SystemVersionCB cb, VersionType type); +extern void register_core_versions(void); #endif /* SYSTEM_VERSION_H */ diff --git a/src/test/regress/expected/sysviews.out b/src/test/regress/expected/sysviews.out index 1e327c2afa4..a5668288ed7 100644 --- a/src/test/regress/expected/sysviews.out +++ b/src/test/regress/expected/sysviews.out @@ -241,3 +241,13 @@ select * from pg_timezone_abbrevs where abbrev = 'LMT'; LMT | @ 7 hours 52 mins 58 secs ago | f (1 row) +-- Check that the pg_system_versions contains something. Three core versions +-- should be present: architecture, core and compiler and glibc. More records +-- may be present depending on the build configuration (e.g. with ICU, GlibC or +-- JIT support). +select exists (table pg_system_versions); + exists +-------- + t +(1 row) + diff --git a/src/test/regress/sql/sysviews.sql b/src/test/regress/sql/sysviews.sql index 507e400ad4a..7d504f9647e 100644 --- a/src/test/regress/sql/sysviews.sql +++ b/src/test/regress/sql/sysviews.sql @@ -104,3 +104,9 @@ select count(distinct utc_offset) >= 24 as ok from pg_timezone_abbrevs; -- One specific case we can check without much fear of breakage -- is the historical local-mean-time value used for America/Los_Angeles. select * from pg_timezone_abbrevs where abbrev = 'LMT'; + +-- Check that the pg_system_versions contains something. Three core versions +-- should be present: architecture, core and compiler and glibc. More records +-- may be present depending on the build configuration (e.g. with ICU, GlibC or +-- JIT support). +select exists (table pg_system_versions); -- 2.55.0