v9-0005-libpq-Include-minor-version-number-in-PQprotocolV.patch

application/octet-stream

Filename: v9-0005-libpq-Include-minor-version-number-in-PQprotocolV.patch
Type: application/octet-stream
Part: 0
Message: Re: Add new protocol message to change GUCs for usage with future protocol-only GUCs

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v9-0005
Subject: libpq: Include minor version number in PQprotocolVersion
File+
doc/src/sgml/libpq.sgml 6 6
src/include/libpq/pqcomm.h 1 0
src/interfaces/libpq/fe-connect.c 3 1
From 2e8222cf10876f327380895f5e8aeb0bb82909d4 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <jelte.fennema@microsoft.com>
Date: Tue, 2 Jan 2024 12:07:35 +0100
Subject: [PATCH v9 05/13] 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 f939274249c..92161413eb2 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -2563,12 +2563,12 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
 int PQprotocolVersion(const PGconn *conn);
 </synopsis>
        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 <productname>PostgreSQL</productname> 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.
       </para>
      </listitem>
     </varlistentry>
diff --git a/src/include/libpq/pqcomm.h b/src/include/libpq/pqcomm.h
index 9ae469c86c4..9703c6e9a45 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 7075134d155..aca78a683fe 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6856,7 +6856,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