From: gogala.mladen@gmail.com
To: Christophe Pettus <xof@thebuild.com>
To: Ravi Krishna <s_ravikrishna@aol.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP
Date: Tue, 18 Oct 2022 22:18:38 -0400
Message-ID: <2b93e12697bb273c493b47e04c762b39a66910ab.camel@gmail.com> (raw)
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>
<VOMN1IM86IU4.HNL8WUVWUAMM3@ravi-surface>
<80B77102-84D3-4AD8-B138-1C0C3341855B@thebuild.com>
On Tue, 2022-10-18 at 14:31 -0700, Christophe Pettus wrote:
>
> 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. 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=100:11:0::::p11_question_id:4951966319022
Surprisingly enough, the argument in the article 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 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
loops 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.
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: gogala.mladen@gmail.com, xof@thebuild.com, s_ravikrishna@aol.com
Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP
In-Reply-To: <2b93e12697bb273c493b47e04c762b39a66910ab.camel@gmail.com>
References: <353091661.109766.1666124045054.ref@mail.yahoo.com> <353091661.109766.1666124045054@mail.yahoo.com> <72BEC008-DC6C-4D80-80A6-E1634CFFA656@thebuild.com> <VOMN1IM86IU4.HNL8WUVWUAMM3@ravi-surface> <80B77102-84D3-4AD8-B138-1C0C3341855B@thebuild.com> <2b93e12697bb273c493b47e04c762b39a66910ab.camel@gmail.com>
* 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