From b1834a527d714dd9532139e78314bda50a169b1d Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 21 Jun 2014 18:28:56 -0500 Subject: [PATCH] Fix prepared statement performance with database/sql --- stdlib/sql.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/stdlib/sql.go b/stdlib/sql.go index dbfe04f8..a26b43e5 100644 --- a/stdlib/sql.go +++ b/stdlib/sql.go @@ -60,7 +60,13 @@ func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error) { return nil, err } - db.SetMaxIdleConns(0) + // Presumably OpenFromConnPool is being used because the user wants to use + // database/sql most of the time, but fast path with pgx some of the time. + // Allow database/sql to use all the connections, but release 2 idle ones. + // Don't have database/sql immediately release all idle connections because + // that would mean that prepared statements would be lost (which kills + // performance if the prepared statements constantly have to be reprepared) + db.SetMaxIdleConns(pool.MaxConnectionCount() - 2) db.SetMaxOpenConns(pool.MaxConnectionCount()) return db, nil