Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wfJZw-005O6i-11 for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jul 2026 15:43:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wfJZv-002A2A-0V for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jul 2026 15:43:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wfJZu-002A22-2p for pgsql-bugs@lists.postgresql.org; Thu, 02 Jul 2026 15:43:50 +0000 Received: from mail-yw1-x1135.google.com ([2607:f8b0:4864:20::1135]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wfJZs-00000001NlN-3bpx for pgsql-bugs@postgresql.org; Thu, 02 Jul 2026 15:43:50 +0000 Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-80cbb0688c8so27038607b3.1 for ; Thu, 02 Jul 2026 08:43:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1783007022; cv=none; d=google.com; s=arc-20260327; b=L8iGykMecV0jrd4XKUDghwCmu3/0ncgjvCksG21IuLmLKS/fU1rT647EBqijwMeyx0 TSdRGL0tTA8BLSCfxPJ+WPJDsPsK0nqVYq5pZrUPhN4znIGldIDcd6+tHq15Ptq+CHPS RnD01n/gTpvx7IzAHgOGxjP/BViMtHOQARnHQP4qHAchmlT+ZhNZI+VwTnHhh5feLR/u Ahsu6l3JvbMbZW/Zu/L8jcDDiIOSTZnL90dMOw8cXaaEnJSQcZgT4ZzZf/LWb/Xr99Ob erwz8bbJ/ufc4UGk4PvzwAvJTgbzCpWii1euTxVPjEU/R5fYdmPRWQLcIKy6fvBn2VXO WaXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=eUuNCCN2xPM01qaH4lWA5WWugxn/Dw69ZTyqEXDmkyo=; fh=riYVluA0IGzw0g1WjyAJCbyOaWCpLC7ox/fnf/MnA8g=; b=RFNdJe44LZgccmtvj4bbDNGSw4sn70zGCUT6ZjQux6mWyGgNluki5EADtFitsjlixx JUL3qy2zZLa+juPcpOgQ8SJXWvMjdDbj4Ck6qX5vnwElsBJtpo4dPiPhnfpMyOFcgAE0 A5ZzyO3yBv8LrKj9XiBoSU2swfuDZKfTRPzw5NB/ZKca2yKAPFQ1sklOWe8ryKv7/juv oyJfC5AN0lqzwWeEwenz/Ateotu8QzRKqtspBWWyVHK5jlTbG7ZYkkwaVBhRBeNn2FB3 7x35t6IxS6abOoRMDrp2JgUjPtIGvxXm+xHMJV5oifw5DzCZODIDsQgxl8NRZOx3kPxg +vBg==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783007022; x=1783611822; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=eUuNCCN2xPM01qaH4lWA5WWugxn/Dw69ZTyqEXDmkyo=; b=oexBnUocg61xyfNXBgkZRqeOmWOnFIBHjvSIIUjalKcrSVH0/DPbRqb0Hk3NNArEec oNDtce8CBp/wReZaLWoovfTTBZdWkJtMWg0YcrXMVXsPsYhBX7qlZb02XeDkMT5rxnbn iMvm+tikOk1eAE8+lLv2vsATQRl4jMD49Vl2fmeacYKutQ2d0HY/Ah8s9fwExtWNi30F GslQwTMI1h/79o4xx6klZsp4NAw34yKdEiXv4Hd3ddqy1F0sQKmqLBPSterMqtR7xyGS olFtEqzf7MQQ+tVJnUUObXmRgKCidemWOx1tipK/dkw9XyFPZsGvYk6ESa1rRRkjW0Ty dKFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783007022; x=1783611822; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=eUuNCCN2xPM01qaH4lWA5WWugxn/Dw69ZTyqEXDmkyo=; b=gbXBVWA279/GNhzV3b2pxX3b2GvkY6gmN1CqdVHIDygY0QfmRrotw3SBg9TIjzkCgc zQ4z4PlbGSyAGpsidk3BbOHO0ap7Bd33+YwWONqrGA2b7xFi9bbHE0ywtmFt6717FYs2 R+w3bE3aOxNxzjOa4nQzbQLES44jXFpqqu30K631/vbpVvYTo4sZMobYX1hOF/kkK6bD LnTmzrvXprY33sEkDHw4I/3cCtdhj7LdxX5N2lJEpIqyEwQGrZpmzYV1IbgNyRXKoQiG vWJgIPEHixVGpWLmW9L5d4YWm8BXpTIUB4UWW07C+d08GMpWhfxMGzvsa6uqg/NGUXu6 aYMA== X-Forwarded-Encrypted: i=1; AHgh+RrtURlLr05J53BYAPIp+fCdLhWef+3+XiCAIvhyPQ9uvvo9FWAyOt5324MscPKLuPEr5sVKO2g+KeXz@postgresql.org X-Gm-Message-State: AOJu0Yy1qDlwWHLQ3v/oG0c862tCouJsnMi/YbW6g8TNNIPUURqvBdZr koDs0w0aPJ71SUYejaMBH7xOXPgJc68tzFDM3ioalDKQTMG66DljoaasBMvQx//oq9bsyulBrIo QKUuX2BMi3A3tqI0ngXt5kwmvZ607+2s= X-Gm-Gg: AfdE7cllhTBud1nqsibssHuOpA3wC5J8aKmCmaz8UoxgdxiC2u11/Jbk78izeqWg9aw rAFJIzM90vUFOy+f5FfY4Cx+MfU2UdqxRxYp3ONCb5LeKKV+3pDNDcG/htja+/0ZTzgXOi0+fmV CH1akiDwC2WFUNSGl8c8iFkXDzv9pg4ft4jNIusKrcYGROvwU+r8ooeuGkXsfJyN+lsLZROw/TB Hx4VwuMt7cJvafZetmElqAdfdOXzoHBakraKeTkC+jciMqxtp2IA0Fx+gfTHi257qA4P+8kLsT9 +io/ej1jAxzwsF0kvnqNy6wJ4PP6 X-Received: by 2002:a05:690c:6e07:b0:80c:e8d3:1166 with SMTP id 00721157ae682-812e98d2971mr65682457b3.26.1783007022242; Thu, 02 Jul 2026 08:43:42 -0700 (PDT) MIME-Version: 1.0 References: <3748108.1783003096@sss.pgh.pa.us> In-Reply-To: <3748108.1783003096@sss.pgh.pa.us> From: Ayush Tiwari Date: Thu, 2 Jul 2026 21:13:30 +0530 X-Gm-Features: AVVi8CfAt8sgDIS-8T7_1uWYP8telXGLEAkXZ51sI1JD2vNuLtF6qIupM5TkwTI Message-ID: Subject: Re: Fw:Re: Fw: gbt_var_consistent in contrib/btree_gist/btree_utils_var.c has internal-node type confusion on the <> strategy, bypassing exclusion constraints To: Tom Lane Cc: =?UTF-8?B?546L6LeD5p6X?= , pgsql-bugs , 3764353996 <3764353996@qq.com> Content-Type: multipart/alternative; boundary="0000000000003161120655a2ac23" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003161120655a2ac23 Content-Type: text/plain; charset="UTF-8" Hi, On Thu, 2 Jul 2026 at 20:08, Tom Lane wrote: > =?UTF-8?B?546L6LeD5p6X?= writes: > > The analysis and fix look correct. The BtreeGistNotEqual branch is > > the only strategy that bypasses the is_leaf check and passes > > potentially truncated internal-node keys directly to f_eq, which is > > unsafe for types like bit and varbit that require a minimum valid > > header size. Returning true for internal nodes is the right > > conservative choice and is consistent with how the other strategies > > handle the internal-node case. > > None of this passes the smell test for me. If it's unsafe to > call the type's f_eq function on a truncated key, how is it > any safer to call the f_cmp function? IOW, why aren't *all* the > cases in gbt_var_consistent() broken? > I think the difference is that for bit/varbit f_eq and f_cmp aren't the same kind of function. f_eq is biteq (reads bit_len, does VARSIZE-8), while f_cmp is byteacmp (plain byte-wise, only needs the varlena header). On internal pages every other strategy goes through f_cmp/byteacmp, and only the <> branch reaches f_eq. So I believe that's why the byte-wise cases stay safe on a truncated key and <> doesn't. > It looks to me like gbt_var_node_truncate adjusts the length words > of the truncated keys so that they are still valid, just shorter. > Or at least it's trying to. That works fine for text and bytea, > but it's not fine for bit/varbit because (a) it fails to update the > "bit_len" field that follows the length word, and (b) the common > prefix length selection logic doesn't know that it mustn't truncate > away any part of the bit_len field. So my own thought about fixing > this is that type bit needs a custom truncation method. > That was my first assumption too, but it looks like the node key has no bit_len to preserve: gbt_bit_l2n/gbt_bit_xfrm seem to drop it and keep only the raw bits before truncation runs. If so, a custom truncation may not help, since the crash seems to come from <> calling biteq rather than a damaged bit_len. Though I'm not fully sure I'm reading the l2n step right. > (I continue to regret that we ever accepted such underdocumented code. > I think we ought to reverse-engineer a comment explaining what > gbt_var_consistent is doing, eg, why are all of the tests seemingly > reversed?) > Agreed, we can add a comment there. Regards, Ayush --0000000000003161120655a2ac23 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

On Thu, 2 Jul 20= 26 at 20:08, Tom Lane <tgl@sss.pgh.= pa.us> wrote:
=3D?UTF-8?B?546L6LeD5p6X?=3D <violin0613@tju.edu.cn> writes:
> The analysis and fix look correct. The BtreeGistNotEqual branch is
> the only strategy that bypasses the is_leaf check and passes
> potentially truncated internal-node keys directly to f_eq, which is > unsafe for types like bit and varbit that require a minimum valid
> header size. Returning true for internal nodes is the right
> conservative choice and is consistent with how the other strategies > handle the internal-node case.

None of this passes the smell test for me.=C2=A0 If it's unsafe to
call the type's f_eq function on a truncated key, how is it
any safer to call the f_cmp function?=C2=A0 IOW, why aren't *all* the cases in gbt_var_consistent() broken?

I thin= k the difference is that for bit/varbit f_eq and f_cmp aren't the
sa= me kind of function. =C2=A0f_eq is biteq (reads bit_len, does VARSIZE-8),while f_cmp is byteacmp (plain byte-wise, only needs the varlena
heade= r).=C2=A0 On internal pages every other strategy goes through
f_cmp/byte= acmp, and only the <> branch reaches f_eq.=C2=A0 So I believe
that= 's why the byte-wise cases stay safe on a truncated key and <>
doesn't.
= =C2=A0
It looks to me like gbt_var_node_truncate adjusts the length words
of the truncated keys so that they are still valid, just shorter.
Or at least it's trying to.=C2=A0 That works fine for text and bytea, but it's not fine for bit/varbit because (a) it fails to update the
"bit_len" field that follows the length word, and (b) the common<= br> prefix length selection logic doesn't know that it mustn't truncate=
away any part of the bit_len field.=C2=A0 So my own thought about fixing this is that type bit needs a custom truncation method.

That was my first assumption too, but it looks like the node key has<= br>no bit_len to preserve: gbt_bit_l2n/gbt_bit_xfrm seem to drop it and
= keep only the raw bits before truncation runs.=C2=A0 If so, a custom
tru= ncation may not help, since the crash seems to come from <> callingbiteq rather than a damaged bit_len.=C2=A0 Though I'm not fully sure = I'm
reading the l2n step right.
=C2=A0
(I continue to regret that we ever accepted such underdocumented code.
I think we ought to reverse-engineer a comment explaining what
gbt_var_consistent is doing, eg, why are all of the tests seemingly
reversed?)

Agreed, we can add a comment there.
=
Regards,
Ayush
=C2=A0
--0000000000003161120655a2ac23--