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 1sn1NW-000RKK-U3 for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 19:45:51 +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 1sn1MW-003GIg-Tn for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 19:44:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sn1MW-003GIY-Ko for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 19:44:48 +0000 Received: from mout.gmx.net ([212.227.17.21]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sn1MP-0002Hl-DX for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 19:44:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1725738277; x=1726343077; i=karsten.hilbert@gmx.net; bh=wCUETbtRwcUWzwkz33ymBKOwyZdaAsxwzS2RrGOomGg=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=R+q/+DlJcSaXR/9pVb8Jn3ZZ/LLKdORWlZoccTXce2qB3Zdix01knkXAI/63pKLY XNtirO0EPlkT9acziBtjDWcYZC4+0QLt79gMKiUmD+Weh8d+EAsM1gSyGLERCPmRy UQLGwc17hrw72UEEPiTEqwudwIGFuNJuVbUva2+OZF1fcZSghp21k2RUsS1Tt0/Cg rQiS/LxfM+lXqwfTo50juKtA7eaJw/Xny2PUQWhngb4UFzZGMYnv191q7FoD22MUQ 26F+ZsuihUGH7XyAVU7jz2y/12KdqSGpegH4AyDjNgRX3AK+h5PuE3auQH0MwTjHL r+AzlMHWtnt3w8Qygw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from hermes ([84.144.218.143]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MC34X-1sskNk360j-00CU8o; Sat, 07 Sep 2024 21:44:37 +0200 Received: from ncq by hermes with local (Exim 4.96) (envelope-from ) id 1sn1MK-0002DS-1Y; Sat, 07 Sep 2024 21:44:36 +0200 Date: Sat, 7 Sep 2024 21:44:36 +0200 From: Karsten Hilbert To: Adrian Klaver Cc: psycopg@lists.postgresql.org, python-list@python.org Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks Message-ID: References: <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com> Ma_X_il-Followup-to: d Re_X_turn-receipt-to: Karsten.Hilbert@gmx.net Di_X_sposition-Notification-To: Karsten.Hilbert@gmx.net X-Confi_X_rm-Reading-To: Karsten.Hilbert@gmx.net X-Pri_X_ority: 2 (High) Sender: X-Provags-ID: V03:K1:Yvu5+EO2oBxOOPUV5oi/hAuISjxfaKwBU2u/c0TTUGr+ZAkKCWM 0IIg3JDw46uoj1GHiqH23kYoiqagimr2n/XYOUMuIHBe3PQEgW9Oh5sGziUlIcY0d1pQu3j jDu/q5mGOU4I9VzZ4JVhw4beUmTgXlhBZmPigvkX1VbRl+m/RMdPp+sbsOCYmVRMC6Ao3Tm xstu1WiqRqF9BGUpmtoMw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:sDl2TmaTtU4=;/b8zDZDHUkHLXjYZZV4UZVXstDP 7x54aAxDVnL2zmyxyKSKluy7roZYVZWuONQU7u+ULY2xRYfm/tftwKvNarhC6r5RsCo8pNihj I4hmXzQRjaLIecVCGY3O6TO5gsfSLZX10dH8XCua6TAjStB6BzZ47I8HG/enaLGEh0fEMvns4 +kVv4rw8XrU+kd5068O2ULhpTr2eC7KsAx/UfQAtnm1Og6rx1j0u4ZiqYFaijHlNFdlK+d9rD g30ztdDgbZaEUGAg+11e4EnvrB/5ZveSMiHniVnzBhHMeC1j12M9X+z05wJHAgkQWMej1LXBE 1hVcs+qO4rHHs6bB3qE+gIxltdaG2PgdEy2KtwwbelGGawbTc0FuInJ92tCcVTFZ2NMpb09gz 0JB/mFQ9auP0xZZh0H4fjBDPuXkkH2nChcx6dKkYCzlusrS4gp2wi4AF6YgDleTkisFPYV5vl BZaoDAacphrOK1PCbxzF3x/MfwdumlFSJnmNyHmeUMWMblbuauaGMT+lFNtXsclJnRNMIjJou ahsUKyWYKC1RbqP1DMmLJ9BT0suNpuuF9W1JPsG2bWcQwsSR9vQ/FJgRTpKGvAeea6altz59d 4IvUBg5eKus99TlODc5PmW8c9QnzpL5ECi+K3j0nDhTGYLs77qstUdD0XMjLZS990fjBzrRem KZeA2Z7v9+bprTykmm7uPAfohULhojY3QfuBiEbecoH+kKhVJFkKY2NTF887ZjFL8pBPvxGrf I7j/DQi2lCKqRb9Xdi90I8NM62xywCKW8FdHjukutSGaA0H+6vDyy31nVwL8qjBue0ZURT1jx f3YvQ9ousBLGpuP/7i2tBjMQ== Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Am Sat, Sep 07, 2024 at 09:46:03AM -0700 schrieb Adrian Klaver: > >unto now I had been thinking this is a wise idiom (in code > >that needs not care whether it fails to do what it tries to > >do^1): > > > > conn =3D psycopg2.connection(...) > > In the above do you have: > > https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.ISOLATI= ON_LEVEL_SERIALIZABLE > > psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE I do indeed. > Or is that in some other concurrent transaction? In fact in that codebase all transactions -- running concurrently or not -- are set to SERIALIZABLE. They are not psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT, for that matter. > > curs =3D conn.cursor() > > try: > > curs.execute(SOME_SQL) > > except PSYCOPG2-Exception: > > some logging being done, and, yes, I > > can safely inhibit propagation^1 > > finally: > > conn.commit() # will rollback, if SOME_SQL failed > > It will if you use with conn:, otherwise it up to you to do the rollback= () > > Are you are doing a rollback() in except PSYCOPG2-Exception: ? No I don't but - to my understanding - an ongoing transaction is being closed upon termination of the hosting connection. Unless .commit() is explicitely being issued somewhere in the code that closing of a transaction will amount to a ROLLBACK. In case of SQL having failed within a given transaction a COMMIT will fail-but-rollback, too (explicit ROLLBACK would succeed while a COMMIT would fail and, in-effect, roll back). IOW, when SOME_SQL has failed it won't matter that I close the connection with conn.commit() and it won't matter that conn.commit() runs a COMMIT on the database -- an open transaction having run that failed SQL will still roll back as if ROLLBACK had been issued. Or else my mental model is wrong. https://www.psycopg.org/docs/connection.html#connection.close In the particular case I was writing about the SQL itself succeeded but then the COMMIT failed due to serialization. I was wondering about where to best place any needed conn.commit(). My knee-jerk reaction was to then put it last in the try: block... All this is probably more related to Python than to PostgreSQL. Thanks, Karsten =2D- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B