Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id A2B051337B99 for ; Tue, 28 Dec 2010 09:54:53 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 19483-10 for ; Tue, 28 Dec 2010 13:54:48 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 5E08C1337B68 for ; Tue, 28 Dec 2010 09:54:46 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id oBSDsjIT061816 for ; Tue, 28 Dec 2010 09:54:46 -0400 (AST) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id oBSDXu3o023666 for pgsql-performance@postgresql.org; Tue, 28 Dec 2010 09:33:56 -0400 (AST) (envelope-from news) From: Lew X-Newsgroups: pgsql.performance Subject: Re: adding foreign key constraint locks up table Date: Tue, 28 Dec 2010 08:34:30 -0500 Organization: albasani.net Lines: 28 Message-ID: References: <75218696-61be-4730-89f6-dd6058fa9eda@a28g2000prb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit In-Reply-To: <75218696-61be-4730-89f6-dd6058fa9eda@a28g2000prb.googlegroups.com> Cancel-Lock: sha1:Loqm1uEsOikktbILdt4oROWKmTA= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7 Injection-Info: news.albasani.net; logging-data="jHCM/JPWVqaBUSKajAB/2vZ42uMt8jK4rIo4ntnFAoQiY7TdFwVsf7JgOJAkoCp/Y4DGKrZwZGczeFMoWAYm2aCIB1a14ma1p3GVQ8569V1OhAxBUjnhayNAjeqlrAYW"; mail-complaints-to="abuse@albasani.net" To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201012/318 X-Sequence-Number: 41770 On 12/28/2010 02:08 AM, kakarukeys wrote: > I have a table "aaa" which is not very big. It has less than 10'000 > rows. However read operations on this table is very frequent. > > Whenever I try to create a new table "bbb" with foreign key pointing > to "aaa". The operation locks, and reading "aaa" is not possible. The > query also never seems to finish. How long did you wait? > ALTER TABLE "bbb" ADD CONSTRAINT "topic_id_refs_id_3942a46c6ab2c0b4" > FOREIGN KEY ("topic_id") REFERENCES "aaa" ("id") DEFERRABLE INITIALLY > DEFERRED; > > The current workaround is to create any new table at off-peak hours, > e.g. [sic] midnight after restarting the db. > > I would like to know if there's any proper solution of this. Is this > an issue affecting all relational databases? My db is PostgreSQL 8.3. Naturally the system has to lock the table to alter it. It also has to check that all records already in "bbb" satisfy the new constraint. What's the longest you've waited for ALTER TABLE to release its lock? -- Lew Ceci n'est pas une pipe.