From d5a69f983ab961cc9821069d19c275a44885de55 Mon Sep 17 00:00:00 2001 From: Aman Ganapathy Date: Thu, 26 Nov 2026 02:14:38 +0000 Subject: [PATCH v1] ltree: Fix GiST index searches for lquery levels that can match zero times. An lquery level like c{0,1} also matches paths that don't contain c at all, but gist_qe() still required c to be in the signature of internal index pages. GiST index scans therefore skipped pages with matching rows and returned too few rows. The same applies to the copy of gist_qe() in _ltree_gist.c, used for ltree[] columns. Don't check the signature for levels that can match zero times. Discussion: https://postgr.es/m/19964-441f645c891a7dec@postgresql.org --- contrib/ltree/_ltree_gist.c | 4 ++- contrib/ltree/expected/ltree.out | 47 ++++++++++++++++++++++++++++++++ contrib/ltree/ltree_gist.c | 4 ++- contrib/ltree/sql/ltree.sql | 18 ++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/contrib/ltree/_ltree_gist.c b/contrib/ltree/_ltree_gist.c index 07d6682..ef3297d 100644 --- a/contrib/ltree/_ltree_gist.c +++ b/contrib/ltree/_ltree_gist.c @@ -448,7 +448,9 @@ gist_qe(ltree_gist *key, lquery *query, int siglen) while (qlen > 0) { - if (curq->numvar && LQL_CANLOOKSIGN(curq)) + /* A level that can match zero times need not appear in the signature */ + if (curq->numvar && LQL_CANLOOKSIGN(curq) && + !((curq->flag & LQL_COUNT) && curq->low == 0)) { bool isexist = false; int vlen = curq->numvar; diff --git a/contrib/ltree/expected/ltree.out b/contrib/ltree/expected/ltree.out index 0ab623c..11cf9de 100644 --- a/contrib/ltree/expected/ltree.out +++ b/contrib/ltree/expected/ltree.out @@ -8236,3 +8236,50 @@ SELECT (l.v > s.v) AS gt_ok, (l.v < s.v) AS lt_ok, (l.v = s.v) AS eq_ok t | f | f (1 row) +-- GiST index search with an lquery level that may occur zero times. +-- 200 rows are enough for the index to have an inner page. +CREATE TABLE ltree_optional (p ltree); +INSERT INTO ltree_optional SELECT 'x_y.b' FROM generate_series(1, 200); +CREATE INDEX ON ltree_optional USING gist (p); +CREATE TABLE ltree_optional_arr (p ltree[]); +INSERT INTO ltree_optional_arr SELECT ARRAY['x_y.b'::ltree] FROM generate_series(1, 3); +CREATE INDEX ON ltree_optional_arr USING gist (p); +SET enable_seqscan = off; +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{0,1}.b'; + count +------- + 200 +(1 row) + +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{0}.b'; + count +------- + 200 +(1 row) + +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{,2}.b'; + count +------- + 200 +(1 row) + +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{1}.b'; + count +------- + 0 +(1 row) + +SELECT count(*) FROM ltree_optional_arr WHERE p ~ 'x_y.c{0,1}.b'; + count +------- + 3 +(1 row) + +SELECT count(*) FROM ltree_optional_arr WHERE p ? ARRAY['x_y.c{0,1}.b'::lquery]; + count +------- + 3 +(1 row) + +RESET enable_seqscan; +DROP TABLE ltree_optional, ltree_optional_arr; diff --git a/contrib/ltree/ltree_gist.c b/contrib/ltree/ltree_gist.c index e845117..504b3b8 100644 --- a/contrib/ltree/ltree_gist.c +++ b/contrib/ltree/ltree_gist.c @@ -487,7 +487,9 @@ gist_qe(ltree_gist *key, lquery *query, int siglen) while (qlen > 0) { - if (curq->numvar && LQL_CANLOOKSIGN(curq)) + /* A level that can match zero times need not appear in the signature */ + if (curq->numvar && LQL_CANLOOKSIGN(curq) && + !((curq->flag & LQL_COUNT) && curq->low == 0)) { bool isexist = false; int vlen = curq->numvar; diff --git a/contrib/ltree/sql/ltree.sql b/contrib/ltree/sql/ltree.sql index 5c32416..83b0b4a 100644 --- a/contrib/ltree/sql/ltree.sql +++ b/contrib/ltree/sql/ltree.sql @@ -483,3 +483,21 @@ WITH s AS (SELECT 'a'::ltree AS v), l AS (SELECT (repeat('a.', 14999) || 'a')::ltree AS v) SELECT (l.v > s.v) AS gt_ok, (l.v < s.v) AS lt_ok, (l.v = s.v) AS eq_ok FROM s, l; + +-- GiST index search with an lquery level that may occur zero times. +-- 200 rows are enough for the index to have an inner page. +CREATE TABLE ltree_optional (p ltree); +INSERT INTO ltree_optional SELECT 'x_y.b' FROM generate_series(1, 200); +CREATE INDEX ON ltree_optional USING gist (p); +CREATE TABLE ltree_optional_arr (p ltree[]); +INSERT INTO ltree_optional_arr SELECT ARRAY['x_y.b'::ltree] FROM generate_series(1, 3); +CREATE INDEX ON ltree_optional_arr USING gist (p); +SET enable_seqscan = off; +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{0,1}.b'; +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{0}.b'; +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{,2}.b'; +SELECT count(*) FROM ltree_optional WHERE p ~ 'x_y.c{1}.b'; +SELECT count(*) FROM ltree_optional_arr WHERE p ~ 'x_y.c{0,1}.b'; +SELECT count(*) FROM ltree_optional_arr WHERE p ? ARRAY['x_y.c{0,1}.b'::lquery]; +RESET enable_seqscan; +DROP TABLE ltree_optional, ltree_optional_arr; -- 2.47.3