BUG #19959: tsquery = ignores weights and prefix flags, so queries that match different documents compare equal

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: james(dot)moore(at)g5labs(dot)ai
Subject: BUG #19959: tsquery = ignores weights and prefix flags, so queries that match different documents compare equal
Date: 2026-11-30 17:32:58
Message-ID: 19959-b98b5a4b2f0dbc22@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19959
Logged by: James Moore
Email address: james(dot)moore(at)g5labs(dot)ai
PostgreSQL version: 19.0
Operating system: Debian 13 (official postgres image), x86_64
Description:

Two tsquery values that match different documents compare equal when they
differ only in a weight restriction or the prefix flag.

Reproduced on 17.11, 18.7 and 19.0:

SELECT 'a:*'::tsquery = 'a'::tsquery AS equal,
to_tsvector('simple','abc') @@ 'a:*'::tsquery AS prefix_matches,
to_tsvector('simple','abc') @@ 'a'::tsquery AS plain_matches;
-- equal | prefix_matches | plain_matches
-- t | t | f

SELECT 'a:A'::tsquery = 'a'::tsquery AS equal,
to_tsvector('simple','a') @@ 'a:A'::tsquery AS weighted_matches,
to_tsvector('simple','a') @@ 'a'::tsquery AS plain_matches;
-- t | f | t

Because equality and the btree ordering treat them as the same value:

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"

SELECT count(DISTINCT q)
FROM (VALUES ('a:*'::tsquery), ('a'::tsquery)) v(q);
-- 1 (expected 2); GROUP BY merges them the same way

Expected: queries with different weights or prefix flags compare unequal.

Cause, as far as we can tell: QTNodeCompare() in
src/backend/utils/adt/tsquery_util.c compares operand values without their
weight and prefix flags.

Impact: an application that stores saved searches in a tsquery column with
a UNIQUE constraint rejects a valid, different query, and DISTINCT or
GROUP BY over tsquery values silently merges queries that return different
rows. A fix would change the ordering of tsquery values, so existing btree
indexes on tsquery would need a REINDEX.

Found by comparing PostgreSQL's results against a formal model of full-text
search.

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-11-30 17:33:20 BUG #19960: Phrase search matches when it should not: phrase positions wrap at 16383
Previous Message PG Bug reporting form 2026-11-30 17:32:28 BUG #19958: jsonpath: a word directly followed by a /* comment */ is silently dropped, so strict becomes lax