dbpg

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 15

Documentation

Overview

Package dbpg provides PostgreSQL connection management with master-slave support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Array added in v0.0.12

func Array(a *[]string) any

Array returns an object that can be passed to Scan for []string.

Types

type DB

type DB struct {
	Master *sql.DB
	Slaves []*sql.DB
	// contains filtered or unexported fields
}

DB represents a database connection with master and slave nodes.

func New

func New(masterDSN string, slaveDSNs []string, opts *Options) (*DB, error)

New creates a new DB instance with master and slave connections.

func (*DB) BatchExec

func (db *DB) BatchExec(ctx context.Context, in <-chan string)

BatchExec executes multiple queries asynchronously in a batch.

func (*DB) BeginTx added in v0.0.11

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a transaction on the master database.

func (*DB) BeginTxWithRetry added in v0.0.11

func (db *DB) BeginTxWithRetry(
	ctx context.Context,
	strategy retry.Strategy,
	opts *sql.TxOptions,
) (*sql.Tx, error)

BeginTxWithRetry starts a transaction with a retry strategy on the master database.

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes a command on the master database.

func (*DB) ExecWithRetry

func (db *DB) ExecWithRetry(
	ctx context.Context,
	strategy retry.Strategy,
	query string,
	args ...interface{},
) (sql.Result, error)

ExecWithRetry executes a command with a retry strategy.

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext executes a query on a slave if available, otherwise on the master.

func (*DB) QueryRowContext added in v0.0.5

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext executes a single-row query on a slave if available, otherwise on the master.

func (*DB) QueryRowWithRetry added in v0.0.5

func (db *DB) QueryRowWithRetry(
	ctx context.Context,
	strategy retry.Strategy,
	query string,
	args ...interface{},
) (*sql.Row, error)

QueryRowWithRetry executes a single-row query with a retry strategy.

func (*DB) QueryWithRetry

func (db *DB) QueryWithRetry(
	ctx context.Context,
	strategy retry.Strategy,
	query string,
	args ...interface{},
) (*sql.Rows, error)

QueryWithRetry executes a query with a retry strategy.

func (*DB) WithTx added in v0.0.11

func (db *DB) WithTx(ctx context.Context, fn func(*sql.Tx) error) error

WithTx executes a function within a transaction on the master database.

func (*DB) WithTxWithRetry added in v0.0.11

func (db *DB) WithTxWithRetry(
	ctx context.Context,
	strategy retry.Strategy,
	fn func(*sql.Tx) error,
) error

WithTxWithRetry executes a function within a transaction on the master database with retry strategy.

type Options

type Options struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
}

Options defines database connection configuration options.

Directories

Path Synopsis
Package pgxdriver provides a robust PostgreSQL client built on pgx/v5, featuring connection retries with exponential backoff and jitter, integrated SQL query building via squirrel, and structured logging.
Package pgxdriver provides a robust PostgreSQL client built on pgx/v5, featuring connection retries with exponential backoff and jitter, integrated SQL query building via squirrel, and structured logging.
transaction
Package transaction provides a transaction manager for PostgreSQL with automatic retry on transient errors such as serialization failures, deadlocks, and connection issues.
Package transaction provides a transaction manager for PostgreSQL with automatic retry on transient errors such as serialization failures, deadlocks, and connection issues.

Jump to

Keyboard shortcuts

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