Documentation
¶
Index ¶
- Constants
- Variables
- func ContextWithRunner(ctx context.Context, r Runner) context.Context
- func IsForeignKeyViolation(err error) bool
- func IsUniqueViolation(err error) bool
- type AdvancedTxManager
- type Client
- func (c *Client) Close()
- func (c *Client) RunnerFromPool() Runner
- func (c *Client) WithSavepoint(ctx context.Context, fn func(ctx context.Context) error) error
- func (c *Client) WithSerializable(ctx context.Context, maxRetries int, fn func(ctx context.Context) error) error
- func (c *Client) WithSerializableRO(ctx context.Context, deferrable bool, fn func(ctx context.Context) error) error
- func (c *Client) WithTx(ctx context.Context, fn func(ctx context.Context) error) (err error)
- func (c *Client) WithTxExplicit(ctx context.Context, fn func(run Runner) error) error
- func (c *Client) WithTxOpts(ctx context.Context, cfg TxConfig, fn func(ctx context.Context) error) (err error)
- func (c *Client) WithTxRO(ctx context.Context, fn func(ctx context.Context) error) error
- func (c *Client) WithTxROExplicit(ctx context.Context, fn func(run Runner) error) error
- type Config
- type ConstraintInfo
- type DBConfig
- type Runner
- type TxConfig
- type TxManager
Constants ¶
const ( SQLStateUniqueViolation = "23505" SQLStateForeignKeyViolation = "23503" SQLStateNotNullViolation = "23502" )
SQLSTATE codes used by this package.
Variables ¶
var ErrRunnerMissingInContext = errors.New("postgres: no Runner in context (outside transaction?)")
Functions ¶
func ContextWithRunner ¶
ContextWithRunner stores a Runner in the context.
func IsForeignKeyViolation ¶
Types ¶
type AdvancedTxManager ¶
type AdvancedTxManager interface {
TxManager
WithTxOpts(ctx context.Context, cfg TxConfig, fn func(ctx context.Context) error) error
WithSerializable(ctx context.Context, maxRetries int, fn func(ctx context.Context) error) error
WithSerializableRO(ctx context.Context, deferrable bool, fn func(ctx context.Context) error) error
WithSavepoint(ctx context.Context, fn func(ctx context.Context) error) error
}
AdvancedTxManager is an extended contract for advanced scenarios (retryable serializable, savepoints, explicit tx options).
type Client ¶
func OpenWithDBConfig ¶
OpenWithDBConfig creates a client from structured DBConfig (host/port/user/...) and maps pool options to Config.
func (*Client) RunnerFromPool ¶
RunnerFromPool returns pool-backed Runner (outside transaction).
func (*Client) WithSavepoint ¶
WithSavepoint creates SAVEPOINT when already in tx, otherwise starts regular tx.
func (*Client) WithSerializable ¶
func (c *Client) WithSerializable(ctx context.Context, maxRetries int, fn func(ctx context.Context) error) error
WithSerializable runs SERIALIZABLE tx with retries for 40001/40P01 and ctx awareness.
func (*Client) WithSerializableRO ¶
func (c *Client) WithSerializableRO(ctx context.Context, deferrable bool, fn func(ctx context.Context) error) error
Convenience helper: SERIALIZABLE + ReadOnly + optional DEFERRABLE.
func (*Client) WithTx ¶
WithTx runs panic-safe read-write transaction with default options. Runner is available via RunnerFromContext(ctx) / MustRunnerFromContext(ctx).
func (*Client) WithTxExplicit ¶
WithTxExplicit is backward-compatible variant with explicit runner callback arg.
func (*Client) WithTxOpts ¶
func (c *Client) WithTxOpts(ctx context.Context, cfg TxConfig, fn func(ctx context.Context) error) (err error)
WithTxOpts runs transaction with options, panic-safe commit/rollback, and optional SET LOCAL timeouts.
type Config ¶
type Config struct {
URL string // postgres://user:pass@host:port/dbname?sslmode=disable
Params map[string]string // extra URL params (override query)
// Pool options
MaxConns int32
MinConns int32
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
HealthCheckPeriod time.Duration
}
Config is a high-level connection config based on URL. Useful for complex DSN cases and backward compatibility.
type ConstraintInfo ¶
type ConstraintInfo struct {
Code string // SQLSTATE (e.g. 23505)
Name string // constraint name from PG
Schema string // optional: pgErr.SchemaName
Table string // optional: pgErr.TableName
Detail string // optional: pgErr.Detail
IsUnique bool
}
func Constraint ¶
func Constraint(err error) (ConstraintInfo, bool)
type DBConfig ¶
type DBConfig struct {
Host string
Port string
User string
Password string
DBName string
SSLMode string
MaxOpenConns int // max connections in pool
MaxIdleConns int // minimum warm connections in pool
ConnMaxLifetime time.Duration // max connection lifetime
ConnMaxIdleTime time.Duration // max idle lifetime
}
DBConfig is a low-level connection config (host/port/...). It is convenient for apps: load from env and pass to the library.
type Runner ¶
type Runner interface {
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}
Runner is a shared interface for pool and transaction.
func MustRunnerFromContext ¶
MustRunnerFromContext extracts the Runner or panics.
func RunnerFromContext ¶
RunnerFromContext extracts the Runner from the context. Falls back to pool runner if no transaction is active.
type TxConfig ¶
type TxConfig struct {
Iso pgx.TxIsoLevel // default: ReadCommitted
ReadOnly bool // default: false
Deferrable bool // valid only for SERIALIZABLE; meaningful for read-only
// Local timeouts for current TX (SET LOCAL ...).
StatementTimeout time.Duration // statement timeout
IdleInTransactionTimeout time.Duration // idle_in_transaction_session_timeout
}
TxConfig contains optional transaction settings.
type TxManager ¶
type TxManager interface {
WithTx(ctx context.Context, fn func(ctx context.Context) error) error
WithTxRO(ctx context.Context, fn func(ctx context.Context) error) error
}
TxManager is a minimal transaction management abstraction. Higher layers can depend on this interface instead of *Client.