Patch
Fix GiST tsquery scans that miss empty queries.
- Branch
- master
- Risk
- –
- Base commit
- a9cb202e9e09fe1031e5dd03408668bebe6f98a2
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1cb79-1b96-74a6-b4ce-f1ada32df900 | jq -r .body | git am
From 344bfc911673dfab89d8aaf2d9287ed6fa04fc83 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Wed, 11 Nov 2026 08:28:56 -0800
Subject: [PATCH] Fix GiST tsquery scans that miss empty queries.
An empty tsquery has an all-zero signature and is contained by every
tsquery. gtsquery_consistent() treated a zero signature as "no
overlap". For <@, an internal page was descended into only when its
union key overlapped the query, so a subtree holding empty entries
was skipped and those rows disappeared from the index scan. For @>
of an empty query, the same test rejected every internal page, so
the scan returned no rows instead of every row.
On an internal page, visit every subtree for <@, and for @> when the
query signature is zero. Leaf tests are unchanged and still recheck.
A <@ scan reads the whole index; the union key does not record that
an empty entry is present.
Discussion: https://postgr.es/m/19856-bdfec1ae4a0ffc4c@postgresql.org
---
src/backend/utils/adt/tsquery_gist.c | 21 ++++++-
src/test/regress/expected/tsearch.out | 86 +++++++++++++++++++++++++++
src/test/regress/sql/tsearch.sql | 22 +++++++
3 files changed, 128 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index 2ca97957019..a9b60c9f42a 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -71,6 +71,15 @@ gtsquery_consistent(PG_FUNCTION_ARGS)
case RTContainsStrategyNumber:
if (GIST_LEAF(entry))
retval = (key & sq) == sq;
+ else if (sq == 0)
+ {
+ /*
+ * An empty query has a zero signature and is contained by
+ * every tsquery. The overlap test below would reject every
+ * internal key.
+ */
+ retval = true;
+ }
else
retval = (key & sq) != 0;
break;
@@ -78,7 +87,17 @@ gtsquery_consistent(PG_FUNCTION_ARGS)
if (GIST_LEAF(entry))
retval = (key & sq) == key;
else
- retval = (key & sq) != 0;
+ {
+ /*
+ * An empty tsquery has a zero signature and is contained by
+ * every query. Union keys OR the child signatures, so an
+ * empty entry adds no bits and can sit under a key that does
+ * not overlap the query. Nothing in the signature proves
+ * that a subtree has no empty entries, so every subtree is
+ * visited and a contained-by scan reads the whole index.
+ */
+ retval = true;
+ }
break;
default:
retval = false;
diff --git a/src/test/regress/expected/tsearch.out b/src/test/regress/expected/tsearch.out
index a726f8437e1..f8b14f3af48 100644
--- a/src/test/regress/expected/tsearch.out
+++ b/src/test/regress/expected/tsearch.out
@@ -3040,3 +3040,89 @@ NOTICE: text-search query contains only stop words or doesn't contain lexemes,
(1 row)
+-- Empty tsqueries have an all-zero GiST signature. They are contained
+-- by every query, and every query contains them. The index must not
+-- skip those rows.
+CREATE TABLE gist_tsq_empty (c tsquery);
+INSERT INTO gist_tsq_empty
+SELECT (ARRAY['', 'a', 'a & b', 'a | b', '!a', 'b:*']::tsquery[])[1 + i % 6]
+FROM generate_series(1, 600) i;
+NOTICE: text-search query doesn't contain lexemes: ""
+LINE 2: SELECT (ARRAY['', 'a', 'a & b', 'a | b', '!a', 'b:*']::tsque...
+ ^
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'zz';
+ count
+-------
+ 100
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ '';
+NOTICE: text-search query doesn't contain lexemes: ""
+LINE 1: SELECT count(*) FROM gist_tsq_empty WHERE c <@ '';
+ ^
+ count
+-------
+ 100
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'a';
+ count
+-------
+ 300
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c @> '';
+NOTICE: text-search query doesn't contain lexemes: ""
+LINE 1: SELECT count(*) FROM gist_tsq_empty WHERE c @> '';
+ ^
+ count
+-------
+ 600
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c @> 'a';
+ count
+-------
+ 400
+(1 row)
+
+CREATE INDEX gist_tsq_empty_idx ON gist_tsq_empty USING gist (c);
+SET enable_seqscan = off;
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'zz';
+ count
+-------
+ 100
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ '';
+NOTICE: text-search query doesn't contain lexemes: ""
+LINE 1: SELECT count(*) FROM gist_tsq_empty WHERE c <@ '';
+ ^
+ count
+-------
+ 100
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'a';
+ count
+-------
+ 300
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c @> '';
+NOTICE: text-search query doesn't contain lexemes: ""
+LINE 1: SELECT count(*) FROM gist_tsq_empty WHERE c @> '';
+ ^
+ count
+-------
+ 600
+(1 row)
+
+SELECT count(*) FROM gist_tsq_empty WHERE c @> 'a';
+ count
+-------
+ 400
+(1 row)
+
+RESET enable_seqscan;
+DROP TABLE gist_tsq_empty;
diff --git a/src/test/regress/sql/tsearch.sql b/src/test/regress/sql/tsearch.sql
index 57b9d443e56..09b2513a43f 100644
--- a/src/test/regress/sql/tsearch.sql
+++ b/src/test/regress/sql/tsearch.sql
@@ -915,3 +915,25 @@ select websearch_to_tsquery('''');
select websearch_to_tsquery('''abc''''def''');
select websearch_to_tsquery('\abc');
select websearch_to_tsquery('\');
+
+-- Empty tsqueries have an all-zero GiST signature. They are contained
+-- by every query, and every query contains them. The index must not
+-- skip those rows.
+CREATE TABLE gist_tsq_empty (c tsquery);
+INSERT INTO gist_tsq_empty
+SELECT (ARRAY['', 'a', 'a & b', 'a | b', '!a', 'b:*']::tsquery[])[1 + i % 6]
+FROM generate_series(1, 600) i;
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'zz';
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ '';
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'a';
+SELECT count(*) FROM gist_tsq_empty WHERE c @> '';
+SELECT count(*) FROM gist_tsq_empty WHERE c @> 'a';
+CREATE INDEX gist_tsq_empty_idx ON gist_tsq_empty USING gist (c);
+SET enable_seqscan = off;
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'zz';
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ '';
+SELECT count(*) FROM gist_tsq_empty WHERE c <@ 'a';
+SELECT count(*) FROM gist_tsq_empty WHERE c @> '';
+SELECT count(*) FROM gist_tsq_empty WHERE c @> 'a';
+RESET enable_seqscan;
+DROP TABLE gist_tsq_empty;
base-commit: a9cb202e9e09fe1031e5dd03408668bebe6f98a2
--
2.53.0