-
-
Notifications
You must be signed in to change notification settings - Fork 186
feat: shared pgx-pool #4393
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
Merged
thomaspoignant
merged 20 commits into
thomaspoignant:main
from
gogbog:feature/shared-pgxpool
Dec 17, 2025
Merged
feat: shared pgx-pool #4393
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6823c96
use pgxpool and singelton to initialize it just once
7a3eb53
format with gofmt
a0082b1
fix imports
e9ff21c
fix formatting issues
117ae1a
simplify postgres db initalization
4617a05
format
5d97bb0
multi postgres pools support
cde1664
format issues
2852577
Pass ctx to DB shutdown
fa090e2
Merge branch 'main' into feature/shared-pgxpool
gogbog 228208a
multiple pools test
de4ed7f
Merge branch 'main' into feature/shared-pgxpool
e3f6a11
merge main
a0bd5ce
Merge branch 'main' into feature/shared-pgxpool
thomaspoignant 39388bb
Merge branch 'main' into feature/shared-pgxpool
thomaspoignant b990bbe
change error message / fix test
d290db0
Merge branch 'main' into feature/shared-pgxpool
gogbog 73d9ed6
Merge branch 'main' into feature/shared-pgxpool
thomaspoignant 9f25828
Merge branch 'main' into feature/shared-pgxpool
thomaspoignant 6ffb852
Merge branch 'main' into feature/shared-pgxpool
thomaspoignant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package postgresqlretriever | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
|
|
||
| "github.com/jackc/pgx/v5/pgxpool" | ||
| ) | ||
|
|
||
| var ( | ||
| pool *pgxpool.Pool | ||
| mu sync.Mutex | ||
| refCount int | ||
| ) | ||
|
|
||
| func GetPool(ctx context.Context, uri string) (*pgxpool.Pool, error) { | ||
|
Check warning on line 16 in retriever/postgresqlretriever/database.go
|
||
| mu.Lock() | ||
| defer mu.Unlock() | ||
|
|
||
| if pool == nil { | ||
| p, err := pgxpool.New(ctx, uri) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := p.Ping(ctx); err != nil { | ||
| p.Close() | ||
| return nil, err | ||
| } | ||
|
|
||
| pool = p | ||
| } | ||
|
|
||
| refCount++ | ||
| return pool, nil | ||
| } | ||
|
|
||
| func ReleasePool() { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
|
|
||
| refCount-- | ||
| if refCount <= 0 { | ||
| if pool != nil { | ||
| pool.Close() | ||
| pool = nil | ||
| } | ||
| refCount = 0 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current singleton implementation for the connection pool doesn't support multiple database URIs. The global
poolvariable will be initialized once with the URI from the first retriever, and all subsequent retrievers, regardless of their configuredURI, will receive this same pool. This can lead to retrievers connecting to the wrong database.To fix this, the pool management should be URI-aware. I suggest using maps to store a connection pool and a reference count for each unique URI. This ensures that each database has its own dedicated pool, which is shared only by retrievers connecting to that same database.