postgres

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package postgres provides a PostgreSQL-backed implementation of the persistence ports. Postgres is the recommended backend for multi- process deployments; SQLite covers single-binary, embedded, and test use cases.

Wire-protocol compatibles supported by this provider via the same driver: CockroachDB, Yugabyte, Neon, Crunchy Bridge, TimescaleDB, AlloyDB Omni. They all speak the libpq wire protocol; Chronos doesn't use any postgres-specific extension.

Per Mnemos ADR 0001 §3, ?namespace= translates to a Postgres schema: CREATE SCHEMA IF NOT EXISTS <ns> + SET search_path TO <ns> runs at every Open. The schema-version table lives inside the namespace, so two tools (Mnemos and Chronos) sharing one Postgres database track their migrations independently.

Index

Constants

View Source
const BulkSaveThreshold = 200

BulkSaveThreshold is the row count above which Save switches to the chunked multi-row INSERT path.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	DB *sql.DB

	EntityStates *EntityStateRepository
	Signals      *SignalRepository
}

Conn bundles the Postgres-backed repositories.

func Open

func Open(connStr string) (*Conn, error)

Open is kept exported for tests and back-compat callers; the preferred entry point is store.Open(ctx, "postgres://...").

func (*Conn) Close

func (c *Conn) Close() error

Close releases the underlying database handle.

type EntityStateRepository

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

EntityStateRepository persists entity states in PostgreSQL.

func (*EntityStateRepository) Count

func (r *EntityStateRepository) Count(ctx context.Context, adapterName string) (int64, error)

Count returns the number of states stored under adapterName.

func (*EntityStateRepository) DeleteOlderThan

func (r *EntityStateRepository) DeleteOlderThan(ctx context.Context, cutoff time.Time, adapterName string) error

DeleteOlderThan removes states observed before cutoff for adapterName.

func (*EntityStateRepository) Ingest

func (r *EntityStateRepository) Ingest(ctx context.Context, adapterName string, state chronos.EntityState) error

Ingest persists a single observation.

func (*EntityStateRepository) ListByEntity

func (r *EntityStateRepository) ListByEntity(ctx context.Context, entityID uuid.UUID) ([]chronos.EntityState, error)

ListByEntity returns all observations of an entity, most recent first.

func (*EntityStateRepository) ListByScope

func (r *EntityStateRepository) ListByScope(ctx context.Context, scopeID uuid.UUID) ([]chronos.EntityState, error)

ListByScope returns all entity states for a scope, most recent first.

func (*EntityStateRepository) ListScopes

func (r *EntityStateRepository) ListScopes(ctx context.Context) ([]uuid.UUID, error)

ListScopes returns the distinct ScopeIDs that have at least one observation. Used by the in-process detection scheduler.

func (*EntityStateRepository) Save

func (r *EntityStateRepository) Save(ctx context.Context, adapterName string, states []chronos.EntityState) error

Save upserts a batch of observations inside a single transaction. Above BulkSaveThreshold rows the batch switches to a multi-row INSERT (chunks of [bulkChunkSize]) instead of one statement per row. This avoids the round-trip overhead of per-row INSERT against the wire protocol while staying portable across every Postgres- wire-compatible engine (CockroachDB, YugabyteDB, Neon, etc.) — including engines whose pgx COPY support is incomplete.

type SignalRepository

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

SignalRepository persists signals and their evidence in PostgreSQL.

func (*SignalRepository) Count

func (r *SignalRepository) Count(ctx context.Context, filter ports.SignalFilter) (int64, error)

Count returns the number of signals matching filter.

func (*SignalRepository) Get

Get returns a single signal by ID, including its evidence.

func (*SignalRepository) List

List returns signals matching filter, ordered detected-at desc then confidence desc.

func (*SignalRepository) Save

func (r *SignalRepository) Save(ctx context.Context, sig domain.Signal) error

Save writes a signal and its evidence atomically.

Jump to

Keyboard shortcuts

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