Hello.

I do some very regular testing on HEAD and my scripts need to know if
the catalog version has changed to determine if it needs to pg_restore
or if a basebackup is okay.  In order to get it, I have to do this:


# Get the catalog version (there is no better way to do this)
tmp=$(mktemp --directory)
$bin/initdb --pgdata=$tmp
catversion=$($bin/pg_controldata $tmp | grep "Catalog version" \
             | cut --delimiter=: --fields=2 | xargs)
rm --recursive --force $tmp


I find this less than attractive, especially since the catalog version
is a property of the binaries and not the data directory.  Attached is a
patchset so that the above can become simply:

catversion=$($bin/pg_config --catversion)

and a second patch that adds a read-only guc to get at it on the SQL
level using SHOW catalog_version; or similar.  I need that because I
also do a dump of pg_settings and I would like for it to appear there.

Please consider.
-- 
Vik Fearing
>From a06fd975ef14930bbef2dac3597272289d6b10eb Mon Sep 17 00:00:00 2001
From: Vik Fearing <v...@postgresfriends.org>
Date: Fri, 13 Nov 2020 11:55:58 +0100
Subject: [PATCH 1/2] Add catalog version to pg_config

---
 doc/src/sgml/ref/pg_config-ref.sgml | 9 +++++++++
 src/bin/pg_config/pg_config.c       | 2 ++
 src/common/config_info.c            | 7 ++++++-
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/pg_config-ref.sgml b/doc/src/sgml/ref/pg_config-ref.sgml
index e177769188..826cd5bf07 100644
--- a/doc/src/sgml/ref/pg_config-ref.sgml
+++ b/doc/src/sgml/ref/pg_config-ref.sgml
@@ -268,6 +268,15 @@ PostgreSQL documentation
      </listitem>
     </varlistentry>
 
+    <varlistentry>
+     <term><option>--catversion</option></term>
+     <listitem>
+      <para>
+       Print the version number of <productname>PostgreSQL</productname>'s catalog.
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry>
      <term><option>--version</option></term>
      <listitem>
diff --git a/src/bin/pg_config/pg_config.c b/src/bin/pg_config/pg_config.c
index c40bb3282e..ca048f6eff 100644
--- a/src/bin/pg_config/pg_config.c
+++ b/src/bin/pg_config/pg_config.c
@@ -64,6 +64,7 @@ static const InfoItem info_items[] = {
 	{"--ldflags_sl", "LDFLAGS_SL"},
 	{"--libs", "LIBS"},
 	{"--version", "VERSION"},
+	{"--catversion", "CATALOG_VERSION"},
 	{NULL, NULL}
 };
 
@@ -99,6 +100,7 @@ help(void)
 	printf(_("  --ldflags_ex          show LDFLAGS_EX value used when PostgreSQL was built\n"));
 	printf(_("  --ldflags_sl          show LDFLAGS_SL value used when PostgreSQL was built\n"));
 	printf(_("  --libs                show LIBS value used when PostgreSQL was built\n"));
+	printf(_("  --catversion          show the PostgreSQL catalog version\n"));
 	printf(_("  --version             show the PostgreSQL version\n"));
 	printf(_("  -?, --help            show this help, then exit\n"));
 	printf(_("\nWith no arguments, all known items are shown.\n\n"));
diff --git a/src/common/config_info.c b/src/common/config_info.c
index e72e7292a0..30c3168890 100644
--- a/src/common/config_info.c
+++ b/src/common/config_info.c
@@ -20,6 +20,7 @@
 #include "postgres_fe.h"
 #endif
 
+#include "catalog/catversion.h"
 #include "common/config_info.h"
 
 
@@ -38,7 +39,7 @@ get_configdata(const char *my_exec_path, size_t *configdata_len)
 	int			i = 0;
 
 	/* Adjust this to match the number of items filled below */
-	*configdata_len = 23;
+	*configdata_len = 24;
 	configdata = (ConfigData *) palloc(*configdata_len * sizeof(ConfigData));
 
 	configdata[i].name = pstrdup("BINDIR");
@@ -191,6 +192,10 @@ get_configdata(const char *my_exec_path, size_t *configdata_len)
 #endif
 	i++;
 
+	configdata[i].name = pstrdup("CATALOG_VERSION");
+	configdata[i].setting = pstrdup(psprintf("%d", CATALOG_VERSION_NO));
+	i++;
+
 	configdata[i].name = pstrdup("VERSION");
 	configdata[i].setting = pstrdup("PostgreSQL " PG_VERSION);
 	i++;
-- 
2.25.1

>From 05d0bca68096dba522075fa76ff1af490a050107 Mon Sep 17 00:00:00 2001
From: Vik Fearing <v...@postgresfriends.org>
Date: Fri, 13 Nov 2020 11:56:03 +0100
Subject: [PATCH 2/2] Add catalog_version guc, accessible from SQL

---
 doc/src/sgml/config.sgml         | 14 ++++++++++++++
 src/backend/utils/misc/check_guc |  2 +-
 src/backend/utils/misc/guc.c     | 14 ++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index e81141e45c..0b877b531c 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -9725,6 +9725,20 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
       </listitem>
      </varlistentry>
 
+     <varlistentry id="guc-catalog-version" xreflabel="catalog_version">
+      <term><varname>catalog_version</varname> (<type>string</type>)
+      <indexterm>
+       <primary><varname>catalog_version</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Reports the catalog version of the server. It is determined by the
+        value of <literal>CATALOG_VERSION_NO</literal> when building the server.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-data-checksums" xreflabel="data_checksums">
       <term><varname>data_checksums</varname> (<type>boolean</type>)
       <indexterm>
diff --git a/src/backend/utils/misc/check_guc b/src/backend/utils/misc/check_guc
index b171ef0e4f..a45f7f1727 100755
--- a/src/backend/utils/misc/check_guc
+++ b/src/backend/utils/misc/check_guc
@@ -16,7 +16,7 @@
 ## if an option is valid but shows up in only one file (guc.c but not
 ## postgresql.conf.sample), it should be listed here so that it
 ## can be ignored
-INTENTIONALLY_NOT_INCLUDED="debug_deadlocks in_hot_standby \
+INTENTIONALLY_NOT_INCLUDED="catalog_version_num debug_deadlocks in_hot_standby \
 is_superuser lc_collate lc_ctype lc_messages lc_monetary lc_numeric lc_time \
 pre_auth_delay role seed server_encoding server_version server_version_num \
 session_authorization trace_lock_oidmin trace_lock_table trace_locks trace_lwlocks \
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 00018abb7d..a619a17922 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -37,6 +37,7 @@
 #include "access/twophase.h"
 #include "access/xact.h"
 #include "access/xlog_internal.h"
+#include "catalog/catversion.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_authid.h"
 #include "catalog/storage.h"
@@ -598,6 +599,7 @@ static char *locale_ctype;
 static char *server_encoding_string;
 static char *server_version_string;
 static int	server_version_num;
+static int	catalog_version_no;
 static char *timezone_string;
 static char *log_timezone_string;
 static char *timezone_abbreviations_string;
@@ -3367,6 +3369,18 @@ static struct config_int ConfigureNamesInt[] =
 		NULL, NULL, NULL
 	},
 
+	{
+		/* Can't be set in postgresql.conf */
+		{"catalog_version", PGC_INTERNAL, PRESET_OPTIONS,
+			gettext_noop("Shows the catalog version number."),
+			NULL,
+			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+		},
+		&catalog_version_no,
+		CATALOG_VERSION_NO, CATALOG_VERSION_NO, CATALOG_VERSION_NO,
+		NULL, NULL, NULL
+	},
+
 	{
 		{"log_temp_files", PGC_SUSET, LOGGING_WHAT,
 			gettext_noop("Log the use of temporary files larger than this number of kilobytes."),
-- 
2.25.1

Reply via email to