postgres

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package postgres is the PostgreSQL dialect of sqlate:

  • how the engine renders the nth bind parameter
  • how its driver's errors classify
  • the session-level advisory lock the migrate protocol takes
  • the statement that reads the server's version
  • its spelling of the keyset predicate a cursor continues by
  • its migrate.Catalog implementation, the history protocol's engine half

It is a sub-module so the driver it names, pgx, enters a consumer's build only through this import, made once where it opens its pool; a consumer opens its own pool with the driver and hands it to sqlate.Wrap with Dialect.

Error classification

Dialect.MapError reads the SQLSTATE the driver exposes. Class 22, a data exception (invalid text for a type, a value out of range, a bad datetime), becomes sqlate.ErrInvalidValue wrapping the driver error, the engine-side half of request validation. Class 23 constraint violations become a sqlate.ConstraintError whose fields are the class sentinel, the driver error, and the violated constraint's name, table, and column when the driver exposes them — a not-null violation names no constraint, so the column is the only handle a consumer has for that case:

  • 23505 unique
  • 23503 foreign key
  • 23514 check
  • 23502 not null

2BP01 (dependent objects still exist) and 40001 (serialization failure) become sqlate.ErrDependentObjects and sqlate.ErrSerializationFailure, the two classes a multi-set migrator's own tests need. MapError returns everything else unchanged, sql.ErrNoRows included, and errors.As finds the driver error through every wrap.

Locking

Dialect.Lock and Dialect.Unlock implement sqlate.Locker over pg_advisory_lock and pg_advisory_unlock on a pinned connection. The name enters the engine's key space through hashtext, so locks are named and never numbered; the lock belongs to the connection's session and outlives any transaction on it. Unlock of a lock the session does not hold is ErrLockNotHeld.

Server version

Dialect.ServerVersion returns SELECT version(), the statement an administrative layer runs to report the engine's version. It is a capability rather than part of sqlate.Dialect, since every engine spells the read differently and the session layer never needs it.

Migration history

Dialect.CreateHistory delegates to migrate.StandardCatalog unchanged. Dialect.HistoryExists qualifies the check by the session's current schema, closing a defect in the standard form: information_schema.tables spans every schema on the search path, so a same-named table in an unrelated schema would satisfy the check even though the current schema's own history table does not exist.

Native forms

The sqlint.toml beside this file exports the engine's native forms: the spellings a standard-tier SQL file must not use, each a regular expression under the name a finding reports. The same file exports the module's overlay directory to the lint.

Patterns

Patterns returns the library's patterns with one overlaid: the keyset predicate a cursor continues by. The library composes it in standard SQL as an expanded chain of disjuncts, (a > x) OR (a = x AND b > y); the overlay spells it as the row-value comparison (a, b) > (x, y), which the engine evaluates as one condition over the keyed columns. Every other pattern is accepted as written, so a program that composes Patterns in place of query.Patterns changes only the text of a continued page's predicate.

Index

Constants

This section is empty.

Variables

View Source
var ErrLockNotHeld = errors.New("advisory lock not held by this session")

ErrLockNotHeld reports an Unlock of a name this session did not hold; the engine answers false rather than failing, so the dialect makes it an error.

Functions

func Patterns added in v0.2.0

func Patterns() query.Source

Patterns returns the library's patterns, query.Patterns, with the keyset predicate respelled as the engine's row-value comparison in place of the standard tier's expanded chain of disjuncts. A program passes it to query.NewCatalog in place of query.Patterns; every other pattern is the library's own.

Types

type Dialect

type Dialect struct{}

Dialect is the PostgreSQL dialect: sqlate.Dialect plus the sqlate.Locker capability and the server-version statement. It has no fields; the zero value is the dialect.

func (Dialect) CreateHistory added in v0.2.0

func (Dialect) CreateHistory(table string) string

CreateHistory returns migrate.StandardCatalog's DDL unchanged: its CREATE TABLE IF NOT EXISTS with text and boolean columns already suits the engine.

func (Dialect) HistoryExists added in v0.2.0

func (Dialect) HistoryExists(param string) string

HistoryExists returns the existence check restricted to the session's current schema. migrate.StandardCatalog's form matches table_name in every schema information_schema.tables lists, so a same-named table in another schema satisfies it while the current schema's history table is missing. current_schema() is a PostgreSQL function, so the fix lives here rather than in StandardCatalog, whose other engines spell it differently.

func (Dialect) Lock

func (Dialect) Lock(ctx context.Context, conn *sql.Conn, name string) error

Lock takes the session-level advisory lock for name on conn, blocking until it is granted or ctx ends. The name enters the engine's 32-bit key space through hashtext. The lock belongs to the connection's session and outlives any transaction on it; Unlock on the same conn releases it.

func (Dialect) MapError

func (Dialect) MapError(err error) error

MapError classifies a driver error by its SQLSTATE: class 22 as sqlate.ErrInvalidValue with the engine's message reachable, 2BP01 as sqlate.ErrDependentObjects, 40001 as sqlate.ErrSerializationFailure, the four class-23 constraint violations as a sqlate.ConstraintError with the constraint, table, and column names the server reports, and everything else unchanged. nil stays nil.

func (Dialect) Name

func (Dialect) Name() string

Name identifies the engine.

func (Dialect) Placeholder

func (Dialect) Placeholder(n int) string

Placeholder renders the 1-based nth bind parameter as $n.

func (Dialect) ServerVersion added in v0.1.1

func (Dialect) ServerVersion() string

ServerVersion returns the statement an administrative read runs to learn the server's version: one row, one text column. Every engine spells it differently, so the dialect carries it as a capability an administrative layer discovers by type assertion.

func (Dialect) Unlock

func (Dialect) Unlock(ctx context.Context, conn *sql.Conn, name string) error

Unlock releases the session-level advisory lock for name on conn.

Jump to

Keyboard shortcuts

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