Documentation
¶
Overview ¶
Package postgres provides pgxpool-backed PostgreSQL access without requiring driver imports.
Index ¶
- Constants
- func Classify(err error) error
- type Client
- func (c *Client) AdvisoryLock(ctx context.Context, key int64) (func(context.Context) error, error)
- func (c *Client) Batch(ctx context.Context, ops []Operation) (out []Result, err error)
- func (c *Client) BeginTx(ctx context.Context, o TxOptions) (*Tx, error)
- func (c *Client) Close()
- func (c *Client) CopyFrom(ctx context.Context, table []string, columns []string, values [][]any) (int64, error)
- func (c *Client) Exec(ctx context.Context, q string, args ...any) (Result, error)
- func (c *Client) Get(ctx context.Context, dst any, q string, args ...any) error
- func (c *Client) Listen(ctx context.Context, channel string, handler func(string) error) error
- func (c *Client) Native() *pgxpool.Pool
- func (c *Client) Notify(ctx context.Context, channel, payload string) error
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Prepare(ctx context.Context, query string) (*Statement, error)
- func (c *Client) Query(ctx context.Context, q string, args ...any) (db.Rows, error)
- func (c *Client) QueryRow(ctx context.Context, q string, args ...any) db.Row
- func (c *Client) SQLDB() *sql.DB
- func (c *Client) Select(ctx context.Context, dst any, q string, args ...any) error
- func (c *Client) Stats() Stats
- func (c *Client) WithinTx(ctx context.Context, o TxOptions, fn func(*Tx) error) error
- type Cluster
- type ClusterConfig
- type Config
- type Consistency
- type IdempotencyStore
- func (s *IdempotencyStore) Begin(ctx context.Context, key, fingerprint string, lease time.Duration) (idempotency.Record, error)
- func (s *IdempotencyStore) CleanupExpired(ctx context.Context, limit int) (int64, error)
- func (s *IdempotencyStore) Complete(ctx context.Context, key, token string, status int, ttl time.Duration) error
- func (s *IdempotencyStore) Fail(ctx context.Context, key, token string, ttl time.Duration) error
- func (s *IdempotencyStore) Renew(ctx context.Context, key, token string, ttl time.Duration) error
- type NamedArgs
- type Operation
- type Result
- type Statement
- func (s *Statement) Close() error
- func (s *Statement) Exec(ctx context.Context, args ...any) (Result, error)
- func (s *Statement) Get(ctx context.Context, dest any, args ...any) error
- func (s *Statement) Native() *sql.Stmt
- func (s *Statement) Query(ctx context.Context, args ...any) (db.Rows, error)
- func (s *Statement) QueryRow(ctx context.Context, args ...any) db.Row
- func (s *Statement) Select(ctx context.Context, dest any, args ...any) error
- type Stats
- type Tx
- func (t *Tx) Commit(ctx context.Context) error
- func (t *Tx) Exec(ctx context.Context, q string, a ...any) (Result, error)
- func (t *Tx) Get(ctx context.Context, d any, q string, a ...any) error
- func (t *Tx) Native() pgx.Tx
- func (t *Tx) Query(ctx context.Context, q string, a ...any) (db.Rows, error)
- func (t *Tx) QueryRow(ctx context.Context, q string, a ...any) db.Row
- func (t *Tx) Rollback(ctx context.Context) error
- func (t *Tx) Select(ctx context.Context, d any, q string, a ...any) error
- func (t *Tx) WithinTx(ctx context.Context, fn func(*Tx) error) error
- type TxOptions
Constants ¶
const IdempotencySchema = `` /* 346-byte string literal not displayed */
IdempotencySchema must be applied explicitly by the application's migration system. No constructor runs DDL.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client owns its configured infrastructure client and supports concurrent operations.
func New ¶
New constructs and validates an explicitly owned instance. See package documentation for defaults and cleanup.
func (*Client) AdvisoryLock ¶
AdvisoryLock holds a session-level lock on a dedicated connection. Release is mandatory.
func (*Client) Batch ¶
Batch sends commands in one protocol batch; all commands execute in an implicit transaction.
func (*Client) Close ¶
func (c *Client) Close()
Close releases owned resources. See package documentation for shutdown ordering.
func (*Client) CopyFrom ¶
func (c *Client) CopyFrom(ctx context.Context, table []string, columns []string, values [][]any) (int64, error)
CopyFrom performs PostgreSQL COPY ingestion with safely quoted identifiers.
func (*Client) Listen ¶
Listen owns a dedicated pool connection until ctx ends. The callback must return promptly.
func (*Client) Native ¶
Native provides an explicit advanced driver escape hatch; preserve SDK ownership rules.
func (*Client) Prepare ¶
Prepare validates and prepares SQL on a pgx-backed connection. Normal queries also retain pgx's native statement cache.
func (*Client) QueryRow ¶
QueryRow returns a single-row scanner; call Scan to execute or observe its error.
func (*Client) SQLDB ¶
SQLDB exposes a database/sql bridge for advanced SQL integrations. Close the bridge before closing this Client.
type Cluster ¶
type Cluster struct {
// contains filtered or unexported fields
}
Cluster owns all configured clients. Writes/transactions belong on Writer; Reader is an explicit consistency choice.
func NewCluster ¶
func NewCluster(ctx context.Context, cfg ClusterConfig) (*Cluster, error)
NewCluster opens pools atomically, cleaning up previously opened pools on any initialization failure.
func (*Cluster) Close ¶
func (c *Cluster) Close()
Close closes replicas in reverse order, then the primary. Drain callers before closing.
func (*Cluster) Reader ¶
func (c *Cluster) Reader(consistency Consistency) (*Client, error)
Reader rotates replicas for Eventual, falling back to primary when none are configured. Strong always uses primary. Replication lag, concurrent writes and failover can invalidate read-after-write assumptions on replicas.
type ClusterConfig ¶
ClusterConfig configures independently owned primary/replica pools. Replicas are explicit and are never guessed from SQL.
type Config ¶
type Config struct {
Hooks db.Hooks
QueryLogger *slog.Logger
ReadOnly bool
AllowPlaintext bool
Observer instrument.Hook
URL string
MaxConnections, MinConnections int32
MaxLifetime, MaxIdleTime, ConnectTimeout, QueryTimeout time.Duration
TLS *tls.Config
ApplicationName string
StatementTimeout time.Duration
AfterConnect func(context.Context) error
}
Config contains explicit constructor settings; see package documentation for defaults.
type Consistency ¶
type Consistency int
Consistency selects primary freshness or potentially stale replica reads.
const ( Strong Consistency = iota Eventual )
type IdempotencyStore ¶
type IdempotencyStore struct {
// contains filtered or unexported fields
}
IdempotencyStore implements atomic durable ownership leases using the database server clock. Expired owners cannot complete; external effects still require authoritative unique business keys or fencing.
func NewIdempotencyStore ¶
func NewIdempotencyStore(c *Client) (*IdempotencyStore, error)
NewIdempotencyStore borrows an existing client. Close the client only after all operations have drained.
func (*IdempotencyStore) Begin ¶
func (s *IdempotencyStore) Begin(ctx context.Context, key, fingerprint string, lease time.Duration) (idempotency.Record, error)
Begin acquires a new/expired/failed lease using one conditional UPSERT. Active/completed conflicts retain their fingerprint.
func (*IdempotencyStore) CleanupExpired ¶
CleanupExpired deletes at most limit expired records without blocking active owners or other cleanup workers.
func (*IdempotencyStore) Complete ¶
func (s *IdempotencyStore) Complete(ctx context.Context, key, token string, status int, ttl time.Duration) error
Complete persists status metadata only for the current unexpired owner.
type NamedArgs ¶
NamedArgs explicitly opts into pgx's SQL-aware @name parameter binding. No SQL literals or comments are rewritten by the SDK. Do not mutate this map while an operation using it is running.
type Statement ¶
type Statement struct {
// contains filtered or unexported fields
}
Statement owns a database/sql prepared statement and a pgxpool bridge. It supports concurrent operations. Close it before closing the Client. PostgreSQL positional placeholders are used in prepared SQL.
func (*Statement) Exec ¶
Exec executes bound arguments. Command is empty because database/sql does not expose PostgreSQL command tags.
func (*Statement) Native ¶
Native is an explicit escape hatch; the statement remains owned by this handle.
type Stats ¶
type Stats struct {
Total, Idle, Acquired, Maximum int32
AcquireCount int64
AcquireDuration time.Duration
}
Stats contains a snapshot of operational counters.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx owns a transaction; do not share it concurrently.
func (*Tx) Native ¶
Native provides an explicit advanced driver escape hatch; preserve SDK ownership rules.
func (*Tx) QueryRow ¶
QueryRow returns a single-row scanner; call Scan to execute or observe its error.