Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D13E9B5FE0F for ; Mon, 6 Jun 2011 16:12:06 -0300 (ADT) 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 63655-01 for ; Mon, 6 Jun 2011 19:11:59 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iy0-f174.google.com (mail-iy0-f174.google.com [209.85.210.174]) by mail.postgresql.org (Postfix) with ESMTP id 5A625B5FE7D for ; Mon, 6 Jun 2011 16:11:59 -0300 (ADT) Received: by iyb14 with SMTP id 14so3347888iyb.19 for ; Mon, 06 Jun 2011 12:11:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=eMi2X4z2i2Br5gruhzCXRkfLwYT9+LL5pSrhENvTbDg=; b=eNzhzFEGEHMOnQMR/XIeYZAe/1X4kQCtI4lnwbIO/4dpzc01NiF9gCUtDOYoC4G812 oGuHHAMJ2GbZSGH8Phi3FlUIRkfvnm9AGrA/zaMvPImVSKlXUANfQ2FvvBjFSRjhnjru aw9Y71t1t4hMLr6S8W3MS0cmluW2HAjQBidDc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=Ud9k9U1jiGJHd9PmZE4vCs6MNLiM0JL4cPtHQZVCbLIUmzOdgMfNs4UMgZ9RCt4NKl hdi4MnAWRTkZL0EXwx/dwS+wb1uzhWMaIRttu2yxRVVf9s7lFyLrD+0JPDCmXMFTPCZB ClGpi30EuAU9iHqsiaefN7FaqxtJ1M/n2xFt4= MIME-Version: 1.0 Received: by 10.231.39.14 with SMTP id d14mr3078597ibe.98.1307387518622; Mon, 06 Jun 2011 12:11:58 -0700 (PDT) Received: by 10.231.199.74 with HTTP; Mon, 6 Jun 2011 12:11:58 -0700 (PDT) In-Reply-To: References: Date: Mon, 6 Jun 2011 14:11:58 -0500 Message-ID: Subject: Re: any tricks to get foreign key constraint builds faster From: Mike Broers To: pgsql-admin@postgresql.org Content-Type: multipart/alternative; boundary=002215045717a19b5504a50fe08a X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.887 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RFC_ABUSE_POST=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201106/26 X-Sequence-Number: 37652 --002215045717a19b5504a50fe08a Content-Type: text/plain; charset=ISO-8859-1 Should this be posted in performance instead? On Fri, Jun 3, 2011 at 9:46 AM, Mike Broers wrote: > I am in the process of implementing cascade on delete constraints > retroactively on rather large tables so I can cleanly remove deprecated > data. The problem is recreating some foreign key constraints on tables of > 55 million rows+ was taking much longer than the maintenance window I had, > and now I am looking for tricks to speed up the process, hopefully there is > something obvious i am overlooking. > > BEGIN; > ALTER TABLE ONLY t1 DROP CONSTRAINT fk_t1_t2_id; > ALTER TABLE ONLY t1 ADD CONSTRAINT fk_t1_t2_id FOREIGN KEY(id) REFERENCES > t2(id) > ON DELETE CASCADE > DEFERRABLE INITIALLY DEFERRED; > COMMIT; > > t1 has 55 million rows > t2 has 72 million rows > > I have tried set constraints deferred, immediate, the id column on table 2 > is indexed, its the primary key. There may be memory settings to tweak, I > was able to get it to run on a faster test server with local storage in > about 10 minutes, but it was running for over an hour in our production > environment.. We took down the application and I verified it wasnt waiting > for an exclusive lock on the table or anything, it was running the alter > table command for that duration. > > An additional question - is there any way to check how long postgres is > estimating an operation will take to complete while it is running? > > > > --002215045717a19b5504a50fe08a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Should this be posted in performance instead?

On Fri, Jun 3, 2011 at 9:46 AM, Mike Broers <mbroers@gmail.com> wrote:
I am in the process of implementing cascade on delete constraints retroacti= vely on rather large tables so I can cleanly remove deprecated data. =A0The= problem is recreating some foreign key constraints on tables of 55 million= rows+ was taking much longer than the maintenance window I had, and now I = am looking for tricks to speed up the process, hopefully there is something= obvious i am overlooking.

BEGIN;
ALTER TABLE ONLY t1 DROP CONSTRAINT fk= _t1_t2_id;
ALTER TABLE ONLY t1 ADD CONSTRAINT fk_t1_t2_id FOREIGN= KEY(id) REFERENCES t2(id)=A0
ON DELETE CASCADE=A0
DEFE= RRABLE INITIALLY DEFERRED;
COMMIT;

t1 has 55 million rows
t2 has 72 million rows

I have tried set constrai= nts deferred, immediate, the id column on table 2 is indexed, its the prima= ry key. =A0There may be memory settings to tweak, I was able to get it to r= un on a faster test server with local storage in about 10 minutes, but it w= as running for over an hour in our production environment.. We took down th= e application and I verified it wasnt waiting for an exclusive lock on the = table or anything, it was running the alter table command for that duration= .

An additional question - is there any way to check how = long postgres is estimating an operation will take to complete while it is = running?




--002215045717a19b5504a50fe08a--