PGX Pool PostgreSQL Query Timeouts
Drive PostgreSQL through a pgx connection pool with statement timeouts, health pings, and context-scoped transactions.
Code
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func newPool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
config, err := pgxpool.ParseConfig(databaseURL)
if err != nil {
return nil, fmt.Errorf("parse dsn: %w", err)
}
config.MaxConns = 20
config.MinConns = 4
config.MaxConnLifetime = 30 * time.Minute
config.MaxConnIdleTime = 5 * time.Minute
config.HealthCheckPeriod = 30 * time.Second
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
return nil, fmt.Errorf("create pool: %w", err)
}
pingCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err := pool.Ping(pingCtx); err != nil {
pool.Close()
return nil, fmt.Errorf("verify connectivity: %w", err)
}
return pool, nil
}
func withTx(ctx context.Context, pool *pgxpool.Pool, fn func(ctx context.Context) error) error {
tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("begin: %w", err)
}
defer tx.Rollback(ctx) // 提交后回滚是空操作,安全兜底
if err := fn(ctx); err != nil {
return err
}
return tx.Commit(ctx)
}
func main() {
pool, err := newPool(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Println(err)
return
}
defer pool.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var version string
if err := pool.QueryRow(ctx, "select version()").Scan(&version); err != nil {
fmt.Println("query failed:", err)
return
}
fmt.Printf("connected to: %.32s\n", version)
}How it works
PGX Pool PostgreSQL Query Timeouts keeps everything in the functions newPool, withTx and main.
Every fallible call checks err != nil (7 sites), so an error is handled where it happens rather than bubbling invisibly. The cleanup call is deferred to function exit — success, early return, and panic all covered.
Configuration comes from the environment — it reads DATABASE_URL, so the same code switches behavior between environments without edits.
What to watch for
- Dependency check:
github.com/jackc/pgx/v5/pgxpool— not built-in, must be present in the project. erroris part of the return tuple, so every caller decides what failure means.
Using it
The main signature reads func newPool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error)
Running the file prints labeled results such as query failed:, connected to: — each operation’s effect is visible in the output.
Launch the file; main takes over and steps through newPool.
Key statements
func withTx(ctx context.Context, pool *pgxpool.Pool, fn func(ctx context.Context) error) error {
tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("begin: %w", err)
}
defer tx.Rollback(ctx) // 提交后回滚是空操作,安全兜底
if err := fn(ctx); err != nil {
return err
}
return tx.Commit(ctx)
}
return nil, fmt.Errorf("verify connectivity: %w", err)
return nil, fmt.Errorf("create pool: %w", err)
Interactive Tools for This Topic
FAQ
- How are errors reported?
- By return value: each fallible call hands back an `error`, checked at 7 places here.
- What does this need from the package registry?
- These are real dependencies: `github.com/jackc/pgx/v5/pgxpool`. Without it the snippet will not load.
Report a problem with this exampleOpens a pre-filled issue on GitHub.