Repository navigation
Description
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 xTablePro Metadata(pool entries). - The connect batch runs once per connection, so
current_schema()and its probes run 3 times. - The table list of
publicis 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:
armPostConnectSchemaLoadwaits fordatabaseDidConnect(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).metadataRoutesends 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.
acquireEntrycallsstartOpen, which runsopenServerDriver: a new driver,connect(), thenprepareSession(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.bulkreads (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.bulklease,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
.bulklease take an idle.interactiveentry 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:
SchemaServiceholds 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_1monce). 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 (
dnctlor Network Link Conditioner), direct and over SSH, median of 10, with signposts around connect, pool open andfetchTables.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
#4575 ships the connection routing and the switchers' lists. Left for a follow-up:
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.populatereads tables and schemas for scopes other than the browse scope, which no other owner holds, so it is unchanged.