pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Does creating readOnly connections, when possible, free up resources in Postgres?
5+ messages / 4 participants
[nested] [flat]

* Does creating readOnly connections, when possible, free up resources in Postgres?
@ 2019-01-27 18:09 David Kremer <jkorders@gmx.com>
  0 siblings, 0 replies; 5+ messages in thread

From: David Kremer @ 2019-01-27 18:09 UTC (permalink / raw)
  To: pgsql-general

<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>I have an API server and I&#39;m trying to be conscientious managing Postgres&#39;s resources carefully. On the client side, I have a Hikari Pool.</div>

<div><br/>
Usually when I need a connection, I simply create a default read/write connection, even if I don&#39;t plan to make any updates or inserts or hold any locks. But most of my database connections are in fact read-only.</div>

<div>&nbsp;</div>

<div>I saw that when you create a JDBC connection, you can specify&nbsp;<code>readOnly=true</code>. Would doing so somehow help Postgres manage its other connections? Perhaps Postgres, knowing that a connection is&nbsp;<code>readOnly</code>&nbsp;and will never even attempt to do an update, will free up some internal resources for other connections. Is this accurate?</div>

<div>&nbsp;</div></div></body></html>




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Does creating readOnly connections, when possible, free up resources in Postgres?
@ 2019-01-27 18:11 David Kremer <jkorders@gmx.com>
  2019-01-27 19:31 ` Re: Does creating readOnly connections, when possible, free up resources in Postgres? Tom Lane <tgl@sss.pgh.pa.us>
  2019-01-27 23:21 ` Re: Does creating readOnly connections, when possible, free up resources in Postgres? Hannes Erven <hannes@erven.at>
  2019-01-28 09:08 ` Re: Does creating readOnly connections, when possible, free up resources in Postgres? Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 3 replies; 5+ messages in thread

From: David Kremer @ 2019-01-27 18:11 UTC (permalink / raw)
  To: pgsql-general

(resending to remove HTML formatting)

I have an API server and I'm trying to be conscientious managing Postgres's resources carefully. On the client side, I have a Hikari Pool.

Usually when I need a connection, I simply create a default read/write connection, even if I don't plan to make any updates or inserts or hold any locks. But most of my database connections are in fact read-only.

I saw that when you create a JDBC connection, you can specify readOnly=true. Would doing so somehow help Postgres manage its other connections? Perhaps Postgres, knowing that a connection is readOnly and will never even attempt to do an update, will free up some internal resources for other connections. Is this accurate?




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Does creating readOnly connections, when possible, free up resources in Postgres?
  2019-01-27 18:11 Does creating readOnly connections, when possible, free up resources in Postgres? David Kremer <jkorders@gmx.com>
@ 2019-01-27 19:31 ` Tom Lane <tgl@sss.pgh.pa.us>
  2 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2019-01-27 19:31 UTC (permalink / raw)
  To: David Kremer <jkorders@gmx.com>; +Cc: pgsql-general

"David Kremer" <jkorders@gmx.com> writes:
> I saw that when you create a JDBC connection, you can specify readOnly=true. Would doing so somehow help Postgres manage its other connections? Perhaps Postgres, knowing that a connection is readOnly and will never even attempt to do an update, will free up some internal resources for other connections. Is this accurate?

No.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Does creating readOnly connections, when possible, free up resources in Postgres?
  2019-01-27 18:11 Does creating readOnly connections, when possible, free up resources in Postgres? David Kremer <jkorders@gmx.com>
@ 2019-01-27 23:21 ` Hannes Erven <hannes@erven.at>
  2 siblings, 0 replies; 5+ messages in thread

From: Hannes Erven @ 2019-01-27 23:21 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi David,


 > I saw that when you create a JDBC connection, you can specify
 > readOnly=true. Would doing so somehow help Postgres manage its other
 > connections?


if you know that a certain connection will be ready-only, you could use 
a more aggressive pooling strategy.

Usually, a connection pool will return a worker thread to the pool when 
the client closes the connection.
pgbouncer for example offers a "statement" pooling strategy, meaning 
that a worker connection is returned to the pool after every completed 
SQL statement.
That way, a single Postgresql server thread can easily serve many client 
connections.
If a specific usecase fits into a single command, you can even use it 
for these writes!

Of course, you can't use server-side prepared statements, temporary 
tables, ... or anything else that depends on subsequent statements 
hitting the same server process.


For an application I'm running, we have dozens of GUI clients keeping a 
JDBC connection open all day checking for updates, and with statement 
pooling this is handled well by 1-3 server processes (auto-scaled by 
pgbouncer).


Best regards,

	-hannes





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Does creating readOnly connections, when possible, free up resources in Postgres?
  2019-01-27 18:11 Does creating readOnly connections, when possible, free up resources in Postgres? David Kremer <jkorders@gmx.com>
@ 2019-01-28 09:08 ` Laurenz Albe <laurenz.albe@cybertec.at>
  2 siblings, 0 replies; 5+ messages in thread

From: Laurenz Albe @ 2019-01-28 09:08 UTC (permalink / raw)
  To: David Kremer <jkorders@gmx.com>; pgsql-general

David Kremer wrote:
> I have an API server and I'm trying to be conscientious managing Postgres's
> resources carefully. On the client side, I have a Hikari Pool.
> 
> Usually when I need a connection, I simply create a default read/write connection,
> even if I don't plan to make any updates or inserts or hold any locks.
> But most of my database connections are in fact read-only.
> 
> I saw that when you create a JDBC connection, you can specify readOnly=true.
> Would doing so somehow help Postgres manage its other connections? Perhaps Postgres,
> knowing that a connection is readOnly and will never even attempt to do an update,
> will free up some internal resources for other connections. Is this accurate?

It won't free any resources, but it is still a good idea if you use the SERIALIZABLE
isolation level.

https://www.postgresql.org/docs/current/transaction-iso.html#XACT-SERIALIZABLE:

  For optimal performance when relying on Serializable transactions for concurrency
  control, these issues should be considered:

  - Declare transactions as READ ONLY when possible.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2019-01-28 09:08 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-27 18:09 Does creating readOnly connections, when possible, free up resources in Postgres? David Kremer <jkorders@gmx.com>
2019-01-27 18:11 Does creating readOnly connections, when possible, free up resources in Postgres? David Kremer <jkorders@gmx.com>
2019-01-27 19:31 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-01-27 23:21 ` Hannes Erven <hannes@erven.at>
2019-01-28 09:08 ` Laurenz Albe <laurenz.albe@cybertec.at>

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