postgres

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package postgres implements usage.Sink, usage.Reader, and usage.Closer backed by PostgreSQL. Events are buffered in-memory and flushed in batches via pgx CopyFrom. PostgreSQL is its own durable store (its own WAL); there is no secondary JSONL segment queue here. On flush error the batch is dropped and counted in Dropped() — best-effort, no re-buffer.

Expected kv ops per request: none (all async). One CopyFrom per flush interval across BatchSize events.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// DSN is the PostgreSQL connection string (postgres://user:pass@host/db).
	DSN string

	// RetentionDays controls how long rows are kept by the daily prune job.
	// <=0 disables pruning. Default 90.
	RetentionDays int

	// BatchSize is the number of events to accumulate before flushing.
	// Default 500.
	BatchSize int

	// FlushInterval is how often buffered events are flushed even if
	// BatchSize is not reached. Default 2s.
	FlushInterval time.Duration

	// Table is the table name. Must match ^[a-z_][a-z0-9_]*$.
	// Default "usage_events".
	Table string
}

Config holds all tunables for the PostgreSQL sink.

type Sink

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

Sink is the PostgreSQL-backed implementation of usage.Sink, usage.Reader, and usage.Closer.

func New

func New(ctx context.Context, cfg Config) (*Sink, error)

New opens a pgxpool connection, validates the table name, ensures the schema exists, and starts the background flush ticker.

func (*Sink) Close

func (s *Sink) Close() error

Close flushes any remaining buffered events and closes the pool.

func (*Sink) Dropped

func (s *Sink) Dropped() uint64

Dropped returns the number of events dropped due to flush errors.

func (*Sink) Events

func (s *Sink) Events(ctx context.Context, q usage.EventQuery) ([]usage.Event, error)

Events returns raw usage events matching q, newest-first.

func (*Sink) Summary

Summary returns aggregated rows grouped by q.GroupBy. Token sums are computed by unnesting the jsonb map in SQL; latency percentiles use percentile_cont. Both queries share the same WHERE and are merged in Go.

func (*Sink) TimeSeries

TimeSeries buckets matched events by q.Interval (date_bin, epoch-aligned), optionally split into one series per q.GroupBy value. Like Summary, token sums come from a second jsonb_each_text query merged in Go. Empty buckets are not emitted; series are ordered by total requests desc, points oldest-first.

func (*Sink) Write

func (s *Sink) Write(ev usage.Event) error

Write appends ev to the in-memory buffer. When BatchSize is reached, a synchronous flush is triggered inline (caller is the Emitter goroutine, not the hot path).

Jump to

Keyboard shortcuts

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