Skip to content

Cache DbApiHook.inspector to avoid creating N engines#62594

Merged
kaxil merged 1 commit intoapache:mainfrom
kaxil:fix/cache-dbapihook-inspector
Feb 27, 2026
Merged

Cache DbApiHook.inspector to avoid creating N engines#62594
kaxil merged 1 commit intoapache:mainfrom
kaxil:fix/cache-dbapihook-inspector

Conversation

@kaxil
Copy link
Member

@kaxil kaxil commented Feb 27, 2026

DbApiHook.inspector was a @property that called get_sqlalchemy_engine() on every access.
get_sqlalchemy_engine() calls create_engine() each time, opening a new connection pool.

When inspecting multiple tables (e.g., GenericTransfer, SQLColumnCheckOperator, or the new
LLMSQLQueryOperator), this created N SQLAlchemy engines instead of reusing one. With default
pool_size=5, inspecting 10 tables could open up to 50 database connections.

Changed @property@cached_property, matching the existing pattern used by dialect_name
and dialect on the same class. The hook is scoped to a single task execution, so the cache
lifetime is appropriate. Updated the .pyi stub to match.


Was generative AI tooling used to co-author this PR?
  • Yes

DbApiHook.inspector was a @Property that called
get_sqlalchemy_engine() on every access. Each call creates a new
SQLAlchemy engine with its own connection pool. When inspecting
multiple tables (e.g., GenericTransfer, SQLColumnCheckOperator),
this created N engines instead of reusing one.

Change @Property to @cached_property so the inspector (and its
underlying engine) is created once per hook instance. The hook is
scoped to a single task execution, so the cache lifetime is
appropriate. Update the .pyi stub to match.
Copy link
Member

@gopidesupavan gopidesupavan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool :)

@kaxil kaxil merged commit 442a9a8 into apache:main Feb 27, 2026
85 checks passed
AkshayArali pushed a commit to AkshayArali/airflow_630 that referenced this pull request Feb 28, 2026
DbApiHook.inspector was a @Property that called
get_sqlalchemy_engine() on every access. Each call creates a new
SQLAlchemy engine with its own connection pool. When inspecting
multiple tables (e.g., GenericTransfer, SQLColumnCheckOperator),
this created N engines instead of reusing one.

Change @Property to @cached_property so the inspector (and its
underlying engine) is created once per hook instance. The hook is
scoped to a single task execution, so the cache lifetime is
appropriate. Update the .pyi stub to match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

2 participants