Skip to content

Commit 7589d5c

Browse files
committed
Disallow USING clause when altering type of generated column
This does not make sense. It would write the output of the USING clause into the converted column, which would violate the generation expression. This adds a check to error out if this is specified. There was a test for this, but that test errored out for a different reason, so it was not effective. Reported-by: Jian He <jian.universality@gmail.com> Reviewed-by: Yugo NAGATA <nagata@sraoss.co.jp> Discussion: https://www.postgresql.org/message-id/flat/c7083982-69f4-4b14-8315-f9ddb20b9834%40eisentraut.org
1 parent 18e3e29 commit 7589d5c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/backend/commands/tablecmds.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11419,6 +11419,16 @@ ATPrepAlterColumnType(List **wqueue,
1141911419
errmsg("cannot alter system column \"%s\"",
1142011420
colName)));
1142111421

11422+
/*
11423+
* Cannot specify USING when altering type of a generated column, because
11424+
* that would violate the generation expression.
11425+
*/
11426+
if (attTup->attgenerated && def->cooked_default)
11427+
ereport(ERROR,
11428+
(errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
11429+
errmsg("cannot specify USING when altering type of generated column"),
11430+
errdetail("Column \"%s\" is a generated column.", colName)));
11431+
1142211432
/*
1142311433
* Don't alter inherited columns. At outer level, there had better not be
1142411434
* any inherited definition; when recursing, we assume this was checked at
@@ -11495,11 +11505,12 @@ ATPrepAlterColumnType(List **wqueue,
1149511505
(errcode(ERRCODE_DATATYPE_MISMATCH),
1149611506
errmsg("column \"%s\" cannot be cast automatically to type %s",
1149711507
colName, format_type_be(targettype)),
11508+
!attTup->attgenerated ?
1149811509
/* translator: USING is SQL, don't translate it */
1149911510
errhint("You might need to specify \"USING %s::%s\".",
1150011511
quote_identifier(colName),
1150111512
format_type_with_typemod(targettype,
11502-
targettypmod))));
11513+
targettypmod)) : 0));
1150311514
}
1150411515

1150511516
/* Fix collations after all else */

src/test/regress/expected/generated.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,8 @@ SELECT * FROM gtest27;
766766
(2 rows)
767767

768768
ALTER TABLE gtest27 ALTER COLUMN x TYPE boolean USING x <> 0; -- error
769-
ERROR: generation expression for column "x" cannot be cast automatically to type boolean
769+
ERROR: cannot specify USING when altering type of generated column
770+
DETAIL: Column "x" is a generated column.
770771
ALTER TABLE gtest27 ALTER COLUMN x DROP DEFAULT; -- error
771772
ERROR: column "x" of relation "gtest27" is a generated column
772773
HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION instead.

0 commit comments

Comments
 (0)