public inbox for [email protected]  
help / color / mirror / Atom feed
From: David Raymond <[email protected]>
To: Psycopg <[email protected]>
Subject: conn.read_only not honored in autocommit mode
Date: Tue, 18 Jul 2023 20:24:46 +0000
Message-ID: <DB7PR07MB39163E0AFB907413EE5C10F68738A@DB7PR07MB3916.eurprd07.prod.outlook.com> (raw)

It appears that the .read_only attribute of a connection is not honored when the connection is in autocommit mode.

There's no mention of this on the Connection classes page for read_only, and in the section linked to in Transactions management you have to read it 3 times to realize it's only used with Connection.transaction().

Is it the intent to not be used for regular autocommit connections, or is it an oversight?

I used conn.set_session(readonly = True, deferrable = False, autocommit = True) in psycopg2, and that seemed to work at preventing accidental changes.
I've just been moving to psycopg recently, and with no set_session I replaced it with putting autocommit = True in the connection call and following it with .read_only = True and .deferrable = False

Since I vastly prefer autocommit mode, is my best bet then to replace
conn.read_only = True
with either
conn.execute("set session characteristics as transaction read only, not deferrable;")
or
conn.execute("set default_transaction_read_only to true;")
conn.execute("set default_transaction_deferrable to false;")
?

(Honestly in my mind I thought this is what changing .read_only, .deferrable, or .isolation_level did in the background already)


Thank you for your patience with me,


view thread (2+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: conn.read_only not honored in autocommit mode
  In-Reply-To: <DB7PR07MB39163E0AFB907413EE5C10F68738A@DB7PR07MB3916.eurprd07.prod.outlook.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox