Skip to content

Commit 9dca88b

Browse files
erthalionhackorum
authored andcommitted
Add infrastructure for pg_system_versions view
Introduce a unified way of reporting versions (PostgreSQL itself, the compiler, the host system, compile and runtime dependencies, etc.) via a new system view pg_system_versions. This is going to be useful for troubleshooting and should enhance bug reports, replacing manual bug-prone collecting of the same information. The view is backed by a hash table, that contains callbacks returning version string for a particular component. The idea is to allow some flexibility in reporting, making components responsible for how and when the information is exposed.
1 parent 4255812 commit 9dca88b

9 files changed

Lines changed: 236 additions & 0 deletions

File tree

‎doc/src/sgml/system-views.sgml‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@
211211
<entry>extended planner statistics for expressions</entry>
212212
</row>
213213

214+
<row>
215+
<entry><link linkend="view-pg-system-versions"><structname>pg_system_versions</structname></link></entry>
216+
<entry>system versions</entry>
217+
</row>
218+
214219
<row>
215220
<entry><link linkend="view-pg-tables"><structname>pg_tables</structname></link></entry>
216221
<entry>tables</entry>
@@ -5158,6 +5163,66 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
51585163

51595164
</sect1>
51605165

5166+
<sect1 id="view-pg-system-versions">
5167+
<title><structname>pg_system_versions</structname></title>
5168+
5169+
<indexterm zone="view-pg-system-versions">
5170+
<primary>pg_system_versions</primary>
5171+
</indexterm>
5172+
5173+
<para>
5174+
The view <structname>pg_system_versions</structname> provides description
5175+
about the versions of various system components, e.g. PostgreSQL itself,
5176+
the compiler used to build it, dependencies.
5177+
</para>
5178+
5179+
<table>
5180+
<title><structname>pg_system_versions</structname> Columns</title>
5181+
<tgroup cols="1">
5182+
<thead>
5183+
<row>
5184+
<entry role="catalog_table_entry"><para role="column_definition">
5185+
Column Type
5186+
</para>
5187+
<para>
5188+
Description
5189+
</para></entry>
5190+
</row>
5191+
</thead>
5192+
5193+
<tbody>
5194+
<row>
5195+
<entry role="catalog_table_entry"><para role="column_definition">
5196+
<structfield>name</structfield> <type>text</type>
5197+
</para>
5198+
<para>
5199+
Component name
5200+
</para></entry>
5201+
</row>
5202+
5203+
<row>
5204+
<entry role="catalog_table_entry"><para role="column_definition">
5205+
<structfield>version</structfield> <type>text</type>
5206+
</para>
5207+
<para>
5208+
Component version
5209+
</para></entry>
5210+
</row>
5211+
5212+
<row>
5213+
<entry role="catalog_table_entry"><para role="column_definition">
5214+
<structfield>type</structfield> <type>text</type>
5215+
</para>
5216+
<para>
5217+
Component type (Compile Time or Run Time)
5218+
</para></entry>
5219+
</row>
5220+
5221+
</tbody>
5222+
</tgroup>
5223+
</table>
5224+
</sect1>
5225+
51615226
<sect1 id="view-pg-tables">
51625227
<title><structname>pg_tables</structname></title>
51635228

‎src/backend/catalog/system_views.sql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,3 +1575,11 @@ CREATE VIEW pg_aios AS
15751575
SELECT * FROM pg_get_aios();
15761576
REVOKE ALL ON pg_aios FROM PUBLIC;
15771577
GRANT SELECT ON pg_aios TO pg_read_all_stats;
1578+
1579+
CREATE VIEW pg_system_versions AS
1580+
SELECT
1581+
name, version,
1582+
CASE type WHEN 0 THEN 'Compile Time'
1583+
WHEN 1 THEN 'Run Time'
1584+
END AS "type"
1585+
FROM pg_get_system_versions();

‎src/backend/utils/misc/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ OBJS = \
3131
sampling.o \
3232
stack_depth.o \
3333
superuser.o \
34+
system_version.o \
3435
timeout.o \
3536
tzparser.o
3637

‎src/backend/utils/misc/meson.build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ backend_sources += files(
1616
'sampling.c',
1717
'stack_depth.c',
1818
'superuser.c',
19+
'system_version.c',
1920
'timeout.c',
2021
'tzparser.c',
2122
)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*------------------------------------------------------------------------
2+
*
3+
* system_version.c
4+
* Functions for reporting version of system components.
5+
*
6+
* A system component is defined very broadly here, it might be the PostgreSQL
7+
* core itself, the compiler, the host system, any dependency that is used at
8+
* compile time or run time.
9+
*
10+
* Version reporting is implemented via a hash table containing the component's
11+
* name as a key and the callback to fetch the version string. Every component
12+
* can register such a callback during initialization and is responsible for
13+
* exposing its own information. The idea is that storing a callback instead of
14+
* a version string directly allows for more flexibility about how and when the
15+
* information could be reported.
16+
*
17+
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
18+
* Portions Copyright (c) 1994, Regents of the University of California
19+
*
20+
*
21+
* IDENTIFICATION
22+
* src/backend/utils/misc/system_version.c
23+
*
24+
*------------------------------------------------------------------------
25+
*/
26+
#include "postgres.h"
27+
28+
#include "funcapi.h"
29+
#include "utils/builtins.h"
30+
#include "utils/hsearch.h"
31+
#include "utils/system_version.h"
32+
#include "utils/tuplestore.h"
33+
34+
static HTAB *versions = NULL;
35+
36+
void
37+
add_system_version(const char *name, SystemVersionCB cb, VersionType type)
38+
{
39+
SystemVersion *hentry;
40+
const char *key;
41+
bool found;
42+
43+
if (!versions)
44+
{
45+
HASHCTL ctl;
46+
47+
ctl.keysize = NAMEDATALEN;
48+
ctl.entrysize = sizeof(SystemVersion);
49+
50+
versions = hash_create("System versions table",
51+
MAX_SYSTEM_VERSIONS,
52+
&ctl,
53+
HASH_ELEM | HASH_STRINGS);
54+
}
55+
56+
key = pstrdup(name);
57+
Assert(strlen(key) < NAMEDATALEN);
58+
59+
hentry = (SystemVersion *) hash_search(versions, key,
60+
HASH_ENTER, &found);
61+
62+
/* Duplicated entries are not expected */
63+
Assert(!found);
64+
65+
hentry->callback = cb;
66+
hentry->type = type;
67+
}
68+
69+
/*
70+
* pg_get_system_versions
71+
*
72+
* List information about system versions.
73+
*/
74+
Datum
75+
pg_get_system_versions(PG_FUNCTION_ARGS)
76+
{
77+
#define PG_GET_SYS_VERSIONS_COLS 3
78+
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
79+
HASH_SEQ_STATUS status;
80+
SystemVersion *hentry;
81+
82+
/* Build tuplestore to hold the result rows */
83+
InitMaterializedSRF(fcinfo, 0);
84+
85+
if (!versions)
86+
return (Datum) 0;
87+
88+
hash_seq_init(&status, versions);
89+
while ((hentry = (SystemVersion *) hash_seq_search(&status)) != NULL)
90+
{
91+
Datum values[PG_GET_SYS_VERSIONS_COLS] = {0};
92+
bool nulls[PG_GET_SYS_VERSIONS_COLS] = {0};
93+
bool available = false;
94+
const char *version = hentry->callback(&available);
95+
96+
if (!available)
97+
continue;
98+
99+
values[0] = CStringGetTextDatum(hentry->name);
100+
values[1] = CStringGetTextDatum(version);
101+
values[2] = Int32GetDatum(hentry->type);
102+
103+
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
104+
}
105+
106+
return (Datum) 0;
107+
}

‎src/include/catalog/pg_proc.dat‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12788,4 +12788,10 @@
1278812788
proname => 'hashoid8extended', prorettype => 'int8',
1278912789
proargtypes => 'oid8 int8', prosrc => 'hashoid8extended' },
1279012790

12791+
{ oid => '9432', descr => 'describe system verions',
12792+
proname => 'pg_get_system_versions', procost => '10', prorows => '10',
12793+
proretset => 't', provolatile => 'v', prorettype => 'record',
12794+
proargtypes => '', proallargtypes => '{text,text,int8}',
12795+
proargmodes => '{o,o,o}', proargnames => '{name,version,type}',
12796+
prosrc => 'pg_get_system_versions' },
1279112797
]

‎src/include/utils/system_version.h‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-------------------------------------------------------------------------
2+
* system_version.h
3+
* Definitions related to system versions reporting
4+
*
5+
* Copyright (c) 2001-2024, PostgreSQL Global Development Group
6+
*
7+
* src/include/utils/system_version.h
8+
* ----------
9+
*/
10+
11+
#ifndef SYSTEM_VERSION_H
12+
#define SYSTEM_VERSION_H
13+
14+
#define MAX_SYSTEM_VERSIONS 100
15+
16+
typedef enum VersionType
17+
{
18+
CompileTime,
19+
RunTime,
20+
} VersionType;
21+
22+
/*
23+
* Callback to return version string of a system component.
24+
* The version might be not available, what is indicated via the argument.
25+
*/
26+
typedef const char *(*SystemVersionCB) (bool *available);
27+
28+
typedef struct SystemVersion
29+
{
30+
char name[NAMEDATALEN]; /* Unique component name, used as a key
31+
* for versions HTAB */
32+
VersionType type;
33+
SystemVersionCB callback; /* Callback to fetch the version string */
34+
} SystemVersion;
35+
36+
void add_system_version(const char *name, SystemVersionCB cb, VersionType type);
37+
38+
#endif /* SYSTEM_VERSION_H */

‎src/test/regress/expected/rules.out‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,6 +2845,14 @@ pg_stats_ext_exprs| SELECT cn.nspname AS schemaname,
28452845
JOIN LATERAL ( SELECT unnest(pg_get_statisticsobjdef_expressions(s.oid)) AS expr,
28462846
unnest(sd.stxdexpr) AS a) stat ON ((stat.expr IS NOT NULL)))
28472847
WHERE (pg_has_role(c.relowner, 'USAGE'::text) AND ((c.relrowsecurity = false) OR (NOT row_security_active(c.oid))));
2848+
pg_system_versions| SELECT name,
2849+
version,
2850+
CASE type
2851+
WHEN 0 THEN 'Compile Time'::text
2852+
WHEN 1 THEN 'Run Time'::text
2853+
ELSE NULL::text
2854+
END AS type
2855+
FROM pg_get_system_versions() pg_get_system_versions(name, version, type);
28482856
pg_tables| SELECT n.nspname AS schemaname,
28492857
c.relname AS tablename,
28502858
pg_get_userbyid(c.relowner) AS tableowner,

‎src/tools/pgindent/typedefs.list‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,7 @@ SysloggerStartupData
30693069
SystemRowsSamplerData
30703070
SystemSamplerData
30713071
SystemTimeSamplerData
3072+
SystemVersion
30723073
TAPtype
30733074
TAR_MEMBER
30743075
TBMIterateResult
@@ -4428,6 +4429,7 @@ varlena
44284429
vartag_external
44294430
vbits
44304431
verifier_context
4432+
VersionType
44314433
walrcv_alter_slot_fn
44324434
walrcv_check_conninfo_fn
44334435
walrcv_connect_fn

0 commit comments

Comments
 (0)