I have a table with about 220 million records :( and I need to add a foreign key constraint.
My command looks something like this:
ALTER TABLE events
ADD CONSTRAINT events_visitor_id_fkey
FOREIGN KEY (visitor_id)
REFERENCES visitors(id)
ON DELETE CASCADE;
It's been running for probably an hour now.
I ran this before hand:
set maintenance_work_mem='1GB';
What's the fastest way to do this, and about how long should it take. The table it references is only 25 million.
I'm running it on an RDS instance of db.r3.large (15 GB of RAM).
EDIT:
Just cancelled the command and got this:
ERROR: canceling statement due to user request
CONTEXT: SQL statement "SELECT fk."visitor_id" FROM ONLY "public"."events" fk LEFT OUTER JOIN ONLY "public"."visitors" pk ON ( pk."id" OPERATOR(pg_catalog.=) fk."visitor_id") WHERE pk."id" IS NULL AND (fk."visitor_id" IS NOT NULL)"