From a6c678f018d3a30a88440d3c20cf8e7cd6592a32 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 6 Sep 2017 11:35:31 -0400 Subject: [PATCH] Add psql variables showing server version and psql version. We already had a psql variable VERSION that shows the verbose form of psql's own version. Add VERSION_NAME to show the short form (e.g., "11devel") and VERSION_NUM to show the numeric form (e.g., 110000). Also add SERVER_VERSION_NAME and SERVER_VERSION_NUM to show the short and numeric forms of the server's version. (We'd probably add SERVER_VERSION with the verbose string if it were readily available; but adding another network round trip to get it seems too expensive.) The numeric forms, in particular, are expected to be useful for scripting purposes, now that psql can do conditional tests. Back-patch of commit 9ae9d8c1549c384dbdb8363e1d932b7311d25c56. Fabien Coelho, reviewed by Pavel Stehule Discussion: https://postgr.es/m/alpine.DEB.2.20.1704020917220.4632@lancre --- doc/src/sgml/ref/psql-ref.sgml | 24 ++++++++++++++++++++++-- src/bin/psql/command.c | 19 +++++++++++++++++++ src/bin/psql/help.c | 7 ++++++- src/bin/psql/startup.c | 3 +++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index c592edac60a..868de184184 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -3670,6 +3670,21 @@ bar + + SERVER_VERSION_NAME + SERVER_VERSION_NUM + + + The server's version number as a string, for + example 9.6.2, 10.1 or 11beta1, + and in numeric form, for + example 90602 or 100001. + These are set every time you connect to a database + (including program start-up), but can be changed or unset. + + + + SHOW_CONTEXT @@ -3733,10 +3748,15 @@ bar VERSION + VERSION_NAME + VERSION_NUM - This variable is set at program start-up to - reflect psql's version. It can be changed or unset. + These variables are set at program start-up to reflect + psql's version, respectively as a verbose string, + a short string (e.g., 9.6.2, 10.1, + or 11beta1), and a number (e.g., 90602 + or 100001). They can be changed or unset. diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 4e04459d45e..d4274887484 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,20 @@ SyncVariables(void) SetVariable(pset.vars, "PORT", PQport(pset.db)); SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding)); + /* this bit should match connection_warnings(): */ + /* Try to get full text form of version, might include "devel" etc */ + server_version = PQparameterStatus(pset.db, "server_version"); + /* Otherwise fall back on pset.sversion */ + 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 +3383,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/help.c b/src/bin/psql/help.c index 57b826af166..c163d35df04 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -336,7 +336,7 @@ helpVariables(unsigned short int pager) * Windows builds currently print one more line than non-Windows builds. * Using the larger number is fine. */ - output = PageOutput(88, pager ? &(pset.popt.topt) : NULL); + output = PageOutput(93, pager ? &(pset.popt.topt) : NULL); fprintf(output, _("List of specially treated variables\n\n")); @@ -368,11 +368,16 @@ helpVariables(unsigned short int pager) fprintf(output, _(" PROMPT2 specifies the prompt used when a statement continues from a previous line\n")); fprintf(output, _(" PROMPT3 specifies the prompt used during COPY ... FROM STDIN\n")); fprintf(output, _(" QUIET run quietly (same as -q option)\n")); + fprintf(output, _(" SERVER_VERSION_NAME server's version (short string)\n")); + fprintf(output, _(" SERVER_VERSION_NUM server's version (numeric format)\n")); fprintf(output, _(" SHOW_CONTEXT controls display of message context fields [never, errors, always]\n")); fprintf(output, _(" SINGLELINE end of line terminates SQL command mode (same as -S option)\n")); fprintf(output, _(" SINGLESTEP single-step mode (same as -s option)\n")); fprintf(output, _(" USER the currently connected database user\n")); fprintf(output, _(" VERBOSITY controls verbosity of error reports [default, verbose, terse]\n")); + fprintf(output, _(" VERSION psql's version (verbose string)\n")); + fprintf(output, _(" VERSION_NAME psql's version (short string)\n")); + fprintf(output, _(" VERSION_NUM psql's version (numeric format)\n")); fprintf(output, _("\nDisplay settings:\n")); fprintf(output, _("Usage:\n")); diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 7f767976a5b..1e48f4ad5ad 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -160,7 +160,10 @@ main(int argc, char *argv[]) EstablishVariableSpace(); + /* Create variables showing psql version number */ 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");