From: | Brian Dunavant <brian(at)omniti(dot)com> |
---|---|
To: | Alexander Farber <alexander(dot)farber(at)gmail(dot)com> |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: How to handle simultaneous FOR-IN UPDATE-RETURNING loops? |
Date: | 2017-07-10 19:38:05 |
Message-ID: | CAJTy2e=noq3igG78BC7yvRVLGLiW_PjdFtMGrz79-vxTn2DzDw@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
"FOR UPDATE" is part of "SELECT" not part of "UPDATE".
You can select the rows "for update" which will lock those rows. You
can then loop over the the results of the 'select' to do the rest of
your logic.
Be careful doing this if other things are also updating these rows.
With SKIP LOCKED you can skip over rows that should have been selected
but were not because another process was updating data that was
unrelated. Without SKIP LOCKED you risk deadlock if you are
selecting multiple rows.
On Mon, Jul 10, 2017 at 3:22 PM, Alexander Farber
<alexander(dot)farber(at)gmail(dot)com> wrote:
> I have tried:
>
> FOR _gid, _loser, _winner IN
> UPDATE words_games
> SET finished = CURRENT_TIMESTAMP
> WHERE finished IS NULL
> AND played1 IS NOT NULL
> AND played2 IS NOT NULL
> AND (played1 < CURRENT_TIMESTAMP - INTERVAL '24 hours'
> OR played2 < CURRENT_TIMESTAMP - INTERVAL '24 hours')
> RETURNING
> gid,
> CASE WHEN played1 < played2 THEN player1 ELSE player2 END,
> CASE WHEN played1 < played2 THEN player2 ELSE player1 END
> FOR UPDATE SKIP LOCKED
> LOOP
> ...
> END LOOP;
>
> but this fails with:
>
> ERROR: syntax error at or near "FOR"
>
> I have also described my problem at SO:
>
>
> https://stackoverflow.com/questions/45015368/how-to-handle-simultaneous-for-in-update-returning-loops
>
> Thank you
> Alex
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2017-07-10 20:02:18 | Re: How to handle simultaneous FOR-IN UPDATE-RETURNING loops? |
Previous Message | rihad | 2017-07-10 19:24:20 | Re: Changing collate & ctype for an existing database |