pgsql: Fix attnum remapping in generateClonedExtStatsStmt()

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix attnum remapping in generateClonedExtStatsStmt()
Date: 2026-04-30 15:17:29
Message-ID: E1wIT8q-003TXY-1g@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix attnum remapping in generateClonedExtStatsStmt()

When cloning extended statistics via CREATE TABLE ... LIKE ... INCLUDING
STATISTICS, stxkeys holds attribute numbers from the source (parent)
table, but get_attname() was being called with the child relation's
OID. If the parent has dropped columns, the child's attribute numbers
are renumbered sequentially and no longer match, so the lookup either
returns the wrong column name (silent corruption) or errors out when
the attnum does not exist in the child.

Fix it by remapping the parent attnum through attmap before the lookup,
consistent with how expression statistics are already handled a few
lines below.

Add a regression test covering both manifestations: a 3-column parent
where the stale attnum refers to no child column (cache-lookup error),
and a 4-column parent where the stale attnum silently refers to the
wrong child column.

Author: Julien Tachoires <julmon(at)gmail(dot)com>
Reviewed-by: Srinath Reddy Sadipiralla <srinath2133(at)gmail(dot)com>
Discussion: https://postgr.es/m/20260415105718.tomuncfbmlt67oel@poseidon.home.virt
Backpatch-through: 14

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/7bb5196358063c61a554052a1ea0ee82b61efabb

Modified Files
--------------
src/backend/parser/parse_utilcmd.c | 8 +++++--
src/test/regress/expected/create_table_like.out | 31 +++++++++++++++++++++++++
src/test/regress/sql/create_table_like.sql | 26 +++++++++++++++++++++
3 files changed, 63 insertions(+), 2 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Álvaro Herrera 2026-04-30 20:58:38 pgsql: Remove working test that was supposed to fail
Previous Message Andrew Dunstan 2026-04-30 14:52:33 pgsql: Avoid SIGSEGV in pg_get_database_ddl() on NULL tablespace