diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index d360fc4d58..924766fce7 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7956,8 +7956,22 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
- Reports the version number of the server as an integer. It is determined
- by the value of PG_VERSION_NUM when building the server.
+ Reports the version number of the server as a short string. It is determined
+ by the value of PG_VERSION when building the server.
+
+
+
+
+
+ server_version_raw (string)
+
+ server_version_raw configuration parameter
+
+
+
+
+ Reports the version of the server as a long string. It is determined
+ by the value of PG_VERSION_STR when building the server.
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index e520cdf3ba..50d6f0a8fc 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -3770,11 +3770,14 @@ bar
+ SERVER_VERSION
SERVER_VERSION_NAME
SERVER_VERSION_NUM
- The server's version number as a string, for
+ The server's version number as a long string, for
+ example PostgreSQL 11devel ...,
+ as a short string, for
example 9.6.2, 10.1 or 11beta1,
and in numeric form, for
example 90602 or 100001.
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index c4c1afa084..49ff61246f 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -500,6 +500,7 @@ static char *locale_collate;
static char *locale_ctype;
static char *server_encoding_string;
static char *server_version_string;
+static char *server_version_raw_string;
static int server_version_num;
static char *timezone_string;
static char *log_timezone_string;
@@ -3295,6 +3296,18 @@ static struct config_string ConfigureNamesString[] =
NULL, NULL, NULL
},
+ {
+ /* Can't be set in postgresql.conf */
+ {"server_version_raw", PGC_INTERNAL, PRESET_OPTIONS,
+ gettext_noop("Shows the server version string."),
+ NULL,
+ GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+ },
+ &server_version_raw_string,
+ PG_VERSION_STR,
+ NULL, NULL, NULL
+ },
+
{
/* Not for general use --- used by SET ROLE */
{"role", PGC_USERSET, UNGROUPED,
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 8cc4de3878..cfac89c8da 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3210,7 +3210,8 @@ void
SyncVariables(void)
{
char vbuf[32];
- const char *server_version;
+ const char *server_version,
+ *server_version_raw;
/* get stuff from connection */
pset.encoding = PQclientEncoding(pset.db);
@@ -3237,6 +3238,17 @@ SyncVariables(void)
snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
+ server_version_raw = PQparameterStatus(pset.db, "server_version_raw");
+ /* fall back again */
+ if (!server_version_raw)
+ {
+ snprintf(vbuf, sizeof(vbuf), "PostgreSQL ");
+ formatPGVersionNumber(pset.sversion, true, vbuf + strlen(vbuf),
+ sizeof(vbuf) - strlen(vbuf));
+ server_version_raw = vbuf;
+ }
+ SetVariable(pset.vars, "SERVER_VERSION", server_version_raw);
+
/* send stuff to it, too */
PQsetErrorVerbosity(pset.db, pset.verbosity);
PQsetErrorContextVisibility(pset.db, pset.show_context);
@@ -3255,6 +3267,7 @@ UnsyncVariables(void)
SetVariable(pset.vars, "HOST", NULL);
SetVariable(pset.vars, "PORT", NULL);
SetVariable(pset.vars, "ENCODING", NULL);
+ SetVariable(pset.vars, "SERVER_VERSION", NULL);
SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
}
diff --git a/src/interfaces/libpq/fe-protocol2.c b/src/interfaces/libpq/fe-protocol2.c
index 5335a91440..0418779f79 100644
--- a/src/interfaces/libpq/fe-protocol2.c
+++ b/src/interfaces/libpq/fe-protocol2.c
@@ -280,6 +280,10 @@ pqSetenvPoll(PGconn *conn)
{
char *ptr;
+ /* keep returned value */
+ pqSaveParameterStatus(conn, "server_version_raw",
+ val);
+
/* strip off PostgreSQL part */
val += 11;
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index 43ac5f5f11..eabb990d4e 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -767,3 +767,14 @@ NOTICE: text search configuration "no_such_config" does not exist
select func_with_bad_set();
ERROR: invalid value for parameter "default_text_search_config": "no_such_config"
reset check_function_bodies;
+-- check consistency of SERVER_VERSION
+-- which is transmitted as GUC "server_version_raw"
+SELECT :'SERVER_VERSION' = VERSION()
+ AND :'SERVER_VERSION' = current_setting('server_version_raw')
+ AND :'SERVER_VERSION' = :'VERSION'
+ AS "SERVER_VERSION is consistent";
+ SERVER_VERSION is consistent
+------------------------------
+ t
+(1 row)
+
diff --git a/src/test/regress/sql/guc.sql b/src/test/regress/sql/guc.sql
index 23e5029780..af2e353da0 100644
--- a/src/test/regress/sql/guc.sql
+++ b/src/test/regress/sql/guc.sql
@@ -288,3 +288,10 @@ set default_text_search_config = no_such_config;
select func_with_bad_set();
reset check_function_bodies;
+
+-- check consistency of SERVER_VERSION
+-- which is transmitted as GUC "server_version_raw"
+SELECT :'SERVER_VERSION' = VERSION()
+ AND :'SERVER_VERSION' = current_setting('server_version_raw')
+ AND :'SERVER_VERSION' = :'VERSION'
+ AS "SERVER_VERSION is consistent";