Documentation
¶
Overview ¶
Package postgres connects rio to PostgreSQL through pgx's database/sql adapter or rio's native execution path.
Index ¶
- func AfterConnect(_ context.Context, conn *pgx.Conn) error
- 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New wraps an existing *sql.DB with the Postgres dialect and error translator. The caller must ensure standard_conforming_strings is on. Options apply last; a supplied translator replaces the default.
func NewFromPool ¶ added in v0.3.0
NewFromPool wraps a caller-built pgxpool.Pool and takes ownership: closing the rio DB closes the pool. The caller must ensure standard_conforming_strings is on.
func NewNativeFromPool ¶ added in v0.3.0
NewNativeFromPool wraps a caller-built pgxpool.Pool for native execution and takes ownership: closing the rio DB closes the pool. The caller must ensure standard_conforming_strings is on.
Example ¶
NewNativeFromPool takes ownership of a pool the caller configured.
package main
import (
"context"
"log"
"github.com/go-rio/postgres"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
ctx := context.Background()
pool, err := pgxpool.New(ctx, "postgres://user:pass@localhost:5432/app")
if err != nil {
log.Fatal(err)
}
db := postgres.NewNativeFromPool(pool)
defer db.Close() // closes the pool
}
Output:
func Open ¶
Open validates a pgx DSN and returns a rio DB using pgx's database/sql adapter. It does not connect; ping and configure the pool via db.Unwrap(). URL and keyword/value DSNs are accepted; standard_conforming_strings=off is rejected, including via PGOPTIONS.
Example ¶
package main
import (
"context"
"log"
"github.com/go-rio/postgres"
"github.com/go-rio/rio"
)
type User struct {
ID int64
Email string
Age int
}
func main() {
db, err := postgres.Open("postgres://user:pass@localhost:5432/app")
if err != nil {
log.Fatal(err)
}
defer db.Close()
users, err := rio.From[User]().Where("age > ?", 18).All(context.Background(), db)
if err != nil {
log.Fatal(err)
}
log.Printf("%d adults", len(users))
}
Output:
func OpenNative ¶ added in v0.3.0
OpenNative validates a pgxpool DSN and returns a rio DB that executes queries directly through pgx. It connects lazily (ping via PoolOf) and, like Open, rejects standard_conforming_strings=off.
rio.WithStmtCache is unsupported; use pgx's default_query_exec_mode. Tx.Unwrap returns nil — use TxOf. db.Unwrap returns a database/sql view; configure pooling through pgxpool, never the view.
Example ¶
OpenNative executes through pgx directly: preload layers batch into one round trip and explicit-key InsertAll streams over COPY.
package main
import (
"context"
"log"
"github.com/go-rio/postgres"
"github.com/go-rio/rio"
)
type User struct {
ID int64
Email string
Age int
}
func main() {
ctx := context.Background()
db, err := postgres.OpenNative(ctx, "postgres://user:pass@localhost:5432/app")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows := []User{{ID: 1, Email: "a@example.com"}, {ID: 2, Email: "b@example.com"}}
if err := rio.InsertAll(ctx, db, rows); err != nil {
log.Fatal(err)
}
}
Output:
func OpenPool ¶ added in v0.3.0
OpenPool validates a pgxpool DSN and returns a rio DB using pgx's database/sql adapter over that pool. It connects lazily (ping via PoolOf) and, like Open, rejects standard_conforming_strings=off. Configure pooling through the DSN, never db.Unwrap(). Closing the rio DB closes the pool and may block until acquired connections return.
Example ¶
OpenPool keeps the database/sql query path while pgxpool owns the connections; PoolOf reaches the pool for pings and statistics.
package main
import (
"context"
"log"
"github.com/go-rio/postgres"
)
func main() {
ctx := context.Background()
db, err := postgres.OpenPool(ctx, "postgres://user:pass@localhost:5432/app?pool_max_conns=10")
if err != nil {
log.Fatal(err)
}
defer db.Close()
if err := postgres.PoolOf(db).Ping(ctx); err != nil {
log.Fatal(err)
}
}
Output:
func PoolOf ¶ added in v0.3.0
PoolOf returns the pool managed by a pool-backed or native rio DB. It returns nil for other constructions and for a nil DB.
func TxOf ¶ added in v0.3.0
TxOf returns the pgx transaction behind a native rio transaction. It returns nil for other constructions and for a nil transaction. Nested savepoints share the root pgx transaction.
Example ¶
TxOf exposes the pgx transaction for session settings the rio API does not cover.
package main
import (
"context"
"log"
"github.com/go-rio/postgres"
"github.com/go-rio/rio"
)
type User struct {
ID int64
Email string
Age int
}
func main() {
ctx := context.Background()
db, err := postgres.OpenNative(ctx, "postgres://user:pass@localhost:5432/app")
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = db.Tx(ctx, func(tx *rio.Tx) error {
if _, err := postgres.TxOf(tx).Exec(ctx, "SET LOCAL statement_timeout = '5s'"); err != nil {
return err
}
_, err := rio.From[User]().Where("id = ?", 1).UpdateAll(ctx, tx, rio.Set{"age": 31})
return err
})
if err != nil {
log.Fatal(err)
}
}
Output:
Types ¶
This section is empty.