Re: Using a TRIGGER with window functions.

From: Michael Lewis <mlewis(at)entrata(dot)com>
To: Pól Ua Laoínecháin <linehanp(at)tcd(dot)ie>
Cc: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Using a TRIGGER with window functions.
Date: 2021-08-16 19:57:05
Message-ID: CAHOFxGp8vGf8B9+hb37gakfHb-8TjTpfa6wCFr3vL6zrQEfQ+g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

If you want to update many rows after updating N rows, you want an after
STATEMENT trigger which is executed after insert, update or delete. You
also want to ensure that only that function maintains sort_order field and
that you don't update recursively, perhaps by executing that function when
NEW.sort_order IS NOT DISTINCT FROM old.sort_order to prevent an infinite
loop, and executing a different function when NEW.sort_order IS DISTINCT
FROM OLD.sort_order such that you ensure all other fields have not changed.

By the way, your window function could be row_number() - 1 instead of
activity_id - FIRST_VALUE(activity_id).

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2021-08-16 20:16:12 Re: Using a TRIGGER with window functions.
Previous Message Andy Hall 2021-08-16 16:31:44 Re: postgres disconnects on master after setting up replication