Looks like we've got to delete or null out Message.parent for orphans. lpmain_staging=# -- Delete the Message rows. lpmain_staging=# DELETE FROM Message USING MessagesToDelete lpmain_staging-# WHERE Message.id = MessagesToDelete.message; ERROR: update or delete on table "message" violates foreign key constraint "message_parent_fk" on table "message" DETAIL: Key (id)=(6664529) is still referenced from table "message". lpmain_staging=# lpmain_staging=# -- Delete the bad BugWatch rows. lpmain_staging=# DELETE FROM BugWatch USING BugsWithBadBugWatches lpmain_staging-# WHERE BugsWithBadBugWatches.bug = BugWatch.bug lpmain_staging-# AND BugWatch.bugtracker = ( lpmain_staging(# SELECT id FROM BugTracker WHERE name = 'debbugs') lpmain_staging-# AND BugWatch.owner = ( lpmain_staging(# SELECT id FROM Person WHERE name = 'bug-watch-updater') lpmain_staging-# AND BugWatch.datecreated > ( lpmain_staging(# TIMESTAMP WITH TIME ZONE '2010-03-31 1100Z') lpmain_staging-# AND BugWatch.datecreated < ( lpmain_staging(# TIMESTAMP WITH TIME ZONE '2010-03-31 2300Z'); ERROR: current transaction is aborted, commands ignored until end of transaction block lpmain_staging=# lpmain_staging=# -- Delete the bad BugCve rows. This could overreach because there is lpmain_staging=# -- no ownership information in BugCve. lpmain_staging=# DELETE FROM BugCve USING BugsWithBadBugWatches lpmain_staging-# WHERE BugsWithBadBugWatches.bug = BugCve.bug lpmain_staging-# AND BugCve.date_created > ( lpmain_staging(# TIMESTAMP WITH TIME ZONE '2010-03-31 1100Z') lpmain_staging-# AND BugCve.date_created < ( lpmain_staging(# TIMESTAMP WITH TIME ZONE '2010-03-31 2300Z'); ERROR: current transaction is aborted, commands ignored until end of transaction block lpmain_staging=# lpmain_staging=# -- Delete the bad BugActivity rows. lpmain_staging=# DELETE FROM BugActivity USING BugsWithBadBugWatches lpmain_staging-# WHERE BugsWithBadBugWatches.bug = BugActivity.bug lpmain_staging-# AND BugActivity.person = ( lpmain_staging(# SELECT id FROM Person WHERE name = 'bug-watch-updater') lpmain_staging-# AND BugActivity.datechanged > ( lpmain_staging(# TIMESTAMP WITH TIME ZONE '2010-03-31 1100Z') lpmain_staging-# AND BugActivity.datechanged < ( lpmain_staging(# TIMESTAMP WITH TIME ZONE '2010-03-31 2300Z') lpmain_staging-# AND BugActivity.whatchanged IN ('bug watch added', 'cve linked'); ERROR: current transaction is aborted, commands ignored until end of transaction block