Documentation
¶
Overview ¶
Package postgres connects github.com/go-rio/rio to PostgreSQL through the pgx driver — via its database/sql adapter (Open, OpenPool) or fully natively (OpenNative, the fastest read path; see the README's tier table).
The package is deliberately thin: it constructs a *rio.DB with the built-in rio.Postgres dialect, installs a precise error translator that maps *pgconn.PgError values onto rio's sentinel errors, keeps the connection settings honest about standard_conforming_strings, and adapts pgx to rio's native-channel SPI. All SQL grammar lives in the rio core; this module never shapes a query.
Index ¶
- func New(db *sql.DB, opts ...rio.Option) *rio.DB
- func NewFromPool(pool *pgxpool.Pool, opts ...rio.Option) *rio.DB
- func NewNativeFromPool(pool *pgxpool.Pool, opts ...rio.Option) *rio.DB
- func Open(dsn string, opts ...rio.Option) (*rio.DB, error)
- func OpenNative(ctx context.Context, dsn string, opts ...rio.Option) (*rio.DB, error)
- func OpenPool(ctx context.Context, dsn string, opts ...rio.Option) (*rio.DB, error)
- func PoolOf(db *rio.DB) *pgxpool.Pool
- func TxOf(tx *rio.Tx) pgx.Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New wraps an existing *sql.DB in a *rio.DB with the Postgres dialect and this package's error translator. Use it when you bring your own pool: a *sql.DB you tuned yourself, or one derived from a pgxpool.Pool via stdlib.OpenDBFromPool.
New performs no connection hygiene — the pool is the caller's; make sure its sessions run with standard_conforming_strings on (the server default since PostgreSQL 9.1), or rio's placeholder rewriting can disagree with the server's lexing (see Open).
Options are applied after the translator, so rio.WithErrorTranslator in opts replaces this package's translation if you need to.
func NewFromPool ¶ added in v0.3.0
NewFromPool wraps a caller-built pgxpool.Pool in a *rio.DB, for pools that need a custom pgxpool.Config — tracers, AfterConnect hooks, MinConns, or a non-default query exec mode on the ConnConfig. Everything else matches OpenPool, including the closing contract: like New taking over the *sql.DB's Close, closing the rio.DB closes the pool you passed in (blocking until acquired connections are returned; your own pool.Close() afterwards is a harmless no-op). Keep the pool out of NewFromPool if it must outlive the rio.DB.
NewFromPool performs no connection hygiene — the pool is the caller's; make sure its sessions run with standard_conforming_strings on (the server default since PostgreSQL 9.1), or rio's placeholder rewriting can disagree with the server's lexing (see Open).
func NewNativeFromPool ¶ added in v0.3.0
NewNativeFromPool wraps a caller-built pgxpool.Pool in a native-channel *rio.DB, for pools that need a custom pgxpool.Config — tracers, AfterConnect hooks, MinConns, or a non-default QueryExecMode on the ConnConfig. Everything else matches OpenNative, including the closing contract: closing the rio.DB closes the pool you passed in. Keep the pool out of NewNativeFromPool if it must outlive the rio.DB.
NewNativeFromPool performs no connection hygiene — the pool is the caller's; make sure its sessions run with standard_conforming_strings on (the server default since PostgreSQL 9.1), or rio's placeholder rewriting can disagree with the server's lexing (see Open).
func Open ¶
Open opens a PostgreSQL database via pgx's database/sql adapter and wraps it in a *rio.DB. The DSN is handed to pgx untouched, so both URL form (postgres://user:pass@host:5432/app) and keyword/value form (host=... user=... dbname=...) work, along with every pgx runtime parameter — except one.
rio rewrites ? placeholders by lexing the SQL with standard_conforming_strings on, the server default since PostgreSQL 9.1: a backslash inside a '...' literal is an ordinary character. A session running with the setting off lexes those literals differently — backslash escapes again — so the server could disagree with rio about which ? are placeholders. Open therefore rejects a configuration that turns the setting off, whether spelled as a runtime parameter (standard_conforming_strings=off) or inside the options startup parameter (options=-c standard_conforming_strings=off — including one pgx inherits from the PGOPTIONS environment variable). An explicit on passes through, and when the setting is never mentioned nothing is injected: Open never connects, so it cannot see the server's value. If your server turns the setting off globally, turn it back on for rio's connections in the DSN — the README shows a paste-ready example.
Open validates the DSN eagerly — pgx's database/sql adapter would otherwise surface a malformed DSN on the first query — but it does not connect; ping the underlying pool (db.Unwrap().PingContext) to verify connectivity. Pool tuning also happens on the *sql.DB returned by Unwrap — rio never replaces or configures the connection pool.
func OpenNative ¶ added in v0.3.0
OpenNative builds a pgxpool.Pool from the DSN and wraps it in a *rio.DB that executes through pgx natively — no database/sql layer on the query path. Every rio semantic is unchanged: same rendered SQL, same scanning rules, same errors, same hooks, same savepoints. What changes is the cost: the driver.Value boxing tax is gone, so the read path allocates a fraction of the stdlib channel's count (see the README's tier table for measured numbers).
The DSN accepts everything OpenPool's does, including pgxpool's pool_* parameters, and rejects standard_conforming_strings=off exactly as in Open. Query execution mode is pgx's own default (QueryExecModeCacheStatement, automatic per-connection statement caching); tune it through the DSN parameter default_query_exec_mode — behind an old transaction-pooling PgBouncer, set default_query_exec_mode=exec (the README has the matrix). Like the other constructors, OpenNative validates eagerly but does not connect; use PoolOf(db).Ping(ctx) to verify connectivity.
Two public API differences against the stdlib channels, both loud: rio.WithStmtCache panics at construction (statement caching belongs to pgx's exec mode here), and Tx.Unwrap returns nil inside transactions (no *sql.Tx exists) — use TxOf for the pgx.Tx. db.Unwrap() still works: it returns a database/sql view over the same pool for pool-agnostic helpers (pings, migrations); never tune pooling on the view.
Closing: db.Close() closes the view and then the pool, blocking until acquired connections are returned. PoolOf returns the pool for CopyFrom, LISTEN/NOTIFY, Stat, and friends.
func OpenPool ¶ added in v0.3.0
OpenPool builds a pgxpool.Pool from the DSN and wraps it in a *rio.DB whose queries run through pgx's database/sql adapter over that pool. Query semantics are exactly Open's — same SQL, same scanning, same errors; what changes is who manages connections. pgxpool brings health checks, connection lifetime and idle caps, the AfterConnect hook, and Stat() metrics; measured against Open the read path is performance-neutral, so the reason to choose OpenPool is pool semantics, not speed.
The DSN accepts everything Open's does plus pgxpool's pool_* parameters (pool_max_conns, pool_min_conns, pool_max_conn_lifetime, pool_max_conn_idle_time, pool_health_check_period). A configuration that turns standard_conforming_strings off is rejected exactly as in Open. Like Open, OpenPool validates eagerly but does not connect — pgxpool connects lazily; use PoolOf(db).Ping(ctx) to verify connectivity.
PoolOf returns the pool behind the *rio.DB: pool statistics, Ping, and pgx-native abilities such as CopyFrom and LISTEN all go through it. For a pool built from your own pgxpool.Config (tracers, AfterConnect, a custom query exec mode), use NewFromPool.
Closing: db.Close() closes both the database/sql view and the pool, blocking until acquired connections are returned (pgxpool.Close semantics); closing the pool again via PoolOf is a harmless no-op. If you close the pool first instead, the view's queries fail with pgxpool's "closed pool" error, and db.Close() remains safe. Do not call SetMaxOpenConns or SetMaxIdleConns on db.Unwrap() — connections belong to the pgxpool configuration, and the view deliberately keeps zero idle database/sql connections (pgx's documented requirement, so an idle view connection never pins a pool connection and starves direct pool users).
func PoolOf ¶ added in v0.3.0
PoolOf returns the pgxpool.Pool behind a *rio.DB built by OpenPool, NewFromPool, OpenNative, or NewNativeFromPool, and nil for every other construction (Open and New manage no pool). It is the door to what the pool alone can do: Ping, Stat, AcquireFunc, CopyFrom, LISTEN/NOTIFY.
func TxOf ¶ added in v0.3.0
TxOf returns the pgx.Tx behind a native-channel *rio.Tx — the door to pgx-only abilities inside a transaction (CopyFrom, LISTEN) — and nil for every other construction (on the stdlib channels use tx.Unwrap, which carries the *sql.Tx). Savepoint-nested Tx values share the root transaction's pgx.Tx, exactly as Unwrap shares the *sql.Tx.
Types ¶
This section is empty.