pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feeddelete inside for plpgsql loop on same relation?
2+ messages / 2 participants
[nested] [flat]
* delete inside for plpgsql loop on same relation?
@ 2019-06-18 18:02 Rob Nikander <rob.nikander@gmail.com>
0 siblings, 1 reply; 2+ messages in thread
From: Rob Nikander @ 2019-06-18 18:02 UTC (permalink / raw)
To: pgsql-general
Hi,
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;
I’m using a temporary table of “things to process” and looping over it, deleting the ones as I go.
I don’t see anything mentioned here: https://www.postgresql.org/docs/11/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING
Rob
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: delete inside for plpgsql loop on same relation?
@ 2019-06-18 18:20 Tom Lane <tgl@sss.pgh.pa.us>
parent: Rob Nikander <rob.nikander@gmail.com>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2019-06-18 18:20 UTC (permalink / raw)
To: Rob Nikander <rob.nikander@gmail.com>; +Cc: pgsql-general
Rob Nikander <rob.nikander@gmail.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
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2019-06-18 18:20 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-06-18 18:02 delete inside for plpgsql loop on same relation? Rob Nikander <rob.nikander@gmail.com>
2019-06-18 18:20 ` Tom Lane <tgl@sss.pgh.pa.us>
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