Report
tsquery equality and btree comparison ignore operand weight and prefix flags, so 'a:*' = 'a' and 'a:A' = 'a': distinct queries collide under UNIQUE, merge in GROUP BY, and ts_rewrite drops the restriction
- Severity
- 25
- 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
Reported on pgsql-bugs as BUG #19959 (against 17, 18 and 19).
tsquery equality and the btree comparison ignore an operand's weight restriction and prefix flag. QTNodeCompare() in tsquery_util.c, which CompareTSQ() uses for =, <>, <, <=, >, >= and tsquery_cmp (the tsquery_ops btree opclass), compares the node type, the operand's CRC and word text, and the operator children. It never reads the operand's weight or prefix fields. Queries that match different documents therefore compare equal: they collide under UNIQUE, merge in DISTINCT and GROUP BY, and are interchangeable in btree index scans.
SELECT 'a:*'::tsquery = 'a'::tsquery; -- t
SELECT 'a:A'::tsquery = 'a'::tsquery; -- t
SELECT 'a:*B'::tsquery = 'a:A'::tsquery; -- t
SELECT tsquery_cmp('a:AB', 'a:A'); -- 0
SELECT 'a & b:*'::tsquery = 'a & b'; -- t (also for |, ! and <->)
CREATE TABLE tq (q tsquery UNIQUE);
INSERT INTO tq VALUES ('a');
INSERT INTO tq VALUES ('a:*');
-- ERROR: duplicate key value violates unique constraint "tq_q_key"
-- DETAIL: Key (q)=('a':*) already exists.
Yet to_tsvector('simple', 'abc') matches 'a:' and not 'a'. On a table holding a, a:, a:A and b with a btree index, WHERE q = 'a:' with enable_seqscan = off returns 'a', 'a': and 'a':A, and GROUP BY q returns one group of 3 for the three of them.
ts_rewrite() matches subtrees with the same comparison, so ts_rewrite('a:* & b', 'a', 'c'), ts_rewrite('a:A & b', 'a', 'c') and ts_rewrite('a & b', 'a:', 'c') all return 'b' & 'c', dropping the prefix or weight restriction. The containment operator @> agrees: 'a: & b' @> 'a' and 'a:A' @> 'a' are both true.
A fix changes the ordering of tsquery values, so existing btree indexes on tsquery columns would need a REINDEX.
Blame is 140d4ebcb46e17cdb1be43892ed797e5e060c8ef ("Tsearch2 functionality migrates to core.", 2007), which added QTNodeCompare() without comparing the weight. A build of that commit shows 'a:A' = 'a' and the UNIQUE collision; its parent has tsquery only in contrib/tsearch2. The prefix part comes from e6dbcb72fafa4031c73cc914e829a6dec96ab6b6 ("Extend GIN to support partial-match searches, and extend tsquery to support prefix matching using this facility.", 2008), which added the prefix flag without adding it to the comparison. Its parent e1bdd07c3c33d4004180506f1493efcbbbc02b4c rejects 'a:', and the commit itself shows 'a:' = 'a'. Later changes to the function (57641a165ff, bb140506df6, which added the phrase distance comparison) leave both gaps in place.
No fix is committed as of master ca492a9197a449bca3953f7e287c9aee6d295c6d. The thread has no patch.
Branches: REL_14 through master (reproduced on the master and REL_19 through REL_14 tips).