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 1lWEQP-00015p-BS for pgsql-general@arkaria.postgresql.org; Tue, 13 Apr 2021 08:29:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWEQN-0008JC-Q4 for pgsql-general@arkaria.postgresql.org; Tue, 13 Apr 2021 08:29:31 +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 1lWEQN-0008J5-If for pgsql-general@lists.postgresql.org; Tue, 13 Apr 2021 08:29:31 +0000 Received: from n3.nabble.com ([162.255.23.22]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWEQG-0001i8-Cp for pgsql-general@postgresql.org; Tue, 13 Apr 2021 08:29:30 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id 610322590AE13 for ; Tue, 13 Apr 2021 01:29:22 -0700 (MST) Date: Tue, 13 Apr 2021 01:29:22 -0700 (MST) From: pinker To: pgsql-general@postgresql.org Message-ID: <1618302562353-0.post@n3.nabble.com> Subject: rollback previous commit if the current one fails MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, i need to emulate oracle's savepoint behaviour inside of the plpgsql function. This function is able to insert all the rows that weren't caught on the exception, but i need also to rollback the insert that happens before the exception. So let's say the exception is thrown when j=3 so i need also to rollback j=2. Any idea how to approach it? DROP TABLE IF EXISTS test; CREATE TABLE test ( id INT ); CREATE OR REPLACE PROCEDURE test() AS $$ DECLARE j INT; BEGIN FOR j IN 0..6 LOOP BEGIN INSERT INTO test VALUES (1 / j); EXCEPTION WHEN OTHERS THEN END; END LOOP; END; $$ LANGUAGE plpgsql; CALL test(); TABLE test; -- Sent from: https://www.postgresql-archive.org/PostgreSQL-general-f1843780.html