db

package
v0.65.9 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MigrationsRoot   = "db/migrations"
	DefaultListLimit = 50
	MaxListLimit     = 100
)

Variables

This section is empty.

Functions

func AdjustListLimit

func AdjustListLimit(limit, offset int) (int, int)

func DayFloor added in v0.63.5

func DayFloor(t time.Time) time.Time

DayFloor truncates t to midnight UTC of its calendar day.

func ParsePartitionDay added in v0.63.5

func ParsePartitionDay(prefix, relname string) (time.Time, bool)

ParsePartitionDay extracts the day a <prefix>YYYYMMDD partition covers.

Types

type DB

type DB struct {
	Pool *pgxpool.Pool
	// contains filtered or unexported fields
}

func NewDB

func NewDB(config DBConfig, serviceName string) (*DB, error)

func (*DB) Close

func (db *DB) Close()

func (*DB) DropExpiredDailyPartitions added in v0.63.5

func (db *DB) DropExpiredDailyPartitions(ctx context.Context, p DailyPartitions, cutoff time.Time) ([]string, error)

DropExpiredDailyPartitions drops each partition whose entire day falls before cutoff and returns their names. Dropping a partition is a fast catalog operation regardless of how many rows it holds. If another replica holds the advisory lock, this call is a no-op and returns (nil, nil) instead of racing it. Child tables not following the <Prefix>YYYYMMDD naming are left alone.

func (*DB) EnsureDailyPartitions added in v0.63.5

func (db *DB) EnsureDailyPartitions(ctx context.Context, p DailyPartitions, from time.Time, aheadDays int) error

EnsureDailyPartitions makes sure a partition exists for the UTC calendar day of from and for each of the following aheadDays days, so a write never fails for lack of a partition to land in. Existing partitions are left untouched. It holds the advisory lock while creating them, so replicas running it concurrently never race on the same CREATE TABLE.

type DBConfig

type DBConfig struct {
	Enabled           bool          `yaml:"enabled"`
	Username          string        `yaml:"username"`
	Password          string        `yaml:"password"`
	Database          string        `yaml:"database"`
	Hostname          string        `yaml:"hostname"`
	Port              int           `yaml:"port"`
	MaxConns          int32         `yaml:"maxConns"`
	MinConns          int32         `yaml:"minConns"`
	MaxConnIdleTime   time.Duration `yaml:"maxConnIdleTime"`
	MaxConnLifetime   time.Duration `yaml:"maxConnLifetime"`
	HealthCheckPeriod time.Duration `yaml:"healthCheckPeriod"`

	// MigrationRetryTimeout and MigrationRetryInterval bound how long
	// runDBMigrations keeps retrying a dirty migration before giving up.
	// Services in a bundle can start in no particular order, and a
	// migration's DDL may depend on a schema/table that another
	// service's own migration creates (see e.g. the provisioner's
	// db/migrations, which FKs into the identity schema). Since the
	// postgres driver sends a whole migration file as a single implicit
	// transaction, a failed migration never leaves partial DDL behind —
	// only the dirty marker survives the rollback — so it's safe to
	// clear that marker and retry until the dependency appears.
	MigrationRetryTimeout  time.Duration `yaml:"migrationRetryTimeout"`
	MigrationRetryInterval time.Duration `yaml:"migrationRetryInterval"`
}

func (*DBConfig) ConnString

func (c *DBConfig) ConnString() string

func (*DBConfig) SetDefaults

func (c *DBConfig) SetDefaults()

type DailyPartitions added in v0.63.5

type DailyPartitions struct {
	// Schema and Table name the partitioned parent table.
	Schema string
	Table  string

	// Prefix of each partition's name, e.g. "threats_" for threats_20261013.
	Prefix string

	// LockKey is the Postgres advisory lock key serializing partition
	// maintenance across replicas of the owning service. It must be unique
	// among the services sharing the database.
	LockKey int64
}

DailyPartitions describes a table partitioned by range on a timestamptz column into one partition per UTC calendar day, each named <Schema>.<Prefix>YYYYMMDD. Partitioning by day lets a retention janitor DROP whole expired partitions instead of running a row-by-row DELETE, which leaves no dead tuples or index bloat behind for autovacuum.

The parent table itself is created by the service's own migration (... PARTITION BY RANGE (<column>)); EnsureDailyPartitions creates the partitions, so the migration needs no partition-management function or extension such as pg_partman.

Jump to

Keyboard shortcuts

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