Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rHMeG-0027yA-VZ for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Dec 2023 11:28:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rHMeE-00ABIP-DM for pgsql-hackers@arkaria.postgresql.org; Sun, 24 Dec 2023 11:27:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rHMeE-00ABEC-2K for pgsql-hackers@lists.postgresql.org; Sun, 24 Dec 2023 11:27:58 +0000 Received: from forward500c.mail.yandex.net ([178.154.239.208]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rHMe9-00Bxq9-Dr for pgsql-hackers@postgresql.org; Sun, 24 Dec 2023 11:27:56 +0000 Received: from mail-nwsmtp-smtp-production-main-81.myt.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-81.myt.yp-c.yandex.net [IPv6:2a02:6b8:c12:5da3:0:640:8139:0]) by forward500c.mail.yandex.net (Yandex) with ESMTP id 82F3660CCB; Sun, 24 Dec 2023 14:27:49 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-81.myt.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id mRPwUuMi7Gk0-ZmRFGzUg; Sun, 24 Dec 2023 14:27:49 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1703417269; bh=Erp++A+hU8d/HmdtY7T62YRyjOSYzOaUwWBmWxO8fkY=; h=In-Reply-To:From:Cc:Date:References:To:Subject:Message-ID; b=TEDmfS/ESfZb/ALyIOX4Pxxl1bk3hjMUNTqkI99OKLsj4VGEONMOYfXz2i2eS/v2H mYpwmX5mfSuLQ7A94F7Aj6pS4KF7rot9NrPD4N73KMUa0eVoTdw9uwVCtVe5kD6NuL Rv0U2rqdwiJXTHzJ3D6PGj1rL6JbwTjlGsfG1as0= Authentication-Results: mail-nwsmtp-smtp-production-main-81.myt.yp-c.yandex.net; dkim=pass header.i=@tantorlabs.com Message-ID: <76bb1c7c-4c3d-4e10-a8ee-0d41a251b689@tantorlabs.com> Date: Sun, 24 Dec 2023 14:27:47 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Autonomous transactions 2023, WIP To: Pavel Stehule Cc: pgsql-hackers@postgresql.org References: <59cb5ad2-f814-464f-9f16-1f74575f11a8@tantorlabs.com> Content-Language: en-US From: Ivan Kush Organization: Tantor Labs LLC In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > 1. The solution based on background workers looks too fragile - it can be easy to exhaust all background workers, and because this feature is proposed mainly for logging, then it is a little bit dangerous, because it means loss of possibility of logging. 1. We could add types for background workers. For each type add guc-settings, like max workers of each type. For examaple, for `common` leave `max_worker_processes` setting for backward compatibility enum bgw_type {   common,   autonomous,   etc.... }; > 2. although the Oracle syntax is interesting, and I proposed PRAGMA more times,  it doesn't allow this functionality in other PL 2. Add `AUTONOMOUS` to `BEGIN` instead of `PRAGMA` in `DECLARE`? `BEGIN AUTONOMOUS`. It shows immediately that we are in autonomous session, no need to search in subsequent lines for keyword. ``` CREATE FUNCTION foo() RETURNS void AS $$ BEGIN AUTONOMOUS   INSERT INTO tbl VALUES (1);   BEGIN AUTONOMOUS    ....    END; END; $$ LANGUAGE plpgsql; ``` > CREATE OR REPLACE FUNCTION ... > AS $$ > $$ LANGUAGE plpgsql AUTONOMOUS TRANSACTION; The downside with the keyword in function declaration, that we will not be able to create autonomous subblocks. With `PRAGMA AUTONOMOUS` or `BEGIN AUTONOMOUS` it's possible to create them. ``` -- BEGIN AUTONOMOUS CREATE FUNCTION foo() RETURNS void AS $$ BEGIN   INSERT INTO tbl VALUES (1);   BEGIN AUTONOMOUS     INSERT INTO tbl VALUES (2);   END; END; $$ LANGUAGE plpgsql; -- or PRAGMA AUTONOMOUS CREATE FUNCTION foo() RETURNS void AS $$ BEGIN   INSERT INTO tbl VALUES (1);   BEGIN   DECLARE AUTONOMOUS_TRANSACTION;     INSERT INTO tbl VALUES (2);   END; END; $$ LANGUAGE plpgsql; START TRANSACTION; foo(); ROLLBACK; ``` ``` Output: 2 ``` > it doesn't allow this functionality in other PL I didn't work out on other PLs at the current time, but... ## Python In plpython we could use context managers, like was proposed in Peter's patch. ``` with plpy.autonomous() as a:     a.execute("INSERT INTO tbl VALUES (1) "); ``` ## Perl I don't programm in Perl. But googling shows Perl supports subroutine attributes. Maybe add `autonomous` attribute for autonomous execution? ``` sub foo :autonomous { } ``` https://www.perl.com/article/untangling-subroutine-attributes/ > Heikki wrote about the possibility to support threads in Postgres. 3. Do you mean this thread? https://www.postgresql.org/message-id/flat/31cc6df9-53fe-3cd9-af5b-ac0d801163f4%40iki.fi Thanks for info. Will watch it. Unfortunately it takes many years to implement threads =( > Surely, the first topic should be the method of implementation. Maybe I missed it, but there is no agreement of background worker based. I agree. No consensus at the current time. Pros of bgworkers are: 1. this entity is already in Postgres. 2. possibility of asynchronous execution of autonomous session in the future. Like in pg_background extension. For asynchronous execution we need a separate process, bgworkers are this separate process. Also maybe later create autonomous workers themselves without using bgworkers internally: launch of separate process, etc. But I think will be many common code with bgworkers. On 21.12.2023 12:35, Pavel Stehule wrote: > Hi > > although I like the idea related to autonomous transactions, I don't > think so this way is the best > > 1. The solution based on background workers looks too fragile - it can > be easy to exhaust all background workers, and because this feature is > proposed mainly for logging, then it is a little bit dangerous, > because it means loss of possibility of logging. > > 2. although the Oracle syntax is interesting, and I proposed PRAGMA > more times,  it doesn't allow this functionality in other PL > > I don't propose exactly  firebird syntax > https://firebirdsql.org/refdocs/langrefupd25-psql-autonomous-trans.html, > but I think this solution is better than ADA's PRAGMAs. I can imagine > some special flag for function like > > CREATE OR REPLACE FUNCTION ... > AS $$ > $$ LANGUAGE plpgsql AUTONOMOUS TRANSACTION; > > as another possibility. > > 3. Heikki wrote about the possibility to support threads in Postgres. > One significant part of this project is elimination of global > variables. It can be common with autonomous transactions. > > Surely, the first topic should be the method of implementation. Maybe > I missed it, but there is no agreement of background worker based. > > Regards > > Pavel > > -- Best wishes, Ivan Kush Tantor Labs LLC