Adding same column to several tables.

From: Łukasz Brodziak <l(dot)brodziak(at)outlook(dot)com>
To: PostgreSQL <pgsql-admin(at)postgresql(dot)org>
Subject: Adding same column to several tables.
Date: 2016-06-16 08:19:10
Message-ID: DUB128-W67EFD26DEC27182E9E45319C560@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hello,
I have a function to add column to all tables from the list:do$$declare relnam pg_class.relname%TYPE; kur cursor for select c.relname from pg_class as c inner join pg_attribute as a on a.attrelid = c.oid where a.attname = 'some_id' and c.relkind = 'r';begin open kur; LOOP Fetch kur into relnam; exit when not found; EXECUTE 'ALTER TABLE '|| relnam|| ' add column another_id integer default -1'; END LOOP; close kur;END;$$
I have 22 table names returned by the query used by cursor. Whe I run the function it executes for 1.5 minutes. Is there a way of doing it faster?

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message David G. Johnston 2016-06-16 20:28:12 Re: Adding same column to several tables.
Previous Message Alvaro Herrera 2016-06-14 17:31:50 Re: MultiXactId xxxxxxxxx has not been created yet -- apparent wraparound