From 74f70d3d6f9c8cb4163c9867dcd5544520948e8c Mon Sep 17 00:00:00 2001 From: Jelte Fennema-Nio Date: Sun, 2 Jun 2024 00:49:43 -0700 Subject: [PATCH v13 2/9] Do not hardcode sending PG_PROTOCOL_LATEST in NegotiateProtocolVersion To be able to update the minor protocol version in a future version this changes the way we build NegotiateProtocolVersion to set the correct version. The previous code would always tell the client to use the latest version that the server supported. Currently that works fine in practice, because the latest version is the only version. However if we introduce a new protocol version then a client might ask for a lower version than the latest version that we support, but at the same tiem ask for a protocol parameter that we do not support. In that case we would send back a higher protocol version in the NegotiateProtocolVersion message than the one that the client asked for. This fixes that, by sending back the version the client asked for in that case. It also simplifies the check by comparing the full version instead of the minor version only. We already check earlier that the major version is correct. Finally this also sets FrontendProtocol to the negotiated minor version. --- src/backend/tcop/backend_startup.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/backend/tcop/backend_startup.c b/src/backend/tcop/backend_startup.c index cfa27551964..cbb3342a821 100644 --- a/src/backend/tcop/backend_startup.c +++ b/src/backend/tcop/backend_startup.c @@ -684,6 +684,9 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done) PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST), PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)))); + if (proto > PG_PROTOCOL_LATEST) + FrontendProtocol = PG_PROTOCOL_LATEST; + /* * Now fetch parameters out of startup packet and save them into the Port * structure. @@ -790,8 +793,7 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done) * the newest minor protocol version we do support and the names of * any unrecognized options. */ - if (PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST) || - unrecognized_protocol_options != NIL) + if (proto > PG_PROTOCOL_LATEST || unrecognized_protocol_options != NIL) SendNegotiateProtocolVersion(unrecognized_protocol_options); } @@ -849,7 +851,7 @@ SendNegotiateProtocolVersion(List *unrecognized_protocol_options) ListCell *lc; pq_beginmessage(&buf, PqMsg_NegotiateProtocolVersion); - pq_sendint32(&buf, PG_PROTOCOL_LATEST); + pq_sendint32(&buf, FrontendProtocol); pq_sendint32(&buf, list_length(unrecognized_protocol_options)); foreach(lc, unrecognized_protocol_options) pq_sendstring(&buf, lfirst(lc)); -- 2.34.1