Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP

From: Christophe Pettus <xof(at)thebuild(dot)com>
To: gogala(dot)mladen(at)gmail(dot)com
Cc: Ravi Krishna <s_ravikrishna(at)aol(dot)com>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP
Date: 2022-10-19 04:02:21
Message-ID: A13DA688-E00A-4679-8368-21265C76D823@thebuild.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On Oct 18, 2022, at 19:18, gogala(dot)mladen(at)gmail(dot)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 nearly as 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 commit regularly so that other processes don't have to wait for the whole thing to finish due to row-level locks, and to give vacuum a chance to deal with the dead tuples. Similarly, while inserting one row at a time and committing is usually not 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 rather than in the procedure.

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message vignesh C 2022-10-19 05:48:05 Re: Support logical replication of DDLs
Previous Message Julien Rouhaud 2022-10-19 03:16:32 Re: Attaching database