Skip to content

perf(connections): use the idle session for the first listing and read each list once #4542

Done

Description

@datlechin

Problem

Connecting to PostgreSQL and opening one table opens 3 server connections and sends 19 statements (18 simple Query messages and 1 extended-protocol execute), from the statement log with log_statement=all and log_connections=on (research/pglog/tablepro.log):

  • Connections: TablePro (the session) and 2 x TablePro Metadata (pool entries).
  • The connect batch runs once per connection, so current_schema() and its probes run 3 times.
  • The table list of public is read twice (first metadata entry at 07.797, second at 15.414).
  • The database list is read twice (07.863 and 14.674) and the schema list twice (07.803 and 14.678), both times on the first metadata entry.
  • The session connection is idle from 07.783 until the table page at 17.727. It answers no catalog read except the temp-schema table list.

Each extra PostgreSQL connection is a full startup: TCP, SSLRequest or TLS, SCRAM and the connect batch, about 5-6 round trips without TLS. On localhost it is about 12 ms. Over a +50 ms link it is an estimated 250-300 ms in front of the first table list (estimate from the round-trip count, not measured).

Cause

  • The sidebar load starts only after the session is up: armPostConnectSchemaLoad waits for databaseDidConnect (apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SchemaLoading.swift:40), which is sent after connect and session setup (apple/TablePro/Core/Database/DatabaseManager+Sessions.swift:182).
  • The table list takes a lease through withMetadataDriver (apple/TablePro/Core/Services/Query/SchemaService+CatalogLoad.swift:40). metadataRoute sends it to the pool whenever the engine can pool (apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift:54-60).
  • The pool holds no entry yet and nothing warms it. acquireEntry calls startOpen, which runs openServerDriver: a new driver, connect(), then prepareSession (apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift:418-454, :573-595, :758-817). The connect batch runs again on that connection (apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift:181-213).
  • The pool key holds the workload and the slot (MetadataConnectionPool.swift:45-69), and a busy entry is shared only with a lease for the same schema (:471-486). So .bulk reads (row count, foreign key prefetch, autocomplete population) and an overlapping lease open their own entries.
  • Each list has several owners that read it on their own lease: SchemaService (tables, schemas), DatabaseTreeMetadataService (databases, schemas, tables; apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift:574-620), SchemaProviderRegistry.populate (tables and schemas on a .bulk lease, apple/TablePro/Core/Services/Query/SchemaProviderRegistry.swift:219-221) and the database switcher (apple/TablePro/ViewModels/DatabaseSwitcherViewModel.swift:118-119).

Overlaps #4459: C11b already covers taking the browsed scope's tree lists from the schema service (tree layout 6 reads to at most 1 on PostgreSQL). This issue keeps C11b's scope and adds the connection routing and the other list owners.

Change

  • Lend the session connection to a metadata lease when the pool has no usable entry for the key and the session is idle: no turn queued on SessionDriverGate, no query in flight, no open transaction. Catalog reads already pin their search path, so they do not depend on session state. Take one lease per read, so the gate is never held across the whole catalog and a user query can still cut in.
  • Dial the pooled entry in the background once the session is set, never in parallel with the session's own login (a wrong password must count as one failed login, not two).
  • Let a .bulk lease take an idle .interactive entry before it opens its own, so the first row count or foreign key prefetch does not pay a fresh handshake.
  • One owner per list for a scope: SchemaService holds the database, schema and table lists, and the tree, the database switcher and autocomplete read from it or join its in-flight read. A refresh goes through that owner.
  • Keep the rule that every metadata read goes through withMetadataDriver; only the route changes.

Measure

  • The same statement log run (connect from a URL, open trips_1m once). Target: 1 connection on the critical path plus at most 1 background pool entry, the connect batch at most twice, and each of the table, database and schema lists once.
  • Connect-to-tables at +50 ms and +200 ms RTT (dnctl or Network Link Conditioner), direct and over SSH, median of 10, with signposts around connect, pool open and fetchTables.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 25, 2026

    @datlechin
    MemberAuthor

    #4575 ships the connection routing and the switchers' lists. Left for a follow-up:

    • The tree's own lists for the browsed scope (C11b in perf(connections): C4, C5c, C5e, C11b, C15a-c and MySQL switch back left from #4445 #4459) still read on the tree's lease. The second reads in the statement log came from the quick switcher (database and schema lists) and are gone; the tree layout reads were not in that run.
    • The quick switcher's all-schema listing (fetchTablesInAllSchemas, the read at 15.414) is a different query from the browsed schema's table list, so it stays. On a database with one user schema it lists the same rows.
    • SchemaProviderRegistry.populate reads tables and schemas for scopes other than the browse scope, which no other owner holds, so it is unchanged.
    • The Measure runs (statement log, +50 ms and +200 ms RTT) need a live server and a link conditioner and were not run here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions