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 1xPjB6-00000000rgR-3ZDm for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Nov 2026 16:22:06 +0000 Received: from localhost ([127.0.0.1]:57128 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xPirG-000000049ly-0AvD 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]:48378) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xPhH6-00000003dH3-25Fd for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 14:20:08 +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 1xPhH3-00000000qqr-2pCr for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 14:20:08 +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=xjd4TVDlnaN55sfRegCqrYI8moB+GN8W3Y7T3e85qAY=; b=b3KPEHWy011fNbYtAbPzWuRubV hm5mFYJ5DK1kfSxqFbKw7kR40pIoMUdK11V3NuN34c1UQTtaX8rWqMYEmFciK7n/rAu/36SVSfSuX RjrwG2Tw+TBc6yzBTv6mTg00bYzqrvPfsQkrr1Go66iet5RsMKO14BfEfAj9oNXYb6ZkMU/2x1qpH /mS0Y+TMkwveZ5uHQqJ8+IVveaY9ER7q/j8FOAFdN5erMFbGcZlmvWRa6NaBzzZIN5CnS794QPv/x 3YpIkvSpHuBWWYC4zwXG8Nkene8ay+6EV4vRQUkE5hK/wTaI7XwZyjCVlWfXUKY9YKBV9raDvD8Y7 1DAdvXQA==; 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 1xPhH2-000000020bc-1zku for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 14:20: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 1xPhH0-00000005432-3MdV for pgsql-bugs@lists.postgresql.org; Sat, 07 Nov 2026 14:20:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19845: pg_dump extended stats break when column numbers change on restore To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 125atishprak@gmail.com Reply-To: 125atishprak@gmail.com, pgsql-bugs@lists.postgresql.org Date: Sat, 07 Nov 2026 14:19:57 +0000 Message-ID: <19845-a2a2ac0ba28b3605@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: 19845 Logged by: Atish Prakash Email address: 125atishprak@gmail.com PostgreSQL version: 19rc1 Operating system: Linux x86_64 Description: =20 pg_dump now emits pg_restore_extended_stats() calls, but pg_ndistinct and pg_dependencies values, and the column order of MCV lists, are tied to attribute numbers. A plain dump does not preserve attribute numbers, so the restored stats can be lost or, worse, silently applied to the wrong columns. Reproducer (see pg-dump-extstats-repro.sql for the same script): -- Case 1: child created first, then attached to a parent. -- The dump recreates it with the inherited column first. CREATE TABLE par(x int); CREATE TABLE chd(y int, x int); ALTER TABLE chd INHERIT par; INSERT INTO chd SELECT i % 2, i % 10 FROM generate_series(1, 10000) i; CREATE STATISTICS st_chd ON x, y FROM chd; -- Case 2: a dropped column before the statistics keys. CREATE TABLE dr(gone int, a int, b int); ALTER TABLE dr DROP COLUMN gone; INSERT INTO dr SELECT i % 10, i % 10 FROM generate_series(1, 10000) i; CREATE STATISTICS st_dr ON a, b FROM dr; ANALYZE; Then dump with "pg_dump --statistics", restore into an empty database, and look at the statistics before the next ANALYZE (autovacuum off makes this reliable). Case 1, no warning at all. Column numbers swap (y=3D1, x=3D2 becomes x=3D1,= y=3D2) and the statistics data is restored unchanged, so it now describes the wrong columns: query actual est. before est. after SELECT * FROM chd WHERE x =3D 3 AND y =3D 1 1000 1000 1 Case 2, with a warning. ndistinct and dependencies are lost, and only MCV survives: WARNING: could not validate "pg_ndistinct" object: invalid attribute number 3 found WARNING: could not validate "pg_dependencies" object: invalid attribute number 3 found query actual est. before est. after SELECT a, b FROM dr GROUP BY a, b 10 10 100 If the swapped columns in case 1 have different types, the MCV list is rejected with a WARNING instead ("incorrect value"). Expected: restored statistics describe the same columns as the originals, or pg_dump leaves out what it cannot restore correctly. Also seen when dumping an 18 server with pg_dump 19. Custom format behaves the same way. pg_upgrade from 18 to 19 is not affected in my test, since it keeps attribute numbers. Impact: with autovacuum running, its next ANALYZE replaces the bad statistics. In my test that happened within 100 seconds. The bad estimates last until then, or until a manual ANALYZE when autovacuum is off or does not analyze the table. Possible direction: attribute statistics already restore by column name, so extended statistics could translate attribute numbers to names on dump and back on restore. I have not written a patch. Found with semantic differential tests. I reproduced and checked the result myself.