Subject: Read-only connectios optimizatios

From: Edson Richter <[email-masked]>
Date: Sat, 25 Jan 2025 14:55:20 +0000

Scenario:
-PostgreSQL 13 latest version;
-I have some reporting users, with "SELECT,REFERENCES" permissions on all tables in the public schema.
-Connections are established using the jdbc "readonly" attribute.

Does PostgreSQL perform any optimization on queries in this scenario to avoid establishing locks? Or are these queries treated like any other?

Thanks 😊

---

From: Laurenz Albe <[email-masked]>
Date: Sat, 25 Jan 2025 19:01:31 +0100

On Sat, 2025-01-25 at 14:55 +0000, Edson Richter wrote:
> Does PostgreSQL perform any optimization on queries in this scenario to avoid
> establishing locks? Or are these queries treated like any other?

The only difference that I am aware of is that read-only transactions at the
SERIALIZABLE isolation level can release predicate locks earlier, which can
benefit performance.

But I don't think that you need to worry: reading transactions only take an
ACCESS SHARE lock on tables, which won't conflict with data modifications.

Yours,
Laurenz Albe

---

From: peter plachta <[email-masked]>
Date: Sat, 25 Jan 2025 14:12:18 -0800

You can still block vacuum from running if you have long running (or very aggressive) read transactions. I don’t think they are very helpful or performant from a Postgres engine perspective.
They can be helpful in application development because they will fail if devs attempt any mutations inside read only (from what I recall).

Sent from my iPhone

> On Jan 25, 2025, at 10:01 AM, Laurenz Albe <[email-masked]> wrote:
>

---

From: "Wong, Kam Fook (TR Technology)" <[email-masked]>
Date: Mon, 27 Jan 2025 17:41:51 +0000

Just a thought if you are moving to AWS Aurora Postgres.  You can point the read queries/transaction/long running read queries to the reader node.  That will not block the autovacuum process.  

-----Original Message-----
From: peter plachta <[email-masked]> 
Sent: Saturday, January 25, 2025 4:12 PM
To: Laurenz Albe <[email-masked]>
Cc: Edson Richter <[email-masked]>; [email-masked]
Subject: [EXT] Re: Read-only connectios optimizatios

External Email: Use caution with links and attachments.

Sent from my iPhone

>> Does PostgreSQL perform any optimization on queries in this scenario 
>> to avoid establishing locks? Or are these queries treated like any other?
> 
> The only difference that I am aware of is that read-only transactions 
> at the SERIALIZABLE isolation level can release predicate locks 
> earlier, which can benefit performance.
> 
> But I don't think that you need to worry: reading transactions only 
> take an ACCESS SHARE lock on tables, which won't conflict with data modifications.
> 
> Yours,
> Laurenz Albe
> 
>