Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdIiP-0006wD-Is for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2019 18:20:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hdIiO-0000Xf-Aw for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2019 18:20:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdIiO-0000XX-1E for pgsql-general@lists.postgresql.org; Tue, 18 Jun 2019 18:20:16 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdIiM-0005Dq-1j for pgsql-general@postgresql.org; Tue, 18 Jun 2019 18:20:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x5IIKBEc017008; Tue, 18 Jun 2019 14:20:11 -0400 From: Tom Lane To: Rob Nikander cc: Postgres General Subject: Re: delete inside for plpgsql loop on same relation? In-reply-to: References: Comments: In-reply-to Rob Nikander message dated "Tue, 18 Jun 2019 21:02:24 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <17006.1560882011.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 18 Jun 2019 14:20:11 -0400 Message-ID: <17007.1560882011@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Rob Nikander writes: > Are there guarantees about how this plpgsql behaves? It=E2=80=99s deleti= ng 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=E2=80=99t interfere. = > for row in select * from some_stuff loop > delete from some_stuff where =E2=80=A6 > ... > 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