Skip to content

Allow keeping PostgresHook SQLAlchemy engines on psycopg2 - #72000

Open
sehajsandhu123 wants to merge 2 commits into
apache:mainfrom
sehajsandhu123:postgres-hook-sqlalchemy-scheme
Open

Allow keeping PostgresHook SQLAlchemy engines on psycopg2#72000
sehajsandhu123 wants to merge 2 commits into
apache:mainfrom
sehajsandhu123:postgres-hook-sqlalchemy-scheme

Conversation

@sehajsandhu123

@sehajsandhu123 sehajsandhu123 commented Aug 23, 2026

Copy link
Copy Markdown

Since postgres provider 7.0.0, PostgresHook builds its SQLAlchemy engines on psycopg (v3) whenever SQLAlchemy 2.x is installed, and the 7.0.0 changelog notes there is no connection or configuration option to keep hooks on psycopg2.

This silently changes semantics for Dag code that uses hook.get_uri() or hook.get_sqlalchemy_engine(). The psycopg (v3) dialect renders typed bind casts, so string parameters that PostgreSQL used to coerce implicitly now fail server-side. The most common casualty is pandas.DataFrame.to_sql into a table with a uuid column, which worked for years on psycopg2 and now fails with:

psycopg.errors.DatatypeMismatch: column "id" is of type uuid but expression is of type character varying
LINE 1: INSERT INTO orders (id, item) VALUES ($1::VARCHAR, $2...

This PR adds the missing opt-out by honoring the sqlalchemy_scheme connection extra (and an equivalent hook parameter), the same convention OdbcHook and DbApiHook.dialect_name already follow. PostgresHook even lists sqlalchemy_scheme in ignored_extra_options, so it is already excluded from libpq connect args — it just wasn't used when building the URL. With this change, a connection that needs the old behaviour can set:

{"sqlalchemy_scheme": "postgresql+psycopg2"}

The value is validated to be postgresql or postgresql+<driver> with no : or /, so a connection extra can't smuggle in a different URL. Nothing changes for connections that don't set it.

Tested against PostgreSQL 17 with pandas 3.0.5 / SQLAlchemy 2.0.51: df.to_sql into a uuid column reproduces the error above on the default psycopg3 engine and succeeds with the extra set. Unit tests cover the override, parameter precedence, get_uri propagation, and rejection of invalid schemes.

related: #71977


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Fable 5); all changes reviewed and validated by the author

Generated-by: Claude Code (Fable 5) following the guidelines

Provider 7.0.0 switched hook-built SQLAlchemy engines to psycopg (v3)
whenever SQLAlchemy 2.x is installed, with no opt-out. The psycopg
dialect renders typed bind casts, so string parameters PostgreSQL
previously coerced implicitly now fail server-side — most visibly
pandas.DataFrame.to_sql into uuid columns (apache#71977). Honor the existing
DbApiHook sqlalchemy_scheme connection extra (and hook parameter) so
connections that rely on psycopg2 behaviour can keep it.
@boring-cyborg

boring-cyborg Bot commented Aug 23, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant