I am using:
set constraints all deferred; (lots of deletes and inserts) commit;
This works as expected. If there are any broken relationships then the commit fails and an error is raised listing ONE of the FKs that it fails on.
The user fixes the offending data and runs again. then hits another FK issue and repeats the process.
What I really want is a list of ALL FKs in one go that would cause the commit to fail.
I can of course write something to check every FK relationship through a select statement (one select per FK), but the beauty of using the deferred session is that this is all handled for me.
First option, you can look into DML error logging. That way you leave your constraints active, then do the inserts, with the erroring rows going into error tables. You can find them there, fix them, re-insert the rows and delete them from the error table.
The second option is, rather than trying the commit, attempt to re-enable each deferred constraint dynamically, catching the errors. You’d be looking at a PL/SQL procedure that queries ALL_CONSTRAINTS for the deferrable ones, then does an EXECUTE IMMEDIATE to make that constraint immediate. That last bit would be in a block with an exception handler so you can catch the ones that fail, but continue on.
Personally, I’d go for option (1) as you do get the individual records that fail, plus you don’t have to worry about deferrable constraints and remembering to make them immediate again after this script so it doesn’t break something later.
I guess somewhere in memory Oracle must be maintaining a list, at least of constraints that failed if not the actual rows. I’m pretty confident it doesn’t rescan the whole set of tables at commit time to check all the constraints.