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 1okuYp-00086g-QX for pgsql-general@arkaria.postgresql.org; Tue, 18 Oct 2022 21:55:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1okuYo-0005R5-CH for pgsql-general@arkaria.postgresql.org; Tue, 18 Oct 2022 21:55:42 +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 1okuYn-0005Qw-U4 for pgsql-general@lists.postgresql.org; Tue, 18 Oct 2022 21:55:42 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1okuYe-0006tV-1A for pgsql-general@lists.postgresql.org; Tue, 18 Oct 2022 21:55:41 +0000 Received: by mail-wr1-x42f.google.com with SMTP id r13so25824748wrj.11 for ; Tue, 18 Oct 2022 14:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=CwjIql6K0hTZLnvbN7XiIKnBLYyINe+DcQ8Vmheum+o=; b=FD78DzUwALRQnldEKg/+3teWOPdD9Jjnk7KH2XUB8jrUymFBHouqVGdLjxO/B925Ub 0dsaOuH+MuVoDFdDdVY5TLMXcule3mW+2ncTz6qkUOZZ2Avc0Bue9Sgm9W3AutGroURG bQKF8cjnPL0zfu70TCR0O8yt8H6wVIJG9dNGP6IvJWJuDW0EWDAhxNCQnqOdHbur8l3X lpZsbf2xQVsFvCLSO43nf/lddfTSiWmx5RnsSwympQpNCP9+DGRQ+Jphzb21KC6b5my+ vbLajXYwbOtMGiVgO8I2BupGeFyo3roL7S3KapKHPf5pq2Nsz4DeE8jy/FPd0vW+f41G w/tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=CwjIql6K0hTZLnvbN7XiIKnBLYyINe+DcQ8Vmheum+o=; b=OAoWxosSk9ac2TTfDOFd/CMQIKTL6mvO8CxBPHXC2gfjSlFvB3RFLfiF24NOx9Ce1s +eH/ZbFq5lXmgqds+qAXmDp7ZOV51FqYhazsF0iOjOdIPmO7uaOiIJVlxcQ3AdKyejYD U+O8OddFhc6/Vp8GgVgWWEk3Yoo0zPDIts1J5kxbldyzb2xhLkvG1Hem2rcSk+eop8uJ IDd2O0pWbUqIU0Xlxe3+YwZE7vQm0DegIHQEusYEygMk+QMv3WmxNC42WtNNIom1iAjQ v6cIMK/LCS3J4nEqMcxYO0gdZNJrue6YkF9Hroc26DXqxpwkDtkBypKIUb4o3CqIHdBI 6YXQ== X-Gm-Message-State: ACrzQf1ubHYoa6t36WzZ0FYOMEDTJbD/35mpk2GUFlNWq0AiVdwyBPj3 chdjXUPc86mMTROqMfxfcOwkPA== X-Google-Smtp-Source: AMsMyM5Xt5LzeWrsdivetcbw1D8paVjTeNPKOH8a6zcu2QQ+tQDAArd0OKXmJIgYfwdtdUFcOkU+zA== X-Received: by 2002:a05:6000:2ad:b0:231:48fb:3a64 with SMTP id l13-20020a05600002ad00b0023148fb3a64mr3000809wry.184.1666130130017; Tue, 18 Oct 2022 14:55:30 -0700 (PDT) Received: from localhost.localdomain (62-46-248-232.adsl.highway.telekom.at. [62.46.248.232]) by smtp.gmail.com with ESMTPSA id u14-20020a05600c19ce00b003b477532e66sm4385058wmq.2.2022.10.18.14.55.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 14:55:29 -0700 (PDT) Message-ID: <811cd41fda48a12fe2c430e03dbf0f7fd57d49d2.camel@cybertec.at> Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP From: Laurenz Albe To: Tom Lane , Bryn Llewellyn Cc: Christophe Pettus , Ravi Krishna , pgsql-general list Date: Tue, 18 Oct 2022 23:55:28 +0200 In-Reply-To: <2746245.1666128817@sss.pgh.pa.us> References: <353091661.109766.1666124045054.ref@mail.yahoo.com> <353091661.109766.1666124045054@mail.yahoo.com> <72BEC008-DC6C-4D80-80A6-E1634CFFA656@thebuild.com> <2746245.1666128817@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2022-10-18 at 17:33 -0400, Tom Lane wrote: > Bryn Llewellyn writes: > > xof@thebuild.com wrote: > > > You can commit in a loop, but not in BEGIN / END block that has an exception handler: that creates a subtransaction for the duration of the BEGIN / END. > > > This surprised me when I first started to use PG (after all those years > > with ORCL). > > Really?  BEGIN with an exception block is a subtransaction because it's > defined to roll back to the database state as of the start of the block > if an exception occurs.  COMMIT in the middle fundamentally conflicts > with that, I should think.  Does Oracle interpret that differently? Looks like Oracle doesn't care much about that: SQL> CREATE TABLE mytab (id integer CHECK (id > 0)); Table created. SQL> CREATE PROCEDURE committest IS 2 BEGIN 3 INSERT INTO mytab VALUES (42); 4 COMMIT; 5 INSERT INTO mytab VALUES (-42); 6 EXCEPTION 7 WHEN OTHERS THEN 8 NULL; 9 END; 10 / Procedure created. SQL> CALL committest(); Call completed. SQL> SELECT * FROM mytab; ID ---------- 42 I looks like Oracle allows you to randomly interfere with its transaction handling. If you run commit and then enter an exception handler, it simply doesn't rollback. Yours, Laurenz Albe