planner

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package planner classifies schema-change statements: for each operation it decides whether PostgreSQL can run it online natively (possibly via a safer idiom it suggests), whether it needs the engine's copy-and-swap path, or whether it is refused. The mapping is the "Needs copy-and-swap?" column of docs/postgres-online-ddl-reference.md, applied conservatively: anything the planner cannot prove safe routes to copy-and-swap or refuse. Classification predicts; executors keep their own protections regardless.

In MySQL terms, the planner is PostgreSQL's missing ALGORITHM= / LOCK= declaration: MySQL lets authors assert the cost bracket (INSTANT/INPLACE/COPY) and the lock impact (NONE/SHARED/EXCLUSIVE) and fails closed; PostgreSQL has no such clause, so the planner proves both dimensions before execution and routes to the safest sequence that exists.

The rules assume PostgreSQL 14, the oldest major the test matrix runs; every rule holds unconditionally across the supported range (14–18). Rules that were version-dependent below that floor — fast default (PG 11+), SET NOT NULL proven by a validated CHECK (PG 12+), DETACH PARTITION CONCURRENTLY (PG 14+) — carry no version annotation because the floor makes them unconditional. A rule that varies within the supported range must carry an explicit version fact before it lands.

Index

Constants

View Source
const RulesPostgresVersions = "14-18"

RulesPostgresVersions is the inclusive PostgreSQL major-version range the classification rules are derived for (see the package comment and docs/postgresql-version-support.md). Offline consumers such as the linter stamp it into their reports so a stored result names the assumptions behind it.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decision

type Decision struct {
	// Operation is the operator-facing label (display only).
	Operation string `json:"operation"`
	// Kind is the operation's parsed shape. The router's availability
	// policy branches on it together with Reason: whether a backend
	// carries a rewrite depends on what the operation does to the table,
	// not only on why PostgreSQL would rewrite it. It is not part of the
	// plan-report contract; a consumer branches on Reason.
	Kind statement.OpKind `json:"-"`
	// Destructive marks operations that discard live structure — a dropped
	// column, constraint, index, or NOT NULL. It is derived from the operation shape
	// here, in the one place every front door shares, so a plan reports the
	// same statement as destructive no matter how it was submitted. It is
	// always emitted, never omitted: a safety flag a consumer gates on must
	// be explicit even when false.
	Destructive bool `json:"destructive"`
	// Route is where the operation goes.
	Route Route `json:"route"`
	// Reason is why.
	Reason Reason `json:"reason"`
	// Unverified marks a decision the planner took without the live facts
	// needed to prove a cheaper one — it failed closed to the heavier
	// route. The route is what the engine would do, not a proven property
	// of the change: with facts (a live introspection or a supplied
	// column type) the same operation may classify as native.
	Unverified bool `json:"unverified,omitempty"`
	// SaferSQL is the ordered safer native sequence, present only for
	// safer-idiom decisions where the planner could construct it. It is a
	// safer form of the submitted statement, not a semantic equivalent: it
	// converges on the same declared end state with different locking,
	// transactionality, and failure modes. SaferSQLExecution carries the
	// execution contract: the steps run one at a time, in order, each in
	// its own implicit transaction — never inside an enclosing transaction
	// block, which the CONCURRENTLY forms refuse. Each sequence constructor
	// documents what a failed step leaves behind and how a retry resumes
	// (a failed CONCURRENTLY build leaves an invalid index the runner must
	// detect via pg_index.indisvalid and rebuild).
	SaferSQL []string `json:"safer_sql,omitempty"`
	// SaferSQLExecution is the typed execution contract for SaferSQL,
	// present exactly when SaferSQL is. Automation branches on it instead
	// of prose — it is what tells a consumer the sequence must not be
	// wrapped in a transaction block.
	SaferSQLExecution Execution `json:"safer_sql_execution,omitempty"`
	// DependentViews names the views and materialized views that read the
	// column, each schema-qualified with every identifier quoted, present
	// exactly for dependent-view decisions.
	DependentViews []string `json:"dependent_views,omitempty"`
}

Decision is the classification of one operation.

func (Decision) ExecutableAsSubmitted

func (d Decision) ExecutableAsSubmitted() bool

ExecutableAsSubmitted reports whether the operation's submitted form is itself safe to run. It is false for the decisions whose submitted form must be replaced by a safer sequence, whether or not one was constructed: a safer-idiom decision, whose submitted form blocks the table, and an app-breaking rename, whose submitted form breaks every running application instance that still uses the old name. Routing fails closed on the combination of a false ExecutableAsSubmitted and an empty SaferSQL.

type Execution

type Execution string

Execution is the typed execution contract for a planner-produced SQL sequence; automation branches on it, never on prose. It tells a consumer how the steps must run and that a failed step can leave partial state the runner owns detecting and recovering.

const (
	// ExecutionAutocommit: the steps run one at a time, in order, each in
	// its own implicit transaction — never inside an enclosing transaction
	// block. The CONCURRENTLY forms refuse an enclosing block outright,
	// and a multi-step sequence inside one block holds every earlier
	// step's locks across the steps designed to avoid them. A failed step
	// leaves partial state the runner must detect and recover before
	// retrying (a failed CONCURRENTLY build leaves an invalid index,
	// pg_index.indisvalid = false).
	ExecutionAutocommit Execution = "autocommit-each-step"
)

The execution contracts a sequence can carry.

func Executions

func Executions() []Execution

Executions returns the closed set of Execution values. It is part of the plan-report contract (docs/plan-report.md): the set changes only with a format_version bump, and a consumer that meets an unrecognized value must treat the sequence as unknown and refuse to run it.

type Facts

type Facts struct {
	// ColumnTypes maps a column name to its live type as rendered by
	// PostgreSQL's format_type (e.g. "character varying(50)").
	ColumnTypes map[string]string
	// ColumnCollations maps each live column whose collation is not its
	// type's default to that collation, as schemadiff renders it. A column
	// absent from the map has the default collation. Retyping a column
	// without a COLLATE clause resets it to the new type's default, so a
	// column present here loses its collation and every index on it is
	// rebuilt: never a relabel.
	ColumnCollations map[string]string
	// ReplicaIdentity is what the replica identity indexes of the table and
	// its partitions key on; the zero value when none selects an index.
	ReplicaIdentity schemadiff.ReplicaIdentityKeys
	// AddColumnProofs maps a column the change adds to what the server
	// proved about adding it (preflight.AddColumnRewrites). A column absent
	// from the map is unproven and classifies by the statement's shape
	// alone, which assumes the worst of any default it cannot read as a
	// constant; the proof leaves out only an ADD COLUMN IF NOT EXISTS for a
	// column the table already has, which the server skips.
	AddColumnProofs map[string]statement.AddColumnProof
	// DependentViews maps a column to the views and materialized views that
	// read it, on the table or on any table below it, which is what a
	// recursive ALTER COLUMN TYPE changes
	// (preflight.ColumnDependentViews). A column absent from the map has
	// none, or the facts were not read, as in an offline lint; either way
	// the server's own refusal at execution remains the backstop.
	DependentViews map[string][]string
	// DropDependentViews is DependentViews for the tables whose copy of
	// the column a recursive DROP COLUMN drops: a child that declares the
	// column itself or also inherits it from a table outside the drop
	// keeps its copy.
	DropDependentViews map[string][]string
	// OwnDependentViews is DependentViews for the table alone, which is
	// all an ALTER TABLE ONLY changes.
	OwnDependentViews map[string][]string
	// ConstraintNames are the names of the constraints already in the
	// table's schema, which the server passes over when it names an
	// unnamed CHECK or FOREIGN KEY. Without them the safer sequence states
	// the server's first choice, and if the table already uses that name
	// its NOT VALID step fails before anything is added.
	ConstraintNames map[string]bool
	// RenamedIndexConstraint is the name of the primary-key, unique, or
	// exclusion constraint the index an ALTER INDEX ... RENAME TO renames
	// backs, which the rename renames with it; empty when the index backs
	// none, or when the fact was not read, as in an offline lint.
	RenamedIndexConstraint string
}

Facts are properties of the live table that sharpen classification, and they are trusted as stated: the CLI fills them by introspecting the target database, and a library caller may supply facts it already holds — but they must describe the database the change will run on, because a wrong fact can upgrade a rewrite to native. Missing facts never let an unsafe change succeed: the zero value is valid, routes every type change to copy-and-swap, and only defers some refusals, such as a column a view reads, to the server's own refusal at execution.

func FactsFrom

func FactsFrom(live schemadiff.Model) Facts

FactsFrom extracts the facts a live introspection model provides: the canonical type of every live column, the collation of every column that has one of its own, and what the table's replica identity keys on. Every front door — the declarative diff and the imperative dry-run — extracts facts through this one function so equivalent changes classify identically.

type Plan

type Plan struct {
	// Statement is the submitted SQL.
	Statement string `json:"statement"`
	// Route is the aggregate route.
	Route Route `json:"route"`
	// Decisions are the per-operation classifications, in statement order.
	Decisions []Decision `json:"decisions"`
}

Plan is the classification of one statement: one decision per operation and the aggregate route (the worst of its decisions — one rewrite makes the whole statement a copy, one refusal refuses it).

func Classify

func Classify(sql string, facts Facts) (Plan, error)

Classify parses one statement and routes each of its operations. A parse failure is an error; an unrecognized operation is not — it comes back as a refuse decision so the caller can render the whole plan.

type Reason

type Reason string

Reason is the typed cause of a routing decision; automation branches on it, never on prose.

const (
	// ReasonMetadataOnly: a brief ACCESS EXCLUSIVE catalog change, no scan
	// and no rewrite.
	ReasonMetadataOnly Reason = "metadata-only"
	// ReasonOnlineIdiom: already the safe native form (CONCURRENTLY,
	// NOT VALID, VALIDATE, USING INDEX).
	ReasonOnlineIdiom Reason = "online-idiom"
	// ReasonFastDefault: ADD COLUMN with a default the catalog stores, so
	// no row is rewritten (PG 11+): a constant, or any default the server
	// proved does not rewrite (a non-volatile expression such as now()).
	ReasonFastDefault Reason = "fast-default"
	// ReasonBinaryCoercible: a type change PostgreSQL relabels without a
	// rewrite (widen varchar, varchar to text, widen numeric precision).
	ReasonBinaryCoercible Reason = "binary-coercible"
	// ReasonSaferIdiom: native, but the submitted form blocks; SaferSQL
	// carries the online rewrite when one can be constructed.
	ReasonSaferIdiom Reason = "safer-idiom"
	// ReasonAppBreakingRename: PostgreSQL executes the rename as a brief
	// metadata-only catalog flip, but it cannot land atomically across
	// running application instances — code still referencing the old
	// column, table, or constraint name starts erroring the instant it
	// commits. For a column the safe sequence is expand/contract: add the
	// new column, dual-write and backfill, switch reads, then drop the old
	// column as its own reviewed change. For a table, coordinate the
	// rename with the application deploy that adopts the new name. For an
	// index that backs a constraint, the rename renames the constraint,
	// so coordinate it with the deploy that adopts the new constraint
	// name. No sequence can be constructed from the statement, so the
	// rename is rewrite-required.
	ReasonAppBreakingRename Reason = "app-breaking-rename"
	// ReasonVolatileDefault: ADD COLUMN whose default PostgreSQL computes
	// for every row, rewriting the table — proven by the server, or assumed
	// (Unverified) when the default is not a constant and nothing proved
	// it.
	ReasonVolatileDefault Reason = "volatile-default"
	// ReasonGeneratedStored: adding a stored generated column computes
	// every row — a full rewrite.
	ReasonGeneratedStored Reason = "generated-stored"
	// ReasonTypeRewrite: a type PostgreSQL must check or convert row by
	// row — a type conversion it cannot relabel (rewrite plus reindex), a
	// type change that moves the column's collation (reindex alone), or an
	// added column of a domain type with constraints.
	ReasonTypeRewrite Reason = "type-rewrite"
	// ReasonRelocation: SET TABLESPACE moves the heap — a rewrite-scale
	// copy.
	ReasonRelocation Reason = "relocation"
	// ReasonPartitionParentLock: creating a partition takes a brief ACCESS
	// EXCLUSIVE on the partitioned parent — no scan, but it queues behind
	// and then blocks every query on the parent while held.
	ReasonPartitionParentLock Reason = "partition-parent-lock"
	// ReasonReplicaIdentity: the operation drops what the table's
	// REPLICA IDENTITY USING INDEX keys on — the index itself, the
	// constraint that owns it, or one of its columns. The setting outlives
	// the index, so the table is left with no usable identity, and every
	// UPDATE and DELETE on it fails while a publication publishes them.
	// Move the identity first (ALTER TABLE ... REPLICA IDENTITY), then
	// re-plan.
	ReasonReplicaIdentity Reason = "replica-identity"
	// ReasonDependentView: the operation changes the type of, or drops, a
	// column that a view or materialized view reads. PostgreSQL refuses
	// both while the view exists, even a type change that rewrites
	// nothing, so the statement could only fail. The decision names the
	// views; change or drop them first, then re-plan.
	ReasonDependentView Reason = "dependent-view"
	// ReasonCascadingDrop: a drop with CASCADE, which also drops whatever
	// depends on what it drops: a view that reads a dropped column, or
	// another table's foreign key on a dropped key. Those objects are not
	// in the statement, so neither the plan nor the table's lock covers
	// them. Drop each dependent object as its own change, then resubmit
	// without CASCADE, which PostgreSQL refuses while anything still
	// depends on the target.
	ReasonCascadingDrop Reason = "cascading-drop"
	// ReasonUnsupportedOperation: the planner does not recognize the
	// operation or knows no safe path for it.
	ReasonUnsupportedOperation Reason = "unsupported-operation"
)

The reasons a decision can carry.

func Reasons

func Reasons() []Reason

Reasons returns the closed set of Reason values. It is part of the plan-report contract (docs/plan-report.md): the set changes only with a format_version bump, and a consumer that meets an unrecognized value must treat the decision as unknown and refuse it.

type Route

type Route string

Route is where an operation is sent.

const (
	// RouteNative: PostgreSQL runs it online natively — directly or via
	// the safer idiom in Decision.SaferSQL.
	RouteNative Route = "native"
	// RouteCopyAndSwap: needs a table rewrite; only the engine's shadow
	// copy + cutover can do it online.
	RouteCopyAndSwap Route = "copy-and-swap"
	// RouteRefuse: no known safe path; not executed.
	RouteRefuse Route = "refuse"
)

The three routes.

func Routes

func Routes() []Route

Routes returns the closed set of Route values, in severity order. It is part of the plan-report contract (docs/plan-report.md): the set changes only with a format_version bump, and a consumer that meets an unrecognized value must treat the statement as unknown and refuse it.

type ScriptClassifier added in v0.4.0

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

ScriptClassifier classifies the statements of one script in order, with zero live facts except the constraint names the script's own earlier statements leave behind. Running the script, the server numbers an unnamed CHECK or FOREIGN KEY past a name an earlier statement claimed, so a later safer sequence must state the numbered name too, or it would collide with the constraint the script just added. Names are tracked per schema as the statement writes it, an unqualified table under its own key. The zero value is ready to use.

func (*ScriptClassifier) Classify added in v0.4.0

func (c *ScriptClassifier) Classify(sql string) (Plan, error)

Classify parses and routes sql, the script's next statement, as Classify does, then records the constraint names it leaves: each constraint an ALTER TABLE adds, by its stated name or the name its NOT VALID sequence states, less each it drops. Any other constraint added unnamed is not recorded, since the planner states no name for it.

Jump to

Keyboard shortcuts

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