Skip to content

Commit 5743c72

Browse files
authored
Index MZ_SOURCE_STATISTICS and decrease its retention (MaterializeInc#19839)
We originally planned to retain 30 days of metrics for this source in order to use it in the console. However, that makes it difficult to index, because it can be very large (hundreds of rows). The lack of index is deemed more serious than the retention policy currently, since we aren't querying this from the console (and have no active plans to do so), and since the lack of index means users that query this relation (even if they only care about the latest values) incur a full scan of a potentially huge persist shard. In one customer environment, a simple `SELECT count(*) FROM mz_internal.mz_source_statistics` was observed to cause `mz_introspection` to lose interactivity for >30s reproducibly. ### Motivation * This PR fixes a previously unreported bug. Querying `mz_internal.mz_source_statistics` causes the `mz_introspection` cluster to lose interactivity in deployments with a large (but realistic) number of sources. ### Checklist - [x] This PR has adequate test coverage / QA involvement has been duly considered. - [x] This PR has an associated up-to-date [design doc](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/README.md), is a design doc ([template](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/00000000_template.md)), or is sufficiently small to not require a design. <!-- Reference the design in the description. --> - [x] If this PR evolves [an existing `$T ⇔ Proto$T` mapping](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/command-and-response-binary-encoding.md) (possibly in a backwards-incompatible way), then it is tagged with a `T-proto` label. - [x] If this PR will require changes to cloud orchestration, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label ([example](https://github.com/MaterializeInc/cloud/pull/5021)). <!-- Ask in #team-cloud on Slack if you need help preparing the cloud PR. --> - [x] This PR includes the following [user-facing behavior changes](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/guide-changes.md#what-changes-require-a-release-note): - Fix a bug where querying `mz_internal.mz_source_statistics` could cause system instability in some cases.
1 parent 1a7460a commit 5743c72

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/adapter/src/catalog/builtin.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ pub static MZ_SOURCE_STATISTICS: Lazy<BuiltinSource> = Lazy::new(|| BuiltinSourc
19861986
.with_column("bytes_received", ScalarType::UInt64.nullable(false))
19871987
.with_column("envelope_state_bytes", ScalarType::UInt64.nullable(false))
19881988
.with_column("envelope_state_count", ScalarType::UInt64.nullable(false)),
1989-
is_retained_metrics_object: true,
1989+
is_retained_metrics_object: false,
19901990
});
19911991
pub static MZ_SINK_STATISTICS: Lazy<BuiltinSource> = Lazy::new(|| BuiltinSource {
19921992
name: "mz_sink_statistics",
@@ -1999,7 +1999,7 @@ pub static MZ_SINK_STATISTICS: Lazy<BuiltinSource> = Lazy::new(|| BuiltinSource
19991999
.with_column("messages_committed", ScalarType::UInt64.nullable(false))
20002000
.with_column("bytes_staged", ScalarType::UInt64.nullable(false))
20012001
.with_column("bytes_committed", ScalarType::UInt64.nullable(false)),
2002-
is_retained_metrics_object: true,
2002+
is_retained_metrics_object: false,
20032003
});
20042004

20052005
pub static MZ_STORAGE_SHARDS: Lazy<BuiltinSource> = Lazy::new(|| BuiltinSource {
@@ -3708,6 +3708,24 @@ ON mz_internal.mz_source_status_history (source_id)",
37083708
is_retained_metrics_object: false,
37093709
};
37103710

3711+
pub const MZ_SOURCE_STATISTICS_IND: BuiltinIndex = BuiltinIndex {
3712+
name: "mz_source_statistics_ind",
3713+
schema: MZ_INTERNAL_SCHEMA,
3714+
sql: "CREATE INDEX mz_source_statistics_ind
3715+
IN CLUSTER mz_introspection
3716+
ON mz_internal.mz_source_statistics (id)",
3717+
is_retained_metrics_object: false,
3718+
};
3719+
3720+
pub const MZ_SINK_STATISTICS_IND: BuiltinIndex = BuiltinIndex {
3721+
name: "mz_sink_statistics_ind",
3722+
schema: MZ_INTERNAL_SCHEMA,
3723+
sql: "CREATE INDEX mz_sink_statistics_ind
3724+
IN CLUSTER mz_introspection
3725+
ON mz_internal.mz_sink_statistics (id)",
3726+
is_retained_metrics_object: false,
3727+
};
3728+
37113729
pub const MZ_CLUSTER_REPLICAS_IND: BuiltinIndex = BuiltinIndex {
37123730
name: "mz_cluster_replicas_ind",
37133731
schema: MZ_INTERNAL_SCHEMA,
@@ -4077,6 +4095,8 @@ pub static BUILTINS_STATIC: Lazy<Vec<Builtin<NameReference>>> = Lazy::new(|| {
40774095
Builtin::Index(&MZ_CLUSTERS_IND),
40784096
Builtin::Index(&MZ_SOURCE_STATUSES_IND),
40794097
Builtin::Index(&MZ_SOURCE_STATUS_HISTORY_IND),
4098+
Builtin::Index(&MZ_SOURCE_STATISTICS_IND),
4099+
Builtin::Index(&MZ_SINK_STATISTICS_IND),
40804100
Builtin::Index(&MZ_CLUSTER_REPLICAS_IND),
40814101
Builtin::Index(&MZ_CLUSTER_REPLICA_SIZES_IND),
40824102
Builtin::Index(&MZ_CLUSTER_REPLICA_STATUSES_IND),

test/sqllogictest/cluster.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,15 @@ CREATE CLUSTER test REPLICAS (foo (SIZE '1'));
405405
query I
406406
SELECT COUNT(name) FROM mz_indexes;
407407
----
408-
114
408+
116
409409

410410
statement ok
411411
DROP CLUSTER test CASCADE
412412

413413
query T
414414
SELECT COUNT(name) FROM mz_indexes;
415415
----
416-
91
416+
93
417417

418418
statement error nvalid SIZE: must provide a string value
419419
CREATE CLUSTER REPLICA default.size_1 SIZE;

test/testdrive/catalog.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ test_table
631631

632632
# There is one entry in mz_indexes for each field_number/expression of the index.
633633
> SELECT COUNT(id) FROM mz_indexes WHERE id LIKE 's%'
634-
91
634+
93
635635

636636
# Create a second schema with the same table name as above
637637
> CREATE SCHEMA tester2

test/testdrive/indexes.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,7 @@ mz_show_sources_ind mz_sources
329329
mz_show_tables_ind mz_tables mz_introspection {schema_id}
330330
mz_show_types_ind mz_types mz_introspection {schema_id}
331331
mz_show_views_ind mz_views mz_introspection {schema_id}
332+
mz_sink_statistics_ind mz_sink_statistics mz_introspection {id}
333+
mz_source_statistics_ind mz_source_statistics mz_introspection {id}
332334
mz_source_status_history_ind mz_source_status_history mz_introspection {source_id}
333335
mz_source_statuses_ind mz_source_statuses mz_introspection {id}

0 commit comments

Comments
 (0)