From: Julien Tachoires <julmon@gmail.com>
Subject: [PATCH 2/2] Fix attnum remapping in generateClonedExtStatsStmt()
Date: Wed, 15 Apr 2026 12:46:46 +0200
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 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.
---
src/backend/parser/parse_utilcmd.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.cindex 37071502a9f..131f33962b5 100644--- a/src/backend/parser/parse_utilcmd.c+++ b/src/backend/parser/parse_utilcmd.c@@ -2100,7 +2100,8 @@ generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
StatsElem *selem = makeNode(StatsElem);
AttrNumber attnum = statsrec->stxkeys.values[i];
- selem->name = get_attname(heapRelid, attnum, false);+ selem->name =+ get_attname(heapRelid, attmap->attnums[attnum - 1], false);
selem->expr = NULL;
def_names = lappend(def_names, selem);
--
2.51.2
--ly2yk5uozlo3oxwh--
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: julmon@gmail.com
Subject: Re: [PATCH 2/2] Fix attnum remapping in generateClonedExtStatsStmt()
In-Reply-To: <no-message-id-25311@localhost>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox