Fix URL encoding and optimize database connection handling#1
Merged
Conversation
1. Replace quote_plus with quote(safe='') for URL-encoding credentials
in the connection string. SQLAlchemy decodes with urllib.parse.unquote
(not unquote_plus), so quote_plus-encoded spaces ('+') were passed
literally to psycopg2 and caused authentication failures.
2. Use a single engine.connect() context in get_external_schema() and
inspect(conn) instead of inspect(engine). In SQLAlchemy 2.0, inspect(engine)
opens a new DB connection for every reflection method call (get_table_names,
get_columns per table), leading to N+1 connection attempts. Using a
single connection context eliminates this overhead and gives a well-defined
connection lifecycle.
3. Remove pool_pre_ping=True from both get_external_schema() and
execute_sql_on_external(). These engines are created, used once, and
disposed — pool_pre_ping adds a redundant SELECT 1 round-trip before
every use. On cold Neon instances this extra round-trip consumed part of
the 10-second connect_timeout budget, causing spurious timeouts.
https://claude.ai/code/session_016qjAVBXoVuhQD9MNdHQ6Fd
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.
Summary
This PR addresses URL encoding issues in database connection strings and optimizes database connection handling by removing unnecessary connection pool configuration.
Key Changes
quote_plus()toquote(safe='')for encoding database credentials and database name in connection strings. This prevents issues where SQLAlchemy'sunquote()function doesn't convert '+' back to spaces, which could cause authentication failures with special characters.pool_pre_ping=Truefrom engine creation in bothschema_service.pyandsql_executor.pyto reduce unnecessary overhead.get_external_schema()to use a single persistent connection viaengine.connect()context manager instead of creating new connections for each inspector call, improving performance during schema reflection.Implementation Details
quote()is preferred overquote_plus()for SQLAlchemy compatibilityhttps://claude.ai/code/session_016qjAVBXoVuhQD9MNdHQ6Fd