Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VbaNi-0001PE-0H for pgsql-performance@arkaria.postgresql.org; Wed, 30 Oct 2013 18:20:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VbaNh-0003xP-G9 for pgsql-performance@arkaria.postgresql.org; Wed, 30 Oct 2013 18:20:37 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VbaNg-0003xJ-KY for pgsql-performance@postgresql.org; Wed, 30 Oct 2013 18:20:36 +0000 Received: from smtp.01.com ([199.36.142.181]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VbaNd-0007Mb-Cr for pgsql-performance@postgresql.org; Wed, 30 Oct 2013 18:20:35 +0000 Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id E7FE7294026 for ; Wed, 30 Oct 2013 13:20:32 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0zRiHce0Lzga for ; Wed, 30 Oct 2013 13:20:32 -0500 (CDT) Received: from smtp.01.com (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id C76A82940F2 for ; Wed, 30 Oct 2013 13:20:32 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id BA382294026 for ; Wed, 30 Oct 2013 13:20:32 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id IOaN5BFJGgNt for ; Wed, 30 Oct 2013 13:20:32 -0500 (CDT) Received: from [172.47.23.102] (50-1-86-31.dsl.dynamic.sonic.net [50.1.86.31]) by smtp-out-1.01.com (Postfix) with ESMTPSA id 7785F29409B for ; Wed, 30 Oct 2013 13:20:31 -0500 (CDT) Message-ID: <52714DEE.9040105@agliodbs.com> Date: Wed, 30 Oct 2013 11:20:30 -0700 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Adding foreign key constraint holds exclusive lock for too long (on production database) References: In-Reply-To: X-Enigmail-Version: 1.6 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Ben, > A couple of questions about the ADD CONSTRAINT. The foreign key column > on the local table is indexed, and there are only ~50 unique values, > so the db *could* come up with the unique values pretty quickly and > then check them. This would indeed be a nice optimization, especially now that we have index-only scans; you could do a VACUUM FREEZE on the tables and then add the constraint. > b) Delete old rows from the table so it's not so big. Feels a bit > hacky just to fix this issue. > > c) Get rid of this foreign key constraint entirely and just check it > in code when we insert. Pragmatic solution, but not ideal. d) add a trigger instead of an actual FK. Slower to execute on subsequent updates/inserts, but doesn't need to be checked. e) do something (slony, scripts, whatever) so that you're incrementally updating this table instead of recreating it from scratch each time. > * We're running "PostgreSQL 9.0.2, compiled by Visual C++ build 1500, > 64-bit" on 64-bit Windows Server 2008 SP1 (6.0.6001) I will point out that you are missing a whole ton of bug fixes, including two critical security patches. > * The "ratesrequests" table has two text columns, one of which often > contains a few hundred to a couple of KB of data in the field. It is > added to rapidly. We regularly VACCUM ANALYZE it. > * As expected, the ADD CONSTRAINT has gotten slower over time as this > table grew. However -- I'm not 100% sure of this, but it seems to have > jumped recently (from 3-4 minutes to 7 minutes). > * http://www.postgresql.org/message-id/20030323112241.W14634-100000@megazone23.bigpanda.com Probably the table just got larger than RAM. -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance