Hello Tom,

I think you are taking unreasonable shortcuts here:

+       SetVariable(pset.vars, "SERVER_VERSION_NAME", PQparameterStatus(pset.db, 
"server_version"));

The existing code in connection_warnings() does this:

           const char *server_version;

           /* Try to get full text form, might include "devel" etc */
           server_version = PQparameterStatus(pset.db, "server_version");
           /* Otherwise fall back on pset.sversion */
           if (!server_version)
           {
               formatPGVersionNumber(pset.sversion, true,
                                     sverbuf, sizeof(sverbuf));
               server_version = sverbuf;
           }

and I think you should duplicate that logic verbatim.  Now admittedly,
server_version has been available for a long time, so that this might
never matter in practice.  But we shouldn't be doing this one way
in one place and differently somewhere else.

Hmmm. I think this code may have been justified around version 6/7. This code could probably be removed: according to the online documentation, "server_version" seems supported at least back to 7.4. Greping old sources suggest that it is not implemented in 7.3, though.

Spending developer time to write code for the hypothetical someone running a psql version 11 linked to a libpq < 7.4, if it can even link, does not look like a very good investment... Anyway, here is required the update.

--
Fabien.
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index c592eda..c611f39 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -3688,6 +3688,18 @@ bar
       </varlistentry>
 
       <varlistentry>
+        <term><varname>SERVER_VERSION_NAME</varname></term>
+        <term><varname>SERVER_VERSION_NUM</varname></term>
+        <listitem>
+        <para>
+        These variables are set when connected to reflects the server's version
+        both in short name (eg "9.6.2", "10.0") and number (eg 90602, 100000).
+        They can be changed or unset.
+        </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><varname>SINGLELINE</varname></term>
         <listitem>
         <para>
@@ -3733,10 +3745,13 @@ bar
 
       <varlistentry>
         <term><varname>VERSION</varname></term>
+        <term><varname>VERSION_NAME</varname></term>
+        <term><varname>VERSION_NUM</varname></term>
         <listitem>
         <para>
-        This variable is set at program start-up to
-        reflect <application>psql</>'s version.  It can be changed or unset.
+        These variables are set at program start-up to reflect
+        <application>psql</>'s version in verbose, short name ("10.0")
+        and number (100000).  They can be changed or unset.
         </para>
         </listitem>
       </varlistentry>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 96f3a40..b58d8b6 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3337,6 +3337,9 @@ checkWin32Codepage(void)
 void
 SyncVariables(void)
 {
+	char vbuf[32];
+	const char *server_version;
+
 	/* get stuff from connection */
 	pset.encoding = PQclientEncoding(pset.db);
 	pset.popt.topt.encoding = pset.encoding;
@@ -3348,6 +3351,19 @@ SyncVariables(void)
 	SetVariable(pset.vars, "PORT", PQport(pset.db));
 	SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
 
+	/* server version information */
+	server_version = PQparameterStatus(pset.db, "server_version");
+	/* Otherwise fall back on pset.sversion for libpq < 7.4 */
+	if (!server_version)
+	{
+		formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
+		server_version = vbuf;
+	}
+	SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
+
+	snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
+	SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
+
 	/* send stuff to it, too */
 	PQsetErrorVerbosity(pset.db, pset.verbosity);
 	PQsetErrorContextVisibility(pset.db, pset.show_context);
@@ -3366,6 +3382,8 @@ UnsyncVariables(void)
 	SetVariable(pset.vars, "HOST", NULL);
 	SetVariable(pset.vars, "PORT", NULL);
 	SetVariable(pset.vars, "ENCODING", NULL);
+	SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
+	SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
 }
 
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 7f76797..4065539 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -161,6 +161,8 @@ main(int argc, char *argv[])
 	EstablishVariableSpace();
 
 	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
+	SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
+	SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
 
 	/* Default values for variables (that don't match the result of \unset) */
 	SetVariableBool(pset.vars, "AUTOCOMMIT");
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to