Re: BUG #19964: ltree GiST index returns too few rows for lquery with a {0,n} quantifier

From: Aman Ganapathy <aman(at)stack-auth(dot)com>
To: aman(at)stack-auth(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19964: ltree GiST index returns too few rows for lquery with a {0,n} quantifier
Date: 2026-12-02 19:00:45
Message-ID: CA+jUSqRPwsTU=tBV2=Uc0A_gt88i9ACxFLTfZSz1E2bG4S0rtA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

I realized I forgot to attach my suggested patch and regression test. When
a GiST search on ltree reaches an internal page, gist_qe() skips the
subtree unless every label in the lquery is in the page's signature. That's
wrong for a level like c{0,1}: rows like x_y.b match without containing c,
so their subtrees get skipped.

The patch skips the signature check for levels whose lower bound is 0. Leaf
entries are still matched against the full lquery, so this can't return
extra rows; at worst it visits a few more pages. Queries without such
levels are unaffected. ltree_gist.c and _ltree_gist.c both have a copy of
gist_qe(), so the fix is in both.

The patch is against master and adds the test case to ltree's regression
tests, which pass

Thanks.

Aman

On Tue, Dec 1, 2026 at 10:21 AM PG Bug reporting form <
noreply(at)postgresql(dot)org> wrote:

> The following bug has been logged on the website:
>
> Bug reference: 19964
> Logged by: Aman Ganapathy
> Email address: aman(at)stack-auth(dot)com
> PostgreSQL version: 19.0
> Operating system: Debian 13 (trixie), aarch64
> Description:
>
> A GiST index on an ltree column returns too few rows for an lquery with
> a label that may appear zero times, such as c{0,1}, c{0} or c{,2}. In
> the test case below a query finds 1000 rows before the index is created
> and 0 rows using the index. The same happens with a GiST index on an
> ltree[] column.
>
> Setup: the official postgres:19.0 Docker image, started with
>
> docker run -d --name pg19 -e POSTGRES_HOST_AUTH_METHOD=trust \
> -e POSTGRES_INITDB_ARGS="--locale=C.UTF-8 --encoding=UTF8" -e TZ=UTC \
> postgres:19.0 postgres -c log_error_verbosity=verbose
>
> Test case, run with "psql -X" in a new, empty database:
>
> CREATE EXTENSION ltree;
> CREATE TABLE t (path ltree);
> INSERT INTO t SELECT 'x_y.b' FROM generate_series(1, 1000);
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{0,1}.b';
> CREATE INDEX ON t USING gist (path);
> SET enable_seqscan = off;
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{0,1}.b';
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{0}.b';
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{,2}.b';
> RESET enable_seqscan;
>
> CREATE TABLE ta (paths ltree[]);
> INSERT INTO ta VALUES ('{x_y.b}');
> SELECT count(*) FROM ta WHERE paths ~ 'x_y.c{0,1}.b';
> CREATE INDEX ON ta USING gist (paths);
> SET enable_seqscan = off;
> SELECT count(*) FROM ta WHERE paths ~ 'x_y.c{0,1}.b';
>
> Output (psql -X -a -q, so each statement is shown above its result):
>
> CREATE EXTENSION ltree;
> CREATE TABLE t (path ltree);
> INSERT INTO t SELECT 'x_y.b' FROM generate_series(1, 1000);
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{0,1}.b';
> count
> -------
> 1000
> (1 row)
>
> CREATE INDEX ON t USING gist (path);
> SET enable_seqscan = off;
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{0,1}.b';
> count
> -------
> 0
> (1 row)
>
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{0}.b';
> count
> -------
> 0
> (1 row)
>
> SELECT count(*) FROM t WHERE path ~ 'x_y.c{,2}.b';
> count
> -------
> 0
> (1 row)
>
> RESET enable_seqscan;
> CREATE TABLE ta (paths ltree[]);
> INSERT INTO ta VALUES ('{x_y.b}');
> SELECT count(*) FROM ta WHERE paths ~ 'x_y.c{0,1}.b';
> count
> -------
> 1
> (1 row)
>
> CREATE INDEX ON ta USING gist (paths);
> SET enable_seqscan = off;
> SELECT count(*) FROM ta WHERE paths ~ 'x_y.c{0,1}.b';
> count
> -------
> 0
> (1 row)
>
> Expected: the same counts with the index as without it, i.e. 1000 for the
> three queries on t after CREATE INDEX, and 1 for the second query on ta.
> The ltree documentation describes foo{n,m} as "Match at least n but not
> more than m occurrences of foo" and lists ~ among the operators supported
> by gist_ltree_ops and gist__ltree_ops.
>
> The same output on master at b4cc5e924b7 (2026-11-25), built with
> --enable-debug --with-icu --enable-cassert.
>
> Environment: PostgreSQL 19.0 (Debian 19.0-2.pgdg13+1) on
> aarch64-unknown-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0,
> 64-bit; psql 19.0 (Debian 19.0-2.pgdg13+1). Debian 13 (trixie) in the
> image, Linux 7.0.14 aarch64, glibc 2.41, 14 CPUs, 16 GB RAM, running in
> an OrbStack VM on macOS 15.7.9 (Apple Silicon).
>
> I have a patch with a regression test and will post it as a reply.
>
>
>

Attachment Content-Type Size
v1-0001-ltree-Fix-GiST-index-searches-for-lquery-levels-t.patch application/octet-stream 4.8 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Jeff Davis 2026-12-02 19:59:19 Re: Regex fails to capture group consisting of empty string in non-greedy case
Previous Message Aman Ganapathy 2026-12-02 18:47:21 Re: BUG #19967: ecpg crashes after "cursor does not exist" on FETCH or MOVE