1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-24 10:47:04 +03:00

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
This commit is contained in:
Robert Haas 2024-09-09 11:54:55 -04:00
parent 5bbdfa8a18
commit cdb6b0fdb0
4 changed files with 47 additions and 9 deletions

View File

@ -2678,22 +2678,44 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry id="libpq-PQprotocolVersion"> <varlistentry id="libpq-PQfullProtocolVersion">
<term><function>PQprotocolVersion</function><indexterm><primary>PQprotocolVersion</primary></indexterm></term> <term><function>PQfullProtocolVersion</function><indexterm><primary>PQfullProtocolVersion</primary></indexterm></term>
<listitem> <listitem>
<para> <para>
Interrogates the frontend/backend protocol being used. Interrogates the frontend/backend protocol being used.
<synopsis> <synopsis>
int PQprotocolVersion(const PGconn *conn); int PQfullProtocolVersion(const PGconn *conn);
</synopsis> </synopsis>
Applications might wish to use this function to determine whether certain Applications might wish to use this function to determine whether certain
features are supported. Currently, the possible values are 3 features are supported. The result is formed by multiplying the server's
(3.0 protocol), or zero (connection bad). The protocol version will major version number by 10000 and adding the minor version number. For
not change after connection startup is complete, but it could example, version 3.2 would be returned as 30002, and version 4.0 would
theoretically change during a connection reset. The 3.0 protocol is be returned as 40000. Zero is returned if the connection is bad. The 3.0
supported by <productname>PostgreSQL</productname> server versions 7.4 protocol is supported by <productname>PostgreSQL</productname> server
and above. versions 7.4 and above.
</para>
<para>
The protocol version will not change after connection startup is
complete, but it could theoretically change during a connection reset.
</para>
</listitem>
</varlistentry>
<varlistentry id="libpq-PQprotocolVersion">
<term><function>PQprotocolVersion</function><indexterm><primary>PQprotocolVersion</primary></indexterm></term>
<listitem>
<para>
Interrogates the frontend/backend protocol major version.
<synopsis>
int PQprotocolVersion(const PGconn *conn);
</synopsis>
Unlike <xref linkend="libpq-PQfullProtocolVersion"/>, this returns only
the major protocol version in use, but it is supported by a wider range
of libpq releases back to version 7.4. Currently, the possible values are
3 (3.0 protocol), or zero (connection bad). Prior to release version
14.0, libpq could additionally return 2 (2.0 protocol).
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View File

@ -86,6 +86,7 @@ is_unixsock_path(const char *path)
#define PG_PROTOCOL_MAJOR(v) ((v) >> 16) #define PG_PROTOCOL_MAJOR(v) ((v) >> 16)
#define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff) #define PG_PROTOCOL_MINOR(v) ((v) & 0x0000ffff)
#define PG_PROTOCOL_FULL(v) (PG_PROTOCOL_MAJOR(v) * 10000 + PG_PROTOCOL_MINOR(v))
#define PG_PROTOCOL(m,n) (((m) << 16) | (n)) #define PG_PROTOCOL(m,n) (((m) << 16) | (n))
/* /*

View File

@ -7158,6 +7158,16 @@ PQprotocolVersion(const PGconn *conn)
return PG_PROTOCOL_MAJOR(conn->pversion); return PG_PROTOCOL_MAJOR(conn->pversion);
} }
int
PQfullProtocolVersion(const PGconn *conn)
{
if (!conn)
return 0;
if (conn->status == CONNECTION_BAD)
return 0;
return PG_PROTOCOL_FULL(conn->pversion);
}
int int
PQserverVersion(const PGconn *conn) PQserverVersion(const PGconn *conn)
{ {

View File

@ -56,6 +56,10 @@ extern "C"
/* Indicates presence of PQsocketPoll, PQgetCurrentTimeUSec */ /* Indicates presence of PQsocketPoll, PQgetCurrentTimeUSec */
#define LIBPQ_HAS_SOCKET_POLL 1 #define LIBPQ_HAS_SOCKET_POLL 1
/* Features added in PostgreSQL v18: */
/* Indicates presence of PQfullProtocolVersion */
#define LIBPQ_HAS_FULL_PROTOCOL_VERSION 1
/* /*
* Option flags for PQcopyResult * Option flags for PQcopyResult
*/ */
@ -393,6 +397,7 @@ extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
extern const char *PQparameterStatus(const PGconn *conn, extern const char *PQparameterStatus(const PGconn *conn,
const char *paramName); const char *paramName);
extern int PQprotocolVersion(const PGconn *conn); extern int PQprotocolVersion(const PGconn *conn);
extern int PQfullProtocolVersion(const PGconn *conn);
extern int PQserverVersion(const PGconn *conn); extern int PQserverVersion(const PGconn *conn);
extern char *PQerrorMessage(const PGconn *conn); extern char *PQerrorMessage(const PGconn *conn);
extern int PQsocket(const PGconn *conn); extern int PQsocket(const PGconn *conn);