Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id DA3E811FAB41 for ; Mon, 16 Apr 2012 22:03:01 -0300 (ADT) Received: from mx2.d2.com ([66.251.81.121]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SJwoq-0008Ck-Kc for pgsql-admin@postgresql.org; Tue, 17 Apr 2012 01:02:59 +0000 Received: from mxb.d2.com ([10.85.4.6]) by mx2.d2.com (8.13.8/8.13.8) with ESMTP id q3H1wseQ001662 for ; Mon, 16 Apr 2012 18:59:09 -0700 X-ASG-Debug-ID: 1334624539-01d10a6c9874b7a0001-ysJW5H Received: from mail01.d2.com ([172.16.66.171]) by mxb.d2.com with ESMTP id KMZwbGQR56G2CiWE for ; Mon, 16 Apr 2012 18:02:19 -0700 (PDT) X-Barracuda-Envelope-From: aalvarez@d2.com Received: from exbew2k3-02.d2.com (exbew2k3-02 [10.78.2.16]) by mail01.d2.com (8.13.8/8.13.8/Debian-3) with ESMTP id q3H12Juv007999 for ; Mon, 16 Apr 2012 18:02:19 -0700 Received: from [10.126.2.220] ([10.126.2.220] RDNS failed) by exbew2k3-02.d2.com with Microsoft SMTPSVC(6.0.3790.3959); Mon, 16 Apr 2012 18:02:18 -0700 Message-ID: <4F8CC11A.5020003@d2.com> Date: Mon, 16 Apr 2012 18:02:18 -0700 From: amador alvarez User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.2.27) Gecko/20120216 Lightning/1.0b2 Thunderbird/3.1.19 MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: Re: Recreate primary key without dropping foreign keys? References: <4F8B6AA1.1040709@zvelo.com> <20120416065717.70a31a6361ac5d7bdad416e8@frank.uvena.de> <4F8BD91C.6070405@zvelo.com> <4F8BDAB2.7010209@frank.uvena.de> <4F8C248E.7070307@zvelo.com> X-ASG-Orig-Subj: Re: [ADMIN] Recreate primary key without dropping foreign keys? In-Reply-To: <4F8C248E.7070307@zvelo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 17 Apr 2012 01:02:18.0932 (UTC) FILETIME=[BCC27F40:01CD1C35] X-Barracuda-Connect: UNKNOWN[172.16.66.171] X-Barracuda-Start-Time: 1334624539 X-Barracuda-URL: http://10.85.4.6:8000/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at d2.com X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using per-user scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=9.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.2.94337 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-Spam-Status: NO, score=-2.6 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, RATWARE_GECKO_BUILD autolearn=ham version=3.2.4 X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on mx2.d2.com X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201204/85 X-Sequence-Number: 39421 How about deferring the FK's while recreating the PK ? or using a temporary parallel table to be pointed by the other tables (FK) and swap it up on the recreation. Cheers, A.A On 04/16/2012 06:54 AM, Chris Ernst wrote: > On 04/16/2012 02:39 AM, Frank Lanitz wrote: >> Am 16.04.2012 10:32, schrieb Chris Ernst: >>> On 04/15/2012 10:57 PM, Frank Lanitz wrote: >>>> On Sun, 15 Apr 2012 18:41:05 -0600 Chris Ernst >>>> wrote: >>>> >>>>> Hi all, >>>>> >>>>> In PostgreSQL 9.1.3, I have a few fairly large tables with >>>>> bloated primary key indexes. I'm trying to replace them using >>>>> newly created unique indexes as outlined in the docs. Something >>>>> like: >>>>> >>>>> CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON >>>>> distributors (dist_id); ALTER TABLE distributors DROP CONSTRAINT >>>>> distributors_pkey, ADD CONSTRAINT distributors_pkey PRIMARY KEY >>>>> USING INDEX dist_id_temp_idx; >>>>> >>>>> However, the initial drop of the primary key constraint fails >>>>> because there are a whole bunch of foreign keys depending on it. >>>>> >>>>> I've done some searching and haven't found a workable solution. >>>>> Is there any way to swap in the new index for the primary key >>>>> constraint without dropping all dependent foreign keys? Or am I >>>>> pretty much stuck with dropping and recreating all of the foreign >>>>> keys? >>>> REINDEX is not working here? >>> Hi Frank, >>> >>> Thanks, but REINDEX is not an option as it would take an exclusive >>> lock on the table for several hours. >> Well, from my little view I guess all rebuilding index action would >> require such, as its the primary key with uniqueness. I'd think of a >> complete reinit of the cluster with pg_dump and restoring, but this >> would also need a downtime at least for write access. >> >> Why is the index so bloated? > As in my original post, you can create a unique index concurrently and > then replace the primary key index with it. This way, the index > creation doesn't require an exclusive lock. You only need a very brief > exclusive lock to drop and recreate the primary key constraint using the > new index. > > However, the index creation is not the issue here. That part is done. > The issue is that there are several foreign keys depending on the > primary key index that I want to drop and replace with the newly built > unique index. I would prefer not to drop and recreate all of the > foreign keys as that would require many hours of down time as well (the > very situation I was trying to avoid by building the index concurrently > and swapping it in). > > I believe the index bloat is due to a combination of under aggressive > autovacuum settings and recently deleting about 30% of the table. > > - Chris >