Documentation
¶
Overview ¶
Package postgres provides PostgreSQL connection pool management with OpenTelemetry tracing, transaction orchestration (Unit of Work), and migration utilities.
Index ¶
- func HasTx(ctx context.Context) bool
- func Migrate(dsn string, migrations ...Migration) error
- type Client
- func (c *Client) Close() error
- func (c *Client) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, ...) (int64, error)
- func (c *Client) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
- func (c *Client) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
- func (c *Client) RegisterMetrics(m *telemetry.Meter) error
- func (c *Client) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
- func (c *Client) WithinTx(ctx context.Context, fn func(ctx context.Context) error) error
- type Config
- type Migration
- type Option
- type TxManager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
Client wraps a pgxpool.Pool, adding transaction lifecycle orchestration and metrics.
func (*Client) CopyFrom ¶ added in v0.1.2
func (c *Client) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
CopyFrom executes a copy operation within the active transaction in ctx if present, or falls back to the pool.
func (*Client) Exec ¶ added in v0.1.2
Exec automatically executes within the active transaction in ctx if present, or falls back to the pool.
func (*Client) Query ¶ added in v0.1.2
Query automatically executes within the active transaction in ctx if present, or falls back to the pool.
func (*Client) QueryRow ¶ added in v0.1.2
QueryRow automatically executes within the active transaction in ctx if present, or falls back to the pool.
func (*Client) RegisterMetrics ¶
RegisterMetrics exports connection pool statistics to Prometheus.
func (*Client) SendBatch ¶ added in v0.1.2
SendBatch sends a batch within the active transaction in ctx if present, or falls back to the pool.
func (*Client) WithinTx ¶ added in v0.1.2
WithinTx executes fn inside an atomic transaction. If fn returns an error or panics, the transaction is rolled back. If fn completes successfully (nil), the transaction is committed. Nested WithinTx calls safely reuse the parent transaction without deadlocks.
type Config ¶
type Config struct {
DSN string `toml:"dsn" env:"DSN" comment:"PostgreSQL database connection string"`
MaxOpenConns int `toml:"max_open_conns" env:"MAX_OPEN_CONNS" env-default:"25" comment:"Maximum open connections"`
MaxIdleConns int `toml:"max_idle_conns" env:"MAX_IDLE_CONNS" env-default:"5" comment:"Maximum idle connections"`
ConnMaxLifetime time.Duration `toml:"conn_max_lifetime" env:"CONN_MAX_LIFETIME" env-default:"15m" comment:"Maximum connection lifetime"`
ConnMaxIdleTime time.Duration `toml:"conn_max_idle_time" env:"CONN_MAX_IDLE_TIME" env-default:"5m" comment:"Maximum connection idle time"`
TxTimeout time.Duration `toml:"tx_timeout" env:"TX_TIMEOUT" env-default:"3s" comment:"Maximum transaction execution timeout"`
}
Config defines PostgreSQL connection pool parameters.
func (*Config) SetDefaults ¶ added in v0.1.3
func (c *Config) SetDefaults()