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 1xMgsi-000000029Tu-2DI3 for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Oct 2026 07:18:33 +0000 Received: from localhost ([127.0.0.1]:51196 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xMgsf-00000008qJy-2c42 for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Oct 2026 07:18:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:53700) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMgcp-00000008Nnc-0hGg for pgsql-bugs@lists.postgresql.org; Fri, 30 Oct 2026 07:02:07 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMgcn-00000001emX-1Y8m for pgsql-bugs@lists.postgresql.org; Fri, 30 Oct 2026 07:02:06 +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=nPSna9bbnxUsLjb3rgSCu6NH+mIqMF9GEDORPJeWxg8=; b=HKoejkGe/7eIHyN+081uxjHD3+ J2Ns28WVAjpqVPj2O+f0HZjtL708h4mRvlj5wWUl/dq0nYrME4ZzKdvuYgYnT4RPbav4ujFzO14bY f6voHFxE9MH1kL3m85P312KZfn4AQDFB2TH1zUqttKqo3EYXpsPGygfG3QZXlu5MYaf9gFFTE1vS6 k7ZzCokmgI4Ti5oBbZYRExJ5DB2RQ/RQjOnVvI3sLUt6Xc5mkfjh+yHsFZgx1Hv/YGL1Ae/p2jyUW ygHEqx9vR/QVEWY8DH5y3e5ky61NOJq1o4Raa7TplQkFtzVSSGBpXPr2B765m3WcnQ0zh+wuGaU85 5v2GYOgg==; 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 1xMgcm-00000004mBb-3sVF for pgsql-bugs@lists.postgresql.org; Fri, 30 Oct 2026 07:02:04 +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 1xMgck-000000077CD-3tum for pgsql-bugs@lists.postgresql.org; Fri, 30 Oct 2026 07:02:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19821: pg_restore_attribute_stats() accepts an unsorted scalar histogram_bounds, producing badly wrong row 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: Fri, 30 Oct 2026 07:01:30 +0000 Message-ID: <19821-0aa4084a3c6d9899@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: 19821 Logged by: chunling qin Email address: 303677365@qq.com PostgreSQL version: 18.6 Operating system: Linux (TencentOS 4.4, x86_64) Description: =20 Hello, pg_restore_attribute_stats() validates that range_bounds_histogram is sorted in ascending order (stat_utils.c:842, commit df293aed46e "Reject incorrect range_bounds_histograms in stats import functions"). The very same function does not apply any ordering check to the scalar histogram_bounds, even though the consumer, histogram_selectivity() (src/backend/utils/adt/selfuncs.c), binary-searches the histogram and therefore assumes ascending order. So an unsorted (e.g. descending) histogram_bounds is accepted -- the function returns true -- and every row estimate made from it afterwards is arbitrary. Reproducer (self-contained): CREATE TABLE h1 (a int) WITH (autovacuum_enabled =3D false); INSERT INTO h1 SELECT g FROM generate_series(1, 100) g; ANALYZE h1; -- baseline: sorted histogram built by ANALYZE EXPLAIN SELECT * FROM h1 WHERE a < 50; -- Seq Scan ... rows=3D48 (true value 49) -- import a DESCENDING histogram: accepted, returns true SELECT pg_restore_attribute_stats( 'schemaname', 'public', 'relname', 'h1', 'attname', 'a', 'inherited', false, 'histogram_bounds', '{100,90,80,70,60,50,40,30,20,10,1}'); -- t EXPLAIN SELECT * FROM h1 WHERE a < 50; -- rows=3D1 (true value 49) EXPLAIN SELECT * FROM h1 WHERE a < 95; -- rows=3D100 (true value 94) EXPLAIN SELECT * FROM h1 WHERE a > 95; -- rows=3D1 (true value 5) For comparison, in the very same function and session, the range variant of the same mistake is properly rejected: SELECT pg_restore_attribute_stats( 'schemaname', 'public', 'relname', 'h1', 'attname', 'a', 'inherited', false, 'range_bounds_histogram', '{"[90,93)","[1,4)","[50,53)"}'); -- WARNING: "range_bounds_histogram" must have its lower and upper -- bounds sorted in ascending order Expected: histogram_bounds should get the same ordering check that range_bounds_histogram already has (an unsorted input should produce a WARNING and be skipped, not silently accepted). The sibling slots most_common_elems and elem_count_histogram also carry ordering/shape contracts from ANALYZE that the import side does not verify, but the scalar histogram is the one with a directly measurable effect on row estimates shown above. Actual: the function returns true and the planner's estimates become arbitrary (49 -> 1, 94 -> 100, 5 -> 1). Reproduced on: - PostgreSQL 20devel (snapshot of 2026-10-27, x86_64-pc-linux-gnu) - PostgreSQL 18.6 (REL_18_STABLE, self-built) A draft fix location would be the STATISTIC_KIND_HISTOGRAM branch of pg_restore_attribute_stats() (src/backend/statistics/attribute_stats.c, around lines 425-440): apply the same ascending-order check as statatt_check_bounds_histogram() (src/backend/statistics/stat_utils.c). Regards, Chunling Qin