postgres

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package postgres implements durable idempotency storage on PostgreSQL using pgx, transaction-scoped advisory locks, row locks, server time, and bounded retention cleanup.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoMigration

func GoMigration() (migrations.Migration, error)

GoMigration returns the schema change in migrations' immutable format.

func RecordKeyDigest

func RecordKeyDigest(key idempotency.Key) []byte

RecordKeyDigest returns the opaque table-key digest used by this adapter.

The returned slice is newly allocated and may be retained or mutated by the caller. Business transactions may use it to lock and validate the same idempotency row before applying a fenced side effect.

Types

type Migration

type Migration struct {
	Version uint
	Name    string
	Up      string
	Down    string
}

Migration describes one reversible PostgreSQL schema change.

func SchemaMigration

func SchemaMigration() Migration

SchemaMigration returns the initial durable-record table migration.

type Options

type Options struct {
	// Retention keeps terminal and abandoned records available before cleanup.
	Retention time.Duration
	// OwnerTokens returns unpredictable, globally unique ownership tokens.
	OwnerTokens func() (string, error)
}

Options configures record retention and ownership-token generation.

type Store

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

Store persists idempotency records through atomic PostgreSQL transactions.

func New

func New(pool *pgxpool.Pool, options Options) (*Store, error)

New constructs a PostgreSQL store using a pgx connection pool.

func (*Store) Acquire

func (s *Store) Acquire(
	ctx context.Context,
	request idempotency.AcquireRequest,
) (result idempotency.AcquireResult, err error)

Acquire atomically elects an owner or returns the existing semantic outcome.

func (*Store) Cleanup

func (s *Store) Cleanup(ctx context.Context, batch int) (int64, error)

Cleanup removes at most batch records whose retention deadline elapsed.

func (*Store) Complete

func (s *Store) Complete(
	ctx context.Context,
	request idempotency.CompleteRequest,
) (idempotency.Record, error)

Complete conditionally persists a current owner's replay result.

func (*Store) CompleteTx

func (s *Store) CompleteTx(
	ctx context.Context,
	tx pgx.Tx,
	request idempotency.CompleteRequest,
) (idempotency.Record, error)

CompleteTx persists completion inside an application-owned pgx transaction. The caller remains responsible for committing or rolling back the transaction.

func (*Store) Expire

func (s *Store) Expire(ctx context.Context, key idempotency.Key) (record idempotency.Record, err error)

Expire marks an elapsed active lease as explicitly expired.

func (*Store) Fail

func (s *Store) Fail(
	ctx context.Context,
	request idempotency.FailRequest,
) (idempotency.Record, error)

Fail conditionally persists a current owner's terminal failure.

func (*Store) Heartbeat

func (s *Store) Heartbeat(
	ctx context.Context,
	request idempotency.HeartbeatRequest,
) (idempotency.Record, error)

Heartbeat extends a current owner's lease using PostgreSQL time.

func (*Store) Inspect

func (s *Store) Inspect(ctx context.Context, key idempotency.Key) (record idempotency.Record, err error)

Inspect returns the current durable record.

func (*Store) Release

func (s *Store) Release(
	ctx context.Context,
	ownership idempotency.Ownership,
) (idempotency.Record, error)

Release abandons a current attempt so a retry can acquire a new fence.

Jump to

Keyboard shortcuts

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