From 8fd91054c39308e0360a3889564905acb4b70ffb Mon Sep 17 00:00:00 2001 From: Jelte Fennema-Nio Date: Tue, 2 Jan 2024 12:07:35 +0100 Subject: [PATCH v14 3/9] libpq: Include minor version number in PQprotocolVersion The return value from PQprotocolVersion so far only returned the major version number. This wasn't an issue practice before because the minor version was always zero. But it has become an issue now because we want to bump the minor protocol version in a future commit. This commit changes the return value to have the same format as PQserverVersion, i.e. we multiply the major version by 10000 and add the minor version. To make sure we return the same value for each protocol version across libpq versions we only do this calculation for protocol versions above 3.0. For protocol version 3.0 we continue returning a plain 3. --- doc/src/sgml/libpq.sgml | 12 ++++++------ src/include/libpq/pqcomm.h | 1 + src/interfaces/libpq/fe-connect.c | 4 +++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 068ee60771c..a609a8cff58 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -2686,12 +2686,12 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName); int PQprotocolVersion(const PGconn *conn); Applications might wish to use this function to determine whether certain - features are supported. Currently, the possible values are 3 - (3.0 protocol), or zero (connection bad). The protocol version will - not change after connection startup is complete, but it could - theoretically change during a connection reset. The 3.0 protocol is - supported by PostgreSQL server versions 7.4 - and above. + features are supported. The result is formed by multiplying the server's + major version number by 10000 and adding the minor version number. For + example, version 3.2 would be returned as 30002, and version 4.0 would + be returned as 40000. The exception to this rule is protocol version + 3.0, which will be returned as the value 3 for backwards compatibility + reasons. Zero is returned if the connection is bad. diff --git a/src/include/libpq/pqcomm.h b/src/include/libpq/pqcomm.h index 527735e3dba..6925f10602b 100644 --- a/src/include/libpq/pqcomm.h +++ b/src/include/libpq/pqcomm.h @@ -86,6 +86,7 @@ is_unixsock_path(const char *path) #define PG_PROTOCOL_MAJOR(v) ((v) >> 16) #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)) /* diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 071b1b34aa1..816dde114e4 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -7137,7 +7137,9 @@ PQprotocolVersion(const PGconn *conn) return 0; if (conn->status == CONNECTION_BAD) return 0; - return PG_PROTOCOL_MAJOR(conn->pversion); + if (conn->pversion == PG_PROTOCOL(3, 0)) + return 3; + return PG_PROTOCOL_FULL(conn->pversion); } int -- 2.34.1