pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19856: GiST index on tsquery misses empty tsquery rows for <@
2+ messages / 2 participants
[nested] [flat]

* BUG #19856: GiST index on tsquery misses empty tsquery rows for <@
@ 2026-11-09 12:57  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 2+ messages in thread

From: PG Bug reporting form @ 2026-11-09 12:57 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: 125atishprak@gmail.com

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.







^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #19856: GiST index on tsquery misses empty tsquery rows for <@
@ 2026-11-09 13:01  Atisss hh <125atishprak@gmail.com>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Atisss hh @ 2026-11-09 13:01 UTC (permalink / raw)
  To: 125atishprak@gmail.com; pgsql-bugs@lists.postgresql.org

On Mon, 9 Nov 2026 at 18:27, PG Bug reporting form <noreply@postgresql.org>
wrote:

> 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.
>
>
>

Attachments:

0001-fix-tsquery-gist-contained-by.patch (text/x-patch, 644B) download diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c index 2ca9795701..dcbd6e7dd7 100644 --- a/src/backend/utils/adt/tsquery_gist.c +++ b/src/backend/utils/adt/tsquery_gist.c @@ -78,7 +78,14 @@ gtsquery_consistent(PG_FUNCTION_ARGS) if (GIST_LEAF(entry)) retval = (key & sq) == key; else - retval = (key & sq) != 0; + { + /* + * An empty tsquery has an empty signature, and it is + * contained by anything. An internal key that does not + * overlap the query can still have such entries below it. + */ + retval = true; + } break; default: retval = false;
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-11-09 13:01 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 12:57 BUG #19856: GiST index on tsquery misses empty tsquery rows for <@ PG Bug reporting form <noreply@postgresql.org>
2026-11-09 13:01 ` Atisss hh <125atishprak@gmail.com>

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