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 1xOxTo-00000000Q8H-1ENv for pgsql-bugs@arkaria.postgresql.org; Thu, 05 Nov 2026 13:26:13 +0000 Received: from localhost ([127.0.0.1]:44152 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xOxTn-0000000CYMA-45jO for pgsql-bugs@arkaria.postgresql.org; Thu, 05 Nov 2026 13:26:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:46292) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xOtv4-0000000BoFu-2sG8 for pgsql-bugs@lists.postgresql.org; Thu, 05 Nov 2026 09:38:06 +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 1xOtv1-00000000L8F-1h3S for pgsql-bugs@lists.postgresql.org; Thu, 05 Nov 2026 09:38:05 +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=xT5wBZ2VVWVMtEEuH9+//DSNRKJPf8eLp1+6yLiFlEQ=; b=KG7YwJL+xHW8VwyOCp4DfqbgRE 4RAKC5AeBQ+U9GPg+iOSwJGn34q+jx0r7kEFANDF7YYY1Whk5FBI012YXyaXokP6IL102ST0H15Y5 m0IYWnA4nZkrJZ01ntSoUj6m4iGsSU8x6vAtBmQPKYSAzjTrzZHnUY0Qud67i4dS8QZpwgHSe9f1q x+zfFmFKIjogtKdTKAfAtYzH6+v95DqQVP1dbgzBOcpe5DhgI55rAlw+9rQU7dkk15OMzmkEnYFop eGwNKNQOUOcrwZcQO3V5XLwfRlkNZ1amnCBJRM86RT+/M+QHDv8RbLKz23jhRgGDyZdkAF2PuDYLM 2scjymgA==; 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 1xOtv1-00000000xqV-1nLM for pgsql-bugs@lists.postgresql.org; Thu, 05 Nov 2026 09:38: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 1xOtv0-00000002Uk5-2cJO for pgsql-bugs@lists.postgresql.org; Thu, 05 Nov 2026 09:38:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19833: ALTER TABLE that rebuilds an extended statistics object silently drops its SET STATISTICS target (st 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: Thu, 05 Nov 2026 09:37:25 +0000 Message-ID: <19833-37de35b8f159e62a@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: 19833 Logged by: chunling qin Email address: 303677365@qq.com PostgreSQL version: 18.6 Operating system: Linux x86_64 (official Docker image postgres:lates Description: =20 **Description:** `ALTER TABLE ... ALTER COLUMN TYPE` (and `ALTER COLUMN ... SET EXPRESSION`) drops and rebuilds extended statistics objects that reference the column. The rebuild preserves the definition (columns/expressions/kinds), the owner, and the comment =E2=80=94 but silently resets `stxstattarget` (the value se= t by `ALTER STATISTICS ... SET STATISTICS N`) to NULL. The next ANALYZE then uses the default target instead of the user's requested one, degrading the statistics detail and planner estimates. Since `pg_dump` dumps `ALTER STATISTICS ... SET STATISTICS N` as a persisted property, dropping it on an unrelated ALTER TABLE is an inconsistency, and the owner/comment preservation shows the rebuild path is *meant* to carry user-visible properties across. **Reproduction (self-contained SQL, works on the official image):** ```sql CREATE TABLE st2 (c int, d int); INSERT INTO st2 SELECT g, g FROM generate_series(1, 1000) g; CREATE STATISTICS st2_s ON c, (c + 1) FROM st2; ALTER STATISTICS st2_s SET STATISTICS 250; ANALYZE st2; SELECT s.stxstattarget AS target FROM pg_statistic_ext s WHERE s.stxname =3D 'st2_s'; -- 250 ALTER TABLE st2 ALTER COLUMN c TYPE bigint; -- rebuilds the statistics object SELECT s.oid, s.stxstattarget AS target, obj_description(s.oid, 'pg_statistic_ext') AS comment FROM pg_statistic_ext s WHERE s.stxname =3D 'st2_s'; -- new oid, target =3D NULL, comment =3D 'keep me' (comment and definition survive) -- Control: altering an UNREFERENCED column does not rebuild, target survives: ALTER STATISTICS st2_s SET STATISTICS 333; ALTER TABLE st2 ALTER COLUMN d TYPE bigint; SELECT s.stxstattarget FROM pg_statistic_ext s WHERE s.stxname =3D 'st2_s'; -- 333 -- Second trigger: virtual generated column SET EXPRESSION also rebuilds and drops the target CREATE TABLE st3 (a int, g int GENERATED ALWAYS AS (a * 2) VIRTUAL); CREATE STATISTICS st3_s ON g, (a + g) FROM st3; ALTER STATISTICS st3_s SET STATISTICS 400; ALTER TABLE st3 ALTER COLUMN g SET EXPRESSION AS (a * 3); SELECT s.stxstattarget FROM pg_statistic_ext s WHERE s.stxname =3D 'st3_s'; -- NULL (was 400) ``` **Root cause:** the statistics-object rebuild path in `src/backend/commands/tablecmds.c` (`RememberStatisticsForRebuilding` and friends) restores owner and comment but not `stxstattarget`. **Expected behavior:** the rebuild should preserve `stxstattarget`, the same way it preserves the owner and the comment.