Report
GiST index on tsquery misses empty tsquery rows for <@
- Severity
- 80
- Blamed commit
- 140d4ebcb46e17cdb1be43892ed797e5e060c8ef
On 20 branches
REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
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
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 withtsq_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 withtsq_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
- 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.