postgres

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 12 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.

README

grpcproc/saga/postgres

A saga.Store in PostgreSQL: the runs of grpcproc/saga kept in a table, so that a run outlasts the program that began it. A nested module, so that grpcproc itself depends on no database.

go get github.com/floatdrop/grpcproc/saga/postgres
db, err := sql.Open("pgx", dsn) // any driver for PostgreSQL
store, err := postgres.NewStore(db, "grpcproc_sagas")
err = store.Migrate(ctx) // or apply store.Schema() with your migrations
eng, err := saga.Start(node, saga.Config{Store: store}, orders)

It takes a *sql.DB the application opens, with the driver, credentials and pool it chooses. Its tests run on pgx and on lib/pq, on PostgreSQL 14; it needs 12 or later, for a generated column.

The tables

NewStore(db, "grpcproc_sagas") keeps the runs in grpcproc_sagas, a row per run, with the signals waiting on the row as a JSON array. A generated column says when a run is due, at once if it holds a signal its owner has not seen, and a partial index on it serves Claim. The name is lower case letters, digits and _, quoted, in the search_path's schema.

Migrate makes the table, or takes it on from where an earlier version of this module left it: grpcproc_sagas_schema keeps how many of its steps were taken. It runs in a transaction, under an advisory lock, so programs that start at once take turns, and on tables that are current it only reads, so a program that may not create tables can call it. Schema returns the same SQL, for an application that applies it with its own migrations; a release that changes the tables says in its notes what to apply.

Engines on several nodes, and programs that share the database, share the runs.

How it keeps the promises

Every change to a run is one statement on its row. A statement that changes a run waits for one that holds its row, and works on what that one wrote; Claim skips such a run instead, and takes it at its next poll.

  • Claim picks the due runs with FOR UPDATE SKIP LOCKED, so engines that claim at once never take one run twice and never wait for each other, and raises each one's epoch in the same statement.
  • Save is fenced by the epoch, and takes the signals it consumed out of the inbox as the row has it then: a signal that came while the owner worked is kept, and returned.
  • Signal numbers a signal and counts the inbox on the row, so signals sent at once get distinct numbers and MaxInbox holds. A run with no room is neither locked nor written, so senders that retry on a full inbox do not keep its owner from claiming it. The refusal says why: ErrNoRun, ErrEnded or ErrInboxFull, for a run that ended or filled while the signal waited for its row too.
  • Create of a run that is there returns it, though another Create made it a moment before.

IDs and saga names are ordered by their bytes, as Go orders strings and saga.Memory does, whatever the database's collation. A payload or data with no bytes reads back as nil through any driver.

An effect's Error, and an Otherwise's Cause, are kept with NUL bytes dropped and invalid UTF-8 replaced, since PostgreSQL text takes neither and a save refused for them would be refused every time. Saga names, IDs, event names and states are kept as they are, so they must be UTF-8 with no NUL: a run whose state prints otherwise cannot be saved.

Leases and due times are read on the database's clock, which every engine shares; the times an engine sets, a run's wake among them, are its own. A timer of an engine whose clock is set apart from the database's fires that much early or late. A lease is safe all the same: an engine counts its own lease from before it asked for it, by its own clock.

Versions are kept as bigint: a Definition.Version past math.MaxInt64 is refused by Create, Claim, Save and Signal.

Tests

go test -race ./...

The tests start a PostgreSQL 14 with embedded-postgres, which downloads it on the first run. They run sagatest.Store through two drivers, signals and claims, creates and saves at once, migrations, and a saga whose program is stopped in the middle of a step and which a new program, on a new node, finishes.

Documentation

Overview

Package postgres keeps the runs of grpcproc/saga in PostgreSQL: Store is a saga.Store, in one table, a row per run, its inbox on the row.

db, err := sql.Open("pgx", dsn) // any driver for PostgreSQL
store, err := postgres.NewStore(db, "grpcproc_sagas")
err = store.Migrate(ctx) // or apply store.Schema() with your migrations
eng, err := saga.Start(node, saga.Config{Store: store}, orders)

It takes a *sql.DB the application opens, with the driver, credentials and pool it chooses. Every change to a run is one statement on its row, so a run's state and its signals change together. A statement that changes a run waits for one that holds its row, and works on what that one wrote; Claim skips such a run instead. Create, Claim and Save return the row as they left it.

Leases and due times are read on the database's clock, which every engine shares; the times an engine sets, a run's Wake among them, are its own. A timer of an engine whose clock is set apart from the database's fires that much early or late; a lease is safe all the same, since an engine counts its own lease from before it asked for it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store is a saga.Store in PostgreSQL. Engines on several nodes, and programs that share the database, share the runs.

func NewStore

func NewStore(db *sql.DB, name string) (*Store, error)

NewStore returns the store kept in the table name of db. It does not touch the database: Migrate, or the application's own migrations applying Schema, creates the table.

func (*Store) Claim

func (s *Store) Claim(ctx context.Context, owner string, sagas map[string]uint64, ttl time.Duration, n int) ([]saga.Record, error)

Claim gives owner up to n runs that are due.

func (*Store) Create

func (s *Store) Create(ctx context.Context, r saga.Record) (saga.Record, bool, error)

Create adds r, unless its saga has a run with its ID: then it returns that run, and false.

func (*Store) Get

func (s *Store) Get(ctx context.Context, sagaName, id string) (saga.Record, bool, error)

Get returns a run, and whether there is one.

func (*Store) List

func (s *Store) List(ctx context.Context, sagaName string) ([]saga.Record, error)

List returns the runs of a saga, by ID.

func (*Store) Migrate

func (s *Store) Migrate(ctx context.Context) error

Migrate brings the store's tables to what this version of the package wants: it makes them, or takes them on from where an earlier version left them, by the version kept beside them. Programs that start at once may each call it: they take turns.

func (*Store) Renew

func (s *Store) Renew(ctx context.Context, sagaName, id string, epoch uint64, ttl time.Duration) error

Renew extends the lease of a run to ttl from now.

func (*Store) Resume

func (s *Store) Resume(ctx context.Context, sagaName, id string) error

Resume makes a Stuck run Active, due at once.

func (*Store) Save

func (s *Store) Save(ctx context.Context, r saga.Record) (saga.Record, error)

Save writes the state of a run its caller owns.

func (*Store) Schema

func (s *Store) Schema() string

Schema is the SQL that makes the store's tables as this version of the package wants them, each statement ending in a semicolon: for an application that applies it with its own migrations.

func (*Store) Signal

func (s *Store) Signal(ctx context.Context, sagaName, id string, sig saga.Signal) error

Signal adds sig to a run's inbox, with the next Seq.

Jump to

Keyboard shortcuts

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