Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to ALTER COLUMN changing data type #13874

Open
hlcianfagna opened this issue Mar 27, 2023 · 2 comments
Open

Ability to ALTER COLUMN changing data type #13874

hlcianfagna opened this issue Mar 27, 2023 · 2 comments

Comments

@hlcianfagna
Copy link
Contributor

hlcianfagna commented Mar 27, 2023

Problem Statement

After a system has been live and ingesting real data for a while, it may become desirable to change the data types on a table.
We may for instance want more precision to deal with new use cases, or we may realise that we can do with less precision and want to save space.

Possible Solutions

  • ADD COLUMN, copy existing data, DROP COLUMN, rename column?
  • an ALTER COLUMN statement that would allow forcing the cast on all existing values (like in other systems like in PostgreSQL - with ALTER COLUMN ... TYPE ... USING)

PostgreSQL Docs:

Example showing the behavior:

create table tbl (x text);
insert into tbl (x) values ('foo');
insert into tbl (x) values ('10');

alter table tbl alter column x type integer;
ERROR:  42804: column "x" cannot be cast automatically to type integer
HINT:  You might need to specify "USING x::integer".
LOCATION:  ATPrepAlterColumnType, tablecmds.c:12302

# alter table tbl alter column x type integer using x::integer;
ERROR:  22P02: invalid input syntax for type integer: "foo"
LOCATION:  pg_strtoint32, numutils.c:232
Time: 0.579 ms
# delete from tbl where x = 'foo';
# alter table tbl alter column x type integer using x::integer;

This implies re-indexing and transactions. We'd need an alternative way to handle this

Considered Alternatives

Move the data to a new table

@matriv
Copy link
Contributor

matriv commented Oct 22, 2023

Since we're going to use drop column and then re-add it with the new data type, we need to document that if the table is already populated, the existing data for the column are lost, we cannot re-index them using the new type, correct?

@jeeminso
Copy link
Contributor

jeeminso commented Oct 23, 2023

Maybe we can add > update > drop? No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants