I am using pgtap to test some deferred constraint triggers by:
- Opening a transaction
- Inserting some test data
- Performing some statements
- Simulating a commit by using
SET CONSTRAINTS ALL IMMEDIATE - Testing post-commit conditions
This works fine, but it limits me to a single test per transaction. If I try and set up more data later, my previously deferred constraints now fire immediately.
Is it possible to revert the affect of SET CONSTRAINTS ALL IMMEDIATE, without reverting the effects of the constraints that it caused to activate? If not, my only option is to move each test to a separate file, which is a little bit cumbersome.
Have you tried
after you call
SET CONSTRAINTS ALL IMMEDIATE?If that would defer more constraints than you want to, you’ll have to name them individually:
It does not revert any effect. It only defers further checks. If a constraint is violated, an
EXCEPTIONis raised. There is nothing that could be reverted here.You could catch the exception in a plpgsql function if that is what you want.
If you ant to revert constraints to their initial state, you’ll have to
SETthem individually and with the explicit state. Sadly – to my knowledge – there is no “RESET CONSTRAINTS” in PostgreSQL 9.1 that would bring them all to their initial state.Manual page.