Pg: Connection pool timeout

Created on 19 Apr 2016  路  8Comments  路  Source: go-pg/pg

I am constantly getting "pg: connection pool timeout" using api v3. However, at the same time I still can connect to the same database using PgAdmin and another go application. The only workaround is to restart the API application completely.

To connect to the database I do the following:

options := pg.Options{
    User:     postgres.User,
    Password: postgres.Password,
    Database: postgres.Database,
    Host:     postgres.Host,
    Port:     postgres.Port,
}
db = pg.Connect(&options)
db = db.WithTimeout(30 * time.Second)

And when the API accepts incoming requests, it queries the database.
I never had this issue when connecting to the local database, but now I need to use the database that is in the cloud through public IP address.

What could it be? Thanks.

Most helpful comment

I am using Stmt created by prepare and was not closing them, after I added defer stmt.Close() to all my stmt after using them, the connection pool error went away. Thanks.

All 8 comments

Could be:

  1. The queries are fairly simple and take 1-40ms.
  2. I do not use any transactions and listeners, and query the database using DB struct

What about panics? Do you use recover?

Overall I use both pg.v3 and pg.v4 in production so there should not be stupid/obvious bugs...

Yes, I have recover middleware implemented in the application. In case of panics, it recovers and sends the full stack trace to my email address (I have not received any of such emails these days).

I am sorry, it was an issue on my side. I am going to close the issue. Thanks again!

@rbatukaev can you please explain what the issue on your side was as I am getting the same behaviour.

I am using Stmt created by prepare and was not closing them, after I added defer stmt.Close() to all my stmt after using them, the connection pool error went away. Thanks.

My issue was related to Azure Load Balancer.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ValorVl picture ValorVl  路  3Comments

mamal72 picture mamal72  路  3Comments

dizzyfool picture dizzyfool  路  3Comments

l0gicgate picture l0gicgate  路  5Comments

owentran picture owentran  路  6Comments