From de9a4c27fefcc0d104bc9c97f4a93a49a25bf66d Mon Sep 17 00:00:00 2001
From: Magnus Hagander <magnus@hagander.net>
Date: Wed, 22 Dec 2010 14:23:56 +0100
Subject: [PATCH] Add PQlibVersion() function to libpq

This function is like the PQserverVersion() function except
it returns the version of libpq, making it possible for a client
program or driver to determine which version of libpq is in
use at runtime, and not just at link time.

Suggested by Harald Armin Massa and several others.
---
 doc/src/sgml/libpq.sgml          | 44 ++++++++++++++++++++++++++++++++
 src/interfaces/libpq/exports.txt |  1 +
 src/interfaces/libpq/fe-misc.c   |  8 ++++++
 src/interfaces/libpq/libpq-fe.h  |  3 +++
 4 files changed, 56 insertions(+)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index c502439356..7aac6f6829 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -5359,6 +5359,50 @@ void *PQresultAlloc(PGresult *res, size_t nBytes);
     </listitem>
    </varlistentry>
 
+   <varlistentry id="libpq-pqlibversion">
+    <term>
+     <function>PQlibVersion</function>
+     <indexterm>
+      <primary>PQlibVersion</primary>
+      <seealso>PQserverVersion</seealso>
+     </indexterm>
+    </term>
+
+    <listitem>
+     <para>
+      Return the version of <productname>libpq</> that is being used.
+<synopsis>
+int PQlibVersion(void);
+</synopsis>
+     </para>
+
+     <para>
+      The result of this function can be used to determine, at
+      runtime, if specific functionality is available in the currently
+      loaded version of libpq. The function can be used, for example,
+      to determine which connection options are available for
+      <function>PQconnectdb</> or if the <literal>hex</> <type>bytea</>
+      output added in PostgreSQL 9.0 is supported.
+     </para>
+
+     <para>
+      The number is formed by converting the major, minor, and revision
+      numbers into two-decimal-digit numbers and appending them together.
+      For example, version 9.1 will be returned as 90100, and version
+      9.1.2 will be returned as 90102 (leading zeroes are not shown).
+     </para>
+
+     <note>
+      <para>
+       This function appeared in <productname>PostgreSQL</> version 9.1, so
+       it cannot be used to detect required functionality in earlier
+       versions, since linking to it will create a link dependency
+       on version 9.1.
+      </para>
+     </note>
+    </listitem>
+   </varlistentry>
+
   </variablelist>
 
  </sect1>
diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt
index a6c73af52e..1af8df699e 100644
--- a/src/interfaces/libpq/exports.txt
+++ b/src/interfaces/libpq/exports.txt
@@ -159,3 +159,4 @@ PQconnectdbParams         156
 PQconnectStartParams      157
 PQping                    158
 PQpingParams              159
+PQlibVersion              160
diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c
index 24ab7cf97a..999f60b0d4 100644
--- a/src/interfaces/libpq/fe-misc.c
+++ b/src/interfaces/libpq/fe-misc.c
@@ -66,6 +66,14 @@ static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
 			  time_t end_time);
 static int	pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
 
+/*
+ * PQlibVersion: return the libpq version number
+ */
+int
+PQlibVersion(void)
+{
+	return PG_VERSION_NUM;
+}
 
 /*
  * fputnbytes: print exactly N bytes to a file
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 271afedb7b..6075ea8151 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -547,6 +547,9 @@ extern int	lo_export(PGconn *conn, Oid lobjId, const char *filename);
 
 /* === in fe-misc.c === */
 
+/* Get the version of the libpq library in use */
+extern int	PQlibVersion(void);
+
 /* Determine length of multibyte encoded char at *s */
 extern int	PQmblen(const char *s, int encoding);