GiST index on tsquery misses empty tsquery rows for <@ ====================================================== AI-generated bug report from the AI Annex Report ID: 01a1cb79-1b92-75d6-8001-8c4b4458ae49 Filed: 2026-11-11 16:37:54 UTC Severity: 80/100 Blamed commit: 140d4ebcb46e17cdb1be43892ed797e5e060c8ef A GiST index on a tsquery column can omit rows that hold an empty tsquery for `column <@ query`. Nothing is raised except the usual notice that a query text has no lexemes. The same consistency check returns no rows for `column @> ''`, which is true of every tsquery. `<@` and `@>` compare the lexemes of the two operands. `tsq_mcontained(left, right)` is true when every lexeme of `left` occurs in `right`. An empty tsquery has no lexemes, so it is contained by every tsquery, and every tsquery contains it. A sequential scan follows that definition. After `CREATE INDEX ... USING gist` the index scan does not, once the index has an internal page. Reported as BUG #19856 on pgsql-bugs: ``` https://www.postgresql.org/message-id/19856-bdfec1ae4a0ffc4c@postgresql.org ``` ## Introducing commit The overlap test arrived with in-core text search in 140d4ebcb46e17cdb1be43892ed797e5e060c8ef ("Tsearch2 functionality migrates to core", 2007-08-21). `gtsquery_consistent()` still descends into an internal page for both operators only when `(key & sq) != 0`. Later edits changed how the signature is passed, not that test. The same predicate is on master and on REL_19_STABLE through REL_14_STABLE. ## Reproduction ```sql CREATE TABLE q(c tsquery); INSERT INTO q SELECT (ARRAY['', 'a', 'a & b', 'a | b', '!a', 'b:*']::tsquery[])[1 + i % 6] FROM generate_series(1, 600) i; SELECT count(*) FROM q WHERE c <@ 'zz'; -- 100 SELECT count(*) FROM q WHERE c <@ ''; -- 100 SELECT count(*) FROM q WHERE c @> ''; -- 600 CREATE INDEX ON q USING gist (c); SET enable_seqscan = off; SELECT count(*) FROM q WHERE c <@ 'zz'; -- 0, expected 100 SELECT count(*) FROM q WHERE c <@ ''; -- 0, expected 100 SELECT count(*) FROM q WHERE c @> ''; -- 0, expected 600 ``` Reproduced on master a9cb202e9e09fe1031e5dd03408668bebe6f98a2. The same wrong counts appear on REL_19_STABLE through REL_14_STABLE. With the fix, the three index scans return 100, 100, and 600. `c <@ 'a'` still returned 300 with the index, the same as a sequential scan. Those empty values sat on pages whose union signature overlapped `'a'`. `'zz'` shares no signature bit with `'a'` or `'b'`, so every internal key was rejected and the empty rows were missed. `c <@ ''` is the same failure for every key, because the query signature itself is zero. Non-empty values that really are contained are not missed: their signature bits are a subset of the query and therefore appear in every ancestor union. ## Diagnosis `makeTSQuerySign()` sets one bit for each `QI_VAL` operand (`valcrc % 64`). A tsquery with no operands has signature 0. GiST stores that signature. `gtsquery_union()` ORs the children together, so an empty entry adds nothing. A page of only empty entries has a zero union, and an empty entry stored beside other values does not show up in the union at all. `gtsquery_consistent()` always asks for a recheck, then: - Leaf, `@>` (`RTContainsStrategyNumber`): `(key & sq) == sq`. A zero query signature matches every leaf. A zero key matches only a zero query. Both agree with `tsq_mcontains`. - Internal, `@>`: `(key & sq) != 0`. A zero query signature overlaps nothing, so the scan never descends and returns no rows. Expected result: every row. - Leaf, `<@` (`RTContainedByStrategyNumber`): `(key & sq) == key`. A zero key matches every query. That agrees with `tsq_mcontained`. - Internal, `<@`: `(key & sq) != 0`. That is a sound reject test for a non-empty contained value, and not for an empty one. Any subtree whose union misses the query bits is skipped, including a subtree that holds empty entries. There is no backtrace. The scan finishes and returns the wrong count. ## Fix On an internal page, `<@` has to visit every subtree. The stored signature cannot prove that a subtree has no empty entry, and an empty entry is contained by every query. A `<@` index scan therefore reads the whole index. The leaf test and the recheck still drop rows that are not actually contained. On an internal page, `@>` of a zero signature has to descend as well. An empty query is contained by every tsquery. A non-empty query keeps the overlap test, which is still a sound way to reject a subtree. Leaf checks stay as they are. The patch does not change the on-disk key or the catalog, so a minor release does not need a catalog version bump, and an existing index starts returning the missing rows without REINDEX. Keeping a bit in the signature for "an empty query is present" would let `<@` prune again, but keys already on disk would not have that bit, so they would stay wrong until REINDEX. Dropping `<@` from `gist/tsquery_ops` would also avoid the wrong answer, and would be a catalog change that a minor release cannot ship. Visiting every subtree is the change that back-patches. `src/test/regress/sql/tsearch.sql` loads the 600-row case, builds the GiST index, turns sequential scans off, and checks `<@` of `'zz'`, `''`, and `'a'`, and `@>` of `''` and `'a'`. ## Severity 80. The index scan returns the wrong set of rows. It is not higher because the missed `<@` rows are empty tsquery values, and `@>` is wrong only when the query itself is empty. Both are uncommon. A sequential scan, or not using this GiST index, returns the right rows. There is no crash and the heap is unchanged.