Speeding up adding fky on a very large table

Started by Ravi Krishnaover 3 years ago3 messagesgeneral
Jump to latest
#1Ravi Krishna
s_ravikrishna@aol.com

AWS Aurora based on PG 13.

Large partitioned table of 5+ billion rows and 7TB in size.
ALTER TABLE abc ADD CONSTRAINT fk_123 FOREIGN KEY (a,b,c) REFERENCES xyz(1,2,3);
It seems this is not parallelized. Is there a way. Or directly going into each partition is the only way ( not even sure it is possible).

#2Ron
ronljohnsonjr@gmail.com
In reply to: Ravi Krishna (#1)
Re: Speeding up adding fky on a very large table

On 10/19/22 08:31, Ravi Krishna wrote:

AWS Aurora based on PG 13.

Large partitioned table of 5+ billion rows and 7TB in size.

ALTER TABLE abc ADD CONSTRAINT fk_123 FOREIGN KEY (a,b,c) REFERENCES
xyz(1,2,3);

It seems this is not parallelized. Is there a way. Or directly going into
each partition is the only way ( not even sure it is possible).

In vanilla Postgresql (including RDS Postgresql) you can add the FK to each
partition and then ALTER TABLE ONLY the parent table.

Don't know about Aurora, though.

--
Angular momentum makes the world go 'round.

#3Ravi Krishna
s_ravikrishna@aol.com
In reply to: Ron (#2)
Re: Speeding up adding fky on a very large table

Our issue is that it takes 20hrs to index the full table. Hopefully we can add FK in multiple child partitions concurrently, otherwise doing it per partition offers no advantage from performance pov.
Need to test. Hopefully PG should not lock the referred table during the first build, stopping concurrent execution of other partitions.

In vanilla Postgresql (including RDS Postgresql) you can add the FK to each partition and then ALTER TABLE ONLY the parent table.

Don't know about Aurora, though