Re: How to optimize a column type change???

From: Jean-Michel POURE <jm(dot)poure(at)freesurf(dot)fr>
To: pgsql-general(at)postgresql(dot)org
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: How to optimize a column type change???
Date: 2001-11-09 06:33:23
Message-ID: 4.2.0.58.20011109073150.00a42200@pop.freesurf.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

The simpler solution is to learn C and add this feature to PostgreSQL
internals.

At 20:52 08/11/01 +0100, you wrote:
>Hello all!
>
>As postgresql does not have alter table modify column or alter table drop
>column, is there
>any simpler way to change a column definition??
>
>For example to change a column varchar(40) to varchar(40)[] here you have
>the steps I follow:
>
>Suppose this table:
> CREATE TABLE "proy_foto" (
> "numero" int4 DEFAULT nextval('proy_foto_numero_seq'::text)
> NOT NULL,
> "idproy" int4,
> "foto" oid,
> "nombre" varchar(40),
> "descrip" text,
> PRIMARY KEY ("numero")
> );
>1. Add the new column def
> alter table proy_foto add nombre2 varchar(40)[];
> alter table proy_foto add descrip2 text[];
>
>2. Initialize with a default value.
>
> update proy_foto set nombre2 = '{ "1" }', descrip2 = '{"2"}';
>
>3.Update the columns with their corresponding values.
>
> UPDATE proy_foto
> SET nombre2[1] = nombre,
> descrip2[1] = descrip
> FROM proy_foto
> WHERE numero = numero;
>
>4. Initialize the obsolete columns
>
> update proy_foto set nombre = '', descrip = '';
>
>5. Rename the obsolete columns
> alter table proy_foto rename column nombre to obsolete1;
> alter table proy_foto rename column descrip to obsolete2;
>
>6. Rename the new columns with the old name.
> alter table proy_foto rename column nombre2 to nombre;
> alter table proy_foto rename column descrip2 to descrip;
>
>
>Any simpler idea?
>
>Thanks in advance
>
>------------
>Evelio Martínez

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Einar Karttunen 2001-11-09 06:38:59 Re: What's the fastest way to do this?
Previous Message PIPER,JIM (A-SantaClara,ex1) 2001-11-09 05:43:02 MS Client ODBC question

Browse pgsql-hackers by date

  From Date Subject
Next Message Brent R. Matzelle 2001-11-09 14:30:34 Re: Postgre for Windows
Previous Message Thomas Lockhart 2001-11-09 06:21:57 Re: Call for objections: revision of keyword classification