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

Commit 9ae9d8c

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

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

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

+22-2
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

+19
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

+8-1
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(140, pager ? &(pset.popt.topt) : NULL);
339+
output = PageOutput(147, pager ? &(pset.popt.topt) : NULL);
340340

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

@@ -387,6 +387,9 @@ helpVariables(unsigned short int pager)
387387
" specifies the prompt used during COPY ... FROM STDIN\n"));
388388
fprintf(output, _(" QUIET\n"
389389
" run quietly (same as -q option)\n"));
390+
fprintf(output, _(" SERVER_VERSION_NAME\n"
391+
" SERVER_VERSION_NUM\n"
392+
" server's version (in short string or numeric format)\n"));
390393
fprintf(output, _(" SHOW_CONTEXT\n"
391394
" controls display of message context fields [never, errors, always]\n"));
392395
fprintf(output, _(" SINGLELINE\n"
@@ -397,6 +400,10 @@ helpVariables(unsigned short int pager)
397400
" the currently connected database user\n"));
398401
fprintf(output, _(" VERBOSITY\n"
399402
" controls verbosity of error reports [default, verbose, terse]\n"));
403+
fprintf(output, _(" VERSION\n"
404+
" VERSION_NAME\n"
405+
" VERSION_NUM\n"
406+
" psql's version (in verbose string, short string, or numeric format)\n"));
400407

401408
fprintf(output, _("\nDisplay settings:\n"));
402409
fprintf(output, _("Usage:\n"));

src/bin/psql/startup.c

+3
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)