pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@depesz.com>
To: Łukasz Brodziak <l.brodziak@outlook.com>
Cc: PostgreSQL <pgsql-admin@postgresql.org>
Subject: Re: Adding same column to several tables.
Date: Fri, 17 Jun 2016 09:58:22 +0200
Message-ID: <20160617075821.GA2937@depesz.com> (raw)
In-Reply-To: <DUB128-W67EFD26DEC27182E9E45319C560@phx.gbl>
References: <DUB128-W67EFD26DEC27182E9E45319C560@phx.gbl>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-admin>

On Thu, Jun 16, 2016 at 10:19:10AM +0200, Łukasz Brodziak wrote:
> 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? 		 	   		  

Two problems:
1. you're adding column with default value, which means it has to
   rewrite the table, to set value for this column is all rows to -1.
2. you're adding column to 22 tables, in single transaction. this means
   it will be prone to deadlocks.

it would be better to write simple shell script to add these columns,
one per connection, in parallel.

Best regards,

depesz

-- 
The best thing about modern society is how easy it is to avoid contact with it.
                                                             http://depesz.com/


-- 
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



view thread (3+ messages)

Message-ID: <20160617075821.GA2937@depesz.com>
Permalink:  ../20160617075821.GA2937@depesz.com/
Also on:    postgresql.org/message-id/20160617075821.GA2937@depesz.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: depesz@depesz.com, l.brodziak@outlook.com
  Subject: Re: Adding same column to several tables.
  In-Reply-To: <20160617075821.GA2937@depesz.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox