Re: BUG #19756: ALTER COLUMN TYPE USING gives wrong values on partitions with IS NULL

From: Sebastian Webber <sebastian(at)swebber(dot)me>
To: zj4204(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19756: ALTER COLUMN TYPE USING gives wrong values on partitions with IS NULL
Date: 2026-10-14 14:45:14
Message-ID: CACV2tSxXUK0BbGzSgDdjuAZcnYDon_4CU_+-b8pBYmCtmT6mew@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

On Tue, Oct 13, 2026 at 17:24:13 +0000, PG Bug reporting form wrote:
> ALTER TABLE ... ALTER COLUMN ... TYPE ... USING stores wrong values in
> partitions and inheritance children whose column numbers differ from
> the parent's

I can reproduce all three cases on 13.23, 14.24, 15.19, 16.15, 17.11
and 18.6 (official Docker images), and on master at d733fcaf5ca. 12.22
gets them right.

This is a regression from 1281a5c907b. That commit kept parse analysis
of ALTER COLUMN TYPE in phase 1, so that USING is parsed against the
table's original state. But ATPrepCmd also runs ATParseTransformCmd
for each child, so each child parses the raw USING expression again.
The result replaces the expression that ATPrepAlterColumnType had
already mapped to the child's column numbers.

The second parse goes wrong because transformExpr rewrites NullTest,
BooleanTest and NamedArgExpr in place. After the parent's pass, their
arguments are already Vars with the parent's attnos, and the child
keeps them as they are. When the argument is a Const, the child's pass
fails with "unrecognized node type".

The same second parse breaks a column qualified with the parent's
name. This works in 12:

CREATE TABLE q (a int, b int);
CREATE TABLE q1 () INHERITS (q);
ALTER TABLE q ALTER COLUMN a TYPE bigint USING q.b;
ERROR: missing FROM-clause entry for table "q"

The attached patch parses USING only when there is no cooked
expression yet, so children keep the mapped expression, as in 12. It
adds regression tests for the wrong values, the Const and the
qualified name.

I tested it on master with a meson debug build with assertions on
Debian trixie. The regression suite passes with the patch, and without
the fix alter_table shows all three problems. The patch also applies
cleanly to REL_14_STABLE through REL_18_STABLE, with the same results
(autoconf builds with assertions).

I used Claude Opus 5.5 (reasoning medium) for the analysis, the patch
and the tests, and Codex gpt-6.1-sol (reasoning high) to review the
patch.

--
Sebastian Webber

Attachment Content-Type Size
v1-0001-Fix-ALTER-COLUMN-TYPE-USING-for-inheritance-child.patch text/x-patch 5.8 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-10-14 15:02:09 Re: BUG #19757: attname in pg_attribute not strictly updated when renaming a column
Previous Message Sami Imseih 2026-10-14 14:41:24 Re: pg_stat_statements stores WITH HOLD cursor query with literals