pgpartition

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pgpartition maintains daily PostgreSQL RANGE partitions and enforces retention policies (dropping expired scratch/dedupe partitions or concurrently detaching expired domain partitions), optionally propagating pgtx tenant RLS policies to newly created child partitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DailyPartitionName

func DailyPartitionName(table string, day time.Time) string

DailyPartitionName returns "<tableName>_pYYYYMMDD" for the given UTC day.

func NewModule

func NewModule(opts Options) appkit.Module

NewModule registers an optional startup maintenance pass (when RunOnStart is true) and a periodic cluster-locked maintenance worker via appkit/job.Every.

func ParsePartitionDate

func ParsePartitionDate(relname string) (time.Time, bool)

ParsePartitionDate extracts the UTC date from a daily partition table name such as "events_p20261129" or "events_p_20261129".

func RetentionCutoff

func RetentionCutoff(now time.Time, retentionDays int) (time.Time, bool)

RetentionCutoff returns the exclusive UTC midnight cutoff date (todayUTC - retentionDays). Daily partitions strictly before this date are considered expired. Returns (zero, false) when retentionDays <= 0.

Types

type MaintenancePlan

type MaintenancePlan struct {
	Schema       string
	Table        string
	CreateSQL    []string
	CreatedNames []string
	ExpireSQL    []string
}

MaintenancePlan holds the deterministic DDL statements planned for a table.

func PlanTable

func PlanTable(spec TableSpec, now time.Time, daysAhead int, existing []string) (MaintenancePlan, error)

PlanTable generates deterministic SQL DDL statements to: 1. Create missing daily RANGE partitions from today-1 through today+daysAhead. 2. Drop or concurrently detach expired daily partitions strictly older than RetentionDays.

type Options

type Options struct {
	Log        *slog.Logger
	Pool       *pgxpool.Pool
	LockPool   *pgxpool.Pool
	DaysAhead  int
	Interval   time.Duration
	LockName   string
	RunOnStart bool
	Tables     []TableSpec
	Now        func() time.Time
}

Options configures partition maintenance execution and its background appkit.Module.

type Report

type Report struct {
	Tables []TableReport
}

Report summarizes one EnsurePartitions run across all configured tables.

func EnsurePartitions

func EnsurePartitions(ctx context.Context, pool *pgxpool.Pool, opts Options) (Report, error)

EnsurePartitions inspects each configured table in Postgres, skips tables that do not exist or are not partitioned (relkind != 'p'), creates future daily partitions (propagating pgtx tenant RLS policies when enabled and parent has RLS enabled), and drops/detaches expired daily partitions.

type RetentionAction

type RetentionAction string

RetentionAction specifies what happens to expired daily partitions on a table.

const (
	ActionKeep   RetentionAction = "keep"
	ActionDetach RetentionAction = "detach"
	ActionDrop   RetentionAction = "drop"
)

type TableReport

type TableReport struct {
	Schema        string
	Table         string
	SkippedReason string
	Created       int
	Expired       int
}

TableReport summarizes maintenance results for one configured table.

type TableSpec

type TableSpec struct {
	Schema             string
	Table              string
	Action             RetentionAction
	RetentionDays      int
	PropagateTenantRLS bool
}

TableSpec declares a schema-qualified partitioned table and its retention policy.

Jump to

Keyboard shortcuts

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