Skip to content

perf(connections): send session setup with the connect instead of in extra round trips #4544

Done

Description

@datlechin

Problem

Several engines spend round trips after login on setup that could ride a request they already send. Each one is a full RTT on every connect, every pool entry and lane, and every reconnect: about 50 ms each at +50 ms, more through an SSH tunnel (estimates from the round-trip count, not measured).

  • PostgreSQL: the connect batch already carries statement_timeout (the statement log shows SET statement_timeout = 60000 inside the first Query on every connection). The saved schema is not: SET search_path goes alone after the batch.
  • MySQL family: the query-timeout SET is its own round trip (two on OceanBase), and an empty Database field restores the last database with a separate USE.
  • Redis: INFO server is sent twice on connect, and the database listing sends CONFIG GET databases and INFO keyspace one after the other.
  • ClickHouse: connect makes up to four sequential HTTP requests.

Cause

  • PostgreSQL: runConnectPlan batches the reads and statement_timeout (apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift:181-213), and applyQueryTimeout skips the timeout the plan applied (:618-626). The selected schema is a separate executeQuery after the batch (:157-162), and an empty current_schema() adds one or two more (:148-152). The post-connect action .selectSchemaFromLastSession sends switchSchemaIfNeeded (apple/TablePro/Core/Database/DatabaseManager+Sessions.swift:445-454). A reconnect calls switchSchema unconditionally (apple/TablePro/Core/Database/DatabaseManager+Health.swift:390-392). Pool entries open on schema nil (apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift:579-584) and moveEntry sends the switch on the first lease (:495).
  • MySQL: applyQueryTimeout runs each queryTimeoutStatements item through executeWithReconnect (apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift:1196-1226, apple/Plugins/MySQLDriverPlugin/MySQLServerFlavor.swift:201-219). The pool repeats it per entry (MetadataConnectionPool.swift:892-909). .selectDatabaseFromLastSession calls plugin.switchDatabase after connect (DatabaseManager+Sessions.swift:421-431), while pool entries already put the database in the connect (MetadataConnectionPool.swift:768-777).
  • Redis: connect runs openContextSync (AUTH or a probe, then SELECT when the index is not 0) and then fetchServerVersionSync, which sends INFO server (apple/Plugins/RedisDriverPlugin/RedisPluginConnection.swift:149-171, :555-566, :918-930). RedisPluginDriver.connect then calls verifyServerMode, which sends INFO server again (apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift:87-100, :169-178). databaseListing awaits CONFIG GET databases, then INFO keyspace (apple/Plugins/RedisDriverPlugin/RedisDatabaseListing.swift:57-73).
  • ClickHouse: SELECT version(), the settings probe, the catalog probe and, when no database is set, SELECT currentDatabase() each wait for the one before (apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift:317-343). A failed probe falls back to defaults without failing the connect (:359-370).

Change

  • PostgreSQL: pass the target schema into the connect plan and add SET search_path to its trailing settings next to statement_timeout. A SET search_path to a missing schema does not error, so it cannot roll back the other settings. This also works through poolers that refuse the startup options parameter. Use it for the session, lanes and pool entries, and make the reconnect restore use switchSchemaIfNeeded.
  • MySQL: send the timeout SET pipelined with the first statement the connection runs, using the mysql_send_query pipelining sendWithSelectLimit already has (apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift:959-1000), and keep today's handling of a refused variable. Put the saved database in the connect handshake, as pool entries do, and connect without it only when the server answers that the database is unknown.
  • Redis: read redis_mode from the INFO server reply already fetched for the version and drop the second INFO. Pipeline SELECT and INFO server after AUTH answers (a failed AUTH must still stop the rest). Send CONFIG GET databases and INFO keyspace in one executePipeline.
  • ClickHouse: ask SELECT version(), currentDatabase() in the first request, which also opens the connection. Keep the settings and catalog probes as their own requests, because each must fail alone without failing the connect. That is 3 requests instead of 4. Parallel requests are not the fix: each opens a new HTTP/1.1 connection with its own TCP and TLS handshake.

Measure

  • Count statements per connect with the server's statement log (PostgreSQL log_statement=all, MySQL general_log, Redis MONITOR, ClickHouse system.query_log) for a session, a pool entry and a reconnect. Targets: no SET search_path and no timeout SET of their own, one INFO server per Redis connect.
  • Connect-to-tables at +50 ms RTT, median of 10, before and after, direct and over SSH.

Part of the performance plan.

Activity

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

    @datlechin
    MemberAuthor

    #4578 ships PostgreSQL (the schema in the connect batch for the session, reconnects, lanes and pool entries; the reconnect restore only when something could have moved it), Redis (SELECT and INFO server in one pipeline after AUTH, one INFO server per connect, the listing's two reads in one pipeline) and ClickHouse (version and database in the first request, 3 requests instead of 4). Left for a follow-up:

    • MySQL: sending the query-timeout SET pipelined with the first statement means deferring it into the execute path, beside sendWithSelectLimit's own pipelining, the reconnect and the refused-variable handling; and putting the saved database in the handshake needs a reconnect without it on ER_BAD_DB_ERROR. Both are their own driver change.
    • An initial schema is not sent when the connection has startup commands, since one of them may move the search path after it; those connections keep the separate SET.
    • The Measure runs against live servers' statement logs and at +50 ms were not run here; the counts in the PR come from the drivers' scripted servers.
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