From 29729db7aedef69f60b082328a13150a6a5503f9 Mon Sep 17 00:00:00 2001 From: Aldo Fuster Turpin Date: Wed, 14 Jan 2026 03:27:23 +0100 Subject: [PATCH] docs(RegisterConnConfig): explain alternative usage Give an alternative for when we have clients with many short-lived connections, to avoid memory leaks. --- stdlib/sql.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/stdlib/sql.go b/stdlib/sql.go index 99a18c055..f57365737 100644 --- a/stdlib/sql.go +++ b/stdlib/sql.go @@ -32,6 +32,12 @@ // connStr := stdlib.RegisterConnConfig(connConfig) // db, _ := sql.Open("pgx", connStr) // +// Consider using stdlib.OpenDB + sqlx.NewDb instead of RegisterConnConfig +// if your client code perform many short-lived connections to multiple DBs (to avoid memory leaks). +// +// db := stdlib.OpenDB(*pgxConfig) +// dbconn := sqlx.NewDb(db, "pgx") +// // pgx uses standard PostgreSQL positional parameters in queries. e.g. $1, $2. It does not support named parameters. // // db.QueryRow("select * from users where id=$1", userID) @@ -393,6 +399,12 @@ func (dc *driverConnector) Driver() driver.Driver { } // RegisterConnConfig registers a ConnConfig and returns the connection string to use with Open. +// It uses a package-level Driver struct that holds a map that tracks all the connections, +// using as key the connection string and as a value the *pgx.ConnConfig. +// Consider using stdlib.OpenDB + sqlx.NewDb instead of RegisterConnConfig +// if your clients perform many short-lived connections to multiple DBs (to avoid memory leaks): +// db := stdlib.OpenDB(*pgxConfig) +// dbconn := sqlx.NewDb(db, "pgx") func RegisterConnConfig(c *pgx.ConnConfig) string { return pgxDriver.registerConnConfig(c) }