pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: 125atishprak@gmail.com
Subject: BUG #19856: GiST index on tsquery misses empty tsquery rows for <@
Date: Mon, 09 Nov 2026 12:57:00 +0000
Message-ID: <19856-bdfec1ae4a0ffc4c@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19856
Logged by:          Atish Prakash
Email address:      125atishprak@gmail.com
PostgreSQL version: 18.6
Operating system:   Linux x86_64
Description:        

A GiST index on a tsquery column can miss rows holding an empty tsquery for
"column <@ query". No error is raised, apart from the usual NOTICE about a
query with
no lexemes.

    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

    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

Cause: an empty tsquery has an all-zero signature. For
RTContainedByStrategyNumber on
an internal page, gtsquery_consistent() descends only if (key & sq) != 0. A
subtree can
hold empty entries, which are contained by anything, even when its union key
does not
overlap the query, so that subtree is skipped.

This looks like the same mistake as the intarray report "intarray GiST index
gets wrong
answers for '{}' <@ anything":
https://www.postgresql.org/message-id/458.1565114141@sss.pgh.pa.us

A possible patch is attached (0001-fix-tsquery-gist-contained-by.patch). It
always
descends for this strategy on internal pages, as the signature cannot rule
out empty
entries. That makes <@ scans visit the whole index, a cost a maintainer may
want to
weigh. With it, the queries above return 100, and all 239 core regression
tests pass on
master. It adds no new test.

Empty tsquery values are probably rare in practice, so this is low severity.

Found by comparing index and sequential scans across the operator classes
listed in
the catalog. I reproduced and checked the result myself.







view thread (2+ messages)  latest in thread

Message-ID: <19856-bdfec1ae4a0ffc4c@postgresql.org>
Permalink:  ../19856-bdfec1ae4a0ffc4c@postgresql.org/
Also on:    postgresql.org/message-id/19856-bdfec1ae4a0ffc4c@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, 125atishprak@gmail.com
  Subject: Re: BUG #19856: GiST index on tsquery misses empty tsquery rows for <@
  In-Reply-To: <19856-bdfec1ae4a0ffc4c@postgresql.org>
  References: <19856-bdfec1ae4a0ffc4c@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox