From: Michael Paquier <michael@paquier.xyz>
To: shihao zhong <zhong950419@gmail.com>
Cc: Tomas Vondra <tomas@vondra.me>
Cc: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>
Subject: Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
Date: Tue, 20 Oct 2026 16:12:59 +0900
Message-ID: <atcUe6vjUEn_K9vu@paquier.xyz> (raw)
In-Reply-To: <CAGRkXqTTTYgav5f5SRdoMVBc50rmx+L8rwdb38i+qdaixuN1qg@mail.gmail.com>
References: <19763-14da3f96c02f07e8@postgresql.org>
<d6cc3cd3-4eea-45a1-a2e4-91423a902063@vondra.me>
<4a008244-3d3f-4563-8caf-0976fc59816f@vondra.me>
<CAGRkXqTTTYgav5f5SRdoMVBc50rmx+L8rwdb38i+qdaixuN1qg@mail.gmail.com>
On Sun, Oct 18, 2026 at 10:43:03PM -0400, shihao zhong wrote:
> Here is a patch. It does for examine_expression() what 3b7a6fa1572
> did for examine_attribute().
>
> The test adds your typanalyze function to test_custom_types. It runs
> your repro and also a single expression object, which builds only
> expression statistics.
Thanks for the patch. A couple of comments as I have played with this
area in the recent past. I didn't think that this would be reachable
in practice, but well, that's a fancy typanalyze callback that you
have here.
+ /*
+ * The typanalyze function sees a different attstattarget here than in
+ * lookup_var_attr_stats(), so it may reject the expression now.
+ */
+ if (result->stats[idx] == NULL)
+ {
+ pfree(result);
+ return NULL;
+ }
I don't understand why this comment is adapted here; it just makes the
situation more confusing to me. This just points to the fact that
examine_expression() could return NULL depending on what the
typanalyze callback decides to return. What your patch does it fancy
enough in the callback by not having the function to be called twice.
Nothing like that would happen in practice, still I agree that the
backend should have more protections. I think that we should just
ditch this comment. It's plain enough that examine_expression() can
return NULL.
/* evaluate expressions (if the statistics object has any) */
data = make_build_data(onerel, stat, numrows, rows, stats, stattarget);
+ if (!data)
+ {
+ if (!IsAutoVacuumWorkerProcess())
+ ereport(WARNING,
+ (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+ errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
+ stat->schema, stat->name,
+ get_namespace_name(onerel->rd_rel->relnamespace),
+ RelationGetRelationName(onerel)),
+ errtable(onerel)));
+ continue;
+ }
This warning is a duplicate, FWIW. Not optimal, just feels slightly
annoying. The patch is OK with this duplication, I guess..
If we begin to care about examine_expression(), I think that we ought
to also care about build_expr_data(), its second caller, no? It looks
like this could also be reached with a fancy undeterministic
typanalyze callback. I have not tested this hypothesis, but it would
be nice to make all the callers examine_expression() consistent.
+-- Same with a single expression, building only expression statistics.
+CREATE STATISTICS test_stats ON (func_int_custom(data)) FROM test_table;
+ANALYZE test_table;
+WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table"
This test is a duplicate of the first one. Let's remove it; it brings
nothing new.
--
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, tomas@vondra.me, noreply@postgresql.org
Subject: Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
In-Reply-To: <atcUe6vjUEn_K9vu@paquier.xyz>
References: <19763-14da3f96c02f07e8@postgresql.org> <d6cc3cd3-4eea-45a1-a2e4-91423a902063@vondra.me> <4a008244-3d3f-4563-8caf-0976fc59816f@vondra.me> <CAGRkXqTTTYgav5f5SRdoMVBc50rmx+L8rwdb38i+qdaixuN1qg@mail.gmail.com> <atcUe6vjUEn_K9vu@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