pgtx

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pgtx 是 tx.Transactor 的 pgx 实现:事务句柄经 tx.With 藏进 ctx, 业务代码只见 context.Context。本包由框架装配注入(组合根 / 模块 Setup), 业务包只 import 接口面 appkit/tx;数据层(internal/postgres)经 From 取 DBTX——ctx 内有事务用事务,无则直接用连接池。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPool

func NewPool(ctx context.Context, dsn string, opts ...PoolOption) (*pgxpool.Pool, error)

NewPool 解析 DSN、应用选项并建池,随即 Ping 一次——配置错误与数据库不可达 都在启动期暴露(fail-fast),而不是留到第一条查询。

Types

type DB

type DB 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
}

DB 与 sqlc 生成代码的 DBTX 接口签名一致:pgx.Tx 与 *pgxpool.Pool 都满足它, sqlc Querier 拿它即可透明地跑在事务内或池上。

func From

func From(ctx context.Context, pool *pgxpool.Pool) DB

From 返回当前应使用的 DBTX:ctx 携带事务句柄(Do 之内)返回该 pgx.Tx, 否则返回 pool。句柄不是 pgx.Tx 时 panic 而非静默落回 pool——静默落回会把 本应在事务内的写悄悄漏出事务边界。

分区域域(NewRouted)的查询必须经 Do:search_path 是事务级的,From 直查 落在池连接的默认 search_path 上,表不存在即报错——失败响亮,不会静默 读写错误的分区,但分区域的业务查询一律包进事务是硬纪律。

type PoolOption

type PoolOption func(*pgxpool.Config)

PoolOption 配置连接池。

func WithAfterConnect

func WithAfterConnect(fn func(context.Context, *pgx.Conn) error) PoolOption

WithAfterConnect 注册连接建立后的回调(典型用途:连接级初始化,如经 otelpgx 装追踪、SET 本连接生效的 GUC)。 多次调用按注册顺序依次执行,任一回调出错该连接即建立失败。

func WithMaxConns

func WithMaxConns(n int32) PoolOption

WithMaxConns 设置连接池上限。按 DESIGN §8,连接池按数据库共享而非按模块独占。

type Transactor

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

Transactor 实现 tx.Transactor。

func New

func New(pool *pgxpool.Pool) *Transactor

New 构造 Transactor。

func NewRouted

func NewRouted(pool *pgxpool.Pool, route func(ctx context.Context) (string, error)) *Transactor

NewRouted 构造分区域 Transactor:每次 Do 在开启事务后调用 route 解析本次 落在哪个 schema,并 SET LOCAL search_path(事务结束自动还原,连接归还池 时是干净的)。route 通常从 callctx 的租户身份查组合根注入的分区映射。

返回错误即本次 Do 失败(事务回滚)——「查无分区」要让调用响亮地失败, 而不是静默落到默认 search_path 上把表不存在的错误甩给最里面那条查询。 返回空 schema 同样报错:想表达「不路由」就别用 NewRouted。

func (*Transactor) Do

func (t *Transactor) Do(ctx context.Context, fn func(ctx context.Context) error) error

Do 划定一个事务边界:ctx 无事务则在池上开新事务;已有事务(嵌套 Do)则在 其上开 savepoint。fn 返回错误或 panic 都会回滚(panic 回滚后继续抛出), 否则提交。fn 收到的 ctx 携带事务句柄,经 From 取到的 DBTX 即该事务。

Jump to

Keyboard shortcuts

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