Repository navigation
perf(postgres,cockroachdb): stop rescanning the catalog for each table - #13050
samuelmbabhazi wants to merge 4 commits into
Conversation
… size loadTables scanned every catalog array once per table, and the constraints once per column, so schema loading grew with tables times rows. The rows are now grouped once by table and by constraint name. Lookups that were global by constraint name stay global, so the loaded tables do not change.
commit: |
Code Review by Qodo
1. Added explanatory comment repeats the code's purpose
|
Description of change
Fixes #12745.
loadTables()scanned the column, constraint, index and foreign key arrays once per table, and the constraints again once per column, so schema loading grew with tables times rows. The rows are now grouped once before the loop, by table, by column and by constraint name.OrmUtils.uniqstill compares within one table's constraints, which I left alone because it is shared code. CockroachDB had the same loop, so it gets the same change.This is meant to change speed only. Lookups that were global by constraint name stay global, so the loaded tables are the same.
Measured against real databases, running the old and new
loadTableson the same schema (19 columns per table, composite primary key, two unique constraints, a check, a foreign key and an index), and comparing the loaded tables withisDeepStrictEqual:A second comparison covered a harder schema on both databases: enums, arrays, generated columns, a partial unique index, a composite foreign key, a table with no constraints, same-named constraints across schemas, and an exclusion constraint on PostgreSQL. The loaded tables were identical there too.
The new test creates a same-named table in two schemas and checks that each one gets only its own columns, foreign key and index. It fails if the grouping key leaves out the schema.
Test runs: the full suite on PostgreSQL 17.9 has 3154 passing and one failure,
RedisQueryResultCache Integration, because testcontainers can't reach Docker on my machine. On CockroachDB v24.3.8 the schema builder, query runner, database schema and migration suites have 541 passing.Most of the diff is indentation, because the column mapping lost a level of nesting. It's easier to read with whitespace hidden (
?w=1).This overlaps #12916, which rewrites the same loop to run sequentially. Whichever lands second needs a rebase. I'm happy to do it if this one goes second.
One thing I left alone on purpose: unique, check and foreign key constraints are looked up by name across all tables. Two tables with a constraint of the same name, for example the same table in two schemas, each get the columns of both. I can open a separate issue for it.
Pull-Request Checklist
masterbranchFixes #12745tests/**.test.ts)docs/docs/**.md) N/A, no behaviour change