pgsql: Fix btree_gist's NotEqual strategy on internal index pages.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix btree_gist's NotEqual strategy on internal index pages.
Date: 2026-07-03 17:50:22
Message-ID: E1wfi1u-001CM6-1A@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix btree_gist's NotEqual strategy on internal index pages.

gbt_var_consistent() handled the <> (BtreeGistNotEqual) strategy without
distinguishing leaf from internal pages, unlike every other strategy.
In particular, it tried to apply the datatype-specific f_eq method,
which is completely wrong since internal keys might not have the same
representation as leaf keys. This led to OOB reads and potentially
crashes, and most likely to wrong query results as well.

On leaf pages we can apply the inverse of what the Equal strategy does.
On internal pages, use a correct implementation of what the previous
code intended: we can descend if the query value equals both bounds,
*so long as the bounds aren't truncated*. With truncated bounds we
don't quite know the range of what's below, so we must always descend.

Adjust the code in gbt_num_consistent() to look similar, too. This
fixes a performance buglet in that there's no need to do two comparisons
on a leaf entry, but the main point is just to keep code consistency.

Reported-by: 王跃林 <violin0613(at)tju(dot)edu(dot)cn>
Author: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/AH*AvQCYKhQGVvPWi1GiU4oY.8.1781609375063.Hmail.3020001251@tju.edu.cn
Backpatch-through: 14

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/eef644e57c38a79eb29bf9f3f05efbcee8fbdfce

Modified Files
--------------
contrib/btree_gist/btree_utils_num.c | 15 +++++++++++++--
contrib/btree_gist/btree_utils_var.c | 23 +++++++++++++++++++++--
2 files changed, 34 insertions(+), 4 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Álvaro Herrera 2026-07-03 18:06:14 pgsql: REPACK CONCURRENTLY: Initialize the range table more honestly
Previous Message Tom Lane 2026-07-03 17:18:20 pgsql: Reverse-engineer some documentation for btree_gist's varlena mod