Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bDohK-0004AM-2A for pgsql-admin@arkaria.postgresql.org; Fri, 17 Jun 2016 08:00:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bDohJ-0001x7-LA for pgsql-admin@arkaria.postgresql.org; Fri, 17 Jun 2016 08:00:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bDofc-0008TB-Hv for pgsql-admin@postgresql.org; Fri, 17 Jun 2016 07:58:28 +0000 Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bDofZ-0006xN-4Y for pgsql-admin@postgresql.org; Fri, 17 Jun 2016 07:58:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20140207; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=UVy42rTBLZ627lT2DJQIKK2jpGL6HyAO8y89UWmAvm4=; b=Ek4BliVLhyVKd5Y30rqARHAq88 MUiwbtvV7IQBgZEykwbXWKfGLosuvChJ8oXUiAbt6XkWTYIhYeWN4giUx4wzSLyUXKlLxZYGxEJOM Hhkx44Cne+YJWLW8xOZuUkJdz; Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by depesz.com with esmtpa (Exim 4.87) (envelope-from ) id 1bDofW-0002Jc-Ei; Fri, 17 Jun 2016 09:58:22 +0200 Date: Fri, 17 Jun 2016 09:58:22 +0200 From: hubert depesz lubaczewski To: =?utf-8?Q?=C5=81ukasz?= Brodziak Cc: PostgreSQL Subject: Re: Adding same column to several tables. Message-ID: <20160617075821.GA2937@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.6.0 (2016-04-01) X-Pg-Spam-Score: -3.4 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org 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