postgres

package module
v0.61.0 Latest Latest
Warning

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

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

Documentation

Overview

Package postgres atomically appends run records and optional Host writes in one PostgreSQL transaction. The append-only log is the only durable fact: its last entry is the head an append compares against, under a per-root advisory lock that serializes creation and every later append.

Install SchemaSQL in a dedicated Host-owned namespace before calling New. The Host owns pool lifetime, authorizes namespace creation, and joins its runtimes before closing a Store or pool. Load finds the newest checkpoint through a partial index, so recovery reads never scan the history before it; Read streams history from one snapshot.

Config.Prepare must derive its mutation solely from the exact Batch. It runs only for a new append, inside the root's serialized transaction. A byte-exact retry is acknowledged without repeating Host writes. Prepare and Apply must not contact remote services or commit their own transaction; those effects cannot participate in PostgreSQL rollback.

Store satisfies run.Store and is exercised by runtest.RunStoreConformance. Run scripts/check-run-store.sh for the isolated database and process-crash checks; no credentials or shared database are used by the default tests.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SchemaSQL

func SchemaSQL(name string) (string, error)

SchemaSQL returns strict DDL for an existing, Host-owned namespace. There is no IF NOT EXISTS: an incompatible or pre-existing store must not be blessed.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/Tangerg/scope/agent/run/postgres"
)

func main() {
	schema, err := postgres.SchemaSQL("agent_execution")
	if err != nil {
		panic(err)
	}
	fmt.Println(strings.Contains(schema, `CREATE TABLE "agent_execution".log`))
}
Output:
true

Types

type Config

type Config struct {
	Pool    *pgxpool.Pool
	Schema  string
	Timeout time.Duration
	// Prepare is deterministic and free of I/O. An acknowledged retry does not
	// invoke it again, so Host writes share the log's idempotency boundary.
	Prepare func(run.Batch) (Mutation, error)
}

type Mutation

type Mutation struct {
	Apply func(context.Context, Writer) error
}

Mutation may only use its supplied transaction; external effects cannot be rolled back together with an append.

type Store

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

func New

func New(ctx context.Context, config Config) (*Store, error)

func (*Store) Append

func (s *Store) Append(ctx context.Context, batch run.Batch) (err error)

func (*Store) Close

func (s *Store) Close()

Close cancels storage operations without deleting the durable log. The Host joins its runtimes before closing the shared pool.

func (*Store) Load

func (s *Store) Load(ctx context.Context, root run.ProcessID) (run.Log, bool, error)

func (*Store) Read

func (s *Store) Read(ctx context.Context, root run.ProcessID, after run.LSN) iter.Seq2[run.Entry, error]

type Writer

type Writer interface {
	Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
}

Writer confines Host writes to the transaction whose append owns their commit.

Jump to

Keyboard shortcuts

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