datastore/postgres: honour PreferSimpleProtocol/PreparedStatements at the pgx layer - #698
Merged
Conversation
… at the pgx layer gorm's postgres driver only honours Config.PreferSimpleProtocol when it parses the DSN itself. The adapter hands it an already-open *sql.DB, so the preference every service sets (and the pool default of true) was silently dropped and pgx stayed in its statement-cache mode. Behind a transaction-mode pooler (PgBouncer, Supavisor, Neon) that fails with "prepared statement ... does not exist / already exists" (SQLSTATE 26000 / 42P05) as soon as pooled connections are reassigned; service-trustage hit this in production on 2026-09-01 and needed a DSN workaround. Set ConnConfig.DefaultQueryExecMode from the connection options: simple_protocol when PreferSimpleProtocol, exec when prepared statements are disabled, cache_statement otherwise. An explicit default_query_exec_mode in the DSN still wins. QueryExecMode is exported so callers can inspect it. Claude-Session: https://claude.ai/code/session_016Bc2hUCUhZkA51qkHQ8UWX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Adapter.OpenConnectionpasses an open*sql.DBtogormpostgres.New, and gorm-postgres only appliesPreferSimpleProtocolwhen it parses the DSN itself. Sopool.WithPreferSimpleProtocol(true)(and the pool default oftrue) never reached pgx, which stayed inQueryExecModeCacheStatement. Behind a transaction-mode pooler (PgBouncer, Supavisor, Neon pooler) that fails withprepared statement "stmtcache_…" does not exist / already exists(SQLSTATE 26000/42P05) once connections are reassigned. service-trustage hit this in production on 2026-09-01 (Supabase pooler :6543) and is running on adefault_query_exec_mode=simple_protocolDSN workaround.Change
applyQueryExecModesetsConnConfig.DefaultQueryExecModefrom the connection options before the pool is created:PreferSimpleProtocolsimple_protocol!PreparedStatementsexec(extended protocol, no server-side prepared statements)cache_statement(pgx default, unchanged)An explicit
default_query_exec_modein the DSN still wins.postgres.QueryExecModeis exported for inspection.Behaviour note
pool.NewPooldefaultsPreferSimpleProtocoltotrue, so services that never overrode it now actually use the simple protocol as documented. Services that want the statement cache should setpool.WithPreferSimpleProtocol(false).Tests: unit tests for mode selection and DSN override;
go test ./datastore/...green locally.https://claude.ai/code/session_016Bc2hUCUhZkA51qkHQ8UWX