Re: Before update trigger causing another after trigger to fire, returning NULL, causing before trigger to not update - does this make sense?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Susan Cassidy <scassidy(at)stbernard(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Before update trigger causing another after trigger to fire, returning NULL, causing before trigger to not update - does this make sense?
Date: 2010-04-06 21:53:00
Message-ID: 28726.1270590780@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Susan Cassidy <scassidy(at)stbernard(dot)com> writes:
> Sequence of events when problem occurred:
> update table a
> causes update table b
> which updates table a again (different column)
> trigger for table b returns null
> update of table a does not happen

Different column of same row, you mean? Yes, I think that would cause
the outer update to not happen after all. By the time control returns
from the trigger, the intended target tuple of the outer update is dead
(having been already obsoleted by the inner update). The interpretation
of such a situation is to do nothing.

In general, having BEFORE triggers cause updates of other rows is bad
design. The rule of thumb is that BEFORE triggers should validate
and/or adjust the row you are about to store, while AFTER triggers are
preferred for propagating information from such an event to other rows
(whether in the same table or different ones). The usual argument for
that is that a BEFORE trigger can't be completely sure what the finally
stored state is going to be; but this sort of loop is another reason not
to do it.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2010-04-06 23:39:55 Re: can't connect to server on localhost
Previous Message Susan Cassidy 2010-04-06 20:58:11 Before update trigger causing another after trigger to fire, returning NULL, causing before trigger to not update - does this make sense?