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 1ol8lx-0001Xm-36 for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 13:06:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ol8lv-0005P4-Vb for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 13:06:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ol8lv-0005Ov-Iv for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 13:06:11 +0000 Received: from mail-qt1-x833.google.com ([2607:f8b0:4864:20::833]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ol8lr-0006Yd-Aa for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 13:06:11 +0000 Received: by mail-qt1-x833.google.com with SMTP id w3so11563936qtv.9 for ; Wed, 19 Oct 2022 06:06:06 -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:reply-to :from:subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=zBNu+J9vGNHKNfSBPXOSgpfBUbOKLnoWEGvW0SEFHyA=; b=O3+a0URGy/dUDzoA0kQKZrbcJLu2ogjrJxMe6/JM2d2I+atqhmvfzQemb3deHDg7f4 de07m70a7hHObs901ldEJf17VfjA9+3LjdegoaKBN1YSiX99PoQMQcKNbk9xG/OkpV4A gSQF71iZ0qrpKiW9CLeQ3yZ0tOwZz4aQ3Pm/shgB+HKVeEO2XwYej3gRDOzTx4GxNS6D CKi6VQXTRxremsFmNaxqGAgtryeL4yBWb7F6QCOFoFv2q77TedZVFlun2GKeCUZa+ADA Bo584pA1yRFQ4hQBBOKkFFN/E0Ucjo/Xqtqhg+oU3HPWz9BpttwhKAy270G0K3ZTWRyl u+1g== 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:reply-to :from:subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=zBNu+J9vGNHKNfSBPXOSgpfBUbOKLnoWEGvW0SEFHyA=; b=W7glSEmgvFqPUTEzloPc5SuojPJ1rmZKLWBzt1EYcgHACpyxFaLui9N9aOqrCTJafM AJR4Yd2ZaMewxwA3l0tX6Dz7fONMs4GUlNKKerQVInrJjuUOfB00HaFba8FSrbIyw+Gs 5TrM5k2C5A+6INql2hMFGJ++933dcuxm8jMqs9qn9BSjQ9l3mXjwHINAAYx9abZP756V ImzeXShZJeHStrMENE0s5gtlM3WInwMy6eDVYWvrIMCguZN/D9XLNiOEfv6rLTThR+Vx icAfqyHW6DloNg/6j/tQTQNHn3svAxgdZx35hDs+rDCWmrEep1yonK2XmaRhMe8cWh8i Js+Q== X-Gm-Message-State: ACrzQf2IsKncVmtCirJPbwHI/dCW4no/brjpskv6GoXRmeKUJqO3O7T7 UHTlcSSCskWN5nvadtz7g20= X-Google-Smtp-Source: AMsMyM6Za/L6/YwJJJYIMuWhQwSnjF+mHcJX7WhWDsv+uehHoAJqvkceZ2egYCp6n5HPxD1PYNH/zw== X-Received: by 2002:a05:622a:40d:b0:397:bd61:ef1d with SMTP id n13-20020a05622a040d00b00397bd61ef1dmr6354983qtx.404.1666184765163; Wed, 19 Oct 2022 06:06:05 -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 he7-20020a05622a600700b003972790deb9sm3950847qtb.84.2022.10.19.06.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 06:06:04 -0700 (PDT) Message-ID: <1c4cb79ca48fa14246fbe78374da4ef9ac435a0a.camel@gmail.com> Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP From: Mladen Gogala Reply-To: gogala.mladen@gmail.com To: Christophe Pettus Cc: Ravi Krishna , "pgsql-general@lists.postgresql.org" Date: Wed, 19 Oct 2022 09:06:03 -0400 In-Reply-To: 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> <2b93e12697bb273c493b47e04c762b39a66910ab.camel@gmail.com> Content-Type: multipart/alternative; boundary="=-aoIqeU8Jw0YKr8CIhSNA" 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 --=-aoIqeU8Jw0YKr8CIhSNA Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Comments in-line. On Tue, 2022-10-18 at 21:02 -0700, Christophe Pettus wrote: >=20 >=20 > > On Oct 18, 2022, at 19:18, gogala.mladen@gmail.com=C2=A0wrote: > >=20 > > Commit within a loop is an extremely bad idea. >=20 > This is an over-generalization.=C2=A0 There are many use-cases for this > (if there were not, procedures wouldn't have been nearly as important > a feature). >=20 > 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.=C2=A0 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. That depends. Multiple commits will slow down the processing. If the goal of the exercise is to let update complete as quickly as possible, then it will be a single commit. If the goal is to enable normal processing and let the enormous update complete in its due time, then you'll do what you describe. >=20 > 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. >=20 > High commit rates happen all the time, and they don't break > PostgreSQL.=C2=A0 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.=C2=A0 PostgreSQ= L > handles it just fine. Point of my post is that the business logic, in your case it's IoT sensors, determines what is transaction and when to commit. Advice like "commit often and commit early", to paraphrase the famous Chicago mayor, is easy to find but I would take it with grain of salt. Regards --=20 Mladen Gogala Database Consultant https://dbwhisperer.wordpress.com --=-aoIqeU8Jw0YKr8CIhSNA Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
Comments in-line.

On= Tue, 2022-10-18 at 21:02 -0700, Christophe Pettus wrote:


On Oct 18, 2022, at 19:18, gog= ala.mladen@gmail.com wrote:

Commit wi= thin a loop is an extremely bad idea.

=
This is an over-generalization.  There are many use-cases for thi= s (if there were not, procedures wouldn't have been nearly as important a f= eature).

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 who= le thing to finish due to row-level locks, and to give vacuum a chance to d= eal with the dead tuples.  Similarly, while inserting one row at a tim= e and committing is usually not a great idea, it can make sense to do large= inserts in batches.

That depends= . Multiple commits will slow down the processing. If the goal of the exerci= se is to let update complete as quickly as possible, then it will be a sing= le commit. If the goal is to enable normal processing and let the enormous = update complete in its due time, then you'll do what you describe.



A= pplications do this kind of thing all the time, very successfully; it was j= ust that the loop was in the application rather than in the procedure.
<= /div>

High commit rates happen all the time, and they do= n't break PostgreSQL.  For example, an IoT application collecting sens= or data and doing many inserts per second is also doing many commits per se= cond, since each bare INSERT is in its own transaction.  PostgreSQL ha= ndles it just fine.

Point of my post = is that the business logic, in your case it's IoT sensors, determines what = is transaction and when to commit. Advice like "commit often and commit ear= ly", to paraphrase the famous Chicago mayor, is easy to find but I would ta= ke it with grain of salt.
Regards
-- 
Mladen Gogala
Database Consultant
--=-aoIqeU8Jw0YKr8CIhSNA--