BUG #19960: Phrase search matches when it should not: phrase positions wrap at 16383

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 #19960: Phrase search matches when it should not: phrase positions wrap at 16383
Date: 2026-11-30 17:33:20
Message-ID: 19960-29734b8086b69aec@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: 19960
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:

A phrase query whose left side combines a lexeme at position 16383 with an
AND/NOT returns true where the same phrase without the AND/NOT, correctly,
returns false.

Reproduced on 17.11, 18.7 and 19.0:

SELECT 'b:16383 c:1 d:4 e:2'::tsvector
@@ '(b & !(c <2> d)) <-> e'::tsquery AS with_and_not,
'b:16383 c:1 d:4 e:2'::tsvector
@@ 'b <-> e'::tsquery AS plain;
-- with_and_not | plain
-- t | f (expected f | f)

With b at position 16381 instead, both return false.

Cause, as far as we can tell: the positions produced while evaluating the
phrase are kept in 16 bits but read back through the 14-bit position mask,
so a position past 16383 wraps around and lines up with e at position 2.

This matters for real documents: to_tsvector() places every word after
position 16383 at 16383 (the documented limit), so lexemes at 16383 are
common in long texts.

Related but different: BUG #17569 (false results from positions clamped at
the 16383 limit) and BUG #17542 (nested phrase operators). This report is
about wraparound in the phrase evaluation itself; the plain query above is
answered correctly.

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:49 BUG #19961: Deadlock detector misses a cycle between a READ ONLY DEFERRABLE safe-snapshot wait and a table lock
Previous Message PG Bug reporting form 2026-11-30 17:32:58 BUG #19959: tsquery = ignores weights and prefix flags, so queries that match different documents compare equal