Skip to content

perf(connections): read less of the catalog on connect, commit and database switch - #4445

Merged
datlechin merged 8 commits into
mainfrom
perf/connections
Nov 23, 2026
Merged

datlechin merged 8 commits into
mainfrom
perf/connections

Conversation

@datlechin

@datlechin datlechin commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Perf audit 2026-11-22, connections lane (findings 5, 9 and 11).

  • A COMMIT or ROLLBACK refreshes only what changed inside the transaction (C9, D3).
    • CatalogChangeService keeps the catalog kinds a statement changed while the connection holds a transaction, and refreshes those at COMMIT, ROLLBACK or the transaction bar's end. A data-only transaction refreshes nothing.
    • A call to a function outside the Read-Only list counts as changing objects and schemas.
    • A connection whose driver cannot say whether it holds a transaction (SessionTransactionTracker.standing(of:) is .unknown) still refreshes objects and schemas at every end.
    • Edits the app runs on the session driver inside the user's transaction (Create Table on PGlite, schema and trigger edits) are kept the same way, so a ROLLBACK refreshes them away.
    • Kinds stay kept while any lane still holds a transaction.
  • The completion sync no longer leases a connection (C5b).
    • CatalogDriverFacts (current schema, bulk column support) is stored beside each catalog's tables when they commit; resetForDatabase(_:tables:facts:connection:) takes it.
    • SQLSchemaProvider.cachedDriver and loadSchema(using:) are gone. The provider reads columns through its own scope's lease.
  • The first catalog load reads the tables on their own lease, then each other kind on a lease of its own (C5a).
    • A table clicked while routines load gets its columns read before the triggers, not after the whole catalog.
    • Completion gets the tables as soon as they commit. A refresh keeps one held lease.
    • A load queued behind another for the same catalog reads nothing.
  • MySQL reads lower_case_table_names once for catalog reads running side by side (C5d).
  • Switching back to a database shown in the last five minutes keeps its catalog when no catalog change, reconnect or failed read reached it (C11a, D5).
    • SchemaService records when each catalog committed and marks it stale on those events. A stale or older catalog is read again, as before. Sidebar state is still pruned.

Measured on the Mac mini. Before: Release build of 9d0d481 (universal). After: build.sh app arm64 --unsigned of 2801608, this branch merged with main at 00f003e. The review fixes after it change only concurrent loads and paths the harness does not drive. Harness: the perf audit's db-roundtrips, --phases count --sessions main --engines pg,mysql (statements and bytes at the 0 ms proxy) and --phases wall --sessions click --engines pg (200 ms RTT). One run each; load1 19 to 107, which the counts do not depend on.

Action Before After
PostgreSQL BEGIN; UPDATE; COMMIT; in the editor: statements, bytes down 12, 142,922 B 3, 55 B
MySQL, same 11, 45,813 B 4, 85 B
PostgreSQL database switch: connections opened 3 2
MySQL database switch: connections opened 2 1
PostgreSQL switch back: statements, bytes down 13, 177,595 B 6, 36,033 B
MySQL connect: statements 16 15
MySQL refresh: statements 11 10
PostgreSQL table clicked while the catalog loads, 200 ms RTT: catalog reads sent ahead of its columns read 4, 4 1, 0
Same: columns read sent after the click 0.98 s, 0.97 s 0.42 s, 0.19 s
Same: rows read sent after the click 1.23 s, 1.21 s 0.67 s, 0.42 s
  • Commit: the 8 (PostgreSQL) and 7 (MySQL) catalog reads after the COMMIT are gone.
  • Click: two interleaved pairs at load1 3 to 6. Times are send times at the proxy, so the 200 ms RTT sets them more than the load. The connect's round-trip depth stays 19: the side kinds still go one after another on one connection (C5c below).
  • Switch back on PostgreSQL reads no catalog list: its 7 catalog reads and the all-columns read are gone.
  • The connection the completion sync held through a switch is not opened any more. The switch back now opens one when a read needs it (0 to 1 on both engines), so a switch and its way back open the same total.
  • The second table open (PostgreSQL 10 to 8, MySQL 9 to 8) comes from perf(sidebar): pace the filter by its last refresh and rank Open Quickly without sorting every match #4433, merged since the before build.
  • Rechecked against main at 00f003e (this branch's merge base) and two more runs of the final head dd909dd: main matches the before column within one ping, and the final head reads no catalog after the COMMIT and none on the PostgreSQL switch back.
  • After a dropped connection on MySQL, the reconnect plus the tree layout and expand read 37, 37, 44, 44 statements here and 20, 20, 38, 38 on main at 00f003e (4 runs each). Split by phase, each part costs the same or less here:
    • the refresh after the drop: 9 and 9;
    • the later redial of the session connection with its catalog reload: 12 here, 15 on main (one lower_case_table_names read and one connection fewer);
    • the tree's own lists: 11 to 14 on main, 11 to 16 here, by how many pooled connections they open (2 statements each on MySQL).
    • The totals differ by when the session redial lands, which the health check sets: outside the measured windows (20, main), before the tree layout (37, 38), or after it, when the tree reads its lists again after the redial (44). PostgreSQL shows the same 24-statement burst on main and here.

Tests changed on purpose:

  • CatalogChangeServiceTests: "every transaction end refreshes objects and schemas, whichever driver ran the change" is replaced by the cases below.
  • SchemaServiceTests: "Switching back to a database shows its tables at once and refreshes them without loading" now first lets a change reach the catalog.
  • SchemaServiceDatabaseSwitchTests: "A lazy load reads a kept catalog again until something has refreshed it" now marks the catalog stale first.
  • SQLSchemaProviderTests: 6 cases removed with the cached driver: "loadSchema fetches tables without bulk column loading", "A failed loadSchema leaves the provider empty and does not throw", "resetForDatabase clears columns, updates tables, and sets driver", "getColumns returns empty when driver is not available", "getColumns uses injected metadata source instead of cached driver", "A requested column preload uses the injected metadata source instead of the cached driver". Provider tests build their provider through the new helper DriverBackedSchemaProvider.

Deliberately not fixed here:

  • An UPDATE whose trigger runs DDL inside a transaction is not seen at COMMIT (Codex P2, repeated each round). Declined under D3: a COMMIT of data changes may leave the sidebar as it is, autocommit writes behave the same today, and Refresh shows it.
  • MySQL switch back still reads the catalog (13 to 14 statements). An engine that switches database on its live connection (MySQL, MariaDB) never calls SchemaService.show(scope:type:) on the switch, so the old database's catalog stays shown until the new load lands and the kept one is never pointed at. Showing the kept catalog there changes what those engines show during a switch; follow-up.
  • C4, a table held behind another tab's query read on a new connection (D4 a): cut for size. Needs a leave token on SessionDriverGate, a pooled re-route with its refusal rules, a waiting state on the tab, result pane UI, strings and screenshots. Measured: a table open waits 8.05 s behind pg_sleep(8).
  • C5c, PostgreSQL catalog reads sent as one Query: cut, a read queue in LibPQDriverCore with medium risk. Plan: refresh 7 serial reads to 4 flights, connect depth 13 to 11.
  • C5e, the SSH probe channel kept for the first connection: cut, needs the hand probe through the test sshd to MySQL with connect_timeout = 2. Plan: handshake round trips 9 to 8.
  • C11b, tree lists taken from the schema service: cut for size. Plan: tree layout 6 reads to at most 1 (PostgreSQL), 5 to 0 (MySQL).
  • C15a, C15b, C15c (SQL Server): no SQL Server on the measuring Macs. C15b was cut after review: under MySQL autocommit=0 a MyISAM read answers idle and a following InnoDB read opens a transaction, so skipping the probe after an idle table read hides it. C15c (SELECT SCHEMA_NAME(), @@VERSION at connect) is in 56f6c31 for the follow-up.

Test plan

  • New: SchemaLeasingLoadTests (10), C9 cases in CatalogChangeServiceTests (9), C11a cases in SchemaServiceTests (7), SchemaRefreshServiceTests (3), SessionTransactionTrackerTests standing, MySQLSchemaNameCaseTests (3).
  • verify.sh test (the whole Unit plan) on the Mac mini at dd909dd: 33,376 passed, 0 failed, 2 skipped.
  • Codex adversarial review, then review rounds until only the declined trigger P2 was left. Rounds on the merged head found and fixed: app-run DDL inside the user's transaction lost its end refresh; a hierarchical catalog whose read failed stayed current; a load queued behind another for the same catalog read the tables again.

@datlechin
datlechin marked this pull request as ready for review November 23, 2026 15:57
@datlechin
datlechin enabled auto-merge (squash) November 23, 2026 15:57
@datlechin
datlechin disabled auto-merge November 23, 2026 17:57
@datlechin
datlechin enabled auto-merge (squash) November 23, 2026 17:59
@datlechin
datlechin merged commit d9b6120 into main Nov 23, 2026
17 of 18 checks passed
@datlechin
datlechin deleted the perf/connections branch November 27, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant