title: postgres - trigger before transaction commit
body: <p>I was wondering if it is indirectly possible to have a trigger executed just before the transaction is about to commit? In this trigger, I will do consistency checks and rollback the transaction if required.</p>
  
  <p>For example, I have three tables:</p>
  
  <pre><code>users (id, name)
  groups (id, name)
  user_in_group (user_id, group_id)
  </code></pre>
  
  <p>I would like to create a trigger which verifies that a user is always part of a group. No orphan users are allowed. Each time an insert into users occurs, this trigger will verify that a correspondering insert into user_in_group also occured. If not, the transaction will not commit.</p>
  
  <p>This cannot be done using a simple row- or statement- based trigger, since the above scenario requires two separate statements.</p>
  
  <p>The other way around, when a delete from user_in_group happens, can be easily done by a row-based trigger.</p>