cluster

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

func NewFromConfigs

func NewFromConfigs(config []*pgxpool.Config, opts ...Option) (*Cluster, error)

NewFromConfigs concurrently opens each underlying physical db. first being used as the primary and the rest as replica.

func Open

func Open(dsn []string, opts ...Option) (*Cluster, error)

Open concurrently opens each underlying physical db. DSN must be valid according to https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING first being used as the primary and the rest as replica.

func (*Cluster) Close

func (conn *Cluster) Close() error

Close closes all physical databases concurrently, releasing any open resources.

func (*Cluster) Exec

func (conn *Cluster) Exec(ctx context.Context, sql string, args ...any) (int64, error)

Exec executes a query on primary without returning any rows and return affected rows.

func (*Cluster) Get

func (conn *Cluster) Get(ctx context.Context, dst any, sql string, args ...any) error

Get retrieve one row. Get uses a replica by default. See Querier.Get for details.

func (*Cluster) Ping

func (conn *Cluster) Ping(ctx context.Context) error

Ping verifies if a connection to each physical database is still alive, establishing a connection if necessary.

func (*Cluster) Primary

func (conn *Cluster) Primary() conn.Querier

Primary returns the primary physical database.

func (*Cluster) Replica

func (conn *Cluster) Replica() conn.Querier

Replica returns one of the replica databases.

func (*Cluster) ScanAPI

func (conn *Cluster) ScanAPI() *pgxscan.API

func (*Cluster) Select

func (conn *Cluster) Select(ctx context.Context, dst any, sql string, args ...any) error

Select multiple records. Select uses a replica by default. See Querier.Select for details.

func (*Cluster) Tx

func (conn *Cluster) Tx(ctx context.Context, f func(n conn.Querier) error, opts ...conn.TxOption) error

Tx starts a transaction on primary and calls f. See Querier.Tx for details.

type Conn

type Conn interface {
	Select(ctx context.Context, dst any, sql string, args ...any) error
	Get(ctx context.Context, dst any, sql string, args ...any) error
	Exec(ctx context.Context, sql string, args ...any) (int64, error)
	Tx(ctx context.Context, f func(n conn.Querier) error, opts ...conn.TxOption) error
	Primary() conn.Querier
	Replica() conn.Querier
	Ping(context.Context) error
	Close() error
}

Conn is a logical database with multiple underlying physical databases forming a single primary multiple replica topology. Reads(Get, Select) and writes(Exec, Tx) are automatically directed to the correct physical db.

type ConnPicker

type ConnPicker func(db Conn, sql string) conn.Querier

type Option

type Option func(*Options)

Option func.

func WithConnPicker

func WithConnPicker(picker ConnPicker) Option

WithConnPicker sets connection picker for Select and Get.

func WithScanAPI

func WithScanAPI(api *pgxscan.API) Option

WithScanAPI sets custom pgxscan api.

type Options

type Options struct {
	Picker  ConnPicker
	ScanAPI *pgxscan.API
}

Options for cluster.

Jump to

Keyboard shortcuts

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