A
A
Alexander Vladimirovich2020-08-12 14:46:56
PostgreSQL
Alexander Vladimirovich, 2020-08-12 14:46:56

How do you deal with postgresql connection pooling in GO?

Greetings!
How to work with connection pools for postgre in GO, how to organize a permanent connection and is it necessary? What kind? sqlx pgx? How to get a free connection from the pool?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uvelichitel, 2020-08-30
@uvelichitel

database.sql.DB from stdlib does not transparently maintain connection pooling under the hood. Methods are proposed for pool management

  • DB.SetMaxOpenConns
  • DB.SetMaxIdleConns
  • DB.SetConnMaxLifetime

The manipulation of these quotas does not have clear recipes; you need to try it locally, depending on the nature of the load.
When you form a transaction db.BeginTx, a free connection from the pool is selected for it.
If you need to manage a persistent connection from the pool, you make
conn, err := Db.Conn(context)
and form a transaction conn.BeginTxon this connection.
sqlx and pgx bring nothing new to connection pooling.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question