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

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: aman(at)stack-auth(dot)com
Subject: BUG #19964: ltree GiST index returns too few rows for lquery with a {0,n} quantifier
Date: 2026-12-01 18:21:01
Message-ID: 19964-441f645c891a7dec@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Konstantin Knizhnik 2026-12-01 19:01:20 Re: BUG #19519: REPACK can fail due to missing chunk for toast value
Previous Message surya poondla 2026-12-01 18:15:40 Re: BUG #19688: pg_dump --schema scans all sequences in PostgreSQL 18, causing severe performance regression