Documentation
¶
Overview ¶
Package postgres runs host-mediated PostgreSQL queries via pgx on the operator.
Index ¶
- func IsReadonlySQL(sql string) bool
- func LogAudit(ev AuditEvent)
- func ParseParams(raw json.RawMessage) ([]any, error)
- func ParseURL(rawURL string) (*url.URL, error)
- func Ping(ctx context.Context, pools *PoolManager, dsn string, timeout time.Duration) error
- func RewriteDSNHostPort(dsn, hostOverride, portOverride string) (string, error)
- func ValidateParamPlaceholders(sql string, n int) error
- func ValidateReadonlySQL(sql string) error
- type AuditEvent
- type CatalogRepository
- func (r *CatalogRepository) GetColumns(ctx context.Context, dsn string) (map[string][]string, error)
- func (r *CatalogRepository) GetDatabases(ctx context.Context, dsn string) ([]string, error)
- func (r *CatalogRepository) GetSchemas(ctx context.Context, dsn string) ([]string, error)
- func (r *CatalogRepository) GetTables(ctx context.Context, dsn string) (map[string][]string, error)
- type ExecOpts
- type ExecResult
- type MigrateOpts
- type MigrateResult
- type PoolManager
- type QueryOpts
- type QueryResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsReadonlySQL ¶
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 ParseURL ¶ added in v0.3.6
ParseURL securely parses a Postgres connection string, properly encoding unencoded passwords containing special characters like '#'.
func RewriteDSNHostPort ¶
RewriteDSNHostPort replaces hostname and/or port in a postgres connection URL or DSN.
func ValidateParamPlaceholders ¶
ValidateParamPlaceholders ensures sql uses $1..$n when args are present.
func ValidateReadonlySQL ¶
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 CatalogRepository ¶ added in v0.3.6
type CatalogRepository struct {
// contains filtered or unexported fields
}
CatalogRepository abstracts Postgres system catalog queries.
func NewCatalogRepository ¶ added in v0.3.6
func NewCatalogRepository(pools *PoolManager) *CatalogRepository
NewCatalogRepository creates a new CatalogRepository.
func (*CatalogRepository) GetColumns ¶ added in v0.3.6
func (r *CatalogRepository) GetColumns(ctx context.Context, dsn string) (map[string][]string, error)
GetColumns returns a map of 'schema.table' to columns, excluding pg_catalog and information_schema.
func (*CatalogRepository) GetDatabases ¶ added in v0.3.6
GetDatabases returns a list of non-template database names.
func (*CatalogRepository) GetSchemas ¶ added in v0.3.6
GetSchemas returns a list of schema names, excluding pg_catalog and information_schema.
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 ¶
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.
type QueryOpts ¶
type QueryOpts struct {
Timeout time.Duration
Readonly bool
PluginID string
HostName string
DryRun bool
}
QueryOpts configures a postgres query.
type QueryResult ¶
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.