postgres

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package postgres provides PostgreSQL integration for USID. It includes migrations for USID functions and sequences, and utilities for coordinating node IDs across application instances.

Index

Constants

This section is empty.

Variables

View Source
var ErrConfigMismatch = errors.New("usid: database config does not match application config")

ErrConfigMismatch is returned when the database has a different USID configuration than the application is trying to use.

Functions

func GenerateSQL

func GenerateSQL(cfg Config) string

GenerateSQL returns the SQL statements for creating USID functions and sequences. This is called by Migrate but can be used directly if you need the raw SQL.

func Migrate

func Migrate(ctx context.Context, db DB, cfgs ...Config) error

Migrate runs the idempotent USID migration. If no config is provided, uses DefaultConfig(). If the database already has a different configuration, returns ErrConfigMismatch.

func NextNode

func NextNode(ctx context.Context, db DB) (int64, error)

NextNode returns the next available node ID from the database sequence. Call once at app startup to get a unique node ID for this instance.

Types

type Config

type Config struct {
	Epoch    int64 // Custom epoch in microseconds
	NodeBits uint8 // Bits allocated for node ID
	SeqBits  uint8 // Bits allocated for sequence number

	// CreateDomain creates a `usid` domain type as an alias for bigint.
	// This provides type safety in your schema but may require configuration
	// in ORMs and code generators like sqlc.
	CreateDomain bool
}

Config holds the USID bit layout configuration for PostgreSQL migrations. This must match the configuration used in the Go application.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default USID configuration. Use this unless you've customized usid.Epoch, usid.NodeBits, or usid.SeqBits.

func GetConfig

func GetConfig(ctx context.Context, db DB) (Config, error)

GetConfig reads the USID configuration from the database.

func (Config) MaxNode

func (c Config) MaxNode() int64

MaxNode returns the maximum node ID value.

func (Config) MaxSeq

func (c Config) MaxSeq() int64

MaxSeq returns the maximum sequence number value.

func (Config) NodeMask

func (c Config) NodeMask() int64

NodeMask returns the bitmask for extracting the node ID.

func (Config) SeqMask

func (c Config) SeqMask() int64

SeqMask returns the bitmask for extracting the sequence number.

func (Config) TimeShift

func (c Config) TimeShift() uint8

TimeShift returns the number of bits to shift for the timestamp component.

type DB

type DB interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

DB is the interface for database operations. Satisfied by *sql.DB, *sql.Tx, and *sql.Conn. For pgx, use stdlib mode: stdlib.OpenDBFromPool(pool).

Jump to

Keyboard shortcuts

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