pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Rob Nikander <rob.nikander@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: delete inside for plpgsql loop on same relation?
Date: Tue, 18 Jun 2019 14:20:11 -0400
Message-ID: <17007.1560882011@sss.pgh.pa.us> (raw)
In-Reply-To: <C12E9EA5-5B1A-44C7-9A4D-F01F12261A07@gmail.com>
References: <C12E9EA5-5B1A-44C7-9A4D-F01F12261A07@gmail.com>

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





view thread (2+ messages)

Message-ID: <17007.1560882011@sss.pgh.pa.us>
Permalink:  ../17007.1560882011@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/17007.1560882011@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rob.nikander@gmail.com
  Subject: Re: delete inside for plpgsql loop on same relation?
  In-Reply-To: <17007.1560882011@sss.pgh.pa.us>
  References: <C12E9EA5-5B1A-44C7-9A4D-F01F12261A07@gmail.com> <17007.1560882011@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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