Skip to content

sqlalchemy need to relax certain server capacities assumption and better handle the errors to work with more databases #10656

@LLuke

Description

@LLuke

Describe the use case

A fix will allow sqlalchemy to work with some database servers implemented enough postgres wire protocol but not all the PostgreSQL features assumed by sqlalchemy.

Description

sqlalchemyattempted to use queries in

 engine.default.get_server_version_info()
 engine.default.._get_default_schema_name()
 engine.default..get_default_isolation_level()

and

 dialects.postgresql.PGDialect.self._set_backslash_escapes()

to probe and enable features for PostgreSQL and compatible databases. While there are good reasons, they could fail with unhandled errors for database servers that support enough postgres wire protocol and features but otherwise doesn't support these particular functionalities. In particular, sqlalchemy assumed the error would always be NotImplementedError in get_server_version_info() and proceeded to use server_version_info without further check the None condition arise from NotImplementedError. However, for drivers like pg8000, errors like ProgrammingError would be thrown instead. To make sqlalchemy usable for the many different drivers and servers combinations, it is desirable to make sqlalchemy as lenient as possible without detrimental side effects.

Databases / Backends / Drivers targeted

A fix is tested with arcadedb 23.10.1 with pg8000. It is possible this will also fix for other servers implemented enough postgres wire protocols

Example Use

After the fix, the following code works

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine(
    'postgresql+pg8000://' + os.environ['DB_USER'] + ':' + os.environ['DB_PASS'] + '@localhost:3333/arcade_testdb', isolation_level="AUTOCOMMIT",
    connect_args={'ssl_context': None}
)
df = pd.read_sql_query("select * from schema:database", con=engine)
engine.dispose()

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    postgresqluse casenot really a feature or a bug; can be support for new DB features or user use cases not anticipatedwontfix / out of scopesomething we decided we aren't doing, for whatever reason

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions