Repository navigation
Conversation
`loadUserDefinedTypes` built its oid -> typname map with a reduce that
spread the accumulator on every row, copying the whole map n times. That
is quadratic and fully synchronous, so it blocks the event loop.
This went unnoticed while the query only selected `typcategory in ('U', 'E')`
(~20 rows on a stock Postgres). #11149 added `'A'` so that array-typed
columns get mapped, and Postgres auto-creates an array type for every
relation — the row count now scales with the number of tables in the
database, not with the number of actual user defined types.
Measured on postgres:16, `SELECT count(*) FROM pg_type WHERE typcategory
in ('U', 'E', 'A')`:
| tables in database | rows | spread reduce | in-place loop |
| ------------------ | ---- | ------------- | ------------- |
| 0 | 313 | 6.3 ms | 0.12 ms |
| 5000 | 5313 | 2355 ms | 0.45 ms |
| ~20000 | - | 35116 ms | 1.10 ms |
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Claude finished @ovr's task in 55s —— View job Review: correct and behavior-preserving — 0 blocking, 2 low (non-blocking, pre-existing)Full review
VerdictThe change is correct and the reasoning in the description holds. Behavior equivalence — worth stating explicitly since the rewrite flips duplicate-key precedence:
This is a no-op in practice because Also verified the consumer is unaffected: Non-blocking observations1. The map still scales with table count ( This PR fixes the quadratic build, but the underlying query still returns one array type per relation, so on a ~20k-table database you retain a ~20k-entry map per driver instance and transfer those rows on first connect. That's now milliseconds and a few MB rather than 35 s, so it's fine — but if this ever needs tightening, narrowing the 2. Concurrent first-connect does redundant work — low, pre-existing
Testing / docs
Nothing else to flag. LGTM. |
…11586) `loadUserDefinedTypes` built its oid -> typname map with a reduce that spread the accumulator on every row, copying the whole map n times. That is quadratic and fully synchronous, so it blocks the event loop. This went unnoticed while the query only selected `typcategory in ('U', 'E')` (~20 rows on a stock Postgres). #11149 added `'A'` so that array-typed columns get mapped, and Postgres auto-creates an array type for every relation — the row count now scales with the number of tables in the database, not with the number of actual user defined types. Measured on postgres:16, `SELECT count(*) FROM pg_type WHERE typcategory in ('U', 'E', 'A')`: | tables in database | rows | spread reduce | in-place loop | | ------------------ | ---- | ------------- | ------------- | | 0 | 313 | 6.3 ms | 0.12 ms | | 5000 | 5313 | 2355 ms | 0.45 ms | | ~20000 | - | 35116 ms | 1.10 ms |
Check List
Description of Changes Made
loadUserDefinedTypesbuilt its oid -> typname map with a reduce that spread the accumulator on every row, copying the whole map n times — quadratic and fully synchronous, so it blocks the event loop. This went unnoticed while the query only selectedtypcategory in ('U', 'E')(~20 rows on a stock Postgres), but #11149 added'A'so that array-typed columns get mapped, and Postgres auto-creates an array type for every relation, so the row count now scales with the number of tables in the database rather than with the number of actual user defined types. This replaces the spread-reduce with an in-place loop.Measured on postgres:16,
SELECT count(*) FROM pg_type WHERE typcategory in ('U', 'E', 'A'):