Skip to content

Commit cdb6b0f

Browse files
committed
Add PQfullProtocolVersion() to surface the precise protocol version.
The existing function PQprotocolVersion() does not include the minor version of the protocol. In preparation for pending work that will bump that number for the first time, add a new function to provide it to clients that may care, using the (major * 10000 + minor) convention already used by PQserverVersion(). Jacob Champion based on earlier work by Jelte Fennema-Nio Discussion: http://postgr.es/m/CAOYmi+mM8+6Swt1k7XsLcichJv8xdhPnuNv7-02zJWsezuDL+g@mail.gmail.com
1 parent 5bbdfa8 commit cdb6b0f

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,22 +2678,44 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
26782678
</listitem>
26792679
</varlistentry>
26802680

2681+
<varlistentry id="libpq-PQfullProtocolVersion">
2682+
<term><function>PQfullProtocolVersion</function><indexterm><primary>PQfullProtocolVersion</primary></indexterm></term>
2683+
2684+
<listitem>
2685+
<para>
2686+
Interrogates the frontend/backend protocol being used.
2687+
<synopsis>
2688+
int PQfullProtocolVersion(const PGconn *conn);
2689+
</synopsis>
2690+
Applications might wish to use this function to determine whether certain
2691+
features are supported. The result is formed by multiplying the server's
2692+
major version number by 10000 and adding the minor version number. For
2693+
example, version 3.2 would be returned as 30002, and version 4.0 would
2694+
be returned as 40000. Zero is returned if the connection is bad. The 3.0
2695+
protocol is supported by <productname>PostgreSQL</productname> server
2696+
versions 7.4 and above.
2697+
</para>
2698+
<para>
2699+
The protocol version will not change after connection startup is
2700+
complete, but it could theoretically change during a connection reset.
2701+
</para>
2702+
</listitem>
2703+
</varlistentry>
2704+
26812705
<varlistentry id="libpq-PQprotocolVersion">
26822706
<term><function>PQprotocolVersion</function><indexterm><primary>PQprotocolVersion</primary></indexterm></term>
26832707

26842708
<listitem>
26852709
<para>
2686-
Interrogates the frontend/backend protocol being used.
2710+
Interrogates the frontend/backend protocol major version.
26872711
<synopsis>
26882712
int PQprotocolVersion(const PGconn *conn);
26892713
</synopsis>
2690-
Applications might wish to use this function to determine whether certain
2691-
features are supported. Currently, the possible values are 3
2692-
(3.0 protocol), or zero (connection bad). The protocol version will
2693-
not change after connection startup is complete, but it could
2694-
theoretically change during a connection reset. The 3.0 protocol is
2695-
supported by <productname>PostgreSQL</productname> server versions 7.4
2696-
and above.
2714+
Unlike <xref linkend="libpq-PQfullProtocolVersion"/>, this returns only
2715+
the major protocol version in use, but it is supported by a wider range
2716+
of libpq releases back to version 7.4. Currently, the possible values are
2717+
3 (3.0 protocol), or zero (connection bad). Prior to release version
2718+
14.0, libpq could additionally return 2 (2.0 protocol).
26972719
</para>
26982720
</listitem>
26992721
</varlistentry>

src/include/libpq/pqcomm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ is_unixsock_path(const char *path)
8686

8787
#define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
8888
#define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
89+
#define PG_PROTOCOL_FULL(v) (PG_PROTOCOL_MAJOR(v) * 10000 + PG_PROTOCOL_MINOR(v))
8990
#define PG_PROTOCOL(m,n) (((m) << 16) | (n))
9091

9192
/*

src/interfaces/libpq/fe-connect.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7158,6 +7158,16 @@ PQprotocolVersion(const PGconn *conn)
71587158
return PG_PROTOCOL_MAJOR(conn->pversion);
71597159
}
71607160

7161+
int
7162+
PQfullProtocolVersion(const PGconn *conn)
7163+
{
7164+
if (!conn)
7165+
return 0;
7166+
if (conn->status == CONNECTION_BAD)
7167+
return 0;
7168+
return PG_PROTOCOL_FULL(conn->pversion);
7169+
}
7170+
71617171
int
71627172
PQserverVersion(const PGconn *conn)
71637173
{

src/interfaces/libpq/libpq-fe.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ extern "C"
5656
/* Indicates presence of PQsocketPoll, PQgetCurrentTimeUSec */
5757
#define LIBPQ_HAS_SOCKET_POLL 1
5858

59+
/* Features added in PostgreSQL v18: */
60+
/* Indicates presence of PQfullProtocolVersion */
61+
#define LIBPQ_HAS_FULL_PROTOCOL_VERSION 1
62+
5963
/*
6064
* Option flags for PQcopyResult
6165
*/
@@ -393,6 +397,7 @@ extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
393397
extern const char *PQparameterStatus(const PGconn *conn,
394398
const char *paramName);
395399
extern int PQprotocolVersion(const PGconn *conn);
400+
extern int PQfullProtocolVersion(const PGconn *conn);
396401
extern int PQserverVersion(const PGconn *conn);
397402
extern char *PQerrorMessage(const PGconn *conn);
398403
extern int PQsocket(const PGconn *conn);

0 commit comments

Comments
 (0)