From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: 125atishprak@gmail.com
Subject: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
Date: Mon, 09 Nov 2026 13:15:35 +0000
Message-ID: <19857-6e808a09ff4b75d2@postgresql.org> (raw)
The following bug has been logged on the website:
Bug reference: 19857
Logged by: Atish Prakash
Email address: 125atishprak@gmail.com
PostgreSQL version: 18.6
Operating system: Linux x86_64
Description:
If a range column has fewer than two non-empty values, ANALYZE stores an
empty
range length histogram. Restoring those statistics fails with a WARNING, so
range_empty_frac is lost too, and estimates change a lot.
CREATE TABLE r(a int4range);
INSERT INTO r SELECT 'empty' FROM generate_series(1, 10000);
ANALYZE r;
Then dump with "pg_dump --statistics" and restore into an empty database:
master: WARNING: "range_length_histogram" must be a one-dimensional
array
18.6: WARNING: argument "range_length_histogram" must be specified
when
argument "range_empty_frac" is specified (message shortened)
Estimates on master, with autovacuum disabled on the table so that a later
ANALYZE
does not replace the statistics:
query actual before after
SELECT * FROM r WHERE a <@ '[1,2)' 10000 10000 89
SELECT * FROM r WHERE a && '[1,2)' 0 1 178
A table with one non-empty value and 9,999 empty ones is affected the same
way
(estimate 9999 before, 87 after, for the first form of query, with actual
10000).
Multirange columns also produce the warning.
pg_upgrade loses them too: after an upgrade from 18.6 to 19rc1,
range_empty_frac for
table r went from 1 to NULL, and the first query above was estimated at 50
rows.
On 19rc1 and master the same check rejects most_common_elems when it is
empty, which
commit 261f89a976 makes common. An ordinary array column with all-distinct
elements
is enough, and tsvector columns are affected too:
CREATE TABLE tags(id int, a int[]);
INSERT INTO tags
SELECT i, ARRAY[i, i + 1000000] FROM generate_series(1, 50000) i;
ANALYZE tags;
WARNING: "most_common_elems" must be a one-dimensional array
query actual before after
SELECT * FROM tags WHERE a @> '{5}' 1 3 ~270
SELECT * FROM tags WHERE a && '{5,6}' 2 7 ~530
Extended statistics are hit harder on master. An empty array in one
expression's
statistics rejects the statistics for every expression in that object,
including
unrelated ones:
WARNING: could not parse "exprs": invalid element in expression -1
Expected: the statistics restore as they were, without a warning.
Cause: on master, pg_stats shows range_length_histogram as '{}' and pg_dump
emits
that. statatt_build_stavalues() requires a one-dimensional array, and an
empty
array has zero dimensions. On 18.6, pg_stats shows it as NULL, so pg_dump
omits it
while still emitting range_empty_frac, and the restore function rejects that
pair.
I have not written a patch.
Found with dump and restore differential tests. I reproduced and checked the
result myself.
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 #19857: Stats restore rejects empty arrays, losing range and array stats
In-Reply-To: <19857-6e808a09ff4b75d2@postgresql.org>
References: <19857-6e808a09ff4b75d2@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