Re: delete inside for plpgsql loop on same relation?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Rob Nikander <rob(dot)nikander(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: delete inside for plpgsql loop on same relation?
Date: 2019-06-18 18:20:11
Message-ID: 17007.1560882011@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Rob Nikander <rob(dot)nikander(at)gmail(dot)com> writes:
> Are there guarantees about how this plpgsql behaves? It’s deleting from a table while it loops over it. So far it seems like the delete is logically after the select, as I hoped, and doesn’t interfere.

> for row in select * from some_stuff loop
> delete from some_stuff where …
> ...
> end loop;

Sure. A query will not see the effects of queries that start after it.
This isn't particularly plpgsql-specific.

Some qualifications are needed if you have triggers or volatile functions
in the first query (those *can* see later effects); but this usage seems
safe enough.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ken Tanzer 2019-06-18 22:23:40 Re: psql UPDATE field [tab] expands to DEFAULT?
Previous Message Andres Freund 2019-06-18 18:10:11 Re: perf tuning for 28 cores and 252GB RAM