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

Fix DB connection leak in buffercache logic, other connection related cleanup #663

Merged
merged 3 commits into from
Jan 8, 2025

Conversation

lfittl
Copy link
Member

@lfittl lfittl commented Jan 8, 2025

See individual commits.

lfittl added 3 commits January 7, 2025 23:31
Because the buffercache logic initializes its own database (so it
can run in parallel to other collection tasks), it needs to also ensure
to close its DB handle so the connection is released again.

In passing avoid the double assignment of "db" in the later parts of the
function.
This makes it clear that we must clean up the database connection that
was established, since we leak connections otherwise.

To support this, refactor the log-based EXPLAIN code so defer can be
used easily.
In almost all cases we have an existing database connection, so use
that to retrieve the value of Postgres settings.
@lfittl lfittl requested a review from keiko713 January 8, 2025 07:35
Copy link
Contributor

@keiko713 keiko713 left a comment

Choose a reason for hiding this comment

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

Looks good 👍 Thanks for fixing!

@@ -72,16 +71,10 @@ SELECT setting
FROM pg_settings
WHERE name = '%s'`

func GetPostgresSetting(ctx context.Context, settingName string, server *state.Server, globalCollectionOpts state.CollectionOpts, prefixedLogger *util.Logger) (string, error) {
func GetPostgresSetting(ctx context.Context, db *sql.DB, settingName string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks cleaner, but I wonder why we weren't doing this previously 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like it started out being used in a context where we didn't have a pre-existing connection (runner/logs.go in cbf2f08), and we later used the same helper in other places, without refactoring it.

@lfittl lfittl merged commit 620a0e3 into main Jan 8, 2025
9 checks passed
@lfittl lfittl deleted the fix-connection-leaks branch January 8, 2025 08:34
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.

2 participants