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

Close prepared statement in (*Transaction).Exec to avoid leaking PostgreSQL prepared statements #48

Closed
sqs opened this issue May 25, 2013 · 2 comments

Comments

@sqs
Copy link
Contributor

sqs commented May 25, 2013

When I run a lot of queries in a single connection, PostgreSQL's memory usage steadily increases (up to 13 GB after ~100k queries, at which point my computer locks up). I think this is because it does not garbage-collect prepared statements that are created in a connection until the connection is closed. The following patch fixes the issue; PostgreSQL memory usage is drastically lower after running 100k queries.

Has anybody else had the same issue?

diff --git a/gorp.go b/gorp.go
index 4086b6b..b7bbfc6 100644
--- a/gorp.go
+++ b/gorp.go
@@ -931,6 +931,7 @@ func (t *Transaction) Exec(query string, args ...interface{}) (sql.Result, error
        if err != nil {
                return nil, err
        }
+       defer stmt.Close()
        return stmt.Exec(args...)
 }
@coopernurse
Copy link
Contributor

wow, good catch. I just pushed this patch to master. I don't see a downside.

@ErgjanJaha
Copy link

This just saved me a ton of time, was missing that myself in my code and couldn't tell what was leaking

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

No branches or pull requests

3 participants