serial not accepted as datatype in ALTER TABLE ... ALTER COLUMN

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: serial not accepted as datatype in ALTER TABLE ... ALTER COLUMN
Date: 2024-02-12 13:50:28
Message-ID: CAExHW5vwCauLutJwcmbvHKttp8-eL5_EMichmKw32HGESpwFjA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi All,
alter table t1 add column c serial;
ALTER TABLE

this works, but not
#alter table t1 add column c int;
ALTER TABLE
#alter table t1 alter column c type serial;
ERROR: type "serial" does not exist

Looking at the documentation [1], the grammar for both mentions data_type

ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type and

ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type

data_type is described on that page as "Data type of the new column,
or new data type for an existing column." but CREATE TABLE
documentation [2] redirects data_type to [3], which mentions serial.
The impression created by the documentation is the second statement
above is a valid statement as should not throw an error; instead
change the data type of the column (and create required sequence).

In code ATPrepAlterColumnType() calls typenameTypeIdAndMod(), whereas
transformColumnDefinition() (called for ALTER TABLE ... ADD COLUMN and
CREATE TABLE) handles "serial" data type separately. Looks like we are
missing a call to transformColumnDefinition() in
transformAlterTableStmt() under case AT_AlterColumnType.

[1] https://www.postgresql.org/docs/current/sql-altertable.html
[2] https://www.postgresql.org/docs/16/sql-createtable.html
[3] https://www.postgresql.org/docs/16/datatype.html

--
Best Wishes,
Ashutosh Bapat

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dave Cramer 2024-02-12 13:51:45 Re: [PATCH] Add native windows on arm64 support
Previous Message Jelte Fennema-Nio 2024-02-12 13:27:42 Re: Support a wildcard in backtrace_functions