Documentation
¶
Overview ¶
Package pg provides PostgreSQL (`database/sql`) wiring and helpers for go-service.
This package integrates the pgx PostgreSQL driver with go-service SQL wiring by:
- registering the pgx stdlib `database/sql` driver under the name "pg" with OpenTelemetry instrumentation, and
- providing an `Open` constructor that creates master/slave connection pools using the shared SQL driver helpers.
Configuration and enablement ¶
PostgreSQL configuration is optional. By convention, a nil `*pg.Config` (or nil embedded config) is treated as "disabled", and constructors such as `Open` return (nil, nil) when disabled.
Master/slave pools ¶
`Open` resolves master and replica DSNs from configuration (DSNs are expressed as go-service "source strings"), connects using a master/slave pool abstraction, applies pool settings (max lifetime/open/idle), and registers OpenTelemetry DB stats metrics.
Start with `Config`, `Register`, and `Open`.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Module = di.Module( di.Register(Register), di.Constructor(Open), )
Module wires PostgreSQL (`database/sql`) support into Fx/Dig.
It registers the pgx stdlib driver under the name "pg" (with OpenTelemetry instrumentation) and provides a constructor that opens master/slave connection pools.
Provided components:
- Registration: `Register`
- Constructor: `Open`
Disabled behavior: `Open` returns (nil, nil) when PostgreSQL configuration is disabled.
Functions ¶
func Open ¶
Open opens PostgreSQL master/slave connection pools.
Disabled behavior: if cfg is nil/disabled, Open returns (nil, nil).
Enabled behavior: Open delegates to the shared SQL driver helper to:
- resolve master and replica DSNs (expressed as go-service "source strings"),
- connect using the previously registered driver name "pg",
- register OpenTelemetry DB stats metrics, and
- apply connection pool limits/lifetime.
The returned pools are closed on Fx lifecycle stop (via an OnStop hook).
func Register ¶
func Register()
Register registers the pgx stdlib `database/sql` driver under the name "pg".
The registration is performed via `database/sql/driver.Register`, which wraps the underlying driver with OpenTelemetry instrumentation (via otelsql.WrapDriver). The returned error from registration is intentionally ignored.
Register is typically called during process initialization via DI wiring (see `pg.Module`).
Types ¶
type Config ¶
Config contains PostgreSQL SQL database configuration.
It embeds `database/sql/config`.Config to reuse common `database/sql` pool settings and DSN configuration.
Optional pointers and "enabled" semantics ¶
This type is intentionally optional. By convention across go-service configuration types, a nil *Config is treated as "PostgreSQL disabled". The embedded `*config.Config` is also optional; IsEnabled returns true only when both the outer *Config and the embedded *config.Config are non-nil.
This allows services to omit either `pg:` or the embedded fields entirely to disable PostgreSQL wiring.