Skip to content

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

Merged
datlechin merged 3 commits into
mainfrom
perf/session-setup-with-connect
Nov 27, 2026
Merged

datlechin merged 3 commits into
mainfrom
perf/session-setup-with-connect

Conversation

@datlechin

@datlechin datlechin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

  • PostgreSQL: the schema a connect should land on now rides the connect batch instead of a Query of its own after it. An initial schema is set right after the schema read, before any read pins the path, and only when the session does not already start in it, so a configured path that names more schemas keeps them, as switchSchemaIfNeeded kept them. A schema chosen on the same connection is set beside statement_timeout. The host names it for the session (the last session's schema), a reconnect (the browsed schema), lanes and pool entries (the lease's schema). A SET search_path to a missing schema does not fail, and only a batch that ran to the end counts it as set; otherwise it goes alone as before. It also works through poolers that refuse the startup options parameter. The host names no schema when the user's startup commands run, since one of them may move the search path after it.
  • A reconnect restores the schema with switchSchemaIfNeeded, unless startup commands or a database switch could have moved it, so the schema the connect landed on is not set twice.
  • A pooled connection opens on the schema its first lease wants, so that lease needs no switch.
  • Redis: SELECT and INFO server go out in one pipeline after AUTH has answered (a refused sign-in still stops the rest), and the mode check reads the kept INFO server reply instead of sending a second one. The database listing sends CONFIG GET databases and INFO keyspace in one pipeline when it wants both.
  • ClickHouse: the first request asks SELECT version(), currentDatabase(), so a connect with no database named makes 3 requests instead of 4. The settings and catalog probes stay their own requests, so each still fails alone.

Measured with the drivers' scripted servers: a PostgreSQL connect to a saved schema sends 1 Query where it sent 2 (PostgreSQLConnectRoundTripTests); a ClickHouse connect with no database makes 3 requests where it made 4 (ClickHouseConnectProfileTests); a Redis listing with key counts is 1 pipeline where it was 2 round trips (RedisDatabaseListingTests). The Redis connect drops from 3 round trips after AUTH to 1 for a non-zero database, and from 2 to 1 for database 0 (from the code path; no hiredis stub here). MySQL is left for a follow-up, noted on the issue.

Test plan

  • New: PostgreSQLConnectRoundTripTests/initialSchemaRidesTheBatch, initialSchemaGoesAloneAfterAFailedBatch; RedisDatabaseListingTests/countAndKeyspaceShareAPipeline; ClickHouseConnectProfileTests/blankDatabaseCostsNoExtraRequest; DatabaseManagerTests/reconnectOnTheSchemaSendsNoSwitch, reconnectAfterStartupCommandsSwitches; MetadataConnectionPoolSchemaTests/connectionOpensOnTheLeasesSchema. Each fails on main.
  • Updated: ClickHouseConnectProfileTests answers the opening query, which replaced SELECT version() and SELECT currentDatabase().
  • Local run of the suites above with MetadataConnectionPoolTests and SessionRecoveryLoopTests: 112 executed, 112 passed (TableProTests 68, ClickHouseDriverTests 10, PostgreSQLDriverTests 11, RedisDriverTests 23). verify.sh plugins passes.

Closes #4544

…extra round trips

PostgreSQL lands on the saved schema inside the connect batch, a reconnect restores it only when something could have moved it, Redis pipelines SELECT with INFO server and keeps the reply for the mode check, its listing pipelines CONFIG GET with INFO keyspace, and ClickHouse reads version and database in one request.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:50
@datlechin
datlechin merged commit 0a2120f into main Nov 27, 2026
11 checks passed
@datlechin
datlechin deleted the perf/session-setup-with-connect branch November 27, 2026 16:37
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.

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

1 participant