From d715e6b3b5451bde1f0e94c159d121e01f9491ee Mon Sep 17 00:00:00 2001 From: Jelte Fennema-Nio Date: Tue, 2 Jan 2024 12:07:35 +0100 Subject: [PATCH v3 2/4] 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 we never had any protocol versions 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 2e9ae41e389..72b66295f69 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -2566,12 +2566,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.1 will be returned as 30001, and version 4.0 will + 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 46a0946b8b2..999ab94b9c8 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 14214cac62d..aa01443cf03 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -7263,7 +7263,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); } const char ** -- 2.34.1