Another idea is to try an outer join:
SELECT child_table.parentid INTO tmp_table
FROM child_table LEFT JOIN parent_table
ON (child_table.parentid = parent_table.parentid)
WHERE parent_table.parentid IS NULL;
There's also
DELETE
FROM child_table
WHERE NOT EXISTS (select 1
from parent_table
I have several large tables (10 million to 200 million rows) that have
foreign keys with each other by *convention*, but no actual FOREIGN KEY
constraints.
Over the course of years, orphaned records (children with no parent) have
accumulated and now I want to clean them up. I can't just create