Re: Changing colums type

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: "A(dot)Bhuvaneswaran" <bhuvansql(at)myrealbox(dot)com>
Cc: Andrei Verovski <andreil1(at)mail(dot)starlett(dot)lv>, <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Changing colums type
Date: 2003-05-02 16:18:30
Message-ID: 20030502091054.A65288-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin


On Fri, 2 May 2003, A.Bhuvaneswaran wrote:

> > Hi,
> >
> > I would like to change column type from int8 to int4 in psql command
> > line utility. I am run command ALTER TABLE blah blah blah, but nothing
> > happens. No error message and no change of column type. Anyone have a
> > clue what is happening?
>
> Way 1:
>
> => BEGIN;
> => ALTER TABLE my_table ADD COLUMN new_column int4;
> => UPDATE my_table SET new_column = old_column;
> => ALTER TABLE my_table DROP COLUMN old_column;
> => COMMIT;
>
> Way 2:
>
> =# BEGIN;
> =# UPDATE pg_attribute SET atttypid = pg_type.oid WHERE attrelid =
> pg_class.oid AND pg_class.relnamespace = pg_namespace.oid AND
> pg_namespace.nspname = 'my_schema' AND pg_class.relname = 'my_table' AND
> pg_attribute.attname = 'my_column' AND pg_type.typname = 'int4';
> =# COMMIT;

I think this second way is a bad idea if there's already data in
the table since I don't think it'll preserve values properly and
in simple tests has gotten me crashes depending on what follows it.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message jonesbl 2003-05-02 18:31:24 pg_dumpall / pg_dump
Previous Message Andrew Sullivan 2003-05-02 14:17:57 Re: Impact of loss of unique SERIAL?