Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1okyfR-0004wi-2W for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 02:18:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1okyfP-0003zR-R7 for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 02:18:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1okyfP-0003zI-Az for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 02:18:47 +0000 Received: from mail-qv1-xf2a.google.com ([2607:f8b0:4864:20::f2a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1okyfI-0000oI-JX for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 02:18:46 +0000 Received: by mail-qv1-xf2a.google.com with SMTP id f14so10546827qvo.3 for ; Tue, 18 Oct 2022 19:18:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:user-agent:references:in-reply-to:date:cc:to:from :subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=GUKfs1YSX+l1tRnU+JV5axfzljXCuDeTnjU2zkmD9EE=; b=iIMg7tQLPvhXsmeimEW46R0epHbYNqftTFIr1zNwSGRifW2XT0QkX+iUCLY6JDx2Eg Q/AfbzvgYuOV0Cg7Vy4Y+7RcGLbTF0gFluxqeMHBW9vNXY98SSp8ioXbIlbGvBeY2abO AtpEkn7sD2QK2In6cLNxZwvyw8nfV7ROb6gP+hHL9KfKaaRDH0ubxzdZ+HkBr5bG71+L osp8KfDy6hOzabg0kqbFVgNwI8eNPQWxZQTAIGxjlX5hgU/03mMc4+LuYXtC4dRBViEC vzeYNsoAvpDOq5Jy9BkttHQAomfea/RLJoxU9I97yaNfLiO+cUqIaTjfM6gZUnr0gips lglQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:references:in-reply-to:date:cc:to:from :subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GUKfs1YSX+l1tRnU+JV5axfzljXCuDeTnjU2zkmD9EE=; b=Yc6aMslqGJeyyxulk4LMC0wh/JFp1yxwbpB4xLI3CP6uja+04ynK2xQqcDEV1qsqvt 2sW155xGQwjSfMuiVWiZfWm/0ktfvTCFwrrrBSNDqz54PklWVkhFb0A9djkgHEqnQrcM +DnnlQISR+hiC2itRu+rWpMIb/bAH6msYoI5IH04LGYpz3WqZRP7Mb0Zj0Ocvs34KY9u iCGbhVcCf+V8dt/AAEyBqU8VdgT0H+7cHHnNjxjIsuEMX2pIHPg8Gb8hA2/hME0EByoH 8od4a2icIdUM7fVUwl/q4SP44zm5ROlNWUWpQ5BLwzVy4uoi+qRIbcPGSzOqNip1y6ib /NWQ== X-Gm-Message-State: ACrzQf1CrY2s1cTasjtaXDlWRgbVDUOLPmkSBSfHc2c+/2woCzo9jcxU rYvol0Zj13+eAX1CsscCjEE= X-Google-Smtp-Source: AMsMyM6mSeonLUKU0tjpX7IKyQEWR4vdabXFsoaPZlE8j948p7X9sHMX46GRAx0USGLvR1cGVF2w9A== X-Received: by 2002:a05:6214:2a85:b0:4b4:3f9:2639 with SMTP id jr5-20020a0562142a8500b004b403f92639mr4917272qvb.93.1666145919652; Tue, 18 Oct 2022 19:18:39 -0700 (PDT) Received: from [192.168.2.100] (pool-72-79-30-4.nwrknj.east.verizon.net. [72.79.30.4]) by smtp.gmail.com with ESMTPSA id hh6-20020a05622a618600b00343057845f7sm2987964qtb.20.2022.10.18.19.18.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 19:18:39 -0700 (PDT) Message-ID: <2b93e12697bb273c493b47e04c762b39a66910ab.camel@gmail.com> Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP From: gogala.mladen@gmail.com To: Christophe Pettus , Ravi Krishna Cc: "pgsql-general@lists.postgresql.org" Date: Tue, 18 Oct 2022 22:18:38 -0400 In-Reply-To: <80B77102-84D3-4AD8-B138-1C0C3341855B@thebuild.com> References: <353091661.109766.1666124045054.ref@mail.yahoo.com> <353091661.109766.1666124045054@mail.yahoo.com> <72BEC008-DC6C-4D80-80A6-E1634CFFA656@thebuild.com> <80B77102-84D3-4AD8-B138-1C0C3341855B@thebuild.com> Content-Type: multipart/alternative; boundary="=-mMQp231x+Hgzwuogw7pn" User-Agent: Evolution 3.44.4 (3.44.4-2.fc36) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-mMQp231x+Hgzwuogw7pn Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, 2022-10-18 at 14:31 -0700, Christophe Pettus wrote: >=20 > Rather than have a loop inside the BEGIN / END, you could put the > BEGIN EXCEPTION END inside the loop, catch the error, store the > important parts of the exception in a variable, and then do the > COMMIT after the END statement but before the next iteration of the > loop.=C2=A0 A bit messier, but it gets the job done. Commit within a loop is an extremely bad idea. Commit is an expensive operation which includes incrementing the XID, which is global and not local to the process. There is also a WAL write which has to be waited on. Every commit implies at least one write operation. If that was not the case, Postgres wouldn't be ACID compliant. There would be problem with the "D". Commit within a loop will have many adverse effects on the performance. Here is what Tom Kyte, who used to be a celebrity in the world of Oracle, said on this topic: https://asktom.oracle.com/pls/apex/f?p=3D100:11:0::::p11_question_id:495196= 6319022 Surprisingly enough, the argument in the article is mostly portable, doesn't depend on the database type at all.=C2=A0Basically, transaction is = a logical unit of work. If 1000 rows need to be updated, it's better to update them in a single transaction than in 1000 transactions. That is particularly true for Postgres which doesn't have problems with the undo tablespace and ORA-1555 "snapshot too old" error because of the different architecture. Also, Oracle has a secret optimization: it doesn't wait for commit, if the commit is issued within PL/SQL loop. Your idea solves the syntactic problem with commits within PLPG/SQL =C2=A0loops but it doesn't solve other problems that such programming causes. Commit within loop is a very bad idea. The best way to resolve the problems with commit within the loop is to remove the programmer trying to do that from the project. In my humble opinion, programmers who do stuff like that should suffer unusual and cruel punishment. PS: ---- I am sure, that if Tom Kyte would dare to make a suggestion on this list, there would be someone who would try to explain "the Postgres way" to him in a condescending manner. I've seen that before. --=-mMQp231x+Hgzwuogw7pn Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
On Tue, 2022-10-18 at 14:31 -0700, Christophe= Pettus wrote:

Rather t= han have a loop inside the BEGIN / END, you could put the BEGIN EXCEPTION E= ND inside the loop, catch the error, store the important parts of the excep= tion in a variable, and then do the COMMIT after the END statement but befo= re the next iteration of the loop.  A bit messier, but it gets the job= done.

Commit within a loop is an= extremely bad idea. Commit is an expensive operation which includes increm= enting the XID, which is global and not local to the process. There is also= a WAL write which has to be waited on. Every commit implies at least one w= rite operation. If that was not the case, Postgres wouldn't be ACID complia= nt. There would be problem with the "D". Commit within a loop will have man= y adverse effects on the performance. Here is what Tom Kyte, who used to be= a celebrity in the world of Oracle, said on this topic:


Surprisingly enough, the argument in the artic= le is mostly portable, doesn't depend on the database type at all. Basically, transaction is a logical unit of work. If 1000 rows need to = be updated, it's better to update them in a single transaction than in 1000= transactions. That is particularly true for Postgres which doesn't have pr= oblems with the undo tablespace and ORA-1555 "snapshot too old" error becau= se of the different architecture. Also, Oracle has a secret optimization: i= t doesn't wait for commit, if the commit is issued within PL/SQL loop. Your= idea solves the syntactic problem with commits within PLPG/SQL  loops= but it doesn't solve other problems that such programming causes. Commit w= ithin loop is a very bad idea. The best way to resolve the problems with co= mmit within the loop is to remove the programmer trying to do that from the= project. In my humble opinion, programmers who do stuff like that should s= uffer unusual and cruel punishment.

PS:
= ----
I am sure, that if Tom Kyte would dare to make a suggestion = on this list, there would be someone who would try to explain "the Postgres= way" to him in a condescending manner. I've seen that before.
--=-mMQp231x+Hgzwuogw7pn--