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 1nSglc-0007zn-8w for pgsql-general@arkaria.postgresql.org; Fri, 11 Mar 2022 15:01:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nSgla-0000oT-5T for pgsql-general@arkaria.postgresql.org; Fri, 11 Mar 2022 15:01:18 +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 1nSglZ-0000oK-R3 for pgsql-general@lists.postgresql.org; Fri, 11 Mar 2022 15:01:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nSglW-0004II-NK for pgsql-general@postgresql.org; Fri, 11 Mar 2022 15:01:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 22BF1CMm830176; Fri, 11 Mar 2022 10:01:12 -0500 From: Tom Lane To: Durumdara cc: Postgres General Subject: Re: Am I in the same transaction block in complex PLPGSQL? In-reply-to: References: Comments: In-reply-to Durumdara message dated "Fri, 11 Mar 2022 09:53:44 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <830174.1647010872.1@sss.pgh.pa.us> Date: Fri, 11 Mar 2022 10:01:12 -0500 Message-ID: <830175.1647010872@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Durumdara writes: > In autocommit mode, when I start a simple update or select, it is one > transaction, so if something fails, the whole modification is rolled back > (there is no "half update", or "only first record updated"). > What will happen with complex statements, like PLPGSQL stored procedure, or > trigger? It's one transaction unless you take steps to make it something else. In plpgsql, you can make "sub transactions" with BEGIN/EXCEPTION blocks (an exception rolls back only side-effects that happened since BEGIN). Or, if it's a procedure not a function, you can explicitly COMMIT what's been done so far, and then your next action starts a new transaction. But without such measures, all actions executed by a function are part of the same transaction as the calling statement. regards, tom lane