jackc / jackc/pgx

Connections not Released when using OpenDBFromPool

Open
#577 8 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

See #523.

When using the new `stdlib.OpenDBFromPool` function to create an `*sqlx.DB` connection from a connection pool, it appears that the connections are never released from the pgx connection pool.

**Background**
We have an application that uses sqlx almost exclusively, but there are two very targeted cases where we need to use a native `pgx.Conn` instead. I was hoping to be able to use the same connection pool for both sqlx and native pgx connections. I tried modifying our application to use `stdlib.OpenDBFromPool`, and it worked well, but I noticed that connections were not being released.

**Example**
I configured the application to periodically dump both sqlx and pgx connection pool stats. After running a while, sqlx reports plenty of available connections, but the pgx pool reports that all connections are in use. No native pgx connections were in use during this test

```
2019/08/14 14:49:35 SQLX Connection Stats: {MaxOpenConnections:10 OpenConnections:2 InUse:1 Idle:1 WaitCount:0 WaitDuration:0s MaxIdleClosed:0 MaxLifetimeClosed:0}
2019/08/14 14:49:35 PGX Connection Pool Stats: {MaxConnections:10 CurrentConnections:10 AvailableConnections:0}
```

Possible **Solution**
I _think_ that, when sqlx closes a connection in its pool, the underlying connection is not being released from the pgx connection pool. I've looked at the pgx source code, and I can't find any references where `Release` is called when a connection is closed. I expect that we'll need to add a `connPool` attribute to the connection struct and then release the connection from the pool when the connection is closed if this attribute is set (similar to the way a connection is released from the pool when a `Rows` object is closed). However, I'm new to this code, so I'm not sure exactly where it should go.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at stdlib.OpenDBFromPool and trace how the wrapped connection is closed, comparing it with the pool-release behavior when a Rows object is closed. Confirm that closing the SQL connection releases the underlying pgx pool connection, then reproduce the issue using the sqlx and pgx pool statistics shown in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.