Repository navigation
SQL-496: Convert the function system tables to catalog views - #40900
SangJunBak wants to merge 3 commits into
Conversation
80fcf1c to
48b2111
Compare
48b2111 to
1023d89
Compare
1023d89 to
caf16d7
Compare
QA LLM Review1. MEDIUM -- Zero-argument functions disappear from arity-zero lookups
After upgrade, DetailsThe old row packer always supplied one dimension with length equal to the argument count. The SQL constructor for 2. MEDIUM -- Small catalog reads now build a dataflow per request
DetailsThe former tables satisfied the unfiltered small-limit fast path at |
ggevay
left a comment
There was a problem hiding this comment.
Some comments inline. The index one and the empty-array one are the important ones.
The description says Closes SQL-495, the title says SQL-496.
| pub static MZ_FUNCTIONS: LazyLock<BuiltinTable> = LazyLock::new(|| { | ||
| BuiltinTable { | ||
| name: "mz_functions", | ||
| pub static MZ_FUNCTIONS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView { |
There was a problem hiding this comment.
The QA bot's second point is wider than LIMIT 1: every read of mz_functions, pg_proc or information_schema.routines now plans and optimizes this view. Medians of 11 runs on the CI images of 8f35c46 (before) and caf16d7 (after):
| query | before | after | after, with indexes |
|---|---|---|---|
SELECT * FROM mz_functions |
48 ms | 116 ms | 28 ms |
SELECT oid FROM mz_functions WHERE name = 'now' |
16 ms | 100 ms | 2.5 ms |
SELECT proname FROM pg_proc WHERE oid = 1299 |
26 ms | 140 ms | 18 ms |
SELECT count(*) FROM information_schema.routines |
21 ms | 124 ms | 13 ms |
SELECT * FROM mz_operators |
42 ms | 66 ms | 23 ms |
| Npgsql's type-loading query | 187 ms | 392 ms | 182 ms |
The last row is the query that Npgsql (the .NET driver) sends when a client first connects. It joins pg_proc twice.
Could we add the indexes the bot suggests, on mz_functions and mz_operators in mz_catalog_server (as mz_types has mz_types_ind)? The last column is with such indexes created ad hoc. Together they hold about 0.3 MB.
| # Argument types resolve to mz_types ids in argument order. | ||
| query TT rowsort | ||
| SELECT a1.name, a2.name | ||
| FROM mz_functions f | ||
| JOIN mz_types a1 ON a1.id = f.argument_type_ids[1] | ||
| JOIN mz_types a2 ON a2.id = f.argument_type_ids[2] | ||
| WHERE f.name = 'date_trunc' AND array_length(f.argument_type_ids, 1) = 2 | ||
| ---- | ||
| text interval | ||
| text timestamp | ||
| text timestamptz |
There was a problem hiding this comment.
The join that resolves argument types depends on its object_type filter, because seven names are both a type and a function (date, daterange, ...). Without the filter, 6 rows of mz_functions get extra argument ids (max and min on date, daterange, extract, datediff), and every check in this file still passes. Maybe compare the argument counts with the generated view:
# Every implementation resolves each of its argument types to exactly one type.
query I
SELECT count(*)
FROM mz_functions f
JOIN mz_internal.mz_builtin_functions b USING (oid)
WHERE coalesce(array_length(f.argument_type_ids, 1), 0) != coalesce(array_length(b.argument_type_names, 1), 0)
----
0
(mz_operators has the same join, but there the pg_operator listing in pg_catalog_proc.slt would notice.)
| fn type_name_sql(name: Option<&str>, type_names: &BTreeSet<&str>) -> String { | ||
| match name { | ||
| Some(name) => { | ||
| // Panic if |
| // Tables, views, materialized views, indexes, types, functions | ||
| // and metric sinks are exposed through materialized views derived | ||
| // from `mz_catalog_raw`, and logs and secrets never had | ||
| // builtin-table rows, so none pack a row here. |
There was a problem hiding this comment.
#40843 changed this to "relations derived from", and this goes back to "materialized views". The type and function relations are plain views now.
| func.oid, | ||
| func_schema.id AS schema_id, | ||
| func.name, | ||
| COALESCE(args.argument_type_ids, ARRAY[]::text[]) AS argument_type_ids, |
There was a problem hiding this comment.
The QA bot's first point: for a function without arguments, array_length(argument_type_ids, 1) was 0 and is NULL now. PostgreSQL's pg_proc.proargtypes gives 0 for such functions too. Could this keep the old value? array_remove(ARRAY[NULL::text], NULL) as the fallback reproduces it. (The check on now in function_relations.slt casts to text, which prints {} for both.)
Closes: [SQL-948](https://linear.app/materializeinc/issue/SQL-948) ### Motivation During a 0dt upgrade, a builtin storage collection that is new in the target version gets its shard inside the read-only process's catalog savepoint, which is never committed, and the promoted leader allocates a different one. So dataflows that hydrated while read-only are rebuilt at cut-over, and anything reading a new collection cannot hydrate before promotion. On main this hits the three query-policy materialized views from #38762, which are small leaves. If #40067's `mz_catalog_items` and #40843 on top of it ship in one upgrade, the 12 catalog-server indexes that depend on `mz_types` only hydrate after cut-over. ### Description Reviewable commit by commit: 1. **Shards from the migration shard.** Builtin tables, sources and materialized views without a shard get theirs from the builtin migration shard, as `Replacement`-migrated collections do, and the read-only process writes them the same way. - An entry is only reused for the same `GlobalId` and builtin. A new builtin's `GlobalId` exists only in the uncommitted savepoint, and a later boot can give it to another builtin, for example after another deploy generation was promoted. - Versions without the new key field decode these entries and never use them. Once released, the field must not be reverted or left out of a backport, or the next version that has it panics on leftover entries. 2. **Leader clock.** For a collection the leader reports no frontier for, such as a new builtin, the caught-up check compares against the leader's clock instead of the current time. The clock is the upper of `mz_cluster_replica_frontiers`, which the leader advances every second. With the old leader down, as when a region is enabled again after a soft delete, such a collection's inputs stop, so against the current time it would lag forever. 3. **New builtins gate promotion.** Only a new storage collection without a shard from the migration shard still seeds the caught-up exclusion. - New views and indexes no longer seed it. Otherwise a new view would keep everything reading it out of the check, such as `mz_types` and its indexes, which read #40843's new `mz_builtin_types`. So a new builtin index over existing collections now has to hydrate before promotion. - With the break-glass flag `enable_0dt_hydrate_migrated_builtin_mvs` off, or a leader older than v26.17, every new builtin seeds it, as before. New tables and sources are written either way. 4. **0dt workflow** `new-builtins-survive-promotion`. It and the two `builtin-schema-migrations-*` workflows now wait until the replica reports its reconciliation metrics, since a leader reports `IsLeader` before it reconnects to its replicas. 5. **Stricter checks in that workflow.** It also compares the `GlobalId`s of the collections read for the new builtins across the promotion, because a replica only counts a dataflow as reused or replaced if it had one with the same ids before. The wait for the reconciliation metrics ends once two reads in a row agree. 6. **Rename** `migrate_replace` to `get_or_insert_shards`, since it now also gets shards for collections that had none. 7. **Shorter leader-down run.** The workflow sets the caught-up check's allowed lag to 10 s on the old leader before stopping it, so that it only has to wait 20 s instead of 75 s. The coverage of tables and sources, the matching by both `GlobalId` and builtin, and the workflow come from Aljoscha's #40904, which fixes the same bug. ### Verification - New unit tests for the allocation and matching, the key encoding, and the leader-clock comparison. Each fails when the behavior it covers is reverted. - The new workflow upgrades from v26.17.1, and from v26.47.1 with the old leader stopped first, after it recorded a `REFRESH` materialized view's frontier in the future. It checks that the new builtin materialized views and sources were written on the read-only generation, promotes, and checks that the collections read for them kept their `GlobalId`s. With a running leader it also asserts that the catalog server replaced no dataflow. With the old leader killed, a promotion can replace dataflows that have nothing to do with new builtins, also on main, so that run does not assert it. On main the workflow fails, because the new builtin materialized views are not written on the read-only generation. - With #40067 and the PRs stacked on it (through #40900) merged in locally, a 0dt upgrade from v26.51.0-rc.3 replaces no dataflow. Without this PR the same upgrade replaces 23: the 12 catalog-server indexes that read `mz_types`, the dataflows of the new collections, and user dataflows that read the converted relations. - Nightly (subsets): https://buildkite.com/materialize/nightly/builds/18803 (upgrade and 0dt jobs, on the first four commits) and https://buildkite.com/materialize/nightly/builds/18805 (0dt jobs, on the first five commits). On the final head, the new workflow and the two `builtin-schema-migrations-*` workflows passed locally on the PR's CI images. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
`mz_functions` and `mz_operators` became views, so the builtin version pin check gets v26.54.0 as their conversion version. The function relations get no migration step. `migrate_builtin_item_types` drops the shard of a builtin that became a view. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
caf16d7 to
fb7d43f
Compare
See commit messages for details.
Much of the data, other than the catalog IDs, come statically since we only have builtin functions. Before reviewing, I'd recommend reading https://claude.ai/artifact/D4cjDcAZj1BNEEi1FoAgVY?sk=OQFwpa83kcrWYEernXwqdw#bg-narrow to get a primer of what column represents.
Motivation
Closes SQL-496
Verification