pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Ciprian Craciun <ciprian.craciun@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Unexpected behavior when combining `generated always` columns and update rules
Date: Thu, 13 Apr 2023 10:37:40 -0400
Message-ID: <1332832.1681396660@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwakXc0RRNU49n1FYmXXDBukvTeDRbXZZ+Fuyy2y-Z5N=g@mail.gmail.com>
References: <CA+Tk8fz+Vuov584ff_PxxptB=dG3tX_buFr+4RTvFCN5ERFN4Q@mail.gmail.com>
	<CAKFQuwakXc0RRNU49n1FYmXXDBukvTeDRbXZZ+Fuyy2y-Z5N=g@mail.gmail.com>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Thursday, April 13, 2023, Ciprian Craciun <ciprian.craciun@gmail.com>
> wrote:
>> create or replace rule propagate_x_and_d as on update to x do also
>> update y set x = new.x, d = new.d where y.x = old.x;
>> 
>> As seen above, although the rule correctly propagates the change to
>> the `x` column, it fails to use the new value for the `d` column, but
>> instead uses the previous one.

> ALSO rules behave like before triggers, not after triggers. The original
> command is appended to the end of the list of commands, not the start.

It depends actually ... per [1],

    For ON INSERT rules, the original query (if not suppressed by INSTEAD)
    is done before any actions added by rules. This allows the actions to
    see the inserted row(s). But for ON UPDATE and ON DELETE rules, the
    original query is done after the actions added by rules. This ensures
    that the actions can see the to-be-updated or to-be-deleted rows;
    otherwise, the actions might do nothing because they find no rows
    matching their qualifications.

			regards, tom lane

[1] https://www.postgresql.org/docs/current/rules-update.html





view thread (7+ messages)  latest in thread

Message-ID: <1332832.1681396660@sss.pgh.pa.us>
Permalink:  ../1332832.1681396660@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1332832.1681396660@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, ciprian.craciun@gmail.com
  Subject: Re: Unexpected behavior when combining `generated always` columns and update rules
  In-Reply-To: <1332832.1681396660@sss.pgh.pa.us>
  References: <CA+Tk8fz+Vuov584ff_PxxptB=dG3tX_buFr+4RTvFCN5ERFN4Q@mail.gmail.com> <CAKFQuwakXc0RRNU49n1FYmXXDBukvTeDRbXZZ+Fuyy2y-Z5N=g@mail.gmail.com> <1332832.1681396660@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox