Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/user/content/sql/system-catalog/mz_internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -1689,10 +1689,14 @@ The `mz_webhook_sources` table contains a row for each webhook source in the sys
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_builtin_indexes -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_builtin_log_indexes -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_builtin_materialized_views -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_builtin_object_mappings -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_builtin_sources -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_builtin_tables -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_builtin_views -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_catalog_item_metadata -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_catalog_items -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_catalog_raw -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_catalog_state -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_cluster_replica_size_internal -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_cluster_workload_classes -->
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_compute_error_counts_raw_unified -->
Expand Down
6 changes: 6 additions & 0 deletions src/catalog/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,12 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne

let mut builtin_items = vec![
Builtin::Source(&MZ_CATALOG_RAW),
Builtin::MaterializedView(&MZ_CATALOG_STATE),
Builtin::MaterializedView(&MZ_CATALOG_ITEMS),
Builtin::View(&MZ_CATALOG_ITEM_METADATA),
Builtin::Index(&MZ_CATALOG_ITEM_METADATA_IND),
Builtin::View(&MZ_BUILTIN_OBJECT_MAPPINGS),
Builtin::Index(&MZ_BUILTIN_OBJECT_MAPPINGS_IND),
Builtin::Log(&MZ_ARRANGEMENT_SHARING_RAW),
Builtin::Log(&MZ_ARRANGEMENT_BATCHES_RAW),
Builtin::Log(&MZ_ARRANGEMENT_RECORDS_RAW),
Expand Down
222 changes: 222 additions & 0 deletions src/catalog/src/builtin/mz_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ use super::{
SUPPORT_SELECT,
};

// TODO(SQL-932): every builtin relation but `mz_catalog_state` and the audit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, there is SQL-948, which I think we should treat as a blocker for this PR: in a 0dt upgrade, a new builtin MV such as mz_catalog_items, and everything that reads it, only hydrates after promotion.

With #40843 on top, those readers include the 12 catalog-server indexes that depend on mz_types, such as mz_objects_ind, mz_show_columns_ind and pg_type_all_databases_ind. (Shipping the readers a release later would avoid this problem on Cloud, but self-managed upgrades can skip releases.)

@ggevay ggevay Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a draft PR for SQL-948:
#40903
I'll try to undraft it today.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, will undraft it tomorrow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries and thanks for working on it to unblock me!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#40903 has merged, so this is unblocked.

// log should stop reading this source and read `mz_catalog_state` instead.
pub static MZ_CATALOG_RAW: LazyLock<BuiltinSource> = LazyLock::new(|| BuiltinSource {
name: "mz_catalog_raw",
schema: MZ_INTERNAL_SCHEMA,
Expand All @@ -53,6 +55,226 @@ pub static MZ_CATALOG_RAW: LazyLock<BuiltinSource> = LazyLock::new(|| BuiltinSou
access: vec![],
ontology: None,
});

/// Every catalog record but the audit log, with its kind as a column.
///
/// The audit log is append-only, so a relation reading `mz_catalog_raw` decodes
/// the whole audit log at hydration. To optimize on hydration time,
/// catalog relations read this copy instead and filter on `kind`.
pub static MZ_CATALOG_STATE: LazyLock<BuiltinMaterializedView> =
LazyLock::new(|| BuiltinMaterializedView {
name: "mz_catalog_state",
schema: MZ_INTERNAL_SCHEMA,
oid: oid::MV_MZ_CATALOG_STATE_OID,
desc: RelationDesc::builder()
.with_column("kind", SqlScalarType::String.nullable(false))
.with_column("data", SqlScalarType::Jsonb.nullable(false))
.finish(),
column_comments: BTreeMap::new(),
sql: "
IN CLUSTER mz_catalog_server
WITH (ASSERT NOT NULL kind) AS
SELECT data->>'kind' AS kind, data
FROM mz_internal.mz_catalog_raw
WHERE data->>'kind' <> 'AuditLog'",
is_retained_metrics_object: false,
access: vec![],
ontology: None,
});
/// Every catalog item that has an `Item` record: user items, including temporary ones, and
/// runtime-alterable builtins such as `mz_internal.mz_analytics`.
/// This is an intermediate layer, intended to be used by
/// downstream builtin item relations (e.g. mz_sources). Without it,
/// they would need to parse DDL statements of all items in the durable catalog
/// then filter down to their specific object type.
///
/// A temporary item has its owning session in `ephemeral_owner_session`, and its
/// `schema_id` is the placeholder `u0`.
///
/// A `create_sql` that fails to decode errors this relation and every reader. Once SQL-795
/// is implemented, per-row errors can be raised in readers through `mz_unsafe.mz_error_if_null`.
pub static MZ_CATALOG_ITEMS: LazyLock<BuiltinMaterializedView> =
LazyLock::new(|| BuiltinMaterializedView {
name: "mz_catalog_items",
schema: MZ_INTERNAL_SCHEMA,
oid: oid::MV_MZ_CATALOG_ITEMS_OID,
desc: RelationDesc::builder()
.with_column("id", SqlScalarType::String.nullable(false))
.with_column("global_id", SqlScalarType::String.nullable(false))
.with_column("oid", SqlScalarType::Oid.nullable(false))
.with_column("schema_id", SqlScalarType::String.nullable(false))
.with_column("name", SqlScalarType::String.nullable(false))
.with_column("owner_id", SqlScalarType::String.nullable(false))
.with_column(
"privileges",
SqlScalarType::Array(Box::new(SqlScalarType::MzAclItem)).nullable(false),
)
.with_column(
"ephemeral_owner_session",
SqlScalarType::Uuid.nullable(true),
)
.with_column("type", SqlScalarType::String.nullable(false))
.with_column("create_sql", SqlScalarType::String.nullable(false))
.with_column("redacted_create_sql", SqlScalarType::String.nullable(false))
.with_column("details", SqlScalarType::Jsonb.nullable(false))
.finish(),
column_comments: BTreeMap::new(),
sql: "
IN CLUSTER mz_catalog_server
WITH (
ASSERT NOT NULL id,
ASSERT NOT NULL global_id,
ASSERT NOT NULL oid,
ASSERT NOT NULL schema_id,
ASSERT NOT NULL name,
ASSERT NOT NULL owner_id,
ASSERT NOT NULL privileges,
ASSERT NOT NULL type,
ASSERT NOT NULL create_sql,
ASSERT NOT NULL redacted_create_sql,
ASSERT NOT NULL details
) AS
WITH
items AS (
SELECT
mz_internal.parse_catalog_id(data->'key'->'gid') AS id,
mz_internal.parse_catalog_id(data->'value'->'global_id') AS global_id,
(data->'value'->>'oid')::oid AS oid,
mz_internal.parse_catalog_id(data->'value'->'schema_id') AS schema_id,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also carry ephemeral_owner_session and global_id? Readers need them:

  • Temporary items are Item rows too, and here they get schema_id u0. mz_tables and mz_views report 0 for them, based on ephemeral_owner_session.
  • mz_kafka_sources reads global_id.

Adding columns later would need another replacement step for this MV.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Makes sense to me

data->'value'->>'name' AS name,
mz_internal.parse_catalog_id(data->'value'->'owner_id') AS owner_id,
mz_internal.parse_catalog_privileges(data->'value'->'privileges') AS privileges,
(data->'value'->>'ephemeral_owner_session')::uuid AS ephemeral_owner_session,
data->'value'->'definition'->'V1'->>'create_sql' AS create_sql
FROM mz_internal.mz_catalog_state
WHERE kind = 'Item'
),
decoded AS (
SELECT
items.*,
mz_internal.parse_catalog_create_sql(create_sql) AS details
FROM items
)
SELECT
id,
global_id,
oid,
schema_id,
name,
owner_id,
privileges,
ephemeral_owner_session,
details->>'type' AS type,
create_sql,
mz_internal.redact_sql(create_sql) AS redacted_create_sql,
details
FROM decoded",
is_retained_metrics_object: false,
access: vec![],
ontology: None,
});

/// `mz_catalog_items` without the statement text such that the index holds
/// no DDL text.
pub static MZ_CATALOG_ITEM_METADATA: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
name: "mz_catalog_item_metadata",
schema: MZ_INTERNAL_SCHEMA,
oid: oid::VIEW_MZ_CATALOG_ITEM_METADATA_OID,
desc: RelationDesc::builder()
.with_column("id", SqlScalarType::String.nullable(false))
.with_column("global_id", SqlScalarType::String.nullable(false))
.with_column("oid", SqlScalarType::Oid.nullable(false))
.with_column("schema_id", SqlScalarType::String.nullable(false))
.with_column("name", SqlScalarType::String.nullable(false))
.with_column("owner_id", SqlScalarType::String.nullable(false))
.with_column(
"privileges",
SqlScalarType::Array(Box::new(SqlScalarType::MzAclItem)).nullable(false),
)
.with_column(
"ephemeral_owner_session",
SqlScalarType::Uuid.nullable(true),
)
.with_column("type", SqlScalarType::String.nullable(false))
.with_column("details", SqlScalarType::Jsonb.nullable(true))
.finish(),
column_comments: BTreeMap::new(),
sql: "
SELECT
id,
global_id,
oid,
schema_id,
name,
owner_id,
privileges,
ephemeral_owner_session,
type,
details - 'definition' AS details
FROM mz_internal.mz_catalog_items",
access: vec![],
ontology: None,
});

pub const MZ_CATALOG_ITEM_METADATA_IND: BuiltinIndex = BuiltinIndex {
name: "mz_catalog_item_metadata_ind",
schema: MZ_INTERNAL_SCHEMA,
oid: oid::INDEX_MZ_CATALOG_ITEM_METADATA_IND_OID,
sql: "IN CLUSTER mz_catalog_server
ON mz_internal.mz_catalog_item_metadata (id)",
is_retained_metrics_object: false,
};

/// The catalog ids of builtin objects.
///
/// Builtin objects' ids come from their `GidMapping` records in the catalog.
pub static MZ_BUILTIN_OBJECT_MAPPINGS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
name: "mz_builtin_object_mappings",
schema: MZ_INTERNAL_SCHEMA,
oid: oid::VIEW_MZ_BUILTIN_OBJECT_MAPPINGS_OID,
desc: RelationDesc::builder()
.with_column("schema_name", SqlScalarType::String.nullable(false))
.with_column("name", SqlScalarType::String.nullable(false))
.with_column("object_type", SqlScalarType::String.nullable(false))
.with_column("id", SqlScalarType::String.nullable(false))
.with_column("global_id", SqlScalarType::String.nullable(false))
.finish(),
column_comments: BTreeMap::new(),
// Although these values should never be nullable, JSONB lookup
// types are inferred as nullable. Thus we coalesce to infer as
// non-nullable.
//
// We exclude runtime-alterable builtins to avoid duplicate entries when
// unioning this view with item records in the durable catalog.
sql: Box::leak(
format!(
"
SELECT
COALESCE(data->'key'->>'schema_name', '') AS schema_name,
COALESCE(data->'key'->>'object_name', '') AS name,
COALESCE(data->'key'->>'object_type', '') AS object_type,
COALESCE('s' || (data->'value'->>'catalog_id'), '') AS id,
COALESCE('s' || (data->'value'->>'global_id'), '') AS global_id
FROM mz_internal.mz_catalog_state
WHERE
kind = 'GidMapping' AND
data->'value'->>'fingerprint' <> '{RUNTIME_ALTERABLE_FINGERPRINT_SENTINEL}'"
)
.into_boxed_str(),
),
access: vec![],
ontology: None,
});

pub const MZ_BUILTIN_OBJECT_MAPPINGS_IND: BuiltinIndex = BuiltinIndex {
name: "mz_builtin_object_mappings_ind",
schema: MZ_INTERNAL_SCHEMA,
oid: oid::INDEX_MZ_BUILTIN_OBJECT_MAPPINGS_IND_OID,
sql: "IN CLUSTER mz_catalog_server
ON mz_internal.mz_builtin_object_mappings (schema_name, name)",
is_retained_metrics_object: false,
};

pub static MZ_POSTGRES_SOURCES: LazyLock<BuiltinMaterializedView> = LazyLock::new(|| {
BuiltinMaterializedView {
name: "mz_postgres_sources",
Expand Down
6 changes: 6 additions & 0 deletions src/pgrepr-consts/src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,9 @@ pub const MV_MZ_QUERY_POLICIES_OID: u32 = 17141;
pub const MV_MZ_QUERY_POLICY_RULES_OID: u32 = 17142;
pub const VIEW_MZ_SHOW_QUERY_POLICIES_OID: u32 = 17143;
pub const MV_MZ_QUERY_POLICY_ATTACHMENTS_OID: u32 = 17144;
pub const MV_MZ_CATALOG_STATE_OID: u32 = 17145;
pub const MV_MZ_CATALOG_ITEMS_OID: u32 = 17146;
pub const VIEW_MZ_CATALOG_ITEM_METADATA_OID: u32 = 17147;
pub const INDEX_MZ_CATALOG_ITEM_METADATA_IND_OID: u32 = 17148;
pub const VIEW_MZ_BUILTIN_OBJECT_MAPPINGS_OID: u32 = 17150;
pub const INDEX_MZ_BUILTIN_OBJECT_MAPPINGS_IND_OID: u32 = 17151;
4 changes: 4 additions & 0 deletions test/sqllogictest/autogenerated/mz_internal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,14 @@ mz_builtin_index_columns
mz_builtin_indexes
mz_builtin_log_indexes
mz_builtin_materialized_views
mz_builtin_object_mappings
mz_builtin_sources
mz_builtin_tables
mz_builtin_views
mz_catalog_item_metadata
mz_catalog_items
mz_catalog_raw
mz_catalog_state
mz_cluster_auto_scaling_strategies
mz_cluster_deployment_lineage
mz_cluster_reconfigurations
Expand Down
Loading