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 1wfZSF-005WIf-1y for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jul 2026 08:40:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wfZSE-006KGz-1h for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jul 2026 08:40:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wfZSE-006KGr-0X for pgsql-bugs@lists.postgresql.org; Fri, 03 Jul 2026 08:40:58 +0000 Received: from mail-yw1-x1134.google.com ([2607:f8b0:4864:20::1134]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wfZSC-00000001I9h-198j for pgsql-bugs@postgresql.org; Fri, 03 Jul 2026 08:40:57 +0000 Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-8114a4542b2so4085557b3.1 for ; Fri, 03 Jul 2026 01:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1783068055; cv=none; d=google.com; s=arc-20260327; b=Uu/aculZ1mCOD3gjkJeXPf3QaMCCw6ZV2VquW8S7Lwbgar5+dY1t2+jQDCmVr779YA 5Xnb0GXxwVloHClnuaa9q46z97N09+Ueir/NI/M8BG00o+V8rBjd7YnfRs/wGyIhLEtv 4jbDRXmQjGXOTEpiCkP1elKyqrWMPkSlU2ABWiwrQ+V4MN0Ft8P+E4A5y6mvaAjihSaP Powyu/Tzx4YToQnLso0FM8bl4h0eAnkZcZG24KYlsRBMsCJIWx6srtpd7Fwl1nHbrhZk knbnuenHdQ/mgzpz5DPUy4JrH7uZSRxf/mqh20U9iyz+1V+/bjhEEeyji/rKgsCu31hK eFzA== 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=T7WMmlHcAu4hOo4rHk+/gG/WMenAnBjb8XXp2nvR4f4=; fh=2Cnjl9zavXSIrJNbpv4vTEAhLRKenzxz+FD/wMiOk6o=; b=nnYofM2UcGF1GJ+cWWoKhXgP0ywNjmf8q02FUz6ylEae6A8863vIdlMPpRGryrkC4s bRyPFaVIN3FQ6oxytwMv5S97VWCzVJQprnpxcJ3oePHo+9+r5aqEKJXNFiZFI852YH0e FcXN7+ahpEMd/gkR5//fje0dzBsiSWIDhGeAGUbM9FK4/0DoiXxi6C0YMZf7r7+AEer4 dS24Y7GQlOsPOGdRSX7u/SlALqNAYRYddvEwDoi5e4HcM5huDmopb3uFIiZxA78cSVqB 0OlQlKOMNNJ4c6EY+Hp/z8sUlMwYu65zGVod7q8xYJUlFxtU2Yn1u5OrWHmjRs7hqZPZ g7pQ==; 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=1783068055; x=1783672855; 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=T7WMmlHcAu4hOo4rHk+/gG/WMenAnBjb8XXp2nvR4f4=; b=bsUx1rHKsMIslifGXgOwRkGRUKytsQF1OjFFp3D2q8cWQRzhMnwnZ0Ws399xAOiEiS 6yMKI+2EdMJXqipjNEmDwfFvJZFGh4eS1vEwDjuISQBfzPG2FhGvJBCb6+35pdhYAMsj v87wl8nY45ZkrNUnrURLGAgbR3w2YAbXc9HT+8DCoVJkwM09DsbFiKCtjZYEyA4Rp8Aw OYCtO9XQlIgTdRgRRL9iS+D+5HHeA4tR18RSq0JMN0JPL/ZtVXl6DBHpTfxN8xdX/7+E FP741j2GP1nh7+8Hq6XiPxnRrjlE3RqDL7t7q/rKfh9D8IRh9kE3BV4UCJ1e3KyMJ6j8 mzIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783068055; x=1783672855; 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=T7WMmlHcAu4hOo4rHk+/gG/WMenAnBjb8XXp2nvR4f4=; b=PsayMznTnXQpjdwrr0c2UsdjMe+Gkw62FHoQbP1C47h57qUd7pALyiluoiX60ltcHn KIEYQe+W5VWNbyWZmzNvlcx69gzgEvE6I3a3arpGmfJn77LPRYjCc8YekQW+pqqHqXCt 6tfBnZAZpxbD9pJxymIJJEUWJlZqLy8UkSUkoqBkGz+rFUr65dIwvibtIpij16WbYGEb QGzlI2kGBL9SSeKH8Qr6BZm8iTh7QA09QUoOOpIT3FL9vXSHmbPSq6jc7x0SANHPt94l NXDVXuedVdWAlStaEwUTbVO6AGYPZKc4D9Y3bmCU0nZIeIlyJDoD2DGP8oGRhSCmyu17 m4cw== X-Forwarded-Encrypted: i=1; AHgh+RpH0zraUXIEI08PELI9GtBjQSy35aN91z9FTKfF8U27ZU/xazmfEa8cTP1kxmoGWmAd2BHZftveWwNp@postgresql.org X-Gm-Message-State: AOJu0YwDqMvUO56szPdFmHgK5WSlzHXqv7dc1ltSbN3QaBnaWpA9Q9FI 5L59kZFD8F1vvPp+UpfiMNbNwuJ2oOFPrAeArqvSgKnhwKJs69SsfmYv6kDzXpbC0adGqCo9IcR jClh54MEG/9E47LedtKBY+CuKPsmQMtg= X-Gm-Gg: AfdE7cmrwfv1wN6ut1kHqIQUaG+w3nv0DV6GHCBrcS/La/+Cw4qAweFY7KTDJ1MDqUF hp1UjNTkRs+e2AO2y0amRWEl09ZoCJ9Udw8rStDw0xr+jaqVMvRlzpdlz+L9jBljtw6Poafpuat iwAFs9L6zg17Y0LVNsOIWBuqxSUHfOJDyzIpkweDH3jQ5EWZyO8cnYD+2HzAJh89neJvdn6ZjmE iezKv163uQgg8gQaBMs5e9BTDKYCFlmBI719lwfSEXmJ+Y7LJQEVxt6fTCzmPkAzwAdNpvRZ6ba lJHbFy+w3n+u2R4lkqK2/tSkrcY= X-Received: by 2002:a05:690c:7346:b0:80c:85e5:875c with SMTP id 00721157ae682-812ec5543bdmr93624617b3.69.1783068055554; Fri, 03 Jul 2026 01:40:55 -0700 (PDT) MIME-Version: 1.0 References: <3748108.1783003096@sss.pgh.pa.us> <3756776.1783011257@sss.pgh.pa.us> <3793780.1783033337@sss.pgh.pa.us> In-Reply-To: <3793780.1783033337@sss.pgh.pa.us> From: Ayush Tiwari Date: Fri, 3 Jul 2026 14:10:44 +0530 X-Gm-Features: AVVi8Ce_k1sTra9j8cxodMvYR16410m9Lt-WjSg2i4xj13eSxScE4K1u1SO_l4g 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="0000000000000fd1d00655b0e2ef" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000000fd1d00655b0e2ef Content-Type: text/plain; charset="UTF-8" Hi, On Fri, 3 Jul 2026 at 04:32, Tom Lane wrote: > Ayush Tiwari writes: > > On Thu, 2 Jul 2026 at 22:24, Tom Lane wrote: > >> Anyway, I think part of our work here has got to be to raise the > >> level of documentation of this code. I've had it with having > >> to reverse-engineer details as fundamental as these. > > > I've added some documentation in code, patch attached. > > I had something considerably more ambitious in mind, so I had a > go at that --- and was glad that I did so, because the review > turned up an additional bug as well as some other opportunities > for improvement. > Thanks for working on the patch! v3-0001 is a documentation-improvement patch. I only touched > the files associated with btree_utils_var.c. Maybe it's worth > doing something similar for the btree_utils_num.c group, but > I have the impression that those are way simpler. > 0001 LGTM > v3-0002 is your patch, editorialized a bit. I left out the > test case because it seemed fairly expensive for something > that won't expose a bug in normal builds. (BTW, I wonder > if rather than documenting the comparisons as being reversed, > we should just flip them all.) > I'd lean towards flipping them for readability, though I'm not sure it's worth the backpatch churn? I guess we could start a new thread for master on this. v3-0003 fixes a bug I identified: index builds that use the > gbt_bit_ssup_cmp infrastructure will sort the entries in a > way that doesn't match the bit types' actual ordering, > leading to what's probably a very inefficient index. It looks right to me, leaf entries are real bit/varbit values, so bitcmp is the right comparator there. > v3-0004 and v3-0005 clean up some things that seem like > dead code to me. > One small thing on 0005: once gbt_var_node_cp_len stops using pg_mblen_range, the "#include "mb/pg_wchar.h"" in btree_utils_var.c looks like it becomes unused. Might be worth dropping it too. > Also, it occurs to me after another look at 0002 that what was > perhaps intended was something like this for the non-leaf case: > > if (is_leaf) > retval = !(tinfo->f_eq(query, key->lower, collation, > flinfo)); > else > retval = tinfo->trnc || > !(tinfo->f_cmp(query, key->lower, collation, flinfo) > == 0 && > tinfo->f_cmp(query, key->upper, collation, flinfo) > == 0); > > That is, if we're looking at non-truncated keys and lower == upper, > then we know that all the keys below this node are exactly that > value, so we can avoid descending if that value is equal to the query. > Most of the time this would not pay off in any savings, but if you > had an index on a column with only a few values, maybe there would be > leaf pages like that? I would think that the planner would avoid > choosing to implement a <> query with an indexscan unless there was > a pretty large fraction of the table that <> would reject, so maybe > this actually is worth doing. > I think that optimization is correct. For the non-truncatable types the internal lower/upper are exact min/max (built via f_cmp in gbt_var_bin_union with no truncation), so if both equal the query then everything below does too, and pruning can't drop a match. Are you planning on including it in the backpatch or to keep the optimization just part of master, and the bug fix backpatched? Regards, Ayush --0000000000000fd1d00655b0e2ef Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

On Fri, 3 Jul 2026 a= t 04:32, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> On Thu, 2 Jul 2026 at 22:24, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Anyway, I think part of our work here has got to be to raise the >> level of documentation of this code.=C2=A0 I've had it with ha= ving
>> to reverse-engineer details as fundamental as these.

> I've added some documentation in code, patch attached.

I had something considerably more ambitious in mind, so I had a
go at that --- and was glad that I did so, because the review
turned up an additional bug as well as some other opportunities
for improvement.

Thanks for working on = the patch!=C2=A0

v3-0001 is a documentation-improvement patch.=C2=A0 I only touched
the files associated with btree_utils_var.c.=C2=A0 Maybe it's worth
doing something similar for the btree_utils_num.c group, but
I have the impression that those are way simpler.

= 0001 LGTM
=C2=A0
v3-0002 is your patch, editorialized a bit.=C2=A0 I left out the
test case because it seemed fairly expensive for something
that won't expose a bug in normal builds.=C2=A0 (BTW, I wonder
if rather than documenting the comparisons as being reversed,
we should just flip them all.)

I'd lean = towards flipping them for readability, though I'm not sure
it's worth the backpatch churn= ? I guess we could start a new=C2=A0
thread for master on = this.

v3-0003 fixes a bug I identified: index builds that use the
gbt_bit_ssup_cmp infrastructure will sort the entries in a
way that doesn't match the bit types' actual ordering,
leading to what's probably a very inefficient index.
<= br>
It looks right to me, leaf entries are real bit/varbit values= , so bitcmp
is the r= ight comparator there.=C2=A0
=C2=A0
v3-0004 and v3-0005 clean up some things that seem like
dead code to me.

One small thing on 0005: once gbt= _var_node_cp_len stops using
pg_mblen_range, the "#include "mb= /pg_wchar.h"" in btree_utils_var.c
looks like it becomes unuse= d.=C2=A0 Might be worth dropping it too.
=C2=A0
Also, it occurs to me after another look at 0002 that what was
perhaps intended was something like this for the non-leaf case:

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (is_leaf)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 retval =3D !(tinfo-= >f_eq(query, key->lower, collation, flinfo));
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 retval =3D tinfo-&g= t;trnc ||
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 !(tin= fo->f_cmp(query, key->lower, collation, flinfo) =3D=3D 0 && =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 tinfo->f_cmp(query, key->upper, collation, flinfo) =3D=3D 0);

That is, if we're looking at non-truncated keys and lower =3D=3D upper,=
then we know that all the keys below this node are exactly that
value, so we can avoid descending if that value is equal to the query.
Most of the time this would not pay off in any savings, but if you
had an index on a column with only a few values, maybe there would be
leaf pages like that?=C2=A0 I would think that the planner would avoid
choosing to implement a <> query with an indexscan unless there was a pretty large fraction of the table that <> would reject, so maybe this actually is worth doing.

I think that o= ptimization is correct.=C2=A0 For the non-truncatable types
the internal= lower/upper are exact min/max (built via f_cmp in
gbt_var_bin_union wit= h no truncation), so if both equal the query
then everything below does too, and pruning can'= t drop a match.
Are you planning on including it in the backpatch= or to keep the=C2=A0
optimization just part of master, and the b= ug fix backpatched?

Regards,
Ayush=C2=A0

=C2=A0
--0000000000000fd1d00655b0e2ef--