Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package postgres is the pgx implementation of metadata.Repository. It is the only code in the system that issues SQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Repo

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

Repo persists File records in Postgres.

func New

func New(pool *pgxpool.Pool) *Repo

New returns a repository backed by the given pool.

func (*Repo) ClaimIdempotencyKey

func (r *Repo) ClaimIdempotencyKey(ctx context.Context, key string) (bool, *metadata.IdempotencyRecord, error)

ClaimIdempotencyKey inserts the key as pending, claiming it. If the key already exists, the insert no-ops and we read back the existing row so the caller can replay (completed) or reject (pending).

func (*Repo) ClaimJob added in v1.3.0

func (r *Repo) ClaimJob(ctx context.Context) (*metadata.Job, error)

ClaimJob takes the oldest pending job. SKIP LOCKED is what makes N replicas safe with no coordination: a row another claim holds locked is passed over rather than waited on, so two workers never claim one job.

func (*Repo) CountPendingJobs added in v1.3.0

func (r *Repo) CountPendingJobs(ctx context.Context) (int, error)

CountPendingJobs is the queue depth, served by the pickup index.

func (*Repo) Create

func (r *Repo) Create(ctx context.Context, f *metadata.File) error

Create inserts a new record. ID and StorageKey are supplied by the caller.

func (*Repo) CreateForKey

func (r *Repo) CreateForKey(ctx context.Context, f *metadata.File, key string) error

CreateForKey inserts the file row and marks the idempotency key completed in a single transaction. Atomicity matters: if the row committed but the key were left pending (two separate writes, crash in between), a retry after the key's TTL would create a duplicate file. One transaction closes that window.

func (*Repo) CreateJob added in v1.3.0

func (r *Repo) CreateJob(ctx context.Context, j *metadata.Job) (bool, string, error)

CreateJob inserts the job as pending. The partial unique index on (archive_id) where the status is active refuses a second live job for one archive; ON CONFLICT DO NOTHING turns that into "no row", and the active job's id is read back so the caller can point at it.

func (*Repo) Delete

func (r *Repo) Delete(ctx context.Context, id string) error

Delete soft-deletes the record by ID, stamping deleted_at. The object is reclaimed later by GC. Returns metadata.ErrNotFound if no live row matched.

func (*Repo) FinishJob added in v1.3.0

func (r *Repo) FinishJob(ctx context.Context, id string, attempt int, res metadata.JobResult) error

FinishJob records the terminal result.

func (*Repo) Get

func (r *Repo) Get(ctx context.Context, id string) (*metadata.File, error)

Get returns the live (non-deleted) record by ID, or metadata.ErrNotFound.

func (*Repo) GetJob added in v1.3.0

func (r *Repo) GetJob(ctx context.Context, id string) (*metadata.Job, error)

GetJob returns the job by id, or metadata.ErrJobNotFound.

func (*Repo) HeartbeatJob added in v1.3.0

func (r *Repo) HeartbeatJob(ctx context.Context, id string, attempt, done, total int) (bool, error)

HeartbeatJob records progress and liveness together and reads the cancel flag back, so the three cost one round trip.

func (*Repo) List

func (r *Repo) List(ctx context.Context, filter metadata.ListFilter) ([]*metadata.File, error)

List returns records matching the filter, newest first — live ones unless the filter asks for tombstones too — filtered by content type, JSONB tag containment and the extracted-entry scope, with keyset pagination. Ordering is by id descending: ids are UUIDv7, so id order is creation order and the primary-key index serves both the sort and the cursor bound.

func (*Repo) ListDeleted

func (r *Repo) ListDeleted(ctx context.Context, limit int) ([]*metadata.File, error)

ListDeleted returns up to limit soft-deleted records, oldest deletion first, so GC purges the longest-pending objects first.

func (*Repo) ParkJob added in v1.3.0

func (r *Repo) ParkJob(ctx context.Context, id string, attempt, done, total int) error

ParkJob returns a running job to pending with its progress kept, for a worker that is shutting down. The next claim resumes it.

func (*Repo) Purge

func (r *Repo) Purge(ctx context.Context, id string) error

Purge hard-deletes a soft-deleted record. The deleted_at guard prevents ever removing a live row through this path.

func (*Repo) PurgeFinishedJobs added in v1.3.0

func (r *Repo) PurgeFinishedJobs(ctx context.Context, before time.Time) (int, error)

PurgeFinishedJobs deletes terminal rows past retention, returning the count.

func (*Repo) PurgeIdempotencyKeys

func (r *Repo) PurgeIdempotencyKeys(ctx context.Context, before time.Time) (int, error)

PurgeIdempotencyKeys deletes keys older than the cutoff, returning the count.

func (*Repo) ReapJobs added in v1.3.0

func (r *Repo) ReapJobs(ctx context.Context, ttl time.Duration) (int, error)

ReapJobs returns stranded running rows — no heartbeat within ttl — to pending. Not to failed: extraction resumes, so the next pickup continues past the children already written. The cutoff is now() minus ttl on the database's clock, the clock that stamped the heartbeat, so no skew between this process and the database can make a live run look dead.

func (*Repo) ReleaseIdempotencyKey

func (r *Repo) ReleaseIdempotencyKey(ctx context.Context, key string) error

ReleaseIdempotencyKey removes a still-pending claim (after a failed upload).

func (*Repo) RequestCancel added in v1.3.0

func (r *Repo) RequestCancel(ctx context.Context, id string) (*metadata.Job, error)

RequestCancel flags a job. Every column reference on the right-hand side of a SET sees the row's old value, so one statement cancels a pending job outright, flags a running one, and leaves a terminal one untouched.

func (*Repo) StorageKeys

func (r *Repo) StorageKeys(ctx context.Context) (map[string]struct{}, error)

StorageKeys returns every storage key in the table (live or soft-deleted) as a set, so GC can identify stored objects with no backing row.

func (*Repo) UpdateMetadata

func (r *Repo) UpdateMetadata(ctx context.Context, id string, tags map[string]any, merge bool) (*metadata.File, error)

UpdateMetadata writes tags into the record's JSONB column: merged (||) when merge is true, replaced otherwise.

Jump to

Keyboard shortcuts

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