Re: Foreign key creation on table with huge record count.

From: Ron <ronljohnsonjr(at)gmail(dot)com>
To: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: Foreign key creation on table with huge record count.
Date: 2023-01-30 21:55:38
Message-ID: 5b627408-a8b1-050b-cac1-3fa71c13b691@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On 1/30/23 14:12, Gambhir Singh wrote:
> Hi,
>
> I have a table with a record count of around 100 Million records. while
> creating foreign key on the table took a significant amount time to get
> created.
>
> Is there any way to speed up the execution of creation of foreign key
> constraint ?

I combine the suggestions of the other two emails:
1. Make sure that there's an index on the relevant field of the referenced
table.
2. ALTER TABLE foo ADD CONSTRAINT foo_fk FOREIGN KEY(bar) REFERENCES blarge
(bar) NOT VALID;
3. ALTER TABLE foo VALIDATE CONSTRAINT.

It's *really* fast.

(Step 1 is vital for operations like purging old records.  Without that
index deletes can take months.)

--
Born in Arizona, moved to Babylonia.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Marcello Lorenzi 2023-01-30 22:57:45 Pgsql best ha solution
Previous Message Lucio Chiessi 2023-01-30 21:21:38 Re: Foreign key creation on table with huge record count.