pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
11+ messages / 4 participants
[nested] [flat]

Commits from this thread:
  2026-10-30 4f89416c9d 4b93eec05e 8b517a30c8 0cea8f85ec b2ca62b1c0 6ffd2f91b7 4bcd0d31c6  Fix more issues with extended statistics on expressions
* BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-15 15:05  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 11+ messages in thread

From: PG Bug reporting form @ 2026-10-15 15:05 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: 1217816127@qq.com

The following bug has been logged on the website:

Bug reference:      19763
Logged by:          Yuelin Wang
Email address:      1217816127@qq.com
PostgreSQL version: 19rc1
Operating system:   Linux (Ubuntu 24.04, x86_64)
Description:        

### What happens?

`make_build_data()` in `src/backend/statistics/extended_stats.c` (line 2586)
calls `examine_expression(expr, stattarget)` for each expression in a
statistics object without checking for a NULL return value.
The function `examine_expression()` can return NULL when a typanalyze
callback returns false for the given `attstattarget` value.
The invariant violated is: every element of `data->stats[]` returned by
`make_build_data()` must be a valid non-NULL `VacAttrStats` pointer before
any stat builder iterates over the array.
The upstream guard in `BuildRelationExtStatistics()` calls
`lookup_var_attr_stats()` first (line 169) and returns early when it yields
NULL.
However, `lookup_var_attr_stats()` calls `examine_attribute(expr)` (line
776) which sets `attstattarget = -1`, whereas `make_build_data()` calls
`examine_expression(expr, stattarget)` (line 2586) which sets `attstattarget
= stattarget` (a positive integer).
A typanalyze callback that behaves differently depending on `attstattarget`
can therefore pass `examine_attribute` (returning true) but fail
`examine_expression` (returning false, causing NULL).
The NULL then propagates through `make_build_data()` into stat builders such
as `statext_ndistinct_build()`, `statext_mcv_build()`, and
`statext_dependencies_build()`, all of which dereference `data->stats[i]`
without a NULL check.

The crash path confirmed by ASAN is:

```
make_build_data extended_stats.c:2586  examine_expression returns NULL for
stattarget-sensitive typanalyze
    data->stats[idx] = NULL
BuildRelationExtStatistics extended_stats.c:206  calls
statext_ndistinct_build(data)
statext_ndistinct_build mvdistinct.c:125  iterates over data->stats[]
ndistinct_for_combination mvdistinct.c:459  VacAttrStats *colstat =
data->stats[combination[i]] == NULL
    typid = colstat->attrtypid   <- NULL dereference (SEGFAULT)
```

### To Reproduce

First build and install the `test_custom_types` extension with an additional
`int_custom_typanalyze_stattarget_sensitive` function added to
`src/test/modules/test_custom_types/test_custom_types.c`.
The function returns true (with valid `compute_stats` and `minrows`) when
`stats->attstattarget < 0` (the value set by `examine_attribute`), and
returns false when `stats->attstattarget >= 0` (the value set by
`examine_expression` with the actual statistics target).

Start a fresh PostgreSQL ASAN instance and run:

```sql
SELECT version();
CREATE EXTENSION test_custom_types;
CREATE TABLE t1 (id int, data int_custom);
INSERT INTO t1 SELECT i, (i % 100)::text::int_custom FROM generate_series(1,
1000) i;
CREATE OR REPLACE FUNCTION func_ic(p int_custom) RETURNS int_custom LANGUAGE
plpgsql AS $$ BEGIN RETURN p; END; $$;
ALTER TYPE int_custom SET (ANALYZE =
int_custom_typanalyze_stattarget_sensitive);
CREATE STATISTICS st1 ON func_ic(data), (id) FROM t1;
ANALYZE t1;
SELECT 'survived' AS result;
```

Trigger output:

```
                              version
--------------------------------------------------------------------
 PostgreSQL 20devel on x86_64-linux, compiled by gcc-13.3.0, 64-bit
(1 row)

CREATE EXTENSION
CREATE TABLE
INSERT 0 1000
CREATE FUNCTION
ALTER TYPE
CREATE STATISTICS
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
connection to server was lost
```

pg.log:

```
2026-10-15 08:38:12.423 +08 [2602104] LOG:  starting PostgreSQL 20devel on
x86_64-linux, compiled by gcc-13.3.0, 64-bit
2026-10-15 08:38:12.423 +08 [2602104] LOG:  listening on IPv4 address
"127.0.0.1", port 14064
2026-10-15 08:38:12.423 +08 [2602104] LOG:  listening on Unix socket
"/tmp/.s.PGSQL.14064"
2026-10-15 08:38:12.432 +08 [2602109] LOG:  database system was shut down at
2026-10-15 08:38:11 +08
2026-10-15 08:38:12.440 +08 [2602104] LOG:  database system is ready to
accept connections
AddressSanitizer:DEADLYSIGNAL
2026-10-15 08:38:14.788 +08 [2602104] LOG:  client backend (PID 2602233) was
terminated by signal 6: Aborted
2026-10-15 08:38:14.788 +08 [2602104] DETAIL:  Failed process was running:
ANALYZE t1;
2026-10-15 08:38:14.788 +08 [2602104] LOG:  terminating any other active
server processes
2026-10-15 08:38:14.793 +08 [2602104] LOG:  all server processes terminated;
reinitializing
```

ASAN report:

```
==2602233==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000004
(pc 0x594bbf1a8535 bp 0x7ffcec007d70 sp 0x7ffcec007ca0 T0)
==2602233==The signal is caused by a READ memory access.
==2602233==Hint: address points to the zero page.
    #0 0x594bbf1a8535 in ndistinct_for_combination
../src/backend/statistics/mvdistinct.c:459
    #1 0x594bbf1a8535 in statext_ndistinct_build
../src/backend/statistics/mvdistinct.c:125
    #2 0x594bbf192013 in BuildRelationExtStatistics
../src/backend/statistics/extended_stats.c:206
    #3 0x594bbeb807f7 in do_analyze_rel
../src/backend/commands/analyze.c:645
    #4 0x594bbeb83d20 in analyze_rel ../src/backend/commands/analyze.c:299
    #5 0x594bbecef696 in vacuum ../src/backend/commands/vacuum.c:653
    #6 0x594bbecf0e6c in ExecVacuum ../src/backend/commands/vacuum.c:467
    #7 0x594bbf286a15 in standard_ProcessUtility
../src/backend/tcop/utility.c:860
    #8 0x594bbf282d46 in PortalRunUtility ../src/backend/tcop/pquery.c:1153
    #9 0x594bbf282fa4 in PortalRunMulti ../src/backend/tcop/pquery.c:1311
    #10 0x594bbf283c64 in PortalRun ../src/backend/tcop/pquery.c:788
    #11 0x594bbf27a670 in exec_simple_query
../src/backend/tcop/postgres.c:1297
    #12 0x594bbf27e970 in PostgresMain ../src/backend/tcop/postgres.c:4946
    #13 0x594bbf27134c in BackendMain
../src/backend/tcop/backend_startup.c:124
    #14 0x594bbf092b51 in postmaster_child_launch
../src/backend/postmaster/launch_backend.c:268
    #15 0x594bbf099c76 in BackendStartup
../src/backend/postmaster/postmaster.c:3650
    #16 0x594bbf099c76 in ServerLoop
../src/backend/postmaster/postmaster.c:1727
    #17 0x594bbf09cb29 in PostmasterMain
../src/backend/postmaster/postmaster.c:1414
    #18 0x594bbe88e20d in main ../src/backend/main/main.c:227
SUMMARY: AddressSanitizer: SEGV ../src/backend/statistics/mvdistinct.c:459
in ndistinct_for_combination
==2602233==ABORTING
```

### Environment

- OS: Ubuntu 24.04.2 LTS, x86_64 (Linux 6.8.0-55-generic)
- PostgreSQL Version: PostgreSQL 20devel on x86_64-linux, compiled by
gcc-13.3.0, 64-bit
- Client: psql, ASAN instrumented build (built from source with
-fsanitize=address)
- Hardware: N/A

### Identity Disclosure

- Full Name: Yuelin
- Affiliation: Individual








^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-15 18:52  Tomas Vondra <tomas@vondra.me>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 11+ messages in thread

From: Tomas Vondra @ 2026-10-15 18:52 UTC (permalink / raw)
  To: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

On 10/15/26 17:05, PG Bug reporting form wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      19763
> Logged by:          Yuelin Wang
> Email address:      1217816127@qq.com
> PostgreSQL version: 19rc1
> Operating system:   Linux (Ubuntu 24.04, x86_64)
> Description:        
> 
> ### What happens?
> 
> `make_build_data()` in `src/backend/statistics/extended_stats.c` (line 2586)
> calls `examine_expression(expr, stattarget)` for each expression in a
> statistics object without checking for a NULL return value.
> The function `examine_expression()` can return NULL when a typanalyze
> callback returns false for the given `attstattarget` value.
> The invariant violated is: every element of `data->stats[]` returned by
> `make_build_data()` must be a valid non-NULL `VacAttrStats` pointer before
> any stat builder iterates over the array.
> The upstream guard in `BuildRelationExtStatistics()` calls
> `lookup_var_attr_stats()` first (line 169) and returns early when it yields
> NULL.
> However, `lookup_var_attr_stats()` calls `examine_attribute(expr)` (line
> 776) which sets `attstattarget = -1`, whereas `make_build_data()` calls
> `examine_expression(expr, stattarget)` (line 2586) which sets `attstattarget
> = stattarget` (a positive integer).
> A typanalyze callback that behaves differently depending on `attstattarget`
> can therefore pass `examine_attribute` (returning true) but fail
> `examine_expression` (returning false, causing NULL).
> The NULL then propagates through `make_build_data()` into stat builders such
> as `statext_ndistinct_build()`, `statext_mcv_build()`, and
> `statext_dependencies_build()`, all of which dereference `data->stats[i]`
> without a NULL check.
> 
> The crash path confirmed by ASAN is:
> 
> ```
> make_build_data extended_stats.c:2586  examine_expression returns NULL for
> stattarget-sensitive typanalyze
>     data->stats[idx] = NULL
> BuildRelationExtStatistics extended_stats.c:206  calls
> statext_ndistinct_build(data)
> statext_ndistinct_build mvdistinct.c:125  iterates over data->stats[]
> ndistinct_for_combination mvdistinct.c:459  VacAttrStats *colstat =
> data->stats[combination[i]] == NULL
>     typid = colstat->attrtypid   <- NULL dereference (SEGFAULT)
> ```
> 
> ### To Reproduce
> 
> First build and install the `test_custom_types` extension with an additional
> `int_custom_typanalyze_stattarget_sensitive` function added to
> `src/test/modules/test_custom_types/test_custom_types.c`.
> The function returns true (with valid `compute_stats` and `minrows`) when
> `stats->attstattarget < 0` (the value set by `examine_attribute`), and
> returns false when `stats->attstattarget >= 0` (the value set by
> `examine_expression` with the actual statistics target).
> 
> Start a fresh PostgreSQL ASAN instance and run:
> 
> ```sql
> SELECT version();
> CREATE EXTENSION test_custom_types;
> CREATE TABLE t1 (id int, data int_custom);
> INSERT INTO t1 SELECT i, (i % 100)::text::int_custom FROM generate_series(1,
> 1000) i;
> CREATE OR REPLACE FUNCTION func_ic(p int_custom) RETURNS int_custom LANGUAGE
> plpgsql AS $$ BEGIN RETURN p; END; $$;
> ALTER TYPE int_custom SET (ANALYZE =
> int_custom_typanalyze_stattarget_sensitive);
> CREATE STATISTICS st1 ON func_ic(data), (id) FROM t1;
> ANALYZE t1;
> SELECT 'survived' AS result;
> ```
> 
> Trigger output:
> 
> ```
>                               version
> --------------------------------------------------------------------
>  PostgreSQL 20devel on x86_64-linux, compiled by gcc-13.3.0, 64-bit
> (1 row)
> 
> CREATE EXTENSION
> CREATE TABLE
> INSERT 0 1000
> CREATE FUNCTION
> ALTER TYPE
> CREATE STATISTICS
> server closed the connection unexpectedly
>         This probably means the server terminated abnormally
>         before or while processing the request.
> connection to server was lost
> ```
> 
> pg.log:
> 
> ```
> 2026-10-15 08:38:12.423 +08 [2602104] LOG:  starting PostgreSQL 20devel on
> x86_64-linux, compiled by gcc-13.3.0, 64-bit
> 2026-10-15 08:38:12.423 +08 [2602104] LOG:  listening on IPv4 address
> "127.0.0.1", port 14064
> 2026-10-15 08:38:12.423 +08 [2602104] LOG:  listening on Unix socket
> "/tmp/.s.PGSQL.14064"
> 2026-10-15 08:38:12.432 +08 [2602109] LOG:  database system was shut down at
> 2026-10-15 08:38:11 +08
> 2026-10-15 08:38:12.440 +08 [2602104] LOG:  database system is ready to
> accept connections
> AddressSanitizer:DEADLYSIGNAL
> 2026-10-15 08:38:14.788 +08 [2602104] LOG:  client backend (PID 2602233) was
> terminated by signal 6: Aborted
> 2026-10-15 08:38:14.788 +08 [2602104] DETAIL:  Failed process was running:
> ANALYZE t1;
> 2026-10-15 08:38:14.788 +08 [2602104] LOG:  terminating any other active
> server processes
> 2026-10-15 08:38:14.793 +08 [2602104] LOG:  all server processes terminated;
> reinitializing
> ```
> 
> ASAN report:
> 
> ```
> ==2602233==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000004
> (pc 0x594bbf1a8535 bp 0x7ffcec007d70 sp 0x7ffcec007ca0 T0)
> ==2602233==The signal is caused by a READ memory access.
> ==2602233==Hint: address points to the zero page.
>     #0 0x594bbf1a8535 in ndistinct_for_combination
> ../src/backend/statistics/mvdistinct.c:459
>     #1 0x594bbf1a8535 in statext_ndistinct_build
> ../src/backend/statistics/mvdistinct.c:125
>     #2 0x594bbf192013 in BuildRelationExtStatistics
> ../src/backend/statistics/extended_stats.c:206
>     #3 0x594bbeb807f7 in do_analyze_rel
> ../src/backend/commands/analyze.c:645
>     #4 0x594bbeb83d20 in analyze_rel ../src/backend/commands/analyze.c:299
>     #5 0x594bbecef696 in vacuum ../src/backend/commands/vacuum.c:653
>     #6 0x594bbecf0e6c in ExecVacuum ../src/backend/commands/vacuum.c:467
>     #7 0x594bbf286a15 in standard_ProcessUtility
> ../src/backend/tcop/utility.c:860
>     #8 0x594bbf282d46 in PortalRunUtility ../src/backend/tcop/pquery.c:1153
>     #9 0x594bbf282fa4 in PortalRunMulti ../src/backend/tcop/pquery.c:1311
>     #10 0x594bbf283c64 in PortalRun ../src/backend/tcop/pquery.c:788
>     #11 0x594bbf27a670 in exec_simple_query
> ../src/backend/tcop/postgres.c:1297
>     #12 0x594bbf27e970 in PostgresMain ../src/backend/tcop/postgres.c:4946
>     #13 0x594bbf27134c in BackendMain
> ../src/backend/tcop/backend_startup.c:124
>     #14 0x594bbf092b51 in postmaster_child_launch
> ../src/backend/postmaster/launch_backend.c:268
>     #15 0x594bbf099c76 in BackendStartup
> ../src/backend/postmaster/postmaster.c:3650
>     #16 0x594bbf099c76 in ServerLoop
> ../src/backend/postmaster/postmaster.c:1727
>     #17 0x594bbf09cb29 in PostmasterMain
> ../src/backend/postmaster/postmaster.c:1414
>     #18 0x594bbe88e20d in main ../src/backend/main/main.c:227
> SUMMARY: AddressSanitizer: SEGV ../src/backend/statistics/mvdistinct.c:459
> in ndistinct_for_combination
> ==2602233==ABORTING
> ```
> 

I tried reproducing it using the provided information, without much
success. Attached is the test_custom_types patch with the typanalyze
function I used, but it always ends for me with

WARNING:  statistics object "public.st1" could not be computed for
relation "public.t1"

Please, provide a working reproducer, or clarify the instructions


regards


-- 
Tomas Vondra

Attachments:

analyze-repro.patch (text/x-patch, 1.6K) download diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..132bbbafb7d 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,11 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +CREATE FUNCTION int_custom_typanalyze_stattarget_sensitive(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 41262044b47..d0b16677179 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,7 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget_sensitive); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -75,6 +76,17 @@ int_custom_typanalyze_false(PG_FUNCTION_ARGS) PG_RETURN_BOOL(false); } +Datum +int_custom_typanalyze_stattarget_sensitive(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget < 0) + PG_RETURN_BOOL(true); + else + PG_RETURN_BOOL(false); +} + /* * Callback used to compute invalid statistics. */
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-15 19:50  Tomas Vondra <tomas@vondra.me>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 11+ messages in thread

From: Tomas Vondra @ 2026-10-15 19:50 UTC (permalink / raw)
  To: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

On 10/15/26 20:52, Tomas Vondra wrote:
> ...
> 
> I tried reproducing it using the provided information, without much
> success. Attached is the test_custom_types patch with the typanalyze
> function I used, but it always ends for me with
> 
> WARNING:  statistics object "public.st1" could not be computed for
> relation "public.t1"
> 
> Please, provide a working reproducer, or clarify the instructions
> 

After a bit more poking, I managed to make it crash. Attached are the
two pieces - patch for test_custom_types, and the SQL reproducer.

I haven't thought about how to fix it yet - it seems very unlikely a
typanalyze function would behave like this, but we should be more careful.


regards

-- 
Tomas Vondra

Attachments:

test_custom_types-repro.patch (text/x-patch, 2.0K) download diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..88f34cb4efc 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,14 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- +-- Typanalyze function whose result depends on attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_stattarget_sensitive(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 41262044b47..8d91318d880 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,7 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget_sensitive); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -110,6 +111,26 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); } +/* + * int_custom_typanalyze_stattarget_sensitive + * + * Accepts the column only when attstattarget is negative (i.e. "use the + * default"), and rejects it otherwise. For expressions in extended + * statistics, examine_attribute() passes -1 while examine_expression() + * passes the computed statistics target, so this behaves differently in + * the two code paths. + */ +Datum +int_custom_typanalyze_stattarget_sensitive(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget >= 0) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + /* * Comparison functions for int_custom type */
repro.sql (application/sql, 418B) download ^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-19 02:43  shihao zhong <zhong950419@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 11+ messages in thread

From: shihao zhong @ 2026-10-19 02:43 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

Hi,

> I haven't thought about how to fix it yet - it seems very unlikely a
> typanalyze function would behave like this, but we should be more
> careful.

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,
Shihao

Attachments:

v1-0001-Handle-failed-typanalyze-PG14-16.patch.txt (text/plain, 8.0K) download From a926c22a86fa37268e602eaaddf92b3d78e02ad5 Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Sun, 18 Oct 2026 20:55:27 -0400 Subject: [PATCH v1] Handle failed typanalyze when building extended statistics make_build_data() calls examine_expression() a second time, now with the computed statistics target, and did not check for a NULL result. A typanalyze function that rejects the expression only in that call made ANALYZE crash. Skip the statistics object with the usual warning instead. Discussion: https://postgr.es/m/19763-14da3f96c02f07e8@postgresql.org --- src/backend/statistics/extended_stats.c | 24 ++++++++++++++ .../expected/test_custom_types.out | 32 +++++++++++++++++++ .../sql/test_custom_types.sql | 22 +++++++++++++ .../test_custom_types--1.0.sql | 8 +++++ .../test_custom_types/test_custom_types.c | 20 ++++++++++++ 5 files changed, 106 insertions(+) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index aeba6ef896a..3f50e02e20d 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -199,6 +199,18 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, /* 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; + } /* compute statistic of each requested type */ foreach(lc2, stat->types) @@ -2492,6 +2504,8 @@ statext_expressions_load(Oid stxoid, bool inh, int idx) * Evaluate the expressions, so that we can use the results to build * all the requested statistics types. This matters especially for * expensive expressions, of course. + * + * Returns NULL if one of the expressions cannot be analyzed. */ static StatsBuildData * make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -2581,6 +2595,16 @@ make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, result->attnums[idx] = k; result->stats[idx] = examine_expression(expr, stattarget); + /* + * 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; + } + idx++; k--; } diff --git a/src/test/modules/test_custom_types/expected/test_custom_types.out b/src/test/modules/test_custom_types/expected/test_custom_types.out index 2fb6b0b10ee..0becdab861f 100644 --- a/src/test/modules/test_custom_types/expected/test_custom_types.out +++ b/src/test/modules/test_custom_types/expected/test_custom_types.out @@ -167,6 +167,38 @@ SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); 1 (3 rows) +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + +DROP STATISTICS test_stats; +-- 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" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + DROP STATISTICS test_stats; -- Cleanup DROP FUNCTION func_int_custom; diff --git a/src/test/modules/test_custom_types/sql/test_custom_types.sql b/src/test/modules/test_custom_types/sql/test_custom_types.sql index 206a17c667f..4c3585f0195 100644 --- a/src/test/modules/test_custom_types/sql/test_custom_types.sql +++ b/src/test/modules/test_custom_types/sql/test_custom_types.sql @@ -98,6 +98,28 @@ SELECT tablename, SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); + +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; +-- Same with a single expression, building only expression statistics. +CREATE STATISTICS test_stats ON (func_int_custom(data)) FROM test_table; +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + -- Cleanup DROP FUNCTION func_int_custom; DROP TABLE test_table; diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..aa680880e3a 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,14 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- +-- Typanalyze function whose result depends on attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_stattarget(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 67e25a80b56..49ee8892921 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,7 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -112,6 +113,25 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); } +/* + * int_custom_typanalyze_stattarget + * + * This function accepts the column only when attstattarget is negative, + * and returns false otherwise. For expressions in extended statistics, + * the first lookup passes -1 while the build passes the computed target, + * so the two calls give different answers. + */ +Datum +int_custom_typanalyze_stattarget(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attr->attstattarget >= 0) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + /* * Comparison functions for int_custom type */ -- 2.37.1 (Apple Git-137.1)
v1-0001-Handle-failed-typanalyze-when-building-extended-s.patch (application/octet-stream, 8.0K) download From 678e067bc7c4d3cbf8e3f2f4300d3e8d526a34ad Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Sun, 18 Oct 2026 20:55:27 -0400 Subject: [PATCH v1] Handle failed typanalyze when building extended statistics make_build_data() calls examine_expression() a second time, now with the computed statistics target, and did not check for a NULL result. A typanalyze function that rejects the expression only in that call made ANALYZE crash. Skip the statistics object with the usual warning instead. Discussion: https://postgr.es/m/19763-14da3f96c02f07e8@postgresql.org --- src/backend/statistics/extended_stats.c | 24 ++++++++++++++ .../expected/test_custom_types.out | 32 +++++++++++++++++++ .../sql/test_custom_types.sql | 22 +++++++++++++ .../test_custom_types--1.0.sql | 8 +++++ .../test_custom_types/test_custom_types.c | 20 ++++++++++++ 5 files changed, 106 insertions(+) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 515095faa4b..5c2f12efadc 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -196,6 +196,18 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, /* evaluate expressions (if the statistics object has any) */ data = make_build_data(onerel, stat, numrows, rows, stats, stattarget); + if (!data) + { + if (!AmAutoVacuumWorkerProcess()) + 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; + } /* compute statistic of each requested type */ foreach(lc2, stat->types) @@ -2496,6 +2508,8 @@ statext_expressions_load(Oid stxoid, bool inh, int idx) * Evaluate the expressions, so that we can use the results to build * all the requested statistics types. This matters especially for * expensive expressions, of course. + * + * Returns NULL if one of the expressions cannot be analyzed. */ static StatsBuildData * make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -2585,6 +2599,16 @@ make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, result->attnums[idx] = k; result->stats[idx] = examine_expression(expr, stattarget); + /* + * 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; + } + idx++; k--; } diff --git a/src/test/modules/test_custom_types/expected/test_custom_types.out b/src/test/modules/test_custom_types/expected/test_custom_types.out index 59f0917c2d7..b6545df0cf6 100644 --- a/src/test/modules/test_custom_types/expected/test_custom_types.out +++ b/src/test/modules/test_custom_types/expected/test_custom_types.out @@ -173,6 +173,38 @@ SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); 1 (3 rows) +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + +DROP STATISTICS test_stats; +-- 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" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + DROP STATISTICS test_stats; -- Cleanup DROP FUNCTION func_int_custom; diff --git a/src/test/modules/test_custom_types/sql/test_custom_types.sql b/src/test/modules/test_custom_types/sql/test_custom_types.sql index f1c75e4ba5b..afac84dacef 100644 --- a/src/test/modules/test_custom_types/sql/test_custom_types.sql +++ b/src/test/modules/test_custom_types/sql/test_custom_types.sql @@ -101,6 +101,28 @@ SELECT tablename, SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); + +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; +-- Same with a single expression, building only expression statistics. +CREATE STATISTICS test_stats ON (func_int_custom(data)) FROM test_table; +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + -- Cleanup DROP FUNCTION func_int_custom; DROP TABLE test_table; diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..aa680880e3a 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,14 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- +-- Typanalyze function whose result depends on attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_stattarget(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 41262044b47..4b5fd504776 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,7 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -110,6 +111,25 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); } +/* + * int_custom_typanalyze_stattarget + * + * This function accepts the column only when attstattarget is negative, + * and returns false otherwise. For expressions in extended statistics, + * the first lookup passes -1 while the build passes the computed target, + * so the two calls give different answers. + */ +Datum +int_custom_typanalyze_stattarget(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget >= 0) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + /* * Comparison functions for int_custom type */ -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-20 07:12  Michael Paquier <michael@paquier.xyz>
  parent: shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Michael Paquier @ 2026-10-20 07:12 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: Tomas Vondra <tomas@vondra.me>; 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-21 00:29  shihao zhong <zhong950419@gmail.com>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 11+ messages in thread

From: shihao zhong @ 2026-10-21 00:29 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Tomas Vondra <tomas@vondra.me>; 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

Hi Michael,

> I think that we should just ditch this comment.

Done.

> This warning is a duplicate, FWIW.

v2 moves it into a small function, so there is only one copy.

> If we begin to care about examine_expression(), I think that we
> ought to also care about build_expr_data(), its second caller, no?

Yes, it can be reached. With a typanalyze function that only accepts the
first call with a real target, ANALYZE crashes in compute_expr_stats().

v2 checks the result there too and skips the object with the same
warning. I did not add a test for it, since it needs another fancy
callback.

> This test is a duplicate of the first one.

Removed.

Thanks,
Shihao

Attachments:

v2-0001-Handle-failed-typanalyze-when-building-extended-s.patch (application/octet-stream, 9.4K) download From 719e60d1ddbb9b3d7a13cdc62e17fd4995c1b8ca Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Sun, 18 Oct 2026 20:55:27 -0400 Subject: [PATCH v2] Handle failed typanalyze when building extended statistics make_build_data() and build_expr_data() call examine_expression() with the computed statistics target and did not check for a NULL result. A typanalyze function that rejects the expression in that call made ANALYZE crash. Skip the statistics object with the usual warning instead. Discussion: https://postgr.es/m/19763-14da3f96c02f07e8@postgresql.org --- src/backend/statistics/extended_stats.c | 53 ++++++++++++++++--- .../expected/test_custom_types.out | 18 +++++++ .../sql/test_custom_types.sql | 14 +++++ .../test_custom_types--1.0.sql | 8 +++ .../test_custom_types/test_custom_types.c | 20 +++++++ 5 files changed, 105 insertions(+), 8 deletions(-) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 515095faa4b..887e7e78c0d 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -95,6 +95,7 @@ static void compute_expr_stats(Relation onerel, AnlExprData *exprdata, static Datum serialize_expr_stats(AnlExprData *exprdata, int nexprs); static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); static AnlExprData *build_expr_data(List *exprs, int stattarget); +static void report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat); static StatsBuildData *make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -170,14 +171,7 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, natts, vacattrstats); if (!stats) { - if (!AmAutoVacuumWorkerProcess()) - 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))); + report_ext_stats_not_computed(onerel, stat); continue; } @@ -196,6 +190,11 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, /* evaluate expressions (if the statistics object has any) */ data = make_build_data(onerel, stat, numrows, rows, stats, stattarget); + if (!data) + { + report_ext_stats_not_computed(onerel, stat); + continue; + } /* compute statistic of each requested type */ foreach(lc2, stat->types) @@ -218,6 +217,12 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, elog(ERROR, "requested expression stats, but there are no expressions"); exprdata = build_expr_data(stat->exprs, stattarget); + if (!exprdata) + { + report_ext_stats_not_computed(onerel, stat); + data = NULL; + break; + } nexprs = list_length(stat->exprs); compute_expr_stats(onerel, exprdata, nexprs, rows, numrows); @@ -226,6 +231,9 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, } } + if (!data) + continue; + /* store the statistics in the catalog */ statext_store(stat->statOid, inh, ndistinct, dependencies, mcv, exprstats, stats); @@ -246,6 +254,22 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, table_close(pg_stext, RowExclusiveLock); } +/* + * Warn that a statistics object could not be built for this relation. + */ +static void +report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat) +{ + if (!AmAutoVacuumWorkerProcess()) + 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))); +} + /* * Test if the given relation has extended statistics objects. */ @@ -2303,6 +2327,11 @@ build_expr_data(List *exprs, int stattarget) thisdata->expr = expr; thisdata->vacattrstat = examine_expression(expr, stattarget); + if (thisdata->vacattrstat == NULL) + { + pfree(exprdata); + return NULL; + } idx++; } @@ -2496,6 +2525,8 @@ statext_expressions_load(Oid stxoid, bool inh, int idx) * Evaluate the expressions, so that we can use the results to build * all the requested statistics types. This matters especially for * expensive expressions, of course. + * + * Returns NULL if one of the expressions cannot be analyzed. */ static StatsBuildData * make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -2585,6 +2616,12 @@ make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, result->attnums[idx] = k; result->stats[idx] = examine_expression(expr, stattarget); + if (result->stats[idx] == NULL) + { + pfree(result); + return NULL; + } + idx++; k--; } diff --git a/src/test/modules/test_custom_types/expected/test_custom_types.out b/src/test/modules/test_custom_types/expected/test_custom_types.out index 59f0917c2d7..a654276e762 100644 --- a/src/test/modules/test_custom_types/expected/test_custom_types.out +++ b/src/test/modules/test_custom_types/expected/test_custom_types.out @@ -173,6 +173,24 @@ SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); 1 (3 rows) +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + DROP STATISTICS test_stats; -- Cleanup DROP FUNCTION func_int_custom; diff --git a/src/test/modules/test_custom_types/sql/test_custom_types.sql b/src/test/modules/test_custom_types/sql/test_custom_types.sql index f1c75e4ba5b..0f909e2468e 100644 --- a/src/test/modules/test_custom_types/sql/test_custom_types.sql +++ b/src/test/modules/test_custom_types/sql/test_custom_types.sql @@ -101,6 +101,20 @@ SELECT tablename, SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); + +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + -- Cleanup DROP FUNCTION func_int_custom; DROP TABLE test_table; diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..aa680880e3a 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,14 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- +-- Typanalyze function whose result depends on attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_stattarget(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 41262044b47..4b5fd504776 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,7 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -110,6 +111,25 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); } +/* + * int_custom_typanalyze_stattarget + * + * This function accepts the column only when attstattarget is negative, + * and returns false otherwise. For expressions in extended statistics, + * the first lookup passes -1 while the build passes the computed target, + * so the two calls give different answers. + */ +Datum +int_custom_typanalyze_stattarget(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget >= 0) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + /* * Comparison functions for int_custom type */ -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-21 05:56  Michael Paquier <michael@paquier.xyz>
  parent: shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Michael Paquier @ 2026-10-21 05:56 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: Tomas Vondra <tomas@vondra.me>; 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

On Tue, Oct 20, 2026 at 08:29:28PM -0400, shihao zhong wrote:
> v2 checks the result there too and skips the object with the same
> warning. I did not add a test for it, since it needs another fancy
> callback.

Fancy typanalyze callbacks are what make the DNA of the test module
test_custom_types.  I'd suggest to add a test for it.  Your previous
duplicated test had little meaning.  This would be a second test for a
separated code path, that acts as its own check.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-22 03:23  shihao zhong <zhong950419@gmail.com>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 11+ messages in thread

From: shihao zhong @ 2026-10-22 03:23 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Tomas Vondra <tomas@vondra.me>; 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

Hi Michael,

Thanks. v3 adds a second typanalyze callback to test_custom_types,
int_custom_typanalyze_once.

Thanks,
Shihao

Attachments:

v3-0001-Handle-failed-typanalyze-when-building-extended-s.patch (application/octet-stream, 11.8K) download From 83d1f29403f67b5d3007fe5eae7e61cd1101d337 Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Sun, 18 Oct 2026 20:55:27 -0400 Subject: [PATCH v3] Handle failed typanalyze when building extended statistics make_build_data() and build_expr_data() call examine_expression() with the computed statistics target and did not check for a NULL result. A typanalyze function that rejects the expression in that call made ANALYZE crash. Skip the statistics object with the usual warning instead. Discussion: https://postgr.es/m/19763-14da3f96c02f07e8@postgresql.org --- src/backend/statistics/extended_stats.c | 53 ++++++++++++++++--- .../expected/test_custom_types.out | 36 +++++++++++++ .../sql/test_custom_types.sql | 28 ++++++++++ .../test_custom_types--1.0.sql | 16 ++++++ .../test_custom_types/test_custom_types.c | 44 +++++++++++++++ 5 files changed, 169 insertions(+), 8 deletions(-) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 515095faa4b..887e7e78c0d 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -95,6 +95,7 @@ static void compute_expr_stats(Relation onerel, AnlExprData *exprdata, static Datum serialize_expr_stats(AnlExprData *exprdata, int nexprs); static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); static AnlExprData *build_expr_data(List *exprs, int stattarget); +static void report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat); static StatsBuildData *make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -170,14 +171,7 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, natts, vacattrstats); if (!stats) { - if (!AmAutoVacuumWorkerProcess()) - 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))); + report_ext_stats_not_computed(onerel, stat); continue; } @@ -196,6 +190,11 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, /* evaluate expressions (if the statistics object has any) */ data = make_build_data(onerel, stat, numrows, rows, stats, stattarget); + if (!data) + { + report_ext_stats_not_computed(onerel, stat); + continue; + } /* compute statistic of each requested type */ foreach(lc2, stat->types) @@ -218,6 +217,12 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, elog(ERROR, "requested expression stats, but there are no expressions"); exprdata = build_expr_data(stat->exprs, stattarget); + if (!exprdata) + { + report_ext_stats_not_computed(onerel, stat); + data = NULL; + break; + } nexprs = list_length(stat->exprs); compute_expr_stats(onerel, exprdata, nexprs, rows, numrows); @@ -226,6 +231,9 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, } } + if (!data) + continue; + /* store the statistics in the catalog */ statext_store(stat->statOid, inh, ndistinct, dependencies, mcv, exprstats, stats); @@ -246,6 +254,22 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, table_close(pg_stext, RowExclusiveLock); } +/* + * Warn that a statistics object could not be built for this relation. + */ +static void +report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat) +{ + if (!AmAutoVacuumWorkerProcess()) + 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))); +} + /* * Test if the given relation has extended statistics objects. */ @@ -2303,6 +2327,11 @@ build_expr_data(List *exprs, int stattarget) thisdata->expr = expr; thisdata->vacattrstat = examine_expression(expr, stattarget); + if (thisdata->vacattrstat == NULL) + { + pfree(exprdata); + return NULL; + } idx++; } @@ -2496,6 +2525,8 @@ statext_expressions_load(Oid stxoid, bool inh, int idx) * Evaluate the expressions, so that we can use the results to build * all the requested statistics types. This matters especially for * expensive expressions, of course. + * + * Returns NULL if one of the expressions cannot be analyzed. */ static StatsBuildData * make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -2585,6 +2616,12 @@ make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, result->attnums[idx] = k; result->stats[idx] = examine_expression(expr, stattarget); + if (result->stats[idx] == NULL) + { + pfree(result); + return NULL; + } + idx++; k--; } diff --git a/src/test/modules/test_custom_types/expected/test_custom_types.out b/src/test/modules/test_custom_types/expected/test_custom_types.out index 59f0917c2d7..56100fc72a0 100644 --- a/src/test/modules/test_custom_types/expected/test_custom_types.out +++ b/src/test/modules/test_custom_types/expected/test_custom_types.out @@ -173,6 +173,42 @@ SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); 1 (3 rows) +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that rejects the second build call. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_once); +-- Extended statistics with an expression accepted when the expression is +-- evaluated but rejected when the expression statistics are built. +CREATE STATISTICS test_stats ON (func_int_custom(data)) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + DROP STATISTICS test_stats; -- Cleanup DROP FUNCTION func_int_custom; diff --git a/src/test/modules/test_custom_types/sql/test_custom_types.sql b/src/test/modules/test_custom_types/sql/test_custom_types.sql index f1c75e4ba5b..7d00328f039 100644 --- a/src/test/modules/test_custom_types/sql/test_custom_types.sql +++ b/src/test/modules/test_custom_types/sql/test_custom_types.sql @@ -101,6 +101,34 @@ SELECT tablename, SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); + +-- Extended statistics with an expression accepted by the first lookup +-- but rejected when the statistics are built. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + +-- Switch type to use typanalyze function that rejects the second build call. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_once); + +-- Extended statistics with an expression accepted when the expression is +-- evaluated but rejected when the expression statistics are built. +CREATE STATISTICS test_stats ON (func_int_custom(data)) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + -- Cleanup DROP FUNCTION func_int_custom; DROP TABLE test_table; diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..5e946593eb6 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,22 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- +-- Typanalyze function whose result depends on attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_stattarget(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- +-- Typanalyze function that rejects the second build-time call +-- +CREATE FUNCTION int_custom_typanalyze_once(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 41262044b47..c0160994f58 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,8 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_once); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -110,6 +112,48 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); } +/* + * int_custom_typanalyze_stattarget + * + * This function accepts the column only when attstattarget is negative, + * and returns false otherwise. For expressions in extended statistics, + * the first lookup passes -1 while the build passes the computed target, + * so the two calls give different answers. + */ +Datum +int_custom_typanalyze_stattarget(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget >= 0) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + +/* + * int_custom_typanalyze_once + * + * This function accepts the column when attstattarget is negative, which + * also resets a counter, and accepts only the first call made with a + * non-negative attstattarget. Later calls return false. An expression in + * extended statistics is looked up once with a negative target, then examined + * for each step of the build, so only a later step is rejected. + */ +Datum +int_custom_typanalyze_once(PG_FUNCTION_ARGS) +{ + static int ncalls = 0; + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget < 0) + ncalls = 0; + else if (++ncalls > 1) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + /* * Comparison functions for int_custom type */ -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-22 04:22  Michael Paquier <michael@paquier.xyz>
  parent: shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Michael Paquier @ 2026-10-22 04:22 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: Tomas Vondra <tomas@vondra.me>; 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

On Wed, Oct 21, 2026 at 11:23:28PM -0400, shihao zhong wrote:
> Thanks. v3 adds a second typanalyze callback to test_custom_types,
> int_custom_typanalyze_once.

The comments on top of each callback feel super bloated, as much as
the comments you have added in the two sequences of tests.  These
could be tweaked much more, IMO..

report_ext_stats_not_computed() could be static inline.

We may want to hear from Tomas as well, as he has already commented on
this thread.  There is a big conference in Europe this week, and my
guess is that he's now busy with the event and that it's going to take
some time before we get a reply.

@Tomas: Could you look at the patch once you have the occasion,
please?
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-23 01:17  shihao zhong <zhong950419@gmail.com>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 11+ messages in thread

From: shihao zhong @ 2026-10-23 01:17 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Tomas Vondra <tomas@vondra.me>; 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

Hi Michael,

Thanks. v4 shortens the comments on both callbacks and in the tests,
and makes report_ext_stats_not_computed() static inline. Nothing else
changed.

Thanks,
Shihao

Attachments:

v4-0001-Handle-failed-typanalyze-when-building-extended-s.patch (application/octet-stream, 11.1K) download From 05fb8f8cf7c1051249c3514f544eeef73d668950 Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Sun, 18 Oct 2026 20:55:27 -0400 Subject: [PATCH v4] Handle failed typanalyze when building extended statistics make_build_data() and build_expr_data() call examine_expression() with the computed statistics target and did not check for a NULL result. A typanalyze function that rejects the expression in that call made ANALYZE crash. Skip the statistics object with the usual warning instead. Discussion: https://postgr.es/m/19763-14da3f96c02f07e8@postgresql.org --- src/backend/statistics/extended_stats.c | 53 ++++++++++++++++--- .../expected/test_custom_types.out | 34 ++++++++++++ .../sql/test_custom_types.sql | 26 +++++++++ .../test_custom_types--1.0.sql | 16 ++++++ .../test_custom_types/test_custom_types.c | 40 ++++++++++++++ 5 files changed, 161 insertions(+), 8 deletions(-) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 515095faa4b..0d5c174c129 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -95,6 +95,7 @@ static void compute_expr_stats(Relation onerel, AnlExprData *exprdata, static Datum serialize_expr_stats(AnlExprData *exprdata, int nexprs); static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); static AnlExprData *build_expr_data(List *exprs, int stattarget); +static inline void report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat); static StatsBuildData *make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -170,14 +171,7 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, natts, vacattrstats); if (!stats) { - if (!AmAutoVacuumWorkerProcess()) - 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))); + report_ext_stats_not_computed(onerel, stat); continue; } @@ -196,6 +190,11 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, /* evaluate expressions (if the statistics object has any) */ data = make_build_data(onerel, stat, numrows, rows, stats, stattarget); + if (!data) + { + report_ext_stats_not_computed(onerel, stat); + continue; + } /* compute statistic of each requested type */ foreach(lc2, stat->types) @@ -218,6 +217,12 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, elog(ERROR, "requested expression stats, but there are no expressions"); exprdata = build_expr_data(stat->exprs, stattarget); + if (!exprdata) + { + report_ext_stats_not_computed(onerel, stat); + data = NULL; + break; + } nexprs = list_length(stat->exprs); compute_expr_stats(onerel, exprdata, nexprs, rows, numrows); @@ -226,6 +231,9 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, } } + if (!data) + continue; + /* store the statistics in the catalog */ statext_store(stat->statOid, inh, ndistinct, dependencies, mcv, exprstats, stats); @@ -246,6 +254,22 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, table_close(pg_stext, RowExclusiveLock); } +/* + * Warn that a statistics object could not be built for this relation. + */ +static inline void +report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat) +{ + if (!AmAutoVacuumWorkerProcess()) + 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))); +} + /* * Test if the given relation has extended statistics objects. */ @@ -2303,6 +2327,11 @@ build_expr_data(List *exprs, int stattarget) thisdata->expr = expr; thisdata->vacattrstat = examine_expression(expr, stattarget); + if (thisdata->vacattrstat == NULL) + { + pfree(exprdata); + return NULL; + } idx++; } @@ -2496,6 +2525,8 @@ statext_expressions_load(Oid stxoid, bool inh, int idx) * Evaluate the expressions, so that we can use the results to build * all the requested statistics types. This matters especially for * expensive expressions, of course. + * + * Returns NULL if one of the expressions cannot be analyzed. */ static StatsBuildData * make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -2585,6 +2616,12 @@ make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, result->attnums[idx] = k; result->stats[idx] = examine_expression(expr, stattarget); + if (result->stats[idx] == NULL) + { + pfree(result); + return NULL; + } + idx++; k--; } diff --git a/src/test/modules/test_custom_types/expected/test_custom_types.out b/src/test/modules/test_custom_types/expected/test_custom_types.out index 59f0917c2d7..84049218a14 100644 --- a/src/test/modules/test_custom_types/expected/test_custom_types.out +++ b/src/test/modules/test_custom_types/expected/test_custom_types.out @@ -173,6 +173,40 @@ SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); 1 (3 rows) +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); +-- Expression rejected in make_build_data(). +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that fails on the second build call. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_once); +-- Expression rejected in build_expr_data(). +CREATE STATISTICS test_stats ON (func_int_custom(data)) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + DROP STATISTICS test_stats; -- Cleanup DROP FUNCTION func_int_custom; diff --git a/src/test/modules/test_custom_types/sql/test_custom_types.sql b/src/test/modules/test_custom_types/sql/test_custom_types.sql index f1c75e4ba5b..36f1f7aff35 100644 --- a/src/test/modules/test_custom_types/sql/test_custom_types.sql +++ b/src/test/modules/test_custom_types/sql/test_custom_types.sql @@ -101,6 +101,32 @@ SELECT tablename, SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that depends on attstattarget. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_stattarget); + +-- Expression rejected in make_build_data(). +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + +-- Switch type to use typanalyze function that fails on the second build call. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_once); + +-- Expression rejected in build_expr_data(). +CREATE STATISTICS test_stats ON (func_int_custom(data)) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + -- Cleanup DROP FUNCTION func_int_custom; DROP TABLE test_table; diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..5e946593eb6 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,22 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- +-- Typanalyze function whose result depends on attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_stattarget(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- +-- Typanalyze function that rejects the second build-time call +-- +CREATE FUNCTION int_custom_typanalyze_once(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 41262044b47..8aad1450661 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,8 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_once); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -110,6 +112,44 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); } +/* + * int_custom_typanalyze_stattarget + * + * Fails unless attstattarget is negative, as in the first lookup of an + * extended statistics expression. + */ +Datum +int_custom_typanalyze_stattarget(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget >= 0) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + +/* + * int_custom_typanalyze_once + * + * Accepts negative attstattarget, which resets the counter, and only the + * first call with a non-negative one. This lets the lookup and + * make_build_data() succeed, and fails build_expr_data(). + */ +Datum +int_custom_typanalyze_once(PG_FUNCTION_ARGS) +{ + static int ncalls = 0; + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attstattarget < 0) + ncalls = 0; + else if (++ncalls > 1) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + /* * Comparison functions for int_custom type */ -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a
@ 2026-10-30 08:41  Michael Paquier <michael@paquier.xyz>
  parent: shihao zhong <zhong950419@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Michael Paquier @ 2026-10-30 08:41 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: Tomas Vondra <tomas@vondra.me>; 1217816127@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

On Thu, Oct 22, 2026 at 09:17:59PM -0400, shihao zhong wrote:
> Thanks. v4 shortens the comments on both callbacks and in the tests,
> and makes report_ext_stats_not_computed() static inline. Nothing else
> changed.

Thanks for the patch, and my apologies for the time it took to come
back here.  I have reworked the thing quite a bit, adjusting and
improving the code and comments, and applied it down to v14.  The
README of the test module also lacked an update.

Note that the test uses a check based on d.stxoid to see if there is
any data at all, which was stronger than your suggestion.  v14 has
kept the stxdexpr, though.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2026-10-30 08:41 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-15 15:05 BUG #19763: ManualBug2 NULL pointer dereference in make_build_data via examine_expression bypass of lookup_var_a PG Bug reporting form <noreply@postgresql.org>
2026-10-15 18:52 ` Tomas Vondra <tomas@vondra.me>
2026-10-15 19:50   ` Tomas Vondra <tomas@vondra.me>
2026-10-19 02:43     ` shihao zhong <zhong950419@gmail.com>
2026-10-20 07:12       ` Michael Paquier <michael@paquier.xyz>
2026-10-21 00:29         ` shihao zhong <zhong950419@gmail.com>
2026-10-21 05:56           ` Michael Paquier <michael@paquier.xyz>
2026-10-22 03:23             ` shihao zhong <zhong950419@gmail.com>
2026-10-22 04:22               ` Michael Paquier <michael@paquier.xyz>
2026-10-23 01:17                 ` shihao zhong <zhong950419@gmail.com>
2026-10-30 08:41                   ` Michael Paquier <michael@paquier.xyz>

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