fix(plugin-postgresql): keep the selected schema applied after reconnect#1542
Merged
Conversation
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.
Closes #1540.
Problem
When a schema is selected in TablePro (sidebar picker or the database tree), unqualified table names in the editor stop resolving against it, so the user has to prefix every table (
myschema.users).Root cause
Selecting a schema already runs
SET search_path TO "schema", publicon the main connection, and editor queries run on that same connection, so it works at first. ButLibPQDriverCoresilently reconnects on connection loss (executeWithReconnect->reconnect()->connect()).connect()re-probescurrent_schema()(the server default) and never re-applies the user's selection. PostgreSQL drops idle connections routinely, so the first query after a pause triggers a silent reconnect, thesearch_pathreverts, and from then on unqualified queries resolve to the wrong schema. The reconnect is invisible to the layers that would otherwise re-apply the schema, so their recovery never fires.Fix
The driver core now remembers the user-selected schema and re-applies it whenever a physical connection is established, so
search_pathstays in sync with the selection across every reconnect path (silent core reconnect, health-monitor, future).LibPQDriverCorestoresselectedSchema(distinct from the server-reportedcurrentSchema).connect()re-applies it after thecurrent_schema()probe, updatingcurrentSchemaonly if the apply succeeds. A newapplySchema(_:)records the selection only after theSETsucceeds, so a failed switch can't poison later reconnects.switchSchemaroutes throughapplySchema, removing the duplicated inline escaping.SET search_pathstatement is built by a purePostgreSQLSchemaQueries.setSearchPath(toSchema:)with identifier quoting (doubles"), so a schema name with quotes can't break out of the identifier.This covers PostgreSQL, Redshift, and CockroachDB (shared
LibPQDriverCore). No PluginKit protocol change, so no ABI bump. The metadata connection pool already scopes its own connections per schema, so introspection is unaffected.Tests
PostgreSQLSearchPathTestscovers the statement builder: plain schema, mixed case, an embedded double quote, and a quote-break injection attempt. It runs in the test target via the existingPluginTestSourcessymlink (no libpq dependency). The reconnect-restore behavior needs a live PostgreSQL connection, so it is verified at runtime rather than in unit tests.Notes
ALTER SESSION SET CURRENT_SCHEMA); left as a follow-up since it is a separate registry plugin with its own release.statement_timeout(applyQueryTimeout) has the same reset-on-silent-reconnect pattern. Out of scope here, worth a separate fix.