Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a6c678f

Browse files
committed
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 9ae9d8c. Fabien Coelho, reviewed by Pavel Stehule Discussion: https://postgr.es/m/alpine.DEB.2.20.1704020917220.4632@lancre
1 parent 4838829 commit a6c678f

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,6 +3670,21 @@ bar
36703670
</listitem>
36713671
</varlistentry>
36723672

3673+
<varlistentry>
3674+
<term><varname>SERVER_VERSION_NAME</varname></term>
3675+
<term><varname>SERVER_VERSION_NUM</varname></term>
3676+
<listitem>
3677+
<para>
3678+
The server's version number as a string, for
3679+
example <literal>9.6.2</>, <literal>10.1</> or <literal>11beta1</>,
3680+
and in numeric form, for
3681+
example <literal>90602</> or <literal>100001</>.
3682+
These are set every time you connect to a database
3683+
(including program start-up), but can be changed or unset.
3684+
</para>
3685+
</listitem>
3686+
</varlistentry>
3687+
36733688
<varlistentry>
36743689
<term><varname>SHOW_CONTEXT</varname></term>
36753690
<listitem>
@@ -3733,10 +3748,15 @@ bar
37333748

37343749
<varlistentry>
37353750
<term><varname>VERSION</varname></term>
3751+
<term><varname>VERSION_NAME</varname></term>
3752+
<term><varname>VERSION_NUM</varname></term>
37363753
<listitem>
37373754
<para>
3738-
This variable is set at program start-up to
3739-
reflect <application>psql</>'s version. It can be changed or unset.
3755+
These variables are set at program start-up to reflect
3756+
<application>psql</>'s version, respectively as a verbose string,
3757+
a short string (e.g., <literal>9.6.2</>, <literal>10.1</>,
3758+
or <literal>11beta1</>), and a number (e.g., <literal>90602</>
3759+
or <literal>100001</>). They can be changed or unset.
37403760
</para>
37413761
</listitem>
37423762
</varlistentry>

src/bin/psql/command.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,9 @@ checkWin32Codepage(void)
33373337
void
33383338
SyncVariables(void)
33393339
{
3340+
char vbuf[32];
3341+
const char *server_version;
3342+
33403343
/* get stuff from connection */
33413344
pset.encoding = PQclientEncoding(pset.db);
33423345
pset.popt.topt.encoding = pset.encoding;
@@ -3348,6 +3351,20 @@ SyncVariables(void)
33483351
SetVariable(pset.vars, "PORT", PQport(pset.db));
33493352
SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
33503353

3354+
/* this bit should match connection_warnings(): */
3355+
/* Try to get full text form of version, might include "devel" etc */
3356+
server_version = PQparameterStatus(pset.db, "server_version");
3357+
/* Otherwise fall back on pset.sversion */
3358+
if (!server_version)
3359+
{
3360+
formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
3361+
server_version = vbuf;
3362+
}
3363+
SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
3364+
3365+
snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
3366+
SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
3367+
33513368
/* send stuff to it, too */
33523369
PQsetErrorVerbosity(pset.db, pset.verbosity);
33533370
PQsetErrorContextVisibility(pset.db, pset.show_context);
@@ -3366,6 +3383,8 @@ UnsyncVariables(void)
33663383
SetVariable(pset.vars, "HOST", NULL);
33673384
SetVariable(pset.vars, "PORT", NULL);
33683385
SetVariable(pset.vars, "ENCODING", NULL);
3386+
SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
3387+
SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
33693388
}
33703389

33713390

src/bin/psql/help.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ helpVariables(unsigned short int pager)
336336
* Windows builds currently print one more line than non-Windows builds.
337337
* Using the larger number is fine.
338338
*/
339-
output = PageOutput(88, pager ? &(pset.popt.topt) : NULL);
339+
output = PageOutput(93, pager ? &(pset.popt.topt) : NULL);
340340

341341
fprintf(output, _("List of specially treated variables\n\n"));
342342

@@ -368,11 +368,16 @@ helpVariables(unsigned short int pager)
368368
fprintf(output, _(" PROMPT2 specifies the prompt used when a statement continues from a previous line\n"));
369369
fprintf(output, _(" PROMPT3 specifies the prompt used during COPY ... FROM STDIN\n"));
370370
fprintf(output, _(" QUIET run quietly (same as -q option)\n"));
371+
fprintf(output, _(" SERVER_VERSION_NAME server's version (short string)\n"));
372+
fprintf(output, _(" SERVER_VERSION_NUM server's version (numeric format)\n"));
371373
fprintf(output, _(" SHOW_CONTEXT controls display of message context fields [never, errors, always]\n"));
372374
fprintf(output, _(" SINGLELINE end of line terminates SQL command mode (same as -S option)\n"));
373375
fprintf(output, _(" SINGLESTEP single-step mode (same as -s option)\n"));
374376
fprintf(output, _(" USER the currently connected database user\n"));
375377
fprintf(output, _(" VERBOSITY controls verbosity of error reports [default, verbose, terse]\n"));
378+
fprintf(output, _(" VERSION psql's version (verbose string)\n"));
379+
fprintf(output, _(" VERSION_NAME psql's version (short string)\n"));
380+
fprintf(output, _(" VERSION_NUM psql's version (numeric format)\n"));
376381

377382
fprintf(output, _("\nDisplay settings:\n"));
378383
fprintf(output, _("Usage:\n"));

src/bin/psql/startup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ main(int argc, char *argv[])
160160

161161
EstablishVariableSpace();
162162

163+
/* Create variables showing psql version number */
163164
SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
165+
SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
166+
SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
164167

165168
/* Default values for variables (that don't match the result of \unset) */
166169
SetVariableBool(pset.vars, "AUTOCOMMIT");

0 commit comments

Comments
 (0)