]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix attnum remapping in generateClonedExtStatsStmt()
authorAndrew Dunstan <andrew@dunslane.net>
Thu, 30 Apr 2026 15:04:57 +0000 (11:04 -0400)
committerAndrew Dunstan <andrew@dunslane.net>
Thu, 30 Apr 2026 15:14:12 +0000 (11:14 -0400)
commit76d15a7ee9de91afda672cd7ed56cdcd9909f4ef
treea91f8b55b3786527090665a2fc03305e3644bc3d
parent76cdf922b0e7525efcc6181dc46e380fc40721e9
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@gmail.com>
Reviewed-by: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Discussion: https://postgr.es/m/20260415105718.tomuncfbmlt67oel@poseidon.home.virt
Backpatch-through: 14
src/backend/parser/parse_utilcmd.c
src/test/regress/expected/create_table_like.out
src/test/regress/sql/create_table_like.sql