From: Michael Paquier <michael@paquier.xyz>
To: shihao zhong <zhong950419@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Atisss hh <125atishprak@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #19849: queries fail after ANALYZE with expression stats on arrays
Date: Tue, 10 Nov 2026 15:10:42 +0900
Message-ID: <avK1Yn8px0eAqiPQ@paquier.xyz> (raw)
In-Reply-To: <CAGRkXqSbEG3M-Nfop4P-n+Mq16EebEH3EK3AP+qwiH8zMoKgWQ@mail.gmail.com>
References: <19849-38b8bd2fea807154@postgresql.org>
<CAGqfTtvt=d-aCEnVE1zRDMpUO9SyL3mAuCYzwdJodoNd0B5w8w@mail.gmail.com>
<CAGRkXqSbfQ4nbaf5jh4cxoEBZX86iLjoVhdCBqH0J_Osviskyg@mail.gmail.com>
<avFekPK2YMzo_l6V@paquier.xyz>
<2492770.1794253395@sss.pgh.pa.us>
<CAGRkXqSbEG3M-Nfop4P-n+Mq16EebEH3EK3AP+qwiH8zMoKgWQ@mail.gmail.com>
On Mon, Nov 09, 2026 at 06:09:19PM -0500, shihao zhong wrote:
> Thanks, Tom. The refactor turned out to be small. The code that fills
> in the pg_statistic columns can move into one function in analyze.c,
> and both places call it.
v3-0003's code cut looks tempting. That would have prevented the
original issue.
@@ -2333,12 +2333,7 @@ build_expr_data(List *exprs, int stattarget)
values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(InvalidAttrNumber);
values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(false);
[...]
@@ -1764,79 +1843,15 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(relid);
values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(stats->tupattnum);
values[Anum_pg_statistic_stainherit - 1] = BoolGetDatum(inh);
These two could also be passed down as arguments of your new
fill_attstats_values(), filling them within the routine instead of
letting the two callers do it.
+ * values[] and nulls[] must have room for Natts_pg_statistic entries. All
One thing that always stresses me with this kind of assumption is that
an error means an out-of-bound write, and a CVE. Here, we could also
use a palloc0'ed area worth Natts_pg_statistic, instead of expecting
the caller to do the right thing. The ANALYZE and expression stats
code paths are not performance-critical. Or ubsan would just catch
any problems?
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
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: michael@paquier.xyz, zhong950419@gmail.com, tgl@sss.pgh.pa.us, 125atishprak@gmail.com
Subject: Re: BUG #19849: queries fail after ANALYZE with expression stats on arrays
In-Reply-To: <avK1Yn8px0eAqiPQ@paquier.xyz>
References: <19849-38b8bd2fea807154@postgresql.org> <CAGqfTtvt=d-aCEnVE1zRDMpUO9SyL3mAuCYzwdJodoNd0B5w8w@mail.gmail.com> <CAGRkXqSbfQ4nbaf5jh4cxoEBZX86iLjoVhdCBqH0J_Osviskyg@mail.gmail.com> <avFekPK2YMzo_l6V@paquier.xyz> <2492770.1794253395@sss.pgh.pa.us> <CAGRkXqSbEG3M-Nfop4P-n+Mq16EebEH3EK3AP+qwiH8zMoKgWQ@mail.gmail.com> <avK1Yn8px0eAqiPQ@paquier.xyz>
* 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