Re: Correct documentation for protocol version

From: Dave Cramer <davecramer(at)gmail(dot)com>
To: Fujii Masao <masao(dot)fujii(at)oss(dot)nttdata(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Correct documentation for protocol version
Date: 2025-04-10 20:17:06
Message-ID: CADK3HHKqFu8qPYv2XuDPKGY1OGwszfBVRuJAPSW58q6tYSff6w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 10 Apr 2025 at 12:17, Fujii Masao <masao(dot)fujii(at)oss(dot)nttdata(dot)com>
wrote:

>
>
> On 2025/04/11 0:49, Dave Cramer wrote:
> >
> >
> > On Thu, 10 Apr 2025 at 11:17, Fujii Masao <masao(dot)fujii(at)oss(dot)nttdata(dot)com
> <mailto:masao(dot)fujii(at)oss(dot)nttdata(dot)com>> wrote:
> >
> >
> >
> > On 2025/04/10 23:40, Dave Cramer wrote:
> > >
> > > On Thu, 10 Apr 2025 at 09:54, Fujii Masao <
> masao(dot)fujii(at)oss(dot)nttdata(dot)com <mailto:masao(dot)fujii(at)oss(dot)nttdata(dot)com> <mailto:
> masao(dot)fujii(at)oss(dot)nttdata(dot)com <mailto:masao(dot)fujii(at)oss(dot)nttdata(dot)com>>> wrote:
> > >
> > >
> > >
> > > On 2025/04/10 18:52, Dave Cramer wrote:
> > > > Greetings,
> > > >
> > > > The current docs say that if a client asks for a protocol
> that the backend doesn't support, it will return the newest minor version.
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-NEGOTIATEPROTOCOLVERSION
> <
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-NEGOTIATEPROTOCOLVERSION>
> <
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-NEGOTIATEPROTOCOLVERSION
> <
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-NEGOTIATEPROTOCOLVERSION>>
> <
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-NEGOTIATEPROTOCOLVERSION
> <
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-NEGOTIATEPROTOCOLVERSION>
> <
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-
> > NEGOTIATEPROTOCOLVERSION <
> https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-NEGOTIATEPROTOCOLVERSION
> >>>
> > > >
> > > > However that isn't what it returns. It actually returns
> the entire newest protocol that it supports. Attached is a patch to fix the
> docs.
> > >
> > > As far as I read the code, the server returns the protocol
> version requested by
> > > the client if it's less than or equal to the latest version
> the server supports.
> > > Otherwise, it returns the latest supported version. So the
> proposed description
> > > doesn't seem accurate either, does it?
> > >
> > > I've added a note as to when this is sent. AFAICT through testing
> and the fact that the pgjdbc driver has never handled this message
> > > I'm pretty sure it only sends this message if the requested
> protocol is not equal to the protocol the server supports.
> >
> > No, the message is also sent when the client requests protocol
> options that
> > the server doesn't recognize. In that case, if the client requests
> an older
> > protocol version along with unknown options, the server responds with
> > the requested protocol version, not the latest one.
> >
> >
> > OK, I hadn't contemplated the unrecognized options. However AFAICT the
> documentation in the code state
> >
> > /*
> > * If the client requested a newer protocol version or if the client
> > * requested any protocol options we didn't recognize, let them know
> > * the newest minor protocol version we do support and the names of
> > * any unrecognized options.
> > */
> >
> > and the code agrees that we send the latest protocol version that we
> support, not the requested one.
>
> SendNegotiateProtocolVersion() sends the NegotiateProtocolVersion message
> and
> returns the protocol version stored in the FrontendProtocol variable:
>
> static void
> SendNegotiateProtocolVersion(List *unrecognized_protocol_options)
> {
> StringInfoData buf;
> ListCell *lc;
>
> pq_beginmessage(&buf, PqMsg_NegotiateProtocolVersion);
> pq_sendint32(&buf, FrontendProtocol);
>
> FrontendProtocol is set by ProcessStartupPacket() as Min(proto,
> PG_PROTOCOL_LATEST),
> where proto is the protocol version requested by the client. So, if the
> client
> requests an older version, i.e., proto is smaller than PG_PROTOCOL_LATEST,
> the server may respond with that requested version:
>
> /*
> * Set FrontendProtocol now so that ereport() knows what format to
> send if
> * we fail during startup. We use the protocol version requested
> by the
> * client unless it's higher than the latest version we support.
> It's
> * possible that error message fields might look different in newer
> * protocol versions, but that's something those new clients
> should be
> * able to deal with.
> */
> FrontendProtocol = Min(proto, PG_PROTOCOL_LATEST);
>
> Am I missing something?
>

No, you are correct.

See new patch

Dave

Attachment Content-Type Size
protocol-3.patch application/octet-stream 1.3 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-04-10 20:33:57 Re: Reduce "Var IS [NOT] NULL" quals during constant folding
Previous Message Robert Haas 2025-04-10 20:07:25 Re: Reduce "Var IS [NOT] NULL" quals during constant folding