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 1mavDk-00025w-2l for psycopg@arkaria.postgresql.org; Thu, 14 Oct 2021 07:32:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mavDh-0000F7-3x for psycopg@arkaria.postgresql.org; Thu, 14 Oct 2021 07:32:05 +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 1maoOl-0008Vu-G3 for psycopg@lists.postgresql.org; Thu, 14 Oct 2021 00:15:03 +0000 Received: from mail.emypeople.net ([68.132.158.180]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1maoOi-00052l-Sh for psycopg@lists.postgresql.org; Thu, 14 Oct 2021 00:15:03 +0000 DKIM-Signature: a=rsa-sha256; t=1634170495; x=1634775295; s=mail; d=emypeople.net; c=relaxed/relaxed; v=1; bh=Ouga0gYdyk3l4/vSMXhnEQ42eESnqUqgbWyw4piPUeI=; h=From:Subject:Date:Message-ID:To:MIME-Version:Content-Type:In-Reply-To:References; b=PZuExjEQEWwu8KKbhN+Hh0An8s2oO/aJWyWWEKPBGq+27HxtGY2oZlkEcq/XrxJo+SpOdFlWuWRFDCIcktqeCvJuwaq4faAVWShNe+SIEFOvl+CqnWoLgXM3xBktmjJl5a3IyQCdkPfa2WJLEKGjpJe6wXTCTPjEvUQZzL5uMMI= Received: from [192.168.0.101] ([174.244.113.197]) by mail.emypeople.net (13.0.1 build 4 RHEL7 x64) with ASMTP id 202110132014542964 for ; Wed, 13 Oct 2021 20:14:54 -0400 Subject: Re: psycopg3 transactions To: psycopg@lists.postgresql.org References: <9a99d2d110b121542d1ea0a05066858b@paolodestefani.it> From: Reuben Rissler Message-ID: Date: Wed, 13 Oct 2021 20:14:53 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------FA8A6B9C9906D9E8DC612053" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------FA8A6B9C9906D9E8DC612053 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit > > Agree. The DBAPI choice to mandate that autocommit be off by default > is a strange and unfortunate choice that in my experience leads to a > never ending series of "surprises" such as this one. > > My conclusion is that the only sane thing to do is: > 1. Only ever create connections in autocommit mode. > 2. Only ever use `with connection.transaction()` to control transactions. > 3. Forget that `connection.commit()` and `connection.rollback()` > exist, and never use them. > > I'm in favour of recommending this as strongly as reasonably possible > in the documentation. > > It's a shame that 1 is not the default. So you still have to remember > to not forget to do this explicitly, every time you create a connection. > > Dani I was going to disagree with this, as some things don't work in an ACID way in autocommit. Then I read point 2, now I'm going to try this and see how I like transactions vs commit/rollback. Reuben Rissler --------------FA8A6B9C9906D9E8DC612053 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

Agree. The DBAPI choice to mandate that autocommit be off by default is a strange and unfortunate choice that in my experience leads to a never ending series of "surprises" such as this one.

My conclusion is that the only sane thing to do is:
1. Only ever create connections in autocommit mode.
2. Only ever use `with connection.transaction()` to control transactions.
3. Forget that `connection.commit()` and `connection.rollback()` exist, and never use them.

I'm in favour of recommending this as strongly as reasonably possible in the documentation.

It's a shame that 1 is not the default. So you still have to remember to not forget to do this explicitly, every time you create a connection.

Dani


I was going to disagree with this, as some things don't work in an ACID way in autocommit. Then I read point 2, now I'm going to try this and see how I like transactions vs commit/rollback.

Reuben Rissler
--------------FA8A6B9C9906D9E8DC612053--