Re: Using a TRIGGER with window functions.

2021-08-17 Thread Pól Ua Laoínecháin
Hi all, > If you want to update many rows after updating N rows, Thanks to those who helped me with this - I think that Michael Lewis has given me some good ideas on how I should go about this. Also, thanks to those who took the time to explain why some of my ideas were not well grounded (wouldn

Re: Using a TRIGGER with window functions.

2021-08-16 Thread Tom Lane
I wrote: > "David G. Johnston" writes: >> On Sun, Aug 15, 2021 at 1:24 AM Pól Ua Laoínecháin wrote: >>> Why are window functions now allowed in UPDATEs > I suspect the error check was just copied from the aggregate-function > case. It's clear why we can't put aggregates in UPDATE: there'd no >

Re: Using a TRIGGER with window functions.

2021-08-16 Thread Tom Lane
"David G. Johnston" writes: > On Sun, Aug 15, 2021 at 1:24 AM Pól Ua Laoínecháin wrote: >> Why are window functions now allowed in UPDATEs > You can get it to work via a subquery/FROM clause computation. That it > doesn't work directly in the SET clause I don't know off-hand, but most > likely

Re: Using a TRIGGER with window functions.

2021-08-16 Thread David G. Johnston
On Sun, Aug 15, 2021 at 1:24 AM Pól Ua Laoínecháin wrote: > > ERROR: window functions are not allowed in UPDATE LINE 2: SET > sort_order = activity_id - FIRST_VALUE(activity_id) > > Why are window functions now allowed in UPDATEs > You can get it to work via a subquery/FROM clause computation.

Re: Using a TRIGGER with window functions.

2021-08-16 Thread Michael Lewis
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.

Using a TRIGGER with window functions.

2021-08-15 Thread Pól Ua Laoínecháin
Hi all, I have a rather contrived example, but I just can't get this to work. A fiddle with all my code is available here: https://dbfiddle.uk/?rdbms=postgres_12&fiddle=9a89ea79ffc617a11c36d63123d2f987 CREATE TABLE t1 ( user_id SMALLINT NOT NULL, activity_id SMALLINT NOT NULL, sort_or