pg

package
v2.298.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 6 Imported by: 0

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

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

func Open(lc di.Lifecycle, fs *os.FS, cfg *Config) (*mssqlx.DBs, error)

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

type Config struct {
	*config.Config `yaml:",inline" json:",inline" toml:",inline"`
}

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.

func (*Config) IsEnabled added in v2.115.0

func (c *Config) IsEnabled() bool

IsEnabled reports whether PostgreSQL configuration is present and enabled.

It returns true only when both the PostgreSQL wrapper config and the embedded shared SQL config are non-nil.

Jump to

Keyboard shortcuts

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