Re: Clarification of triggers with 'OF column_name' in 'Overview of Trigger Behavior'

From: Josh Silver <josh(dot)ag(at)paladin(dot)insure>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Pg Docs <pgsql-docs(at)lists(dot)postgresql(dot)org>
Subject: Re: Clarification of triggers with 'OF column_name' in 'Overview of Trigger Behavior'
Date: 2022-05-02 23:42:20
Message-ID: CADRdQa27rS+GpdYu_sV9Bjdu9vVtCthdFSv7ABZt0M9xHiPSMA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

On Mon, May 2, 2022 at 3:22 PM David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
wrote:

> On Mon, May 2, 2022 at 11:14 AM PG Doc comments form <
> noreply(at)postgresql(dot)org> wrote:
>
>> The following documentation comment has been logged on the website:
>>
>> Page: https://www.postgresql.org/docs/13/trigger-datachanges.html
>> Description:
>>
>> UPDATE test SET alpha = 5;
>>
>> only trigger b2_ab will fire, even though it will update column beta.
>
>
> Column beta eventually changed but you never issued an UPDATE command with
> beta in the SET clause. The trigger only care about the later.
>
> And in fact triggers on a table should never re-issue an actual command
> against the same table or you end up with infinite triggers.
>

I now understand that column specific triggers only consider the set
clause, but that is not documented on "Overview of Trigger Behavior" and is
only documented in the notes of the "CREATE TRIGGER" page. It seems like
useful information that the WHEN clause of a TRIGGER evaluates the return
value of the previously executed trigger function but the column specifier
only considers the SET clause.

>
> If i
>> change the declaration of b3_bc by removing the column list or including
>> column alpha,
>
> things work as I expected and b2_ab cascades to b3_bc.
>>
>
> But that isn't how this works. There is no cascading. As soon as the
> UPDATE query is planned the set of triggers it is going to trigger is
> basically known and nothing those triggers do individually will change that
> (aside from raising an error). All you did by changing b3_bc is get it
> included in the ordered list of triggers that will be executed each time,
> and only when, an UPDATE command is executed against the named table.
>

Cascades was a bad choice of words on my part. Unlike the WHEN clause which
is checked right before the function executes and which evaluates against
the return value of the previous trigger function, the column specific
trigger is only checked against the original NEW row. I think that the
behavior of column-specific triggers, especially their difference in
when/what they check against from triggers with a WHEN CLAUSE would be
useful to document on this page.

>
> If you still believe a meaningful change can be had here a concrete
> suggestion would be helpful. Otherwise maybe someone takes an interest at
> some point - but this particular confusion seems extremely rare.
>

I'm proposing that the "Overview of Trigger Behavior" page include
information about column specific triggers as well, because they have
different behavior from how the return value from one BEFORE trigger is the
input to the next BEFORE trigger and from how the WHEN clause gets checked
right before statement execution. Both those "see" the effects of
previously executed BEFORE triggers but column specific triggers don't.

Josh

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message David G. Johnston 2022-05-02 23:53:08 Re: Clarification of triggers with 'OF column_name' in 'Overview of Trigger Behavior'
Previous Message David G. Johnston 2022-05-02 22:21:41 Re: Clarification of triggers with 'OF column_name' in 'Overview of Trigger Behavior'