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 #19849: queries fail after ANALYZE with expression stats on arrays
Date: Sat, 07 Nov 2026 15:36:50 +0000
Message-ID: <19849-38b8bd2fea807154@postgresql.org> (raw)

The following bug has been logged on the website:

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

With extended statistics on an array or tsvector expression, once ANALYZE
finds no
elements common enough to store, queries that use the statistics fail at
planning
time with an internal error. Elements that are all distinct are enough to
trigger
this, as is a column of empty arrays.

    CREATE TABLE tags(id int, a int[]);
    INSERT INTO tags
        SELECT i, ARRAY[i, i + 1000000] FROM generate_series(1, 50000) i;
    CREATE STATISTICS s_a ON (a || a) FROM tags;
    ANALYZE tags;

    SELECT count(*) FROM tags WHERE (a || a) @> '{5}';

Actual, on 19rc1 and master:

    ERROR:  unexpected null value in cached tuple for catalog pg_statistic
column stavalues3

Expected, as on 18.6: the query runs and returns 1.

Also failing the same way:
- (t || t) on text[], with @>
- (v || v) on tsvector, with @@ (the message names stavalues1)
- the array operators && and <@, and = ANY

A range expression with no non-empty values also stores a NULL stavalues,
but the
query I tried, (a * a) && '[1,2)', did not fail.

A plain column with the same data, and an expression index with the same
data, do
not fail.

Cause: commit 261f89a976 ("Track the maximum possible frequency of non-MCE
array
elements") stores a statistics slot even when there are no values. It
changed the
test in update_attstats() from numvalues > 0 to stavalues != NULL, but
serialize_expr_stats() in extended_stats.c still tests numvalues[k] > 0, so
the slot
kind and numbers are stored with a NULL stavalues. get_attstatsslot() then
fails in
SysCacheGetAttrNotNull(). I identified the commit from the code and the
commit
message, and did not bisect.

A possible patch is attached
(0001-fix-expression-stats-empty-stavalues.patch). It
changes that test to match update_attstats(). With it, the queries above
return
correct results on master, and all 239 core regression tests pass. It adds
no new
test.

Found with statistics round-trip tests. I reproduced and checked the result
myself.







view thread (12+ messages)  latest in thread

Message-ID: <19849-38b8bd2fea807154@postgresql.org>
Permalink:  ../19849-38b8bd2fea807154@postgresql.org/
Also on:    postgresql.org/message-id/19849-38b8bd2fea807154@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 #19849: queries fail after ANALYZE with expression stats on arrays
  In-Reply-To: <19849-38b8bd2fea807154@postgresql.org>
  References: <19849-38b8bd2fea807154@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