Documentation
¶
Index ¶
- func WithPrimary(ctx context.Context) context.Context
- func WithReplicaPolicy(ctx context.Context, name string) context.Context
- func WithReplicaRead(ctx context.Context, opts ReplicaReadOptions) context.Context
- type DB
- func (db *DB) Aggregate(ctx context.Context, spec runtime.AggregateSpec) pgx.Row
- func (db *DB) Close()
- func (db *DB) Query(ctx context.Context, table, sql string, args ...any) (pgx.Rows, error)
- func (db *DB) QueryRow(ctx context.Context, table, sql string, args ...any) pgx.Row
- func (db *DB) Reader(ctx context.Context) Pool
- func (db *DB) Select(ctx context.Context, spec runtime.SelectSpec) (pgx.Rows, error)
- func (db *DB) SetReplicaHealthInterval(interval time.Duration)
- func (db *DB) UseObserver(observer runtime.QueryObserver)
- func (db *DB) UseReplicaHealthCheck(check ReplicaHealthCheck)
- func (db *DB) UseReplicaPolicies(defaultPolicy string, policies map[string]ReplicaReadOptions)
- func (db *DB) Writer() Pool
- type Option
- func WithHealthCheckPeriod(d time.Duration) Option
- func WithMaxConnIdleTime(d time.Duration) Option
- func WithMaxConnLifetime(d time.Duration) Option
- func WithMaxConns(n int32) Option
- func WithMinConns(n int32) Option
- func WithPoolConfig(pc PoolConfig) Option
- func WithTracer(tracer tracing.Tracer) Option
- type Pool
- type PoolConfig
- type ReplicaConfig
- type ReplicaHealthCheck
- type ReplicaHealthReport
- type ReplicaReadOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithPrimary ¶
WithPrimary ensures the primary writer connection is used even when replica policies would normally route the query elsewhere.
func WithReplicaPolicy ¶
WithReplicaPolicy annotates the context with a routing policy name. The driver resolves the policy against the set configured via UseReplicaPolicies.
func WithReplicaRead ¶
func WithReplicaRead(ctx context.Context, opts ReplicaReadOptions) context.Context
WithReplicaRead requests that read operations attempt to use a replica according to the provided options.
Types ¶
type DB ¶
type DB struct {
Pool Pool
Observer runtime.QueryObserver
// contains filtered or unexported fields
}
DB manages writer and replica pools plus observability hooks.
func ConnectCluster ¶
func ConnectCluster(ctx context.Context, url string, replicas []ReplicaConfig, opts ...Option) (*DB, error)
ConnectCluster initialises a primary pool plus the provided replicas.
func (*DB) Aggregate ¶
Aggregate issues an aggregate query generated from runtime specs against the appropriate pool.
func (*DB) QueryRow ¶
QueryRow routes read statements returning a single row through the replica routing machinery.
func (*DB) Reader ¶
Reader returns the pool selected for read operations after applying routing policies.
func (*DB) Select ¶
Select issues a SELECT generated from runtime specs against the appropriate pool.
func (*DB) SetReplicaHealthInterval ¶
SetReplicaHealthInterval adjusts the interval between health checks. Zero or negative values force probes before every read attempt.
func (*DB) UseObserver ¶
func (db *DB) UseObserver(observer runtime.QueryObserver)
UseObserver attaches a query observer to the database handle.
func (*DB) UseReplicaHealthCheck ¶
func (db *DB) UseReplicaHealthCheck(check ReplicaHealthCheck)
UseReplicaHealthCheck overrides the health probe used for replicas.
func (*DB) UseReplicaPolicies ¶
func (db *DB) UseReplicaPolicies(defaultPolicy string, policies map[string]ReplicaReadOptions)
UseReplicaPolicies registers named replica routing policies. The default policy name is applied when reads do not explicitly opt-in via context.
type Option ¶
Option configures pgx connections.
func WithHealthCheckPeriod ¶
WithHealthCheckPeriod configures the background health check period.
func WithMaxConnIdleTime ¶
WithMaxConnIdleTime configures how long an idle connection may remain in the pool.
func WithMaxConnLifetime ¶
WithMaxConnLifetime configures the maximum connection lifetime.
func WithPoolConfig ¶
func WithPoolConfig(pc PoolConfig) Option
WithPoolConfig applies a group of pool settings derived from configuration.
func WithTracer ¶
WithTracer enables pgx tracing using the provided tracer abstraction.
type Pool ¶
type Pool interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
Close()
}
Pool exposes the subset of pgxpool behaviour required by generated clients.
type PoolConfig ¶
type PoolConfig struct {
MaxConns int32
MinConns int32
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
HealthCheckPeriod time.Duration
}
PoolConfig describes connection pool tuning knobs exposed via configuration.
type ReplicaConfig ¶
ReplicaConfig describes a read replica participating in the cluster.
type ReplicaHealthCheck ¶
type ReplicaHealthCheck func(ctx context.Context, pool Pool) (ReplicaHealthReport, error)
ReplicaHealthCheck probes a replica connection to determine whether it can satisfy reads.
type ReplicaHealthReport ¶
ReplicaHealthReport captures the result of a health probe executed against a replica.
type ReplicaReadOptions ¶
ReplicaReadOptions influence how reads target replicas.
func (ReplicaReadOptions) AllowFallback ¶
func (opts ReplicaReadOptions) AllowFallback() bool
AllowFallback reports whether the read should fall back to the primary when the replica errors.