Skip to content

refactor: optimize table metadata parsing in PostgresIntrospector#1776

Open
rubenferreira97 wants to merge 1 commit intokysely-org:nextfrom
rubenferreira97:master
Open

refactor: optimize table metadata parsing in PostgresIntrospector#1776
rubenferreira97 wants to merge 1 commit intokysely-org:nextfrom
rubenferreira97:master

Conversation

@rubenferreira97
Copy link
Copy Markdown
Contributor

Motivation / Context
This PR is a follow-up optimization to the O(n) improvements made in #1774 for the Postgres introspector bottleneck reported in #1707.

While the previous implementation successfully reduced the time complexity to O(n) using a single Map, profiling showed that allocating composite string keys (`schema:${schema};table:${table}`) on every iteration created unnecessary overhead for databases with tens of thousands of columns. Furthermore, calling Array.from(tableDictionary.values()) at the end added an extra iteration step.

Changes

  • Replaced the single string-keyed Map with a nested structure: Map<string, Map<string, TableMetadata>> (Schema -> Table -> Metadata).
  • Eliminated the composite string key allocations inside the hot loop.
  • Introduced a flat tables array to push newly created references directly, removing the need for Array.from() at the end of the execution.

Performance Impact
I've manually benchmarked these changes locally to confirm the performance gains. To keep the PR clean and avoid polluting the repository with one-off testing files, I haven't included the benchmark scripts in the code changes.

This approach maintains the O(n) lookup complexity but significantly lowers the constant factor overhead by avoiding repetitive string instantiation. Based on previous benchmarks for large schemas (~50,000+ columns), this nested map approach benchmarks roughly ~5x faster (in node) than the string-key lookup method.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 2, 2026

@rubenferreira97 is attempting to deploy a commit to the Kysely Team Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants