postgres

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package postgres runs host-mediated PostgreSQL queries via pgx on the operator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsReadonlySQL

func IsReadonlySQL(sql string) bool

IsReadonlySQL reports whether sql passes the read-only classifier.

func LogAudit

func LogAudit(ev AuditEvent)

LogAudit emits a structured audit log line (SQL hash + truncated preview, never DSN/params).

func ParseParams

func ParseParams(raw json.RawMessage) ([]any, error)

ParseParams unmarshals params JSON into pgx query arguments.

func Ping

func Ping(ctx context.Context, pools *PoolManager, dsn string, timeout time.Duration) error

Ping verifies connectivity without running user SQL.

func RewriteDSNHostPort

func RewriteDSNHostPort(dsn, hostOverride, portOverride string) (string, error)

RewriteDSNHostPort replaces hostname and/or port in a postgres connection URL or DSN.

func ValidateParamPlaceholders

func ValidateParamPlaceholders(sql string, n int) error

ValidateParamPlaceholders ensures sql uses $1..$n when args are present.

func ValidateReadonlySQL

func ValidateReadonlySQL(sql string) error

ValidateReadonlySQL returns nil when sql is a single read-only statement.

Types

type AuditEvent

type AuditEvent struct {
	PluginID     string
	HostName     string
	Action       string
	SQL          string
	Readonly     bool
	DryRun       bool
	RowCount     int
	RowsAffected int64
}

AuditEvent describes a postgres host-function call for structured logging.

type ExecOpts

type ExecOpts struct {
	Timeout  time.Duration
	Readonly bool
	PluginID string
	HostName string
	DryRun   bool
}

ExecOpts configures a postgres exec.

type ExecResult

type ExecResult struct {
	RowsAffected int64
}

ExecResult holds rows affected from a write statement.

func Exec

func Exec(ctx context.Context, pools *PoolManager, dsn, sql string, args []any, opts ExecOpts) (ExecResult, error)

Exec runs INSERT/UPDATE/DDL via pgx.

type MigrateOpts

type MigrateOpts struct {
	Timeout  time.Duration
	Readonly bool
	PluginID string
	HostName string
	DryRun   bool
}

MigrateOpts configures schema migration.

type MigrateResult

type MigrateResult struct {
	Applied []string
	Stdout  string
}

MigrateResult summarizes applied migrations.

func Migrate

func Migrate(ctx context.Context, pools *PoolManager, dsn, migrationsDir string, files []string, opts MigrateOpts) (MigrateResult, error)

Migrate applies ordered .sql files inside migrationsDir.

type PoolManager

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

PoolManager caches pgx pools keyed by DSN hash.

func NewPoolManager

func NewPoolManager() *PoolManager

NewPoolManager creates an empty pool manager.

func (*PoolManager) Acquire

func (m *PoolManager) Acquire(ctx context.Context, dsn string) (*pgxpool.Pool, error)

Acquire returns a pooled connection for dsn.

func (*PoolManager) Close

func (m *PoolManager) Close()

Close closes all cached pools.

type QueryOpts

type QueryOpts struct {
	Timeout  time.Duration
	Readonly bool
	PluginID string
	HostName string
	DryRun   bool
}

QueryOpts configures a postgres query.

type QueryResult

type QueryResult struct {
	Rows []map[string]any
}

QueryResult holds rows from a SELECT-style query.

func Query

func Query(ctx context.Context, pools *PoolManager, dsn, sql string, args []any, opts QueryOpts) (QueryResult, error)

Query runs a read-only SQL query via pgx.

Jump to

Keyboard shortcuts

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