pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #19857: Stats restore rejects empty arrays, losing range and array stats
13+ messages / 3 participants
[nested] [flat]
Commits from this thread:
2026-11-12 2656f66461 a3f99652ee Allow empty arrays of values in pg_restore_attribute_stats()
* BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-09 13:15 PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 13+ messages in thread
From: PG Bug reporting form @ 2026-11-09 13:15 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: 125atishprak@gmail.com
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.
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-10 17:01 Corey Huinker <corey.huinker@gmail.com>
parent: PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 13+ messages in thread
From: Corey Huinker @ 2026-11-10 17:01 UTC (permalink / raw)
To: 125atishprak@gmail.com; pgsql-bugs@lists.postgresql.org
On Tue, Nov 10, 2026 at 4:48 AM PG Bug reporting form <
noreply@postgresql.org> wrote:
> 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.
>
>
Looking into this, probably a duplicate of 19842
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-10 21:36 Corey Huinker <corey.huinker@gmail.com>
parent: Corey Huinker <corey.huinker@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Corey Huinker @ 2026-11-10 21:36 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: 125atishprak@gmail.com, Michael Paquier <michael@paquier.xyz>
>
>
>>
>>
> Looking into this, probably a duplicate of 19842
>
It's definitely a duplicate of 19842, but because it would need to be
backported to 18, we'd want to handle this one first, and build
the 19842-stuff on top of that.
I have relaxed the test in stats_check_required_arg() to allow for empty
arrays, which itself creates a burden of checking the number of dimensions
of an array before asking for the number of vals in the 0th dimension,
which needs to be done twice in attribute_statistics_update_internal() and
nowhere else, mostly because there are only two arrays (most_common_values
and most common_freqs) that need to have an equal number of values (MCELEM
has additional values over its freqs, and range_length_hisotgram is paired
with the scalar range_null_frac as a way to save slots, I presume.
Added additional test cases to show that the length agreement requirement
still works on MCV/MCF when one is empty, and that both store empty arrays
without complaint.
Attachments:
v1-0001-Allow-empty-arrays-as-input-for-pg_restore_attrib.patch (text/x-patch, 7.0K) download
From aec86883b9348b7c36c165f70ddf3f7f446757a9 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Tue, 10 Nov 2026 16:14:56 -0500
Subject: [PATCH v1 1/2] Allow empty arrays as input for
pg_restore_attribute_stats.
The attribute statistic code required that most_common_vals and
most_common_freqs arrays, among others, must be one dimensional, which
ruled out the possiblity of empty arrays.
This fix relaxes that requirement to allow for empty arrays , and then
makes all array length tests aware that an empty array has no
dimensions. Currently these checks only happen for most_common_vals and
most_common_freqs, as those two arrays must have the same length. All
other usages have no such requirement.
This was reported on its own in Bug #19857, which was recognized as
being the same problem as Bug #19842. This fix is provided on its own
because it needs to be backported to 18, whereas 19857 only needs to be
backported to 19.
Reported-by: Atish Prakash <125atishprak@gmail.com>
Reported-by: chunling qin <303677365@qq.com>
Author: Corey Huinker <corey.huinker@gmail.com>
Discussion: https://postgr.es/m/19842-ca042326dd2430a4@postgresql.org
Bug: #19842
Bug: #19857
Backpatch-through: 18
---
src/backend/statistics/attribute_stats.c | 4 +-
src/backend/statistics/stat_utils.c | 8 ++--
src/test/regress/expected/stats_import.out | 52 +++++++++++++++++++++-
src/test/regress/sql/stats_import.sql | 31 +++++++++++++
4 files changed, 88 insertions(+), 7 deletions(-)
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index f4bb846e88f..027b90b8ea7 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -405,8 +405,8 @@ attribute_statistics_update_internal(Oid reloid,
{
ArrayType *vals_arr = DatumGetArrayTypeP(stavalues);
ArrayType *nums_arr = DatumGetArrayTypeP(stanumbers);
- int nvals = ARR_DIMS(vals_arr)[0];
- int nnums = ARR_DIMS(nums_arr)[0];
+ int nvals = (ARR_NDIM(vals_arr) > 0) ? ARR_DIMS(vals_arr)[0] : 0;
+ int nnums = (ARR_NDIM(nums_arr) > 0) ? ARR_DIMS(nums_arr)[0] : 0;
if (nvals != nnums)
{
diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c
index e8117ede407..7929a6b6827 100644
--- a/src/backend/statistics/stat_utils.c
+++ b/src/backend/statistics/stat_utils.c
@@ -64,7 +64,7 @@ stats_check_required_arg(FunctionCallInfo fcinfo,
/*
* Check that argument is either NULL or a one dimensional array with no
- * NULLs.
+ * NULLs, or an empty array.
*
* If a problem is found, emit a WARNING, and return false. Otherwise return
* true.
@@ -81,7 +81,7 @@ stats_check_arg_array(FunctionCallInfo fcinfo,
arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
- if (ARR_NDIM(arr) != 1)
+ if (ARR_NDIM(arr) > 1)
{
ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -622,11 +622,11 @@ statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid ty
pfree(s);
- if (ARR_NDIM(DatumGetArrayTypeP(result)) != 1)
+ if (ARR_NDIM(DatumGetArrayTypeP(result)) > 1)
{
ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("\"%s\" must be a one-dimensional array", staname)));
+ errmsg("\"%s\" must be a one-dimensional array or empty", staname)));
*ok = false;
return (Datum) 0;
}
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 60a0f20b488..8f91dc3a70d 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -967,7 +967,7 @@ SELECT pg_catalog.pg_restore_attribute_stats(
'most_common_vals', '{{2,1},{3,4}}'::text,
'most_common_freqs', '{0.3,0.25,0.05,0.04}'::real[]
);
-WARNING: "most_common_vals" must be a one-dimensional array
+WARNING: "most_common_vals" must be a one-dimensional array or empty
pg_restore_attribute_stats
----------------------------
f
@@ -4324,6 +4324,56 @@ SELECT count(*) FROM pg_statistic s
0
(1 row)
+-- Bug #19842/#19857
+SELECT * FROM pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'arange',
+ 'inherited', 'f'::boolean,
+ 'range_length_histogram', '{}'::text,
+ 'range_empty_frac', '1'::real);
+ pg_restore_attribute_stats
+----------------------------
+ t
+(1 row)
+
+-- Bug #19842/#19857 - one empty mismatch
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{1,3}'::text,
+ 'most_common_freqs', '{}'::real[]);
+WARNING: could not parse "most_common_vals": incorrect number of elements (same as "most_common_freqs" required)
+ pg_restore_attribute_stats
+----------------------------
+ f
+(1 row)
+
+-- Bug #19842/#19857 - both empty
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{}'::text,
+ 'most_common_freqs', '{}'::real[]);
+ pg_restore_attribute_stats
+----------------------------
+ t
+(1 row)
+
+SELECT most_common_vals, most_common_freqs
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND attname = 'id';
+ most_common_vals | most_common_freqs
+------------------+-------------------
+ {} | {}
+(1 row)
+
DROP SCHEMA stats_import CASCADE;
NOTICE: drop cascades to 39 other objects
DETAIL: drop cascades to view stats_import.pg_stats_stable
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index e537a9d6f67..5bcd86288bf 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -3063,4 +3063,35 @@ SELECT count(*) FROM pg_statistic s
AND a.attnum = s.staattnum
AND NOT a.attisdropped);
+-- Bug #19842/#19857
+SELECT * FROM pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'arange',
+ 'inherited', 'f'::boolean,
+ 'range_length_histogram', '{}'::text,
+ 'range_empty_frac', '1'::real);
+-- Bug #19842/#19857 - one empty mismatch
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{1,3}'::text,
+ 'most_common_freqs', '{}'::real[]);
+-- Bug #19842/#19857 - both empty
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{}'::text,
+ 'most_common_freqs', '{}'::real[]);
+
+SELECT most_common_vals, most_common_freqs
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND attname = 'id';
+
DROP SCHEMA stats_import CASCADE;
base-commit: 952bab8b74d40fcfd25e1949865c3907e676504e
--
2.56.0
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-11 01:50 Corey Huinker <corey.huinker@gmail.com>
parent: Corey Huinker <corey.huinker@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Corey Huinker @ 2026-11-11 01:50 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: 125atishprak@gmail.com, Michael Paquier <michael@paquier.xyz>
Same patch, commit message updated to have both discussion threads.
Attachments:
v2-0001-Allow-empty-arrays-as-input-for-pg_restore_attrib.patch (text/x-patch, 7.0K) download
From 6cca2e003c1e43d4b3369e8802c116cc89691fed Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Tue, 10 Nov 2026 16:14:56 -0500
Subject: [PATCH v2 1/3] Allow empty arrays as input for
pg_restore_attribute_stats.
The attribute statistic code required that most_common_vals and
most_common_freqs arrays, among others, must be one dimensional, which
ruled out the possiblity of empty arrays.
This fix relaxes that requirement to allow for empty arrays , and then
makes all array length tests aware that an empty array has no
dimensions. Currently these checks only happen for most_common_vals and
most_common_freqs, as those two arrays must have the same length. All
other usages have no such requirement.
This was reported on its own in Bug #19857, which was recognized as
being the same problem as Bug #19842. This fix is provided on its own
because it needs to be backported to 18, whereas 19857 only needs to be
backported to 19.
Reported-by: Atish Prakash <125atishprak@gmail.com>
Reported-by: chunling qin <303677365@qq.com>
Author: Corey Huinker <corey.huinker@gmail.com>
Discussion: https://postgr.es/m/19842-6e808a09ff4b75d2@postgresql.org
Discussion: https://postgr.es/m/19857-6e808a09ff4b75d2@postgresql.org
Bug: #19842
Bug: #19857
Backpatch-through: 18
---
src/backend/statistics/attribute_stats.c | 4 +-
src/backend/statistics/stat_utils.c | 8 ++--
src/test/regress/expected/stats_import.out | 52 +++++++++++++++++++++-
src/test/regress/sql/stats_import.sql | 31 +++++++++++++
4 files changed, 88 insertions(+), 7 deletions(-)
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index f4bb846e88f..027b90b8ea7 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -405,8 +405,8 @@ attribute_statistics_update_internal(Oid reloid,
{
ArrayType *vals_arr = DatumGetArrayTypeP(stavalues);
ArrayType *nums_arr = DatumGetArrayTypeP(stanumbers);
- int nvals = ARR_DIMS(vals_arr)[0];
- int nnums = ARR_DIMS(nums_arr)[0];
+ int nvals = (ARR_NDIM(vals_arr) > 0) ? ARR_DIMS(vals_arr)[0] : 0;
+ int nnums = (ARR_NDIM(nums_arr) > 0) ? ARR_DIMS(nums_arr)[0] : 0;
if (nvals != nnums)
{
diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c
index e8117ede407..7929a6b6827 100644
--- a/src/backend/statistics/stat_utils.c
+++ b/src/backend/statistics/stat_utils.c
@@ -64,7 +64,7 @@ stats_check_required_arg(FunctionCallInfo fcinfo,
/*
* Check that argument is either NULL or a one dimensional array with no
- * NULLs.
+ * NULLs, or an empty array.
*
* If a problem is found, emit a WARNING, and return false. Otherwise return
* true.
@@ -81,7 +81,7 @@ stats_check_arg_array(FunctionCallInfo fcinfo,
arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
- if (ARR_NDIM(arr) != 1)
+ if (ARR_NDIM(arr) > 1)
{
ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -622,11 +622,11 @@ statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid ty
pfree(s);
- if (ARR_NDIM(DatumGetArrayTypeP(result)) != 1)
+ if (ARR_NDIM(DatumGetArrayTypeP(result)) > 1)
{
ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("\"%s\" must be a one-dimensional array", staname)));
+ errmsg("\"%s\" must be a one-dimensional array or empty", staname)));
*ok = false;
return (Datum) 0;
}
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 60a0f20b488..8f91dc3a70d 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -967,7 +967,7 @@ SELECT pg_catalog.pg_restore_attribute_stats(
'most_common_vals', '{{2,1},{3,4}}'::text,
'most_common_freqs', '{0.3,0.25,0.05,0.04}'::real[]
);
-WARNING: "most_common_vals" must be a one-dimensional array
+WARNING: "most_common_vals" must be a one-dimensional array or empty
pg_restore_attribute_stats
----------------------------
f
@@ -4324,6 +4324,56 @@ SELECT count(*) FROM pg_statistic s
0
(1 row)
+-- Bug #19842/#19857
+SELECT * FROM pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'arange',
+ 'inherited', 'f'::boolean,
+ 'range_length_histogram', '{}'::text,
+ 'range_empty_frac', '1'::real);
+ pg_restore_attribute_stats
+----------------------------
+ t
+(1 row)
+
+-- Bug #19842/#19857 - one empty mismatch
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{1,3}'::text,
+ 'most_common_freqs', '{}'::real[]);
+WARNING: could not parse "most_common_vals": incorrect number of elements (same as "most_common_freqs" required)
+ pg_restore_attribute_stats
+----------------------------
+ f
+(1 row)
+
+-- Bug #19842/#19857 - both empty
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{}'::text,
+ 'most_common_freqs', '{}'::real[]);
+ pg_restore_attribute_stats
+----------------------------
+ t
+(1 row)
+
+SELECT most_common_vals, most_common_freqs
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND attname = 'id';
+ most_common_vals | most_common_freqs
+------------------+-------------------
+ {} | {}
+(1 row)
+
DROP SCHEMA stats_import CASCADE;
NOTICE: drop cascades to 39 other objects
DETAIL: drop cascades to view stats_import.pg_stats_stable
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index e537a9d6f67..5bcd86288bf 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -3063,4 +3063,35 @@ SELECT count(*) FROM pg_statistic s
AND a.attnum = s.staattnum
AND NOT a.attisdropped);
+-- Bug #19842/#19857
+SELECT * FROM pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'arange',
+ 'inherited', 'f'::boolean,
+ 'range_length_histogram', '{}'::text,
+ 'range_empty_frac', '1'::real);
+-- Bug #19842/#19857 - one empty mismatch
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{1,3}'::text,
+ 'most_common_freqs', '{}'::real[]);
+-- Bug #19842/#19857 - both empty
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{}'::text,
+ 'most_common_freqs', '{}'::real[]);
+
+SELECT most_common_vals, most_common_freqs
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND attname = 'id';
+
DROP SCHEMA stats_import CASCADE;
base-commit: 952bab8b74d40fcfd25e1949865c3907e676504e
--
2.56.0
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-11 07:41 Michael Paquier <michael@paquier.xyz>
parent: Corey Huinker <corey.huinker@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2026-11-11 07:41 UTC (permalink / raw)
To: Corey Huinker <corey.huinker@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Tue, Nov 10, 2026 at 08:50:39PM -0500, Corey Huinker wrote:
> Same patch, commit message updated to have both discussion threads.
arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
- if (ARR_NDIM(arr) != 1)
+ if (ARR_NDIM(arr) > 1)
{
ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
This change would trigger a CVE, I suspect.. Under valgrind, and for
example these sequences:
CREATE TABLE t (a int);
-- most_common_freqs
SELECT pg_restore_attribute_stats(
'schemaname', 'public', 'relname', 't', 'attname', 'a',
'inherited', false,
'most_common_vals', '{}'::text,
'most_common_freqs', '{}'::real[]);
EXPLAIN SELECT * FROM t WHERE a = 1;
CREATE TABLE t2 (b int[]);
-- most_common_elem_freqs
SELECT pg_restore_attribute_stats(
'schemaname', 'public', 'relname', 't2', 'attname', 'b',
'inherited', false,
'most_common_elems', '{}'::text,
'most_common_elem_freqs', '{}'::real[]);
EXPLAIN SELECT * FROM t2 WHERE b @> '{1}';
-- elem_count_histogram
SELECT pg_clear_attribute_stats('public', 't2', 'b', false);
SELECT pg_restore_attribute_stats(
'schemaname', 'public', 'relname', 't2', 'attname', 'b',
'inherited', false,
'elem_count_histogram', '{}'::real[]);
EXPLAIN SELECT * FROM t2, unnest(b);
Then get_attstatsslot() does an out-of-bound read for
most_common_freqs, most_common_elem_freqs and elem_count_histogram
because an array with 0 dimension has no follow-up data as far as I
can see, giving this pattern:
==804366== Invalid read of size 4
==804366== at 0x1A58924: get_attstatsslot (lsyscache.c:3636)
==804366== by 0x1950B6C: var_eq_const (selfuncs.c:422)
==804366== by 0x195063F: eqsel_internal (selfuncs.c:349)
==804366== by 0x1950297: eqsel (selfuncs.c:302)
==804366== by 0x1ACE474: FunctionCall4Coll (fmgr.c:1215)
==804366== by 0x1AD0C37: OidFunctionCall4Coll (fmgr.c:1451)
==804366== by 0x122CC7F: restriction_selectivity (plancat.c:2242)
==804366== by 0x1063476: clause_selectivity_ext (clausesel.c:854)
==804366== by 0x1060B84: clauselist_selectivity_ext (clausesel.c:136)
==804366== by 0x1060AB1: clauselist_selectivity (clausesel.c:106)
==804366== by 0x10823BD: set_baserel_size_estimates (costsize.c:5615)
==804366== by 0x104B64A: set_plain_rel_size (allpaths.c:641)
Instead of allowing all these arrays to be empty, we should snipe at
range_length_histogram *and* most_common_elems, and only all these
two without generalizing the rule. The other three are rejected
anyway at load, so blocking them at restore would be a consistency
move.
Perhaps we should update get_attstatsslot() as well to avoid the
over-read issue to begin with; that feels like a sloppy thing to do if
there is an empty array to process.
We'll likely need to do something similar for the extstats. One step
at a time. We could of course do a more relaxing approach like you
are proposing, but I cannot really get excited to let that go through
at restore time as the loads would fail even if get_attstatsslot() is
adjusted (actually this path should be adjusted anyway), and the
option-basis thing is not that invasive in the attached, at least it
seems so to me.
Attached is a cleaned-up patch. So, thoughts or comments?
--
Michael
Attachments:
0001-Allow-empty-arrays-of-values-in-pg_restore_attribute.patch (text/plain, 9.6K) download
From f6a8026a4b995e0d0dc55bd73b770ddfa4c77889 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 11 Nov 2026 16:25:33 +0900
Subject: [PATCH] Allow empty arrays of values in pg_restore_attribute_stats()
ANALYZE can store an empty array of values for range_length_histogram
and most_common_elems, which pg_dump emits as '{}'. These were rejected
by pg_restore_attribute_stats(), as an empty array has no dimensions,
and the statistics were lost after a dump/restore or an upgrade.
This commit allows empty arrays for these two cases only. Arrays stored
as stanumbers are still required to be non-empty, as ANALYZE never
generates them. get_attstatsslot() fails on them.
Some tests are added to cover both the authorized and non-authorized
patterns, in case somebody has a change of mind in the future regarding
one or more of them.
Reported-by: Atish Prakash <125atishprak@gmail.com>
Author: Corey Huinker <corey.huinker@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/19857-6e808a09ff4b75d2@postgresql.org
Backpatch-through: 18
---
src/include/statistics/stat_utils.h | 3 +-
src/backend/statistics/attribute_stats.c | 10 ++--
src/backend/statistics/stat_utils.c | 12 +++-
src/test/regress/expected/stats_import.out | 67 ++++++++++++++++++++++
src/test/regress/sql/stats_import.sql | 40 +++++++++++++
5 files changed, 123 insertions(+), 9 deletions(-)
diff --git a/src/include/statistics/stat_utils.h b/src/include/statistics/stat_utils.h
index 957bddc5b2b9..d160f9fbd4bd 100644
--- a/src/include/statistics/stat_utils.h
+++ b/src/include/statistics/stat_utils.h
@@ -57,7 +57,8 @@ extern void statatt_set_slot(Datum *values, bool *nulls, bool *replaces,
Datum stavalues, bool stavalues_isnull);
extern Datum statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d,
- Oid typid, int32 typmod, bool *ok);
+ Oid typid, int32 typmod, bool allow_empty,
+ bool *ok);
extern bool statatt_get_elem_type(TypeCacheEntry *basetypcache,
Oid *elemtypid, Oid *elem_eq_opr);
extern bool statatt_get_range_type(TypeCacheEntry *basetypcache,
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index f4bb846e88fb..48e827d16af2 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -399,7 +399,7 @@ attribute_statistics_update_internal(Oid reloid,
&array_in_fn,
PG_GETARG_DATUM(MOST_COMMON_VALS_ARG),
atttypid, atttypmod,
- &converted);
+ false, &converted);
if (converted)
{
@@ -439,7 +439,7 @@ attribute_statistics_update_internal(Oid reloid,
&array_in_fn,
PG_GETARG_DATUM(HISTOGRAM_BOUNDS_ARG),
atttypid, atttypmod,
- &converted);
+ false, &converted);
if (converted)
{
@@ -476,7 +476,7 @@ attribute_statistics_update_internal(Oid reloid,
&array_in_fn,
PG_GETARG_DATUM(MOST_COMMON_ELEMS_ARG),
elemtypid, atttypmod,
- &converted);
+ true, &converted);
if (converted)
{
@@ -518,7 +518,7 @@ attribute_statistics_update_internal(Oid reloid,
&array_in_fn,
PG_GETARG_DATUM(RANGE_BOUNDS_HISTOGRAM_ARG),
bounds_typid, atttypmod,
- &converted);
+ false, &converted);
if (converted &&
statatt_check_bounds_histogram(stavalues))
@@ -546,7 +546,7 @@ attribute_statistics_update_internal(Oid reloid,
stavalues = statatt_build_stavalues("range_length_histogram",
&array_in_fn,
PG_GETARG_DATUM(RANGE_LENGTH_HISTOGRAM_ARG),
- FLOAT8OID, 0, &converted);
+ FLOAT8OID, 0, true, &converted);
if (converted)
{
diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c
index e8117ede407b..9f8da2202c7b 100644
--- a/src/backend/statistics/stat_utils.c
+++ b/src/backend/statistics/stat_utils.c
@@ -85,7 +85,7 @@ stats_check_arg_array(FunctionCallInfo fcinfo,
{
ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("argument \"%s\" must not be a multidimensional array",
+ errmsg("argument \"%s\" must be a non-empty one-dimensional array",
arginfo[argnum].argname)));
return false;
}
@@ -597,11 +597,16 @@ statatt_get_range_type(TypeCacheEntry *basetypcache, Oid *rangetypid)
* If an error is encountered, capture it and throw a WARNING, with "ok" set
* to false. If the resulting array contains NULLs, raise a WARNING and
* set "ok" to false. When the operation succeeds, set "ok" to true.
+ *
+ * The array must be one-dimensional. If "allow_empty" is true, an empty
+ * array is also accepted.
*/
Datum
statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid,
- int32 typmod, bool *ok)
+ int32 typmod, bool allow_empty, bool *ok)
{
+ int ndim;
+
char *s;
Datum result;
ErrorSaveContext escontext = {T_ErrorSaveContext};
@@ -622,7 +627,8 @@ statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid ty
pfree(s);
- if (ARR_NDIM(DatumGetArrayTypeP(result)) != 1)
+ ndim = ARR_NDIM(DatumGetArrayTypeP(result));
+ if (ndim != 1 && !(allow_empty && ndim == 0))
{
ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 60a0f20b488e..bc9ab2498736 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -4324,6 +4324,73 @@ SELECT count(*) FROM pg_statistic s
0
(1 row)
+-- Empty arrays of values
+-- ok: range_length_histogram
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'arange',
+ 'inherited', false::boolean,
+ 'range_length_histogram', '{}'::text,
+ 'range_empty_frac', '1'::real);
+ pg_restore_attribute_stats
+----------------------------
+ t
+(1 row)
+
+-- ok: most_common_elems
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'tags',
+ 'inherited', false::boolean,
+ 'most_common_elems', '{}'::text,
+ 'most_common_elem_freqs', '{0.1,0.1,0}'::real[]);
+ pg_restore_attribute_stats
+----------------------------
+ t
+(1 row)
+
+SELECT attname, range_length_histogram, range_empty_frac,
+ most_common_elems, most_common_elem_freqs
+ FROM pg_stats
+ WHERE schemaname = 'stats_import'
+ AND tablename = 'test'
+ AND attname IN ('arange', 'tags')
+ ORDER BY attname;
+ attname | range_length_histogram | range_empty_frac | most_common_elems | most_common_elem_freqs
+---------+------------------------+------------------+-------------------+------------------------
+ arange | {} | 1 | |
+ tags | | | {} | {0.1,0.1,0}
+(2 rows)
+
+-- warn: most_common_vals and most_common_freqs
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{}'::text,
+ 'most_common_freqs', '{}'::real[]);
+WARNING: argument "most_common_freqs" must be a non-empty one-dimensional array
+ pg_restore_attribute_stats
+----------------------------
+ f
+(1 row)
+
+-- warn: histogram_bounds
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'histogram_bounds', '{}'::text);
+WARNING: "histogram_bounds" must be a one-dimensional array
+ pg_restore_attribute_stats
+----------------------------
+ f
+(1 row)
+
DROP SCHEMA stats_import CASCADE;
NOTICE: drop cascades to 39 other objects
DETAIL: drop cascades to view stats_import.pg_stats_stable
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index e537a9d6f67b..6e682b83493d 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -3063,4 +3063,44 @@ SELECT count(*) FROM pg_statistic s
AND a.attnum = s.staattnum
AND NOT a.attisdropped);
+-- Empty arrays of values
+-- ok: range_length_histogram
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'arange',
+ 'inherited', false::boolean,
+ 'range_length_histogram', '{}'::text,
+ 'range_empty_frac', '1'::real);
+-- ok: most_common_elems
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'tags',
+ 'inherited', false::boolean,
+ 'most_common_elems', '{}'::text,
+ 'most_common_elem_freqs', '{0.1,0.1,0}'::real[]);
+SELECT attname, range_length_histogram, range_empty_frac,
+ most_common_elems, most_common_elem_freqs
+ FROM pg_stats
+ WHERE schemaname = 'stats_import'
+ AND tablename = 'test'
+ AND attname IN ('arange', 'tags')
+ ORDER BY attname;
+-- warn: most_common_vals and most_common_freqs
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'most_common_vals', '{}'::text,
+ 'most_common_freqs', '{}'::real[]);
+-- warn: histogram_bounds
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'attname', 'id',
+ 'inherited', false::boolean,
+ 'histogram_bounds', '{}'::text);
+
DROP SCHEMA stats_import CASCADE;
--
2.56.0
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-11 13:46 Corey Huinker <corey.huinker@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Corey Huinker @ 2026-11-11 13:46 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Wed, Nov 11, 2026 at 2:41 AM Michael Paquier <michael@paquier.xyz> wrote:
> On Tue, Nov 10, 2026 at 08:50:39PM -0500, Corey Huinker wrote:
> > Same patch, commit message updated to have both discussion threads.
>
> arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
>
> - if (ARR_NDIM(arr) != 1)
> + if (ARR_NDIM(arr) > 1)
> {
> ereport(WARNING,
> (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
>
Nitpick: I think the error message itself needs to change based on
allow_empty, as true allows 1-D or empty, and false allows only 1-D.
> Instead of allowing all these arrays to be empty, we should snipe at
> range_length_histogram *and* most_common_elems, and only all these
> two without generalizing the rule. The other three are rejected
> anyway at load, so blocking them at restore would be a consistency
> move.
>
I thought about that, and wanted to at least try this method first. I'm
glad we could rule it out early.
Perhaps we should update get_attstatsslot() as well to avoid the
> over-read issue to begin with; that feels like a sloppy thing to do if
> there is an empty array to process.
>
> We'll likely need to do something similar for the extstats. One step
> at a time. We could of course do a more relaxing approach like you
> are proposing, but I cannot really get excited to let that go through
> at restore time as the loads would fail even if get_attstatsslot() is
> adjusted (actually this path should be adjusted anyway), and the
> option-basis thing is not that invasive in the attached, at least it
> seems so to me.
>
The more relaxing approach just means a lot more NDIM-checks, which undoes
the elegance of the initial fix.
This way seems not invasive at all, so I like it better.
> Attached is a cleaned-up patch. So, thoughts or comments?
I'll redo the ext stats patchset to this pattern. Do you intend to commit
this soon? Asking so I know whether to rebase-away the 0001 patch.
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-11 22:40 Michael Paquier <michael@paquier.xyz>
parent: Corey Huinker <corey.huinker@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2026-11-11 22:40 UTC (permalink / raw)
To: Corey Huinker <corey.huinker@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Wed, Nov 11, 2026 at 08:46:56AM -0500, Corey Huinker wrote:
> Nitpick: I think the error message itself needs to change based on
> allow_empty, as true allows 1-D or empty, and false allows only 1-D.
Yeah, point taken that this could be a little bit better, in a shape
similar to what you are suggesting. I can see that we have similar
requirements in some of the array functions, though.. Will try to
shape that this way.
> This way seems not invasive at all, so I like it better.
Okay, noted.
> I'll redo the ext stats patchset to this pattern. Do you intend to commit
> this soon? Asking so I know whether to rebase-away the 0001 patch.
Yeah, I intend to wrap this one this morning.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-12 01:26 Michael Paquier <michael@paquier.xyz>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2026-11-12 01:26 UTC (permalink / raw)
To: Corey Huinker <corey.huinker@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Thu, Nov 12, 2026 at 07:40:47AM +0900, Michael Paquier wrote:
> Yeah, I intend to wrap this one this morning.
Applied this down to v19, to match with the empty array cases that
have been introduced in 261f89a976b.
I think that this leaves an extra case, though... In v17 and v18, it
is possible for range_empty_frac to be stored with
range_length_histogram as NULL. On v19 and HEAD,
range_length_histogram is an empty array, so the two are always set.
The restore functions require the two together when upgrading to 18~,
meaning that some stats values could be lost when upgrading/dumping
the stats from a v17 or v18.
That's one pattern that Atish was mentioning originally in his report,
even if I got a bit lost in my mind due to the dump/upgrade mentions
with v19 and HEAD.
I cannot really get excited to allow this combination in the restore
functions, though, as it means being more lenient on
range_length_histogram forever, and the v19~ decision of using an
empty array is much better to me in the long term. An extra argument
is that this case is super narrow anyway, unlikely to be hit in
practice? As far as I can see, you need a [multi]range column with
less than *2* non-empty values..
I agree that we should move on with fixing the extstats restore
functions, of course, in terms of the handling of the empty array
values, to map now with what the attribute restores are able to do.
And fix this second issue down to v19. Let's do that on the other
thread, we are done here for me.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-12 01:32 Michael Paquier <michael@paquier.xyz>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2026-11-12 01:32 UTC (permalink / raw)
To: Corey Huinker <corey.huinker@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Thu, Nov 12, 2026 at 10:26:00AM +0900, Michael Paquier wrote:
> I cannot really get excited to allow this combination in the restore
> functions, though, as it means being more lenient on
> range_length_histogram forever, and the v19~ decision of using an
> empty array is much better to me in the long term. An extra argument
> is that this case is super narrow anyway, unlikely to be hit in
> practice? As far as I can see, you need a [multi]range column with
> less than *2* non-empty values..
Or we could do something on the dump side, forcing an empty array out
if seeing a NULL range_length_histogram.. Still not sure if that's
worth bothering, just saying.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-12 04:47 Corey Huinker <corey.huinker@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Corey Huinker @ 2026-11-12 04:47 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Wed, Nov 11, 2026 at 8:32 PM Michael Paquier <michael@paquier.xyz> wrote:
> On Thu, Nov 12, 2026 at 10:26:00AM +0900, Michael Paquier wrote:
> > I cannot really get excited to allow this combination in the restore
> > functions, though, as it means being more lenient on
> > range_length_histogram forever, and the v19~ decision of using an
> > empty array is much better to me in the long term. An extra argument
> > is that this case is super narrow anyway, unlikely to be hit in
> > practice? As far as I can see, you need a [multi]range column with
> > less than *2* non-empty values..
>
> Or we could do something on the dump side, forcing an empty array out
> if seeing a NULL range_length_histogram.. Still not sure if that's
> worth bothering, just saying.
> --
> Michael
>
I had also considered fixing it dump-side as well, which seems cleaner but
also means that the bug would persist in pre-existing dump files, though as
you mention it may not be worth bothering because this is a corner case
that only occurs if the histogram was less than 2 elements long, thus the
query plan wouldn't be that far off. Also, such a pre-existing dump file
would itself be restored to a cold-start database, and therefore unlikely
to be getting flooded with queries before vacuumdb has had time to run in
the way that a pg_upgrade-ed database would.
Commit looks good on master and REL_19_STABLE.
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-12 04:55 Corey Huinker <corey.huinker@gmail.com>
parent: Corey Huinker <corey.huinker@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Corey Huinker @ 2026-11-12 04:55 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
> Commit looks good on master and REL_19_STABLE.
>
But the problem still exists on 18, and that isn't a clean backport.
Michael, do you want me to take that one?
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-12 05:10 Michael Paquier <michael@paquier.xyz>
parent: Corey Huinker <corey.huinker@gmail.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2026-11-12 05:10 UTC (permalink / raw)
To: Corey Huinker <corey.huinker@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Wed, Nov 11, 2026 at 11:55:08PM -0500, Corey Huinker wrote:
> But the problem still exists on 18, and that isn't a clean backport.
> Michael, do you want me to take that one?
The only problem that can be reached is if we dump or upgrade from a
v17 or v18 instance with the pattern of a range_empty_frac set with
range_length_histogram as NULL, where we could perhaps switch
range_length_histogram to '{}' under-the-hood on the dump side if a
range_empty_frac exists. I don't want the restore functions to live
eternally with a path authorizing for range_length_histogram=NULL if
there is a range_empty_frac, especially now that
range_length_histogram has to be an empty array since v19~.
And the end, I have no plans to bother based on the table definition
this requires. I could still care with something on the dump side,
but I doubt that's worth the effort anyway. What's been done on HEAD
and v19 is enough for these new empty-array cases. :)
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #19857: Stats restore rejects empty arrays, losing range and array stats
@ 2026-11-12 05:31 Corey Huinker <corey.huinker@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 0 replies; 13+ messages in thread
From: Corey Huinker @ 2026-11-12 05:31 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-bugs@lists.postgresql.org; 125atishprak@gmail.com
On Thu, Nov 12, 2026 at 12:10 AM Michael Paquier <michael@paquier.xyz>
wrote:
> On Wed, Nov 11, 2026 at 11:55:08PM -0500, Corey Huinker wrote:
> > But the problem still exists on 18, and that isn't a clean backport.
> > Michael, do you want me to take that one?
>
> The only problem that can be reached is if we dump or upgrade from a
> v17 or v18 instance with the pattern of a range_empty_frac set with
> range_length_histogram as NULL, where we could perhaps switch
> range_length_histogram to '{}' under-the-hood on the dump side if a
> range_empty_frac exists. I don't want the restore functions to live
> eternally with a path authorizing for range_length_histogram=NULL if
> there is a range_empty_frac, especially now that
> range_length_histogram has to be an empty array since v19~.
>
> And the end, I have no plans to bother based on the table definition
> this requires. I could still care with something on the dump side,
> but I doubt that's worth the effort anyway. What's been done on HEAD
> and v19 is enough for these new empty-array cases. :)
> --
> Michael
>
Sorry, I should have clarified - I was considering "18 can't import
{}+empty_frac" a separate over the already-settled-not-gonna-do-it "18
can't import NULL+empty_frac" issue, but your response above indicates even
less appetite for allowing what would have to be synthetic stats just
because that's the future valid value.
I'll make the v18-specific pg_dump fix and additional patch in the other
thread.
^ permalink raw reply [nested|flat] 13+ messages in thread
end of thread, other threads:[~2026-11-12 05:31 UTC | newest]
Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 13:15 BUG #19857: Stats restore rejects empty arrays, losing range and array stats PG Bug reporting form <noreply@postgresql.org>
2026-11-10 17:01 ` Corey Huinker <corey.huinker@gmail.com>
2026-11-10 21:36 ` Corey Huinker <corey.huinker@gmail.com>
2026-11-11 01:50 ` Corey Huinker <corey.huinker@gmail.com>
2026-11-11 07:41 ` Michael Paquier <michael@paquier.xyz>
2026-11-11 13:46 ` Corey Huinker <corey.huinker@gmail.com>
2026-11-11 22:40 ` Michael Paquier <michael@paquier.xyz>
2026-11-12 01:26 ` Michael Paquier <michael@paquier.xyz>
2026-11-12 01:32 ` Michael Paquier <michael@paquier.xyz>
2026-11-12 04:47 ` Corey Huinker <corey.huinker@gmail.com>
2026-11-12 04:55 ` Corey Huinker <corey.huinker@gmail.com>
2026-11-12 05:10 ` Michael Paquier <michael@paquier.xyz>
2026-11-12 05:31 ` Corey Huinker <corey.huinker@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