pgunit

package
v0.0.13-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pgunit runs Core's PostgreSQL transactions: pooled transactions for public and administrative work, and the execution owner's transactions on the connection that holds the database's execution lease.

Index

Constants

View Source
const ExecutionTimeout = 5 * time.Second

ExecutionTimeout bounds every leased operation, including its wait for the connection gate and for row locks. Code that groups several statements under one execution deadline uses it too.

Variables

View Source
var (
	ErrLeaseHeld   = errors.New("another execution service owns this database")
	ErrLeaseClosed = errors.New("execution lease is closed")
)
View Source
var ErrInvalidID = errors.New("nonzero UUID required")

ErrInvalidID reports a value that is not a nonzero UUID. Adapters translate it into their domain's invalid-input error.

Functions

func IsUnstorableText

func IsUnstorableText(err error) bool

IsUnstorableText reports PostgreSQL rejecting client text it cannot represent: U+0000 or invalid UTF-8 in a text parameter (22021), or a jsonb \u0000 escape (22P05). The rejected statement stores nothing, and writes sharing its transaction roll back. Adapters return textvalue.ErrUnstorable in its place and never keep the database error in the chain.

func LookupCursor

func LookupCursor(value string) string

LookupCursor resolves the cursor of a list whose unresolved cursor is a 404 (Agents, Sessions, Turns, Templates, Vaults and Credentials) like a path identifier: a value that cannot name a resource becomes an identifier that never exists, so the list follows exactly the missing-cursor path.

func ParseID

func ParseID(value string) (pgtype.UUID, error)

ParseID parses a resource identifier that must name a resource, such as a request-body reference. A value that is not a nonzero UUID is ErrInvalidID.

func PathID

func PathID(value string) pgtype.UUID

PathID parses a caller-supplied resource path identifier. A value that cannot name a resource resolves to an identifier that never exists, so the request follows exactly the path of a well-formed missing identifier, including validation order. Request-body references use ParseID; cursors use LookupCursor.

Types

type LargeObject

type LargeObject struct {
	OID    uint32
	Size   int64
	SHA256 string
}

LargeObject is stored content: its large object's OID, size and hex SHA-256 digest.

type LargeObjectWriter

type LargeObjectWriter struct {
	// contains filtered or unexported fields
}

LargeObjectWriter streams content into a new large object in bounded chunks and records its size and digest. The first failure is kept: every later Write returns it, and Err reports it.

func CreateLargeObject

func CreateLargeObject(ctx context.Context, tx pgx.Tx) (*LargeObjectWriter, error)

CreateLargeObject creates an empty large object in tx and opens it for writing. The object belongs to tx: it disappears when tx rolls back.

func (*LargeObjectWriter) Close

func (w *LargeObjectWriter) Close() (LargeObject, error)

Close closes the object after the content is complete and describes it.

func (*LargeObjectWriter) Err

func (w *LargeObjectWriter) Err() error

Err returns the first write failure, or nil.

func (*LargeObjectWriter) Write

func (w *LargeObjectWriter) Write(p []byte) (int, error)

type Lease

type Lease struct {
	// contains filtered or unexported fields
}

Lease owns the connection used for execution writes, not just election: the PostgreSQL advisory lock belongs to that session, so every execution write runs on it. Its gate serializes pgx operations on the connection; no daemon or model work holds the gate. Losing or closing the lease never falls back to a pooled connection.

func AcquireLease

func AcquireLease(ctx context.Context, pool *pgxpool.Pool) (*Lease, error)

AcquireLease takes the database's execution lease on a dedicated connection from pool. It enforces single-service ownership per database and fails with ErrLeaseHeld when another service owns it.

func (*Lease) CancelOperations

func (l *Lease) CancelOperations(ctx context.Context, cancel context.CancelFunc) error

CancelOperations cancels coordinator-owned contexts between leased operations. Cancelling an in-flight pgx operation can close the connection that owns the advisory lock, so cancel runs only while the gate is held and after the connection answers a ping. cancel must only invoke synchronous context cancel functions; it must not perform database, provider or wait work. Caller cancellation and operation deadlines keep their own semantics.

func (*Lease) CheckOwnership

func (l *Lease) CheckOwnership(ctx context.Context) error

CheckOwnership pings the leased connection, confirming that this service still owns the database before external work.

func (*Lease) Close

func (l *Lease) Close(ctx context.Context) error

Close releases the lease by closing its connection and waits, within ctx, for the driver's asynchronous cleanup. A later Close resumes that wait.

func (*Lease) Transaction

func (l *Lease) Transaction(ctx context.Context, apply func(context.Context, pgx.Tx) error) error

Transaction runs apply in a read committed transaction on the leased connection and commits only when apply returns nil. apply receives the context carrying the execution deadline.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool runs transactions on pooled connections. It grants no execution authority.

func NewPool

func NewPool(pool *pgxpool.Pool) *Pool

func (*Pool) Queries

func (p *Pool) Queries() *sqlc.Queries

Queries returns queries bound to the pool, outside any transaction. Use it only for a read that is a single statement and needs no transaction, such as a per-request key lookup, where Snapshot would add BEGIN and COMMIT round trips. A read of several statements uses Snapshot, and a write uses Transaction.

func (*Pool) Snapshot

func (p *Pool) Snapshot(ctx context.Context, apply func(context.Context, pgx.Tx) error) error

Snapshot runs apply in a read-only repeatable read transaction, so every statement reads the same snapshot.

func (*Pool) Transaction

func (p *Pool) Transaction(ctx context.Context, apply func(context.Context, pgx.Tx) error) error

Transaction runs apply in a read committed transaction and commits only when apply returns nil.

type Transactor

type Transactor interface {
	Transaction(ctx context.Context, apply func(context.Context, pgx.Tx) error) error
}

Transactor runs read committed transactions: a Pool on pooled connections, or the Lease on the execution connection.

Jump to

Keyboard shortcuts

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