I have a question about the ALTER TABLE
command on a really large table (almost 30 millions rows).
One of its columns is a varchar(255)
and I would like to resize it to a varchar(40)
.
Basically, I would like to change my column by running the following command:
ALTER TABLE mytable ALTER COLUMN mycolumn TYPE varchar(40);
I have no problem if the process is very long but it seems my table is no more readable during the ALTER TABLE
command.
Is there a smarter way? Maybe add a new column, copy values from the old column, drop the old column and finally rename the new one?
Note: I use PostgreSQL 9.0.
resizing
will not make the table occupy less space?varchar(255)
to PostgreSQL then it will not allocate 255 bytes for a value which real length is 40 bytes. It will allocate 40 bytes (plus some internal overhead). The only thing which willbe changed by the
ALTER TABLE` is the maximum number of bytes you can store in that column without getting an error from PG.