On this page
Package catalog is the shared, scoped pg_catalog query layer answering per-object existence and attribute questions at migration-precondition granularity.
#internal/catalog
#internal/catalog
Package catalog is the shared, SCOPED pg_catalog query layer (roadmap 5.5+5.7). It answers PER-OBJECT existence and attribute questions at PRECONDITION granularity ("does this one table exist?", "does this column have this type/nullability/default?", "is this index present AND valid?"), which is exactly what the migration predicate executor (internal/predicate) needs and what introspect's ~45 per-schema BULK extractors are the wrong granularity for.
It is the SINGLE place per-object pg_catalog queries live: the divergence bug class (two independent sets of catalog queries drifting apart) is the class this extraction exists to kill. introspect adopts the shared entry points where natural (e.g. Version); it is NOT rewritten wholesale.
Version-conditional queries gate through the EXISTING internal/pgcap capability registry — catalog never regrows version logic of its own.
#Querier
type Querier interfaceQuerier is the read surface catalog needs. Both *pgx.Conn and pgx.Tx satisfy it, so callers pass whichever transactional scope a precondition runs in.
#ColumnInfo
type ColumnInfo structColumnInfo is a column's precondition-relevant attributes.
#IndexInfo
type IndexInfo structIndexInfo is an index's precondition-relevant state.
#Version
func Version(ctx context.Context, q Querier) (int, error)Version returns the server's major version (e.g. 18). It is the ONE catalog entry point introspect naturally shares. Parses "18.3 (Fedora ...)" etc.
#TableExists
func TableExists(ctx context.Context, q Querier, schema, name string) (bool, error)TableExists reports whether an ordinary or partitioned table exists.
#ViewExists
func ViewExists(ctx context.Context, q Querier, schema, name string) (bool, error)ViewExists reports whether a view exists.
#MatViewExists
func MatViewExists(ctx context.Context, q Querier, schema, name string) (bool, error)MatViewExists reports whether a materialized view exists.
#SequenceExists
func SequenceExists(ctx context.Context, q Querier, schema, name string) (bool, error)SequenceExists reports whether a sequence exists.
#EnumExists
func EnumExists(ctx context.Context, q Querier, schema, name string) (bool, error)EnumExists reports whether an enum type exists.
#DomainExists
func DomainExists(ctx context.Context, q Querier, schema, name string) (bool, error)DomainExists reports whether a domain type exists.
#CompositeExists
func CompositeExists(ctx context.Context, q Querier, schema, name string) (bool, error)CompositeExists reports whether a free-standing composite type exists. (Table row types are also typtype 'c'; this is scoped by the caller passing a composite-type name, matching how the model records them.)
#FunctionExists
func FunctionExists(ctx context.Context, q Querier, schema, name, argSig string) (bool, error)FunctionExists reports whether a function with the given (schema, name) and parenthesized argument-type signature exists (e.g. argSig "(integer, text)"). It resolves through to_regprocedure, which keys on argument TYPES — matching PostgreSQL overload resolution and the manifest key's ArgSig.
#Column
func Column(ctx context.Context, q Querier, pgVersion int, schema, table, column string) (*ColumnInfo, bool, error)Column returns a column's attributes, or (nil, false) when the table or column does not exist. pgVersion gates the generated-column probe through pgcap: the pg_attribute.attgenerated column exists only on PG 12+, so on older servers the Generated flag is left false rather than querying a nonexistent column.
#ColumnTypeMatches
func ColumnTypeMatches(ctx context.Context, q Querier, schema, table, column, expectedType string) (match, present bool, found string, err error)ColumnTypeMatches reports whether the column's type equals expectedType by OID via to_regtype — the alias-robust, pure-computable probe (roadmap 5.5+5.7 matching-strategy resolution). to_regtype resolves aliases (int4 integer, varchar character varying) to the same OID, so equivalent spellings do NOT false-drift. It returns the found canonical type text for diagnostics. When the column is absent, present is false. A NULL to_regtype (unparseable expectedType) yields match=false rather than an error.
NOTE (typmod gap): to_regtype discards the type modifier, so this OID probe does not distinguish e.g. varchar(10) from varchar(20) on the same base type. Length/ precision drift on an unchanged base type is not caught by the type probe; it is a documented limitation of the pure to_regtype probe (a full typmod comparison would require the round-trip mechanism reserved for definitional bodies).
#ConstraintDef
func ConstraintDef(ctx context.Context, q Querier, schema, table, constraint string) (string, bool, error)ConstraintDef returns pg_get_constraintdef for the named constraint on the table, or ("", false) when absent. The returned text is the canonical Postgres rendering used for present-and-matching precondition comparison.
#Index
func Index(ctx context.Context, q Querier, schema, name string) (*IndexInfo, bool, error)Index returns an index's definition and validity, or (nil, false) when absent. Valid=false is the recoverable state the create-index resume protocol keys on (roadmap L8): an index present but invalid must be DROP-rebuilt, not skipped.
#EnumHasValue
func EnumHasValue(ctx context.Context, q Querier, schema, name, value string) (bool, error)EnumHasValue reports whether the enum type has the given label.
#TriggerExists
func TriggerExists(ctx context.Context, q Querier, schema, table, trigger string) (bool, error)TriggerExists reports whether a (non-internal) trigger of the given name exists on the table.
#PolicyExists
func PolicyExists(ctx context.Context, q Querier, schema, table, policy string) (bool, error)PolicyExists reports whether an RLS policy of the given name exists on the table.
#ExtensionExists
func ExtensionExists(ctx context.Context, q Querier, name string) (bool, error)ExtensionExists reports whether an extension is installed.