postgres

package
v0.3.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPgxUnsupported = errors.New("unsupported")

ErrPgxUnsupported is an error indicating that the operation is not supported by the pgx library.

Functions

func NewSqlOptions

func NewSqlOptions() *driver.SqlOptions

NewSqlOptions creates a new instance of SqlOptions with predefined configurations for postgres.

func Open

func Open(ctx context.Context, dsn string, options ...OpenOption) (db.ConnPool, error)

Open initializes a connection pool to a database using the provided DSN and optional configuration options.

Types

type OpenOption

type OpenOption func(options *pgxpool.Config)

OpenOption is a function type that applies modifications to a pgxpool.Config instance.

func WithAfterConnect

func WithAfterConnect(f func(context.Context, *pgx.Conn) error) OpenOption

WithAfterConnect is called after a connection is established, but before it is added to the pool.

func WithAfterRelease

func WithAfterRelease(f func(*pgx.Conn) bool) OpenOption

WithAfterRelease is called after a connection is released, but before it is returned to the pool. It must return true to return the connection to the pool or false to destroy the connection.

func WithBeforeAcquire

func WithBeforeAcquire(f func(context.Context, *pgx.Conn) bool) OpenOption

WithBeforeAcquire is called before a connection is acquired from the pool. It must return true to allow the acquisition or false to indicate that the connection should be destroyed and a different connection should be acquired.

func WithBeforeClose

func WithBeforeClose(f func(*pgx.Conn)) OpenOption

WithBeforeClose is called right before a connection is closed and removed from the pool.

func WithBeforeConnect

func WithBeforeConnect(f func(context.Context, *pgx.ConnConfig) error) OpenOption

WithBeforeConnect is called before a new connection is made. It is passed a copy of the underlying pgx.ConnConfig and will not impact any existing open connections.

func WithHealthCheckPeriod

func WithHealthCheckPeriod(d time.Duration) OpenOption

WithHealthCheckPeriod is the duration between checks of the health of idle connections.

func WithMaxConnIdleTime

func WithMaxConnIdleTime(d time.Duration) OpenOption

WithMaxConnIdleTime is the duration after which the health check will automatically close an idle connection.

func WithMaxConnLifetime

func WithMaxConnLifetime(d time.Duration) OpenOption

WithMaxConnLifetime duration since creation after which a connection will be automatically closed.

func WithMaxConnLifetimeJitter

func WithMaxConnLifetimeJitter(d time.Duration) OpenOption

WithMaxConnLifetimeJitter is the duration after MaxConnLifetime to randomly decide to close a connection. This helps prevent all connections from being closed at the exact same time, starving the pool.

func WithMaxConns

func WithMaxConns(n int32) OpenOption

WithMaxConns is the maximum size of the pool. The default is the greater of 4 or runtime.NumCPU().

func WithMinConns

func WithMinConns(n int32) OpenOption

WithMinConns is the minimum size of the pool. After the connection closes, the pool might dip below MinConns. A low number of MinConns might mean the pool is empty after MaxConnLifetime until the health check has a chance to create new connections.

func WithMinIdleConns

func WithMinIdleConns(n int32) OpenOption

WithMinIdleConns is the minimum number of idle connections in the pool. You can increase this to ensure that there are always idle connections available. This can help reduce tail latencies during request processing, as you can avoid the latency of establishing a new connection while handling requests. It is superior to MinConns for this purpose. Similar to MinConns, the pool might temporarily dip below MinIdleConns after the connection closes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL