ALTER TABLE x ALTER COLUMN y TYPE z

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <pgsql-docs(at)postgresql(dot)org>
Subject: ALTER TABLE x ALTER COLUMN y TYPE z
Date: 2006-03-22 17:58:13
Message-ID: 44213BD5.EE98.0025.0@wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

On this page:

http://www.postgresql.org/docs/8.1/interactive/sql-altertable.html

there is this user comment:

--------------------

To change the data type of a column, do this:

BEGIN;
ALTER TABLE tab ADD COLUMN new_col new_data_type;
UPDATE tab SET new_col = CAST(old_col AS new_data_type);
ALTER TABLE tab RENAME old_col TO temp_name;
ALTER TABLE tab RENAME new_col TO old_col;
ALTER TABLE tab DROP COLUMN temp_name;
COMMIT;

You might then want to do VACUUM FULL tab to reclaim the disk space
used by the expired rows.

--------------------

The 8.1 release (and the 8.0 release) support the same functionality
with a single line:

ALTER TABLE tab ALTER COLUMN old_col TYPE new_data_type;

I think the user comment should be removed, unless there is some
benefit to using the multi-step process. If there is some benefit, I
think it should be described, so that users know when to use it instead
of the simpler technique.

-Kevin

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Tom Lane 2006-03-22 18:36:57 Re: ALTER TABLE x ALTER COLUMN y TYPE z
Previous Message Jim C. Nasby 2006-03-22 13:33:28 Re: Online Backups: Minor Caveat, Major Addition?