Skip to main content

pgevolve_core/catalog/
mod.rs

1//! Catalog reader: live Postgres `pg_catalog` → [`crate::ir::catalog::Catalog`].
2//!
3//! The reader is split into:
4//!
5//! - [`CatalogQuerier`] — a sync, driver-agnostic trait. Adapters (the binary
6//!   uses `tokio-postgres`) execute parameterized SQL and return [`rows::Row`]
7//!   values.
8//! - Per-version SQL strings in [`queries`].
9//! - [`filter::CatalogFilter`] — managed-schema list + ignore globs.
10//! - [`read_catalog`] — top-level entry point that orchestrates the queries
11//!   and assembles their rows into IR.
12
13pub mod cluster;
14pub mod error;
15pub mod filter;
16#[cfg(feature = "tokio-postgres-querier")]
17pub mod pg_querier;
18pub mod queries;
19pub mod rows;
20pub mod version;
21
22pub use error::CatalogError;
23pub use filter::CatalogFilter;
24pub use rows::{Row, Value};
25pub use version::PgVersion;
26
27mod assemble;
28pub(crate) mod collations;
29pub(crate) mod grants;
30pub(crate) mod publications;
31pub(crate) mod reloptions;
32pub(crate) mod statistics;
33pub(crate) mod subscriptions;
34
35use crate::identifier::{Identifier, QualifiedName};
36use crate::ir::catalog::Catalog;
37
38/// Drift detected between the canonical catalog IR and the live Postgres state.
39///
40/// The catalog reader always surfaces all constraints and indexes in the IR
41/// regardless of their validation state. This report captures the *extra*
42/// observation that some of them are in a transitional / incomplete state:
43/// - `pending_validation`: constraints with `pg_constraint.convalidated = false`
44///   (added `NOT VALID`, never validated).
45/// - `invalid_indexes`: indexes with `pg_index.indisvalid = false` (e.g., a
46///   `CREATE INDEX CONCURRENTLY` that failed and left an INVALID index).
47/// - `unmanaged_language_routines`: routines whose `LANGUAGE` is neither `sql`
48///   nor `plpgsql` (e.g., `plperl`, `python3u`). pgevolve v0.2 does not
49///   manage these; they are surfaced in the drift report so callers can
50///   inspect them. The associated row is skipped and never appears in
51///   `catalog.functions` / `catalog.procedures`.
52/// - `unmanaged_aggregates`: ordered-set / hypothetical-set aggregates, or
53///   aggregates whose state/final function is in an unmanaged language. The
54///   associated row is skipped and never appears in `catalog.aggregates`.
55/// - `unmanaged_casts`: `WITH FUNCTION` casts whose conversion function is in
56///   a language other than `sql` / `plpgsql`. The associated row is skipped
57///   and never appears in `catalog.casts`.
58/// - `unreadable_subscriptions`: the connection used for the catalog read had
59///   insufficient privilege to query `pg_subscription` (sqlstate 42501). The
60///   subscription list in the returned catalog is empty; the operator must use
61///   a superuser connection to get subscription data.
62///
63/// The differ consumes this report and emits [`crate::diff::change::Change::ValidateConstraint`]
64/// and [`crate::diff::change::Change::RecreateIndex`] to recover automatically.
65#[derive(Debug, Clone, Default, PartialEq, Eq)]
66pub struct DriftReport {
67    /// Constraints present in the catalog but with `convalidated = false`.
68    /// Identified by `(table_qname, constraint_name)`.
69    pub pending_validation: Vec<(QualifiedName, Identifier)>,
70    /// Indexes present in the catalog but with `indisvalid = false`.
71    /// Identified by index qname.
72    pub invalid_indexes: Vec<QualifiedName>,
73    /// Routines whose `LANGUAGE` is not `sql` or `plpgsql`.
74    /// Identified by `(qname, language_name)`.
75    pub unmanaged_language_routines: Vec<(QualifiedName, String)>,
76    /// Aggregates pgevolve does not manage: ordered-set / hypothetical-set
77    /// aggregates (`aggkind <> 'n'`), or aggregates whose state / final
78    /// function is in a language other than `sql` / `plpgsql`. Identified by
79    /// the aggregate qname. The associated row is skipped and never appears in
80    /// `catalog.aggregates`.
81    pub unmanaged_aggregates: Vec<QualifiedName>,
82    /// Casts pgevolve does not manage: `WITH FUNCTION` casts whose conversion
83    /// function is in a language other than `sql` / `plpgsql`. Identified by
84    /// `(source_qname, target_qname)`. The associated row is skipped and never
85    /// appears in `catalog.casts`.
86    pub unmanaged_casts: Vec<(QualifiedName, QualifiedName)>,
87    /// `pg_subscription` was unreadable due to insufficient privilege (sqlstate
88    /// 42501). `catalog.subscriptions` will be empty when this is `true`.
89    pub unreadable_subscriptions: bool,
90}
91
92/// Identifier for each catalog query the reader runs. Adapters dispatch on
93/// this enum to pick the per-version SQL string.
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
95pub enum CatalogQuery {
96    /// `SHOW server_version_num`.
97    PgVersion,
98    /// `pg_namespace` rows for managed schemas.
99    Schemas,
100    /// `pg_class` (relkind='r') for managed tables.
101    Tables,
102    /// `pg_attribute` joined with `pg_attrdef`/`pg_type` for managed tables.
103    Columns,
104    /// `pg_constraint` for managed tables (PK/UNIQUE/FK/CHECK).
105    Constraints,
106    /// `pg_index` for managed tables (excluding constraint-backing indexes).
107    Indexes,
108    /// `pg_class` (relkind='S') joined with `pg_sequence`.
109    Sequences,
110    /// `pg_description` (currently inlined into the per-object queries).
111    Comments,
112    /// `pg_depend` rows linking sequences to their owning columns.
113    Dependencies,
114    /// `pg_class` (relkind IN ('v','m')) joined with `pg_get_viewdef`.
115    ViewsAndMvs,
116    /// `pg_attribute` for view and materialized view columns.
117    ViewColumns,
118    /// `pg_type` filtered to `typtype IN ('e','d','c')` for user-defined types.
119    UserTypes,
120    /// `pg_enum` labels for enum types.
121    EnumValues,
122    /// Base-type and nullability details for domain types.
123    DomainDetails,
124    /// Named CHECK constraints attached to domain types.
125    DomainChecks,
126    /// Attributes (fields) of composite types.
127    CompositeAttributes,
128    /// `pg_proc` rows for functions and procedures (prokind IN 'f','p').
129    Functions,
130    /// `pg_aggregate` rows joined to their wrapper `pg_proc` entry.
131    ///
132    /// Schema-filtered via `$1::text[]` (same param group as [`Self::Functions`]).
133    /// The assembler skips ordered-set / hypothetical-set aggregates and any
134    /// aggregate whose state/final function is in an unmanaged language,
135    /// recording those in [`DriftReport::unmanaged_aggregates`].
136    Aggregates,
137    /// `pg_extension` rows for installed extensions.
138    Extensions,
139    /// `pg_trigger` rows for user triggers (excluding internal + extension-owned).
140    Triggers,
141    /// `pg_class` (relkind='p') rows for partitioned-table parents.
142    PartitionedTables,
143    /// `pg_class` (relispartition=true) rows for child partitions.
144    Partitions,
145    /// `pg_authid` rows for cluster roles (with `pg_shdescription` for comments).
146    ///
147    /// Uses `$1::text[]` as the bootstrap-role filter (names to exclude), not a
148    /// managed-schema list. `takes_text_array_param` returns `true` so the adapter
149    /// passes the parameter; the cluster reader supplies bootstrap role names.
150    ClusterRoles,
151    /// `pg_auth_members` edges joined to `pg_authid` for role names.
152    ///
153    /// Same `$1::text[]` bootstrap-role filter as [`Self::ClusterRoles`].
154    ClusterMembers,
155    /// `pg_tablespace` rows (with `pg_shdescription` for comments) for managed
156    /// cluster tablespaces.
157    ///
158    /// Built-in `pg_default` / `pg_global` are always excluded. Uses
159    /// `$1::text[]` as the bootstrap filter (names to exclude), in the same
160    /// param-group as [`Self::ClusterRoles`]; `takes_text_array_param` returns
161    /// `true`.
162    ClusterTablespaces,
163    /// `pg_default_acl` rows joined to `pg_authid` and `pg_namespace`.
164    ///
165    /// Returns one row per (`target_role`, schema, `object_type`) tuple. Rows for
166    /// predefined `pg_*` roles are filtered out. Takes **no** `$1::text[]`
167    /// parameter; `takes_text_array_param` returns `false` for this variant.
168    DefaultPrivileges,
169    /// `pg_policies` rows for managed schemas.
170    ///
171    /// Returns one row per policy, scoped to `schemaname = ANY($1::text[])`.
172    /// Decoded into [`crate::ir::policy::Policy`] and attached to their
173    /// owning `Table` by the assembler. Policies on unmanaged tables are
174    /// silently dropped.
175    Policies,
176    /// `pg_publication` rows for all publications in the database.
177    ///
178    /// Publications are database-global (not schema-scoped); takes **no**
179    /// `$1::text[]` parameter (`takes_text_array_param` returns `false`).
180    Publications,
181    /// `pg_publication_rel` rows — one per (publication, table) membership.
182    ///
183    /// PG 15+ includes `prqual` (row filter) and `prattrs` (column list);
184    /// PG 14 variant substitutes `NULL` for both. Takes **no** parameter.
185    PublicationRel,
186    /// `pg_publication_namespace` rows — one per (publication, schema)
187    /// membership (PG 15+ only). PG 14 variant returns zero rows.
188    /// Takes **no** parameter.
189    PublicationNamespace,
190    /// `pg_attribute` rows for every column of every table referenced by
191    /// any publication. Used to resolve column attnums to names. Takes **no**
192    /// parameter.
193    PublicationAttributes,
194    /// `pg_event_trigger` rows for all event triggers in the database.
195    ///
196    /// Event triggers are database-global (not schema-scoped); takes **no**
197    /// `$1::text[]` parameter (`takes_text_array_param` returns `false`).
198    /// Extension-owned event triggers (`pg_depend.deptype = 'e'`) are excluded
199    /// at the SQL layer.
200    EventTriggers,
201    /// `pg_subscription` rows for all subscriptions in the database.
202    ///
203    /// Subscriptions are database-global (not schema-scoped). Takes **no**
204    /// `$1::text[]` parameter (`takes_text_array_param` returns `false`).
205    ///
206    /// `pg_subscription` is superuser-readable only. Non-super connections
207    /// will receive an empty result or a permission error; the assembler
208    /// catches the error and sets `DriftReport::unreadable_subscriptions`.
209    Subscriptions,
210    /// `pg_statistic_ext` rows for managed schemas. Takes `$1::text[]`
211    /// (managed schema names).
212    Statistics,
213    /// Column-attnum resolver for statistics target tables. Bulk-fetched once;
214    /// grouped by `target_oid` in the assembler. Takes `$1::text[]`.
215    StatisticAttributes,
216    /// Bulk expression decode via `pg_get_statisticsobjdef_expressions` for all
217    /// statistics in managed schemas. Returns one row per expression entry with
218    /// columns `(stat_oid, expr_index, expr_sql)`. Takes `$1::text[]`
219    /// (managed schema names).
220    StatisticExpressions,
221    /// `pg_collation` rows for managed schemas — user-defined collations only
222    /// (built-in and extension-owned collations are filtered out at the SQL
223    /// layer). Takes `$1::text[]` (managed schema names).
224    Collations,
225    /// `pg_cast` rows for all user-defined casts in the database.
226    ///
227    /// Casts are database-global (not schema-scoped); takes **no**
228    /// `$1::text[]` parameter (`takes_text_array_param` returns `false`).
229    /// System / built-in casts (`oid < 16384`) and extension-owned casts
230    /// (`pg_depend.deptype = 'e'`) are excluded at the SQL layer. The
231    /// assembler skips casts whose conversion function is in an unmanaged
232    /// language (anything other than `sql` / `plpgsql`), recording those in
233    /// [`DriftReport::unmanaged_casts`].
234    Casts,
235}
236
237impl CatalogQuery {
238    /// Whether this query accepts a `$1::text[]` argument.
239    ///
240    /// The semantic meaning of the array varies by variant: managed-schema
241    /// names for per-DB queries, bootstrap-role names for cluster queries.
242    /// The adapter is responsible for passing the right slice to the right
243    /// variant.
244    ///
245    /// A few variants (`PgVersion`, `Extensions`) take no parameters at all;
246    /// this method returns `false` for those.
247    #[must_use]
248    pub const fn takes_text_array_param(self) -> bool {
249        !matches!(
250            self,
251            Self::PgVersion
252                | Self::Extensions
253                | Self::DefaultPrivileges
254                | Self::Publications
255                | Self::PublicationRel
256                | Self::PublicationNamespace
257                | Self::PublicationAttributes
258                | Self::EventTriggers
259                | Self::Subscriptions
260                | Self::Casts
261        )
262    }
263
264    // Note: `Policies` takes `$1::text[]` (managed schemas), so it is NOT in
265    // the exclusion list above — `takes_text_array_param` returns `true` for it.
266}
267
268/// Sync, driver-agnostic catalog query interface.
269///
270/// Interface implemented by callers (typically the binary) to execute catalog
271/// queries against a live database. Implementations are expected to be sync —
272/// async drivers can wrap their runtime in [`fetch`](Self::fetch).
273pub trait CatalogQuerier {
274    /// Execute the named query with the supplied `$1::text[]` parameter (when
275    /// applicable; see [`CatalogQuery::takes_text_array_param`]).
276    ///
277    /// The semantic meaning of `text_array_param` varies by variant:
278    /// managed-schema names for per-DB queries, bootstrap-role names for
279    /// cluster queries. Pass an empty slice for queries that take no parameter.
280    fn fetch(
281        &self,
282        query: CatalogQuery,
283        text_array_param: &[&str],
284    ) -> Result<Vec<Row>, CatalogError>;
285}
286
287/// Read every catalog query, assemble the IR, and canonicalize.
288///
289/// Returns a `(Catalog, DriftReport)` tuple. The catalog contains all objects
290/// including those in transitional states (NOT VALID constraints, INVALID
291/// indexes). The drift report captures which objects are in those states so the
292/// differ can emit recovery changes.
293pub fn read_catalog(
294    querier: &dyn CatalogQuerier,
295    filter: &CatalogFilter,
296) -> Result<(Catalog, DriftReport), CatalogError> {
297    let version = PgVersion::detect(querier)?;
298    let managed: Vec<&str> = filter.managed_schemas_param();
299
300    let schemas_rows = querier.fetch(CatalogQuery::Schemas, &managed)?;
301    let tables_rows = querier.fetch(CatalogQuery::Tables, &managed)?;
302    let columns_rows = querier.fetch(CatalogQuery::Columns, &managed)?;
303    let constraints_rows = querier.fetch(CatalogQuery::Constraints, &managed)?;
304    let indexes_rows = querier.fetch(CatalogQuery::Indexes, &managed)?;
305    let sequences_rows = querier.fetch(CatalogQuery::Sequences, &managed)?;
306    let dependencies_rows = querier.fetch(CatalogQuery::Dependencies, &managed)?;
307    let views_and_mvs_rows = querier.fetch(CatalogQuery::ViewsAndMvs, &managed)?;
308    let view_columns_rows = querier.fetch(CatalogQuery::ViewColumns, &managed)?;
309    let user_types_rows = querier.fetch(CatalogQuery::UserTypes, &managed)?;
310    let enum_values_rows = querier.fetch(CatalogQuery::EnumValues, &managed)?;
311    let domain_details_rows = querier.fetch(CatalogQuery::DomainDetails, &managed)?;
312    let domain_checks_rows = querier.fetch(CatalogQuery::DomainChecks, &managed)?;
313    let composite_attributes_rows = querier.fetch(CatalogQuery::CompositeAttributes, &managed)?;
314    let functions_rows = querier.fetch(CatalogQuery::Functions, &managed)?;
315    let aggregates_rows = querier.fetch(CatalogQuery::Aggregates, &managed)?;
316    let extensions_rows = querier.fetch(CatalogQuery::Extensions, &managed)?;
317    let triggers_rows = querier.fetch(CatalogQuery::Triggers, &managed)?;
318    let partitioned_tables_rows = querier.fetch(CatalogQuery::PartitionedTables, &managed)?;
319    let partitions_rows = querier.fetch(CatalogQuery::Partitions, &managed)?;
320    let default_privileges_rows = querier.fetch(CatalogQuery::DefaultPrivileges, &[])?;
321    let policies_rows = querier.fetch(CatalogQuery::Policies, &managed)?;
322    let publications_rows = querier.fetch(CatalogQuery::Publications, &[])?;
323    let publication_rels_rows = querier.fetch(CatalogQuery::PublicationRel, &[])?;
324    let publication_namespaces_rows = querier.fetch(CatalogQuery::PublicationNamespace, &[])?;
325    let publication_attributes_rows = querier.fetch(CatalogQuery::PublicationAttributes, &[])?;
326    let event_triggers_rows = querier.fetch(CatalogQuery::EventTriggers, &[])?;
327
328    // `pg_subscription` is superuser-only. If the querier returns a
329    // `QueryFailed` error whose message contains the PG sqlstate 42501
330    // (insufficient_privilege), we silently return empty rows and record the
331    // gap in the drift report. Any other error is propagated normally.
332    let (subscriptions_rows, unreadable_subscriptions) =
333        match querier.fetch(CatalogQuery::Subscriptions, &[]) {
334            Ok(rows) => (rows, false),
335            Err(CatalogError::QueryFailed { message, .. })
336                if message.contains("42501") || message.contains("insufficient_privilege") =>
337            {
338                (vec![], true)
339            }
340            Err(e) => return Err(e),
341        };
342
343    // Statistics — schema-scoped. Attribute rows resolve stxkeys attnums to
344    // column names. Expression rows are bulk-fetched for all managed schemas.
345    let statistics_rows = querier.fetch(CatalogQuery::Statistics, &managed)?;
346    let statistic_attributes_rows = querier.fetch(CatalogQuery::StatisticAttributes, &managed)?;
347    let statistic_expressions_rows = querier.fetch(CatalogQuery::StatisticExpressions, &managed)?;
348
349    // Collations — schema-scoped. User-defined only; built-ins and extension-
350    // owned collations filtered at the SQL layer.
351    let collations_rows = querier.fetch(CatalogQuery::Collations, &managed)?;
352
353    // Casts — database-global. System / extension-owned casts excluded at the
354    // SQL layer. Takes no schema param.
355    let casts_rows = querier.fetch(CatalogQuery::Casts, &[])?;
356
357    let raw = assemble::RawRows {
358        version,
359        schemas: schemas_rows,
360        tables: tables_rows,
361        columns: columns_rows,
362        constraints: constraints_rows,
363        indexes: indexes_rows,
364        sequences: sequences_rows,
365        dependencies: dependencies_rows,
366        views_and_mvs: views_and_mvs_rows,
367        view_columns: view_columns_rows,
368        user_types: user_types_rows,
369        enum_values: enum_values_rows,
370        domain_details: domain_details_rows,
371        domain_checks: domain_checks_rows,
372        composite_attributes: composite_attributes_rows,
373        functions: functions_rows,
374        aggregates: aggregates_rows,
375        extensions: extensions_rows,
376        triggers: triggers_rows,
377        partitioned_tables: partitioned_tables_rows,
378        partitions: partitions_rows,
379        default_privileges: default_privileges_rows,
380        policies: policies_rows,
381        publications: publications_rows,
382        publication_rels: publication_rels_rows,
383        publication_namespaces: publication_namespaces_rows,
384        publication_attributes: publication_attributes_rows,
385        event_triggers: event_triggers_rows,
386        subscriptions: subscriptions_rows,
387    };
388    let (mut catalog, mut drift) = assemble::assemble(raw, filter)?;
389    drift.unreadable_subscriptions = unreadable_subscriptions;
390
391    // Assemble statistics after the main assemble pass. All three row sets
392    // (base rows, attribute rows, expression rows) are already bulk-fetched.
393    catalog.statistics = assemble::statistics::assemble_statistics(
394        &statistics_rows,
395        &statistic_attributes_rows,
396        &statistic_expressions_rows,
397    )?;
398
399    // Assemble collations from the bulk-fetched rows.
400    catalog.collations = assemble::collations::build_collations(&collations_rows)?;
401
402    // Assemble casts from the bulk-fetched rows.
403    catalog.casts = assemble::casts::assemble_casts(&casts_rows, &mut drift)?;
404
405    Ok((catalog.canonicalize()?, drift))
406}
407
408#[cfg(test)]
409mod tests {
410    use super::*;
411    use std::cell::RefCell;
412    use std::collections::HashMap;
413
414    /// Mock querier that returns canned rows by query name.
415    struct MockQuerier {
416        rows: RefCell<HashMap<CatalogQuery, Vec<Row>>>,
417    }
418
419    impl MockQuerier {
420        fn new() -> Self {
421            Self {
422                rows: RefCell::new(HashMap::new()),
423            }
424        }
425        fn set(&self, q: CatalogQuery, rows: Vec<Row>) {
426            self.rows.borrow_mut().insert(q, rows);
427        }
428    }
429
430    impl CatalogQuerier for MockQuerier {
431        fn fetch(
432            &self,
433            q: CatalogQuery,
434            _text_array_param: &[&str],
435        ) -> Result<Vec<Row>, CatalogError> {
436            Ok(self.rows.borrow().get(&q).cloned().unwrap_or_default())
437        }
438    }
439
440    #[test]
441    fn empty_catalog_round_trips() {
442        let m = MockQuerier::new();
443        m.set(
444            CatalogQuery::PgVersion,
445            vec![Row::new().with("server_version_num", Value::Integer(160_000))],
446        );
447        let filter = CatalogFilter::new(vec![], vec![]).unwrap();
448        let (cat, drift) = read_catalog(&m, &filter).expect("reads");
449        assert!(cat.tables.is_empty());
450        assert!(cat.schemas.is_empty());
451        assert!(drift.pending_validation.is_empty());
452        assert!(drift.invalid_indexes.is_empty());
453    }
454}