Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP - Mailing list pgsql-general

From Christophe Pettus
Subject Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP
Date
Msg-id A13DA688-E00A-4679-8368-21265C76D823@thebuild.com
Whole thread Raw
In response to Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP  (gogala.mladen@gmail.com)
Responses Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP
List pgsql-general

> On Oct 18, 2022, at 19:18, gogala.mladen@gmail.com wrote:
>
> Commit within a loop is an extremely bad idea.

This is an over-generalization.  There are many use-cases for this (if there were not, procedures wouldn't have been
nearlyas important a feature). 

For example, if you are processing a large update (in the hundreds of thousands or more of rows), you often want to
commitregularly so that other processes don't have to wait for the whole thing to finish due to row-level locks, and to
givevacuum a chance to deal with the dead tuples.  Similarly, while inserting one row at a time and committing is
usuallynot a great idea, it can make sense to do large inserts in batches. 

Applications do this kind of thing all the time, very successfully; it was just that the loop was in the application
ratherthan in the procedure. 

High commit rates happen all the time, and they don't break PostgreSQL.  For example, an IoT application collecting
sensordata and doing many inserts per second is also doing many commits per second, since each bare INSERT is in its
owntransaction.  PostgreSQL handles it just fine. 


pgsql-general by date:

Previous
From: Julien Rouhaud
Date:
Subject: Re: Attaching database
Next
From: Yavuz TANRIVERDİ
Date:
Subject: Is this error expected ?