diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index be2f54c914..47b59e1412 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -23206,6 +23206,20 @@ SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n);
+
+
+
+ pg_servername
+
+ pg_servername ()
+ name
+
+
+ Returns the server system hostname, more precisely, the hostname
+ associated with the server calling process's UTS namespace.
+
+
+
diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c
index 640fc37dc8..25e94cd057 100644
--- a/src/backend/utils/adt/network.c
+++ b/src/backend/utils/adt/network.c
@@ -11,6 +11,7 @@
#include
#include
#include
+#include
#include "access/stratnum.h"
#include "catalog/pg_opfamily.h"
@@ -2102,3 +2103,18 @@ clean_ipv6_addr(int addr_family, char *addr)
*pct = '\0';
}
}
+
+/* pg_servername will return the server hostname in the current UTS */
+Datum
+pg_servername(PG_FUNCTION_ARGS)
+{
+ char servername[_SC_HOST_NAME_MAX + 1];
+ int ret;
+
+ ret = gethostname(servername, sizeof(servername));
+
+ if (ret != 0)
+ PG_RETURN_NULL();
+
+ PG_RETURN_DATUM(cstring_to_text(servername));
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 6996073989..e1572dc86c 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -4245,6 +4245,11 @@
proname => 'inet_server_port', proisstrict => 'f', provolatile => 's',
proparallel => 'r', prorettype => 'int4', proargtypes => '',
prosrc => 'inet_server_port' },
+{ oid => '2173', descr => 'returns the server system hostname',
+ proname => 'pg_servername', proisstrict => 'f', provolatile => 's',
+ proparallel => 'r', prorettype => 'text', proargtypes => '',
+ prosrc => 'pg_servername' },
+
{ oid => '2627',
proname => 'inetnot', prorettype => 'inet', proargtypes => 'inet',