pgsql

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

AdvisoryLock implements primitives to use PostgreSQL's advisory locks.

These locks can be used to pipeline concurrent access between multiple database sessions. Keep in mind, both [Lock] and [TryLock] are stackable, can be called multiple times on the same session, requiring the same amount of calls to [Unlock] to free up the lock:

   var l := NewAdvisoryLock(conn, 32)
   l.Lock(context.Background()) // lock does not exist previously in this session, so locks successfully
   l.Lock(context.Background()) // lock already exists, but this increments the lock

   l.Unlock() // lock is not freed yet, as it was locked twice
   l.Unlock() // only here lock is released

See https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS for more details on the specifics

of PostgreSQL advisory locks

Note: since pgxpool is used, the lock will keep a connection from the connection pool for himself; this connection is freed when the number of locked unlocks is achieved

Index

Constants

View Source
const (
	DefaultMinConns           = 2
	DefaultMaxConns           = 4
	DefaultConnLifeTimeSecond = 3600
	DefaultConnIdleTimeSecond = 1800
	DefaultHealthCheckSecond  = 60
	DefaultConnTimeoutSecond  = 5

	ErrEmptyDSN                   = utils.Error("Empty DSN")
	ErrNilConfig                  = utils.Error("Config is nil")
	ErrInvalidMinConns            = utils.Error("Invalid minConns")
	ErrInvalidMaxConns            = utils.Error("Invalid maxConns")
	ErrInvalidMinMaxConns         = utils.Error("minConns must be <= maxConns")
	ErrInvalidConnLifeTime        = utils.Error("connLifeTime must be >= 1")
	ErrInvalidConnIdleTime        = utils.Error("connIdleTime must be >= 1")
	ErrInvalidHealthCheckInterval = utils.Error("healthCheckInterval must be >= 1")
	ErrInvalidConnTimeout         = utils.Error("connTimeout must be >= 1")
)
View Source
const (
	// engine constants
	EngineSchema      = "public"
	MigrationTable    = "db_migration"
	EngineSchemaTable = EngineSchema + "." + MigrationTable

	MigrationLockId = 2343452349
)
View Source
const (
	SchemaDefault = "public"

	TblTypeTable        = "BASE TABLE"
	TblTypeView         = "VIEW"
	TblTypeForeignTable = "FOREIGN TABLE"
	TblTypeLocal        = "LOCAL TEMPORARY"
)

Variables

This section is empty.

Functions

func ForeignTableExists added in v0.1.3

func ForeignTableExists(db *pgxpool.Conn, ctx context.Context, tableName string, schema string) (bool, error)

ForeignTableExists returns true if specified foreign table exists

func GetServerVersion added in v0.1.3

func GetServerVersion(db *pgxpool.Conn, ctx context.Context) (string, error)

GetServerVersion fetch postgresql version

func NewClient

func NewClient(config *ClientConfig) (*db.SqlClient, error)

func NewClientX added in v0.1.1

func NewClientX(ctx context.Context, config *ClientConfig) (*pgx.Conn, error)

func NewMigrationManager added in v0.1.3

func NewMigrationManager(pool *pgxpool.Pool) migrations.Manager

func NewPool added in v0.1.1

func NewPool(ctx context.Context, config *PoolConfig) (*pgxpool.Pool, error)

func TableExists added in v0.1.3

func TableExists(db *pgxpool.Conn, ctx context.Context, tableName string, schema string) (bool, error)

TableExists returns true if specified table exists

func TableObjectExists added in v0.1.3

func TableObjectExists(db *pgxpool.Conn, ctx context.Context, tableType string, tableName string, schema string) (bool, error)

TableObjectExists checks if given table-like object exists

func ViewExists added in v0.1.3

func ViewExists(db *pgxpool.Conn, ctx context.Context, tableName string, schema string) (bool, error)

ViewExists returns true if specified view exists

Types

type AdvisoryLock added in v0.1.1

type AdvisoryLock interface {
	Lock(ctx context.Context) error
	TryLock(ctx context.Context) (bool, error)
	Unlock(ctx context.Context) error
}

func NewAdvisoryLock added in v0.1.1

func NewAdvisoryLock(pool *pgxpool.Pool, id int64) AdvisoryLock

type ClientConfig

type ClientConfig struct {
	DSN string `json:"dsn"`
}

func (ClientConfig) Validate

func (c ClientConfig) Validate() error

type PoolConfig added in v0.1.1

type PoolConfig struct {
	DSN      string `json:"dsn"`      // DSN database connection string
	MinConns int32  `json:"minConns"` // MinConns minimum number of pool connections
	MaxConns int32  `json:"maxConns"` // MaxConns max number of pool connections

	// ConnLifeTime is the duration in seconds since creation after which a connection will be automatically closed
	ConnLifeTime int `json:"connLifeTime"`
	// ConnIdleTime is the duration in seconds after which an idle connection will be automatically closed by the health check
	ConnIdleTime int `json:"connIdleTime"`
	// HealthCheckInterval is the duration in seconds between checks of the health of idle connections
	HealthCheckInterval int `json:"healthCheckInterval"`
	// ConnTimeout is the max duration in seconds of a database operation until timeout is reached
	ConnTimeout int `json:"connTimeout"`

	// optional method override
	BeforeConnect func(context.Context, *pgx.ConnConfig) error
	AfterConnect  func(context.Context, *pgx.Conn) error
	BeforeAcquire func(context.Context, *pgx.Conn) bool
	AfterRelease  func(*pgx.Conn) bool
	BeforeClose   func(*pgx.Conn)
}

func NewPoolConfig added in v0.1.1

func NewPoolConfig() *PoolConfig

func (PoolConfig) Validate added in v0.1.1

func (c PoolConfig) Validate() error

Jump to

Keyboard shortcuts

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