pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mike Broers <mbroers@gmail.com>
To: pgsql-admin@postgresql.org
Subject: Re: any tricks to get foreign key constraint builds faster
Date: Mon, 6 Jun 2011 14:11:58 -0500
Message-ID: <BANLkTikyv-no6x+H_NTnOFJi6CYttOsjNA@mail.gmail.com> (raw)
In-Reply-To: <BANLkTikB3v9zHAH4F5h=u_pRtNHmyv9t9A@mail.gmail.com>
References: <BANLkTikB3v9zHAH4F5h=u_pRtNHmyv9t9A@mail.gmail.com>

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
> 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?
>
>
>
>

view thread (3+ messages)  latest in thread

Message-ID: <BANLkTikyv-no6x+H_NTnOFJi6CYttOsjNA@mail.gmail.com>
Permalink:  ../BANLkTikyv-no6x+H_NTnOFJi6CYttOsjNA@mail.gmail.com/
Also on:    postgresql.org/message-id/BANLkTikyv-no6x+H_NTnOFJi6CYttOsjNA@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: mbroers@gmail.com
  Subject: Re: any tricks to get foreign key constraint builds faster
  In-Reply-To: <BANLkTikyv-no6x+H_NTnOFJi6CYttOsjNA@mail.gmail.com>
  References: <BANLkTikB3v9zHAH4F5h=u_pRtNHmyv9t9A@mail.gmail.com> <BANLkTikyv-no6x+H_NTnOFJi6CYttOsjNA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox