Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add *sql.DB construction from *pgxpool.Pool #1718

Merged
merged 8 commits into from
Sep 9, 2023

Conversation

lzakharov
Copy link
Contributor

Closes #1065

@jackc
Copy link
Owner

jackc commented Aug 19, 2023

There seems to be a great deal of code. I would not have expected a sql.DB <-> pgxpool.Pool adapter to be more than 50 LOC. stdlib already uses *pgx.Conn internally. I would have expected a small shim for stdlib that uses pgxpool.Pool.Acquire instead of pgx.ConnectConfig and pgxpool.Conn.Release instead of pgx.Conn.Close.

@lzakharov
Copy link
Contributor Author

Agree, started to make this solution as a separate module, that's why I had to duplicate a lot of code. Rewrote to a more compact version.

@lzakharov lzakharov force-pushed the convert-pgxpool-to-sql-db branch from e5715ff to c95bd95 Compare August 19, 2023 18:46
@lzakharov lzakharov force-pushed the convert-pgxpool-to-sql-db branch from c95bd95 to 85fb231 Compare August 19, 2023 18:47
@lzakharov lzakharov force-pushed the convert-pgxpool-to-sql-db branch from 2ae940f to 01d33b4 Compare August 19, 2023 19:03
@lzakharov lzakharov marked this pull request as ready for review August 21, 2023 12:40
stdlib/sql.go Outdated
} else {
var pconn *pgxpool.Conn

if err = c.BeforeAcquire(ctx, c.pool); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the use case for sql.Driver level BeforeAcquire? Pool already has this callbacks internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point, fixed!

@lzakharov lzakharov requested a review from redbaron August 29, 2023 11:17
@lzakharov
Copy link
Contributor Author

@jackc can u review again, please?

@jackc
Copy link
Owner

jackc commented Sep 2, 2023

The code seems reasonable.

But this approach requires the database/sql pool to be configured with SetMaxIdleConns or the like to avoid acquiring all the connections from the pgxpool and starving any direct users of the pgxpool.

At the very least proper usage should be documented. But if SetMaxIdleConns(0) should always be called, maybe OpenDBFromPool should call it automatically.

@redbaron
Copy link
Contributor

redbaron commented Sep 3, 2023

At the very least proper usage should be documented. But if SetMaxIdleConns(0) should always be called, maybe OpenDBFromPool should call it automatically.

It does: https://github.com/jackc/pgx/pull/1718/files#diff-32dbcc70c656d819ec542b81ab30702bb835f38d8db8c0574c6da16b7ce04b35R229

@zekth
Copy link
Contributor

zekth commented Sep 8, 2023

I did just try this branch in integration with a project using bun https://github.com/uptrace/bun And perfectly works.

@jackc
Copy link
Owner

jackc commented Sep 9, 2023

At the very least proper usage should be documented. But if SetMaxIdleConns(0) should always be called, maybe OpenDBFromPool should call it automatically.

It does: https://github.com/jackc/pgx/pull/1718/files#diff-32dbcc70c656d819ec542b81ab30702bb835f38d8db8c0574c6da16b7ce04b35R229

@redbaron Huh, not sure how I missed that.

@jackc jackc merged commit b301530 into jackc:master Sep 9, 2023
14 checks passed
@jackc
Copy link
Owner

jackc commented Sep 9, 2023

LGTM

@lzakharov lzakharov deleted the convert-pgxpool-to-sql-db branch September 9, 2023 13:17
@zekth zekth mentioned this pull request Sep 9, 2023
@GoosvandenBekerom
Copy link

When can we expect the next version to be tagged / released?
Would love to use this!

@lzakharov
Copy link
Contributor Author

The answer can be found in #1767:

I expect v5.5.0 to be released within a few weeks.

@eamora-ab
Copy link

The only thing I don't like is the inability to return the correct DBStats from the pool *sql.DB
Is that still the case?

If this it I'd like to see if there would be a way to get those stats (critical for pool tuning)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert pgxpool conn to stdlib connection
6 participants