Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPjB7-00000000rgR-2spa for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Nov 2026 16:22:07 +0000 Received: from localhost ([127.0.0.1]:57122 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xPirG-000000049lg-02Q3 for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Nov 2026 16:01:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:44676) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPXxO-00000002IYc-0jmS for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 04:23:10 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPXxL-00000000kpO-0y3L for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 04:23:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=ydSrFG3mMLUomweNn1m2Ji9mfweqH2arYIWhlQPwtSY=; b=hb/oGYZwb/3KF/kadrkV6v/uRU 2jnGHKdntgARFS+hEXsV5YTm/Exkv/qFbtDwIUKRBz+nHIxSjIuajoBGzWSq1BC8E2GYwGcxlnR0x t7vFOUy8bih8EHDuK9fy8f4/iD6KKxm73ivwJu0R/bJcpkqn/M5OI+Ih89gbKs9Or2F9kiRX3XHdz KGMWFIr/N1Mdm3lmhR3Od8KNMH6/lza4UcKBg1fxAiISl9pF3a9uRiNFZP2SWOHjPytNgo+07MB18 NOK6AXzIM9RdyP0clHH8jlPAVYW1juG4NFKQ9wuBbWoGoWhmO7TfsBDAvNPUy5xFKphLCcvr3hTYI kypLW+fA==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPXxJ-00000001oXH-0CGz for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 04:23:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xPXxG-00000004Xec-2xj4 for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 04:23:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19842: pg_restore_extended_stats() rejects a faithful pg_dump of ANALYZE-produced range expression statisti To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 303677365@qq.com Reply-To: 303677365@qq.com, pgsql-bugs@lists.postgresql.org Date: Sat, 07 Nov 2026 04:22:06 +0000 Message-ID: <19842-ca042326dd2430a4@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19842 Logged by: chunling qin Email address: 303677365@qq.com PostgreSQL version: 19rc1 Operating system: Linux x86_64; platform-independent (reproduced on Description: =20 **Description:** When an extended-statistics object has an expression of a range/multirange type, and fewer than 2 non-empty range values are sampled by ANALYZE (a 2-row table, or a column where most ranges are empty), `compute_range_stats()` still produces a statistics slot whose only range field is `range_empty_frac`. This is legal and intentional: `pg_stats_ext_exprs` exposes it, and `pg_dump --statistics-only` therefore emits a `pg_restore_extended_stats()` call whose `exprs` JSON contains only `range_empty_frac` (no `range_length_histogram`, no `range_bounds_histogram`). The restore side rejects exactly this input with a WARNING (and returns false), so on restore/pg_upgrade the expression statistics are silently lost. The planner then has no `range_empty_frac` and wrongfully assumes "no empty ranges", producing wrong selectivity/row estimates. By the rule established by the statistics-import series (commit 142fd8ff13d and its series =E2=80=94 "the source and the target should hold the same st= ats data after restore"), a restore function must accept the statistics that ANALYZE itself can produce; this is a case that series does not cover. **Reproduction (self-contained SQL; verified on master):** ```sql -- source side: 2-row table, range expression with < 2 non-empty values DROP TABLE IF EXISTS t; CREATE TABLE t (id int, r int4range); INSERT INTO t VALUES (1, int4range(1,5)), (2, 'empty'::int4range); CREATE STATISTICS stx ON (r * int4range(1,10)) FROM t; ANALYZE t; -- ANALYZE legitimately produced ONLY range_empty_frac (0.5): SELECT e.range_length_histogram, e.range_empty_frac, e.range_bounds_histogram FROM pg_stats_ext_exprs e WHERE e.statistics_name=3D'stx' AND NOT e.inherited; -- (NULL) | 0.5 | (NULL) -- what pg_dump --statistics-only emits for this object (replayed verbatim): SELECT pg_catalog.pg_clear_extended_stats('public','t','public','stx',false); SELECT * FROM pg_catalog.pg_restore_extended_stats( 'version', '200000'::integer, 'schemaname', 'public', 'relname', 't', 'statistics_schemaname', 'public', 'statistics_name', 'stx', 'inherited', 'f'::boolean, 'exprs', '[{"avg_width": "13","null_frac": "0","n_distinct": "-1", "range_empty_frac": "0.5"}]'::jsonb ); -- WARNING: ... (rejected, returns false; restore continues, statistics lost) -- after restore: the statistics are gone (expected: range_empty_frac =3D 0.5): SELECT e.range_length_histogram, e.range_empty_frac, e.range_bounds_histogram FROM pg_stats_ext_exprs e WHERE e.statistics_name=3D'stx' AND NOT e.inherited; -- (NULL) | (NULL) | (NULL) <- lost ``` **Actual results:** `pg_restore_extended_stats()` raises a WARNING and returns false for the exact `exprs` JSON that pg_dump emits from this legal ANALYZE output; the restored cluster has no `range_empty_frac`. **Expected behavior:** the restore function should accept a stats slot whose only range field is `range_empty_frac` (equivalently: treat missing histogram arrays the same way `compute_range_stats()` does =E2=80=94 an emp= ty array means "no histogram"), restoring `range_empty_frac =3D 0.5`.