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

Поиск
Список
Период
Сортировка
Искать

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

От:
PG Bug reporting form <noreply@postgresql.org>
Дата:
The following bug has been logged on the website:

Bug reference:      19756
Logged by:          kalakala
Email address:      zj4204@gmail.com
PostgreSQL version: 18.6
Operating system:   Debian 13 (official Docker image postgres:18.6)
Description:        

ALTER TABLE ... ALTER COLUMN ... TYPE ... USING stores wrong values in
partitions and inheritance children whose column numbers differ from the
parent's (here the parent has a dropped column). This happens when the USING
expression contains IS [NOT] NULL, IS [NOT] TRUE/FALSE/UNKNOWN, or a named
function argument.

CREATE TABLE pu (x int, k int, a int, b int, c int) PARTITION BY LIST (k);
ALTER TABLE pu DROP COLUMN x;
CREATE TABLE pu1 PARTITION OF pu FOR VALUES IN (1);
INSERT INTO pu VALUES (1, 5, NULL, 9);
ALTER TABLE pu ALTER COLUMN a TYPE bigint USING CASE WHEN b IS NULL THEN -1
ELSE a END;
SELECT k, a, b, c FROM pu;

 k | a | b | c
---+---+---+---
 1 | 5 |   | 9

Expected a = -1, because b is NULL. The same statements on a table without
partitions give a = -1, and so does 12.22. With num_nulls(b) = 1 in place of
b IS NULL, the result is also -1. b IS UNKNOWN and b IS NOT TRUE (with
boolean b and c) give the same wrong result.

With a named argument on the same layout, USING extract(day from
make_interval(days => b))::int on a row with b = 2, c = 30 sets a = 30. The
positional form make_interval(0, 0, 0, b) sets a = 2.

Two variants fail with an internal error instead:
CREATE TABLE r (a int);
CREATE TABLE r1 () INHERITS (r);
ALTER TABLE r ALTER COLUMN a TYPE int USING (1 IS NULL)::int;
ERROR:  XX000: unrecognized node type: 7
LOCATION:  transformExprRecurse, parse_expr.c:376

CREATE TABLE p1 (x int, a int, b int);
ALTER TABLE p1 DROP COLUMN x;
CREATE TABLE c1 () INHERITS (p1);
INSERT INTO c1 VALUES (30, NULL);
ALTER TABLE p1 ALTER COLUMN a TYPE int USING (b IS NULL)::int;
ERROR:  XX000: attribute number 3 exceeds number of columns 2
LOCATION:  CheckVarSlotCompatibility, execExprInterp.c:2411

The first of these fails although r1 has the same column layout as r.

Reproduced on 18.6, 19beta4 and 13.23 (PGDG Debian packages in the official
Docker images). 12.22 gives the expected result in every case above.




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

От:
Sebastian Webber <sebastian@swebber.me>
Дата:
Вложения:
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
FAQ