Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 516b875

Browse files
committed
Do not hardcode PG_PROTOCOL_LATEST in NegotiateProtocolVersion
We shouldn't ask the client to use a protocol version later than the one that they requested. To avoid that, if the client requests a version newer than the latest one we support, set FrontendProtocol to the latest version we support, not the requested version. Then, use that value when building the NegotiateProtocolVersion message. (It seems good on general principle to avoid setting FrontendProtocol to a version we don't support, anyway.) None of this really matters right now, because we only support a single protocol version, but if that ever changes, we'll need this. Jelte Fennema-Nio, reviewed by me and incorporating some of my proposed wording Discussion: https://postgr.es/m/CAGECzQTyXDNtMXdq2L-Wp=OvOCPa07r6+U_MGb==h90MrfT+fQ@mail.gmail.com
1 parent 8dc28d7 commit 516b875

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/backend/tcop/backend_startup.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,13 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
690690

691691
/*
692692
* Set FrontendProtocol now so that ereport() knows what format to send if
693-
* we fail during startup.
693+
* we fail during startup. We use the protocol version requested by the
694+
* client unless it's higher than the latest version we support. It's
695+
* possible that error message fields might look different in newer
696+
* protocol versions, but that's something those new clients should be
697+
* able to deal with.
694698
*/
695-
FrontendProtocol = proto;
699+
FrontendProtocol = Min(proto, PG_PROTOCOL_LATEST);
696700

697701
/* Check that the major protocol version is in range. */
698702
if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
@@ -852,9 +856,12 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
852856

853857
/*
854858
* Send a NegotiateProtocolVersion to the client. This lets the client know
855-
* that they have requested a newer minor protocol version than we are able
856-
* to speak. We'll speak the highest version we know about; the client can,
857-
* of course, abandon the connection if that's a problem.
859+
* that they have either requested a newer minor protocol version than we are
860+
* able to speak, or at least one protocol option that we don't understand, or
861+
* possibly both. FrontendProtocol has already been set to the version
862+
* requested by the client or the highest version we know how to speak,
863+
* whichever is older. If the highest version that we know how to speak is too
864+
* old for the client, it can abandon the connection.
858865
*
859866
* We also include in the response a list of protocol options we didn't
860867
* understand. This allows clients to include optional parameters that might
@@ -870,7 +877,7 @@ SendNegotiateProtocolVersion(List *unrecognized_protocol_options)
870877
ListCell *lc;
871878

872879
pq_beginmessage(&buf, PqMsg_NegotiateProtocolVersion);
873-
pq_sendint32(&buf, PG_PROTOCOL_LATEST);
880+
pq_sendint32(&buf, FrontendProtocol);
874881
pq_sendint32(&buf, list_length(unrecognized_protocol_options));
875882
foreach(lc, unrecognized_protocol_options)
876883
pq_sendstring(&buf, lfirst(lc));

0 commit comments

Comments
 (0)