title: Execution order of cascading triggers in postgresql
body: <p>I'm wondering about the execution/firing order of triggers for FKs with UPDATE CASCADE, concerning grandchild tables, in PostgreSQL (9.3)</p>
  
  <p>Here's what I have:</p>
  
  <pre><code>Parent:      Invoices           (invoice_id)
  Child:       Invoice_Lines      (invoice_id, line_nbr)
  Grandchild:  Invoice_Line_Taxes (invoice_id, line_nbr, tax)
  
  Invoice_Lines has FOREIGN KEY (invoice_id) REFERENCES Invoices ON UPDATE CASCADE;
  Invoice_Line_Taxes has FOREIGN KEY (invoice_id, line_nbr) REFERENCES Invoice_Lines ON UPDATE CASCADE;
  </code></pre>
  
  <p>I also have a custom UPDATE trigger on Invoices (who's name start with S, which comes after RI_).  This trigger sums up the invoice amounts.  And my actual update statement changes invoice_id, which gets propagated down to the child and grandchild.</p>
  
  <p>Trouble is, in/during my custom trigger, the invoice_id for Invoice_Lines has already changed, but not for the grandchild Invoice_Line_Taxes.</p>
  
  <p>I've dumped the rows from within the custom trigger using RAISE:</p>
  
  <pre><code>invoice_lines: (5,1)
  invoice_line_taxes: (-1,1,HST)
  </code></pre>
  
  <p>After the trigger:</p>
  
  <pre><code>SELECT * from invoice_line_taxes where invoice_id IN (5,-1);
   invoice_id | line_nbr | tax_nm
  ------------+----------+--------
            5 |        1 | HST
  </code></pre>
  
  <p>So I'm wondering, what's the trigger execution order with respect to cascading triggers?</p>
  
  <p>I would have assumed something like this:</p>
  
  <pre><code>RI_on_invoices
  RI_on_invoice_lines
  S_custom_trigger
  </code></pre>
  
  <p>Any ideas?  Anybody know where I can get official documentation on the execution ordering?  I've tried looking for detailed documentation about this, but all I've found is that ordering is alphabetical.  Perhaps if I find the specific details about the ordering I could build something around it.  But right now, I'd be basing it on guess work.</p>
  
  <p>Thank you.</p>