|
| 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 | +} |
0 commit comments