Re: pgsql: Make cancel request keys longer

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Jacob Champion <jacob(dot)champion(at)enterprisedb(dot)com>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: pgsql: Make cancel request keys longer
Date: 2025-05-09 06:37:58
Message-ID: a92c439b-6f6d-4460-b5ec-1aacd80a866b@iki.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

On 09/05/2025 01:28, Jacob Champion wrote:
> On Thu, May 8, 2025 at 12:11 PM Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:
>> Polished this up a tiny bit, and committed.
>
> Thanks! I think the uint8->int change for cancel_key_len is more than
> just cosmetic; it most likely fixes a bug where a key size of 256
> wrapped around to 0. I'll double-check that this fixes that later;
> I've gotten side-tracked from the protocol stuff a bit.

True, although I'm pretty sure you'd fail the later cross-check that the
whole message was consumed. ("message contents do not agree with length
in message type"). But it's fixed now in any case.

> While I have you, though, is the following just a really complicated
> way to say `msgLength - 4`, or is there some other reason to do the
> pointer math?
>
> cancel_key_len = 5 + msgLength - (conn->inCursor - conn->inStart);

Yes, it amounts to 'msgLength - 4'. I agree it looks pretty obscure. The
way to read it is:

/* full length of the message, including the type code byte and the
length field itself */
fullMsgLength = 5 + msgLength;

/* number of bytes consumed from the message so far */
lengthConsumed = (conn->inCursor - conn->inStart);

/* the cancel key consumes all the remaining bytes of the message */
cancel_key_len = fullMsgLength - lengthConsumed;

It didn't occur to me that you could write it simply as 'msgLength - 4'.
That depends on knowing that the preceding fields are exactly 4 bytes
long, but that's clear enough if we just add a comment on that, see
attached.

--
Heikki Linnakangas
Neon (https://neon.tech)

Attachment Content-Type Size
simplify-cancel_key_len-math.patch text/x-patch 632 bytes

In response to

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2025-05-09 07:05:46 pgsql: doc: Put new options in consistent order on man pages
Previous Message Jacob Champion 2025-05-08 22:28:24 Re: pgsql: Make cancel request keys longer

Browse pgsql-hackers by date

  From Date Subject
Next Message Junwang Zhao 2025-05-09 06:48:35 Re: queryId constant squashing does not support prepared statements
Previous Message Richard Guo 2025-05-09 05:54:37 Re: Assert failure in base_yyparse