postgres

package
v0.5.13 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package postgres registers the PostgreSQL engine for a postgres:// DSN.

Blank-import it from the application entry point to link it:

import _ "github.com/shibukawa/popcornweb/database/postgres"

A TinyGo build needs -scheduler=threads. Under the cooperative scheduler a blocking socket call holds the whole runtime, so the driver's cancellation watcher never runs and a query outlives its deadline without reporting one.

Index

Constants

View Source
const Dialect = "postgres"

Dialect is the canonical engine name this package registers.

Variables

This section is empty.

Functions

func FromPool added in v0.5.13

func FromPool(pool *pgxpool.Pool) pwruntime.Connection

FromPool serves requests on a pool the caller built, as the connection pw.WithDatabase and testutil.WithDatabase accept.

The framework opens its own pools from configuration, which is the right shape for a deployed process and the wrong one for a test that starts a fresh database per test case: that test already holds the pool, and the database it points at exists nowhere in configuration. This hands that pool over on the same native path a configured PostgreSQL connection takes, so generated queries, pw.Transaction, and WithConn run on it unchanged.

pool, err := pgxpool.New(ctx, server.DSN())
...
app := testutil.TestRun(t, Handlers(), nil,
	testutil.WithDatabase(postgres.FromPool(pool)),
	testutil.WithMigrations("../migrations"))

The pool stays the caller's: the framework never closes it, so a pool shared by several test cases survives each one's shutdown. Its connection string is recorded as the connection's DataSource, which is what migration and seeding tooling dial for the database/sql handle they need. That is the string the pool's configuration was parsed from, so it has to be a URL, the form the framework's own DSNs take. If connection settings or DialFunc were changed after parsing, set DataSource to a URL that tooling can use to reach that same database. A custom dialer is not reconstructed. The group is left empty, which NewConnectionSet reads as the default group; set Group, ReadOnly, or Label on the result before passing it when the connection stands in for another.

func WithConn

func WithConn(ctx context.Context, fn func(*pgx.Conn) error) error

WithConn hands the pgx connection behind the effective PostgreSQL connection to fn, so pgx capabilities stay reachable without any pgx type entering the framework API.

The request-time path runs on a pgxpool rather than database/sql, so pw.DB reports no *sql.DB for a PostgreSQL group and sql.Conn.Raw — the escape hatch on the database/sql path — cannot be reached from a handler. This is that hatch for the native path. It serves batches, CopyFrom, and LISTEN/NOTIFY:

err := postgres.WithConn(ctx, func(conn *pgx.Conn) error {
	batch := &pgx.Batch{}
	for _, row := range rows {
		batch.Queue("INSERT INTO t (a) VALUES ($1)", row.A)
	}
	results := conn.SendBatch(ctx, batch)
	defer results.Close()
	for range rows {
		if _, err := results.Exec(); err != nil {
			return err
		}
	}
	return results.Close()
})

Inside pw.Transaction the callback receives the connection that transaction is executing on, so its work joins the transaction and rolls back with it. Outside one, a pooled connection is leased for the call and returned after. Either way nothing derived from the connection may outlive fn: rows must be read, and results closed, before it returns.

Work done here does not pass through the framework's instrumented executor, so it produces no query log entry and no span of its own. Wrap it in one:

ctx, span := pw.StartSpanKind(ctx, "insert-batch", pw.SpanKindClient)
defer span.End()

A group that is not PostgreSQL returns an error naming the dialect it found.

Types

This section is empty.

Jump to

Keyboard shortcuts

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