Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package postgres stores independent scoped logs in a host-owned PostgreSQL schema. Append and SQL source triggers queue facts in the mutation transaction; a deferred trigger assigns commit-safe positions after locking all its scopes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, pool *pgxpool.Pool, schema string) error

Migrate installs the shared tables/functions once. The host owns and creates the schema. Run before accepting mutations, using a connection allowed DDL.

func MigrationSQL

func MigrationSQL(schema string, version int) (string, error)

MigrationSQL returns an immutable version of the shared schema for inclusion in a product's transactional SQL migration. Keep the returned SQL verbatim in a generated segment and verify it against this function in a contract test. Products may add their own FK/cascade constraints after this segment. This lets a single product migration install the engine, copy retained history and switch its SQL capture atomically. Only version 1 is currently defined.

func NewID

func NewID() string

NewID returns an opaque ID suitable for an event without a product-owned ID.

Types

type Config

type Config struct {
	// Source is the stable product/log namespace; Event.Source is provenance.
	Schema, Source, Scope string
	Retention             time.Duration
	Now                   func() time.Time
}

type Store

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

func New

func New(pool *pgxpool.Pool, c Config) (*Store, error)

func (*Store) AppendTx

func (s *Store) AppendTx(ctx context.Context, tx pgx.Tx, e eventlog.Event) error

AppendTx queues a fact in the caller's mutation transaction. The log namespace and scope come from this authorized store. Event.Source is product-owned provenance. ID must be stable on retries; an identical retained fact is a no-op; a retained ID with different content fails the transaction rather than overwriting history. Products must never reuse an ID for a different fact, including after pruning. Direct SQL source triggers may insert these same fields into eventlog_pending. Do not force its deferred constraint to immediate or change transaction_id.

func (*Store) EnsureScope

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

EnsureScope creates a stable empty stream before its first read. It is idempotent; hosts normally call it when creating their tenant/merchant/app.

func (*Store) ListEvents

func (s *Store) ListEvents(ctx context.Context, q eventlog.Query) (eventlog.Page, error)

func (*Store) Prune

func (s *Store) Prune(ctx context.Context, limit int) (int64, error)

Prune deletes at most limit expired rows in this scope and persists their boundary. Reads enforce retention even if this method is never called.

Jump to

Keyboard shortcuts

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