Skip to content

Airflow 3.2.1 locks heartbeat queries causing DB connection pool exhaustion and slow responses #65712

Closed as not planned

Description

@serhii-karelov

Under which category would you file this issue?

Providers

Apache Airflow version

3.2.1

What happened and how to reproduce it?

When have tens of tasks in-flight, and while surfing the UI, occasionally we get

  1. Queued up hertbeat UPDATE queries in the airflow api
  2. Connection pool exhaustion
  3. API response times to the heartbeats is high, causing workers to timeout, and retry.
  4. Some tasks terminated and put into a retry state

What you think should happen instead?

No response

Operating System

No response

Deployment

Other Docker-based deployment

Apache Airflow Provider(s)

celery

Versions of Apache Airflow Providers

No response

Official Helm Chart version

Not Applicable

Kubernetes Version

No response

Helm Chart configuration

No response

Docker Image customizations

No response

Anything else?

Some query prevents the heartbeat UPDATEs

usename    state                xact_duration    query_duration    query
db_user  idle in transaction  00:00:22.17721   00:00:00.06658    SELECT task_instance.state AS task_instance_state
                                                                   FROM task_instance
                                                                   WHERE task_instance.id = '0<zzzyyy>54'::uuid::UUID
db_user  active               00:00:22.123495  00:00:22.120761   UPDATE task_instance SET last_heartbeat_at='2026-04-23T09:03:18.258390+00:00'::timestamptz, updated_at='2026-04-23T09:03:18.261693+00:00'::timestamptz WHERE task_instance.id
db_user  active               00:00:22.058237  00:00:22.054038   UPDATE task_instance SET last_heartbeat_at='2026-04-23T09:03:18.320462+00:00'::timestamptz, updated_at='2026-04-23T09:03:18.324080+00:00'::timestamptz WHERE task_instance.id
db_user  active               00:00:21.901896  00:00:21.89822    UPDATE task_instance SET last_heartbeat_at='2026-04-23T09:03:18.479643+00:00'::timestamptz, updated_at='2026-04-23T09:03:18.483318+00:00'::timestamptz WHERE task_instance.id
db_user  active               00:00:21.537818  00:00:21.534485   UPDATE task_instance SET last_heartbeat_at='2026-04-23T09:03:18.844164+00:00'::timestamptz, updated_at='2026-04-23T09:03:18.847447+00:00'::timestamptz WHERE task_instance.id
...

Slow endpoints

# Duration (s) Status Method Endpoint
1 72.02 200 GET /api/v2/dags/XYZ/dagRuns/~/hitlDetails?state=deferred&response_received=false
2 71.88 200 GET /ui/grid/runs/QAZ?limit=25&order_by=-run_after
3 71.05 204 PATCH /execution/task-instances/ZAQ/state
4 71.01 200 GET /api/v2/dags/QWE/details
5 70.91 200 GET /ui/dags/EWQ/latest_run

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Apr 23, 2026
  2. boring-cyborg commented on Apr 23, 2026

    @boring-cyborg

    Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

  3. added
    area:MetaDB
    priority:high
    on Apr 23, 2026
  4. Subham-KRLX commented on Apr 23, 2026

    @Subham-KRLX
    Contributor

    Confirmed this is a mix of connection pool exhaustion from streaming endpoints and lock contention in the TI update path I have found a few spots where transactions are held open unnecessarily blocking heartbeats I am currently putting together a PR to decouple these sessions and optimize the hot paths.

  5. jscheffl commented on Apr 23, 2026

    @jscheffl
    Contributor

    We had the same in Airflow 3.1.5 and mainly at that time it was caused by slow scheduler queries blocking the task_instance table also blocking heartbeat calls.

    Can you double check this is related to UI streaming calls or might be (still?) related to locks by scheduler? In our case it helped to set scheduler_health_check_threshold: 3000 in the configs (helm chart).

  6. serhii-karelov commented on Apr 24, 2026

    @serhii-karelov
    Author

    We had the same in Airflow 3.1.5 and mainly at that time it was caused by slow scheduler queries blocking the task_instance table also blocking heartbeat calls.

    Can you double check this is related to UI streaming calls or might be (still?) related to locks by scheduler? In our case it helped to set scheduler_health_check_threshold: 3000 in the configs (helm chart).

    I can see that the issue persists even when no any API requests from UI.

    Also I tried to see whether there are any slow API calls (10sec or more) except of heartbeats (that are slow because they are locked), and I can see only occasional method=PATCH path=/execution/task-instances/xxxx/state query=. So it doesn't look like there is a particular API endpoint that locks task instances.

    Happy to provide more information if needed.

  7. mdw00d commented on May 5, 2026

    @mdw00d

    Not sure if this is related but we are experiencing what may be a similar issue where we have lots of tasks accumulating in the "idle in transaction" state executing a query of the form:

    SELECT task_instance.task_id, task_instance.state, task_instance.dag_version_id, task_instance.start_date, task_instance.end_date, dag_version.version_number 
    FROM task_instance LEFT OUTER JOIN dag_version ON task_instance.dag_version_id = dag_version.id 
    WHERE task_instance.dag_id = {OUR DAG} AND task_instance.run_id = 'scheduled__2026-04-30T05:00:00+00:00' ORDER BY task_instance.task_id
    

    Not sure where this query is originating; perhaps someone more knowledgeable would know where?

    This eventually causes an unrecoverable error situation.

  8. sdg9670f commented on May 7, 2026

    @sdg9670f

    Same here. I couldn't identify a slow query, but it seems some are being delayed by locks.

  9. shivaam commented on May 21, 2026

    @shivaam
    Contributor

    Looks like this may be fixed by #67246, which landed yesterday and will be in 3.2.2. The slow PATCH /state you flagged matches what that PR addresses.

    @avolant — does this look related to what you were fixing?

    @serhii-karelov — would you be able to retest once 3.2.2 is out?

  10. 2 remaining items

  11. Gourigupta2004 commented on Jun 28, 2026

    @Gourigupta2004

    Hi @serhii-karelov,

    I would like investigate and work on this issue. Please assign this to me

  12. potiuk commented on Jun 29, 2026

    @potiuk
    Member

    Hi @serhii-karelov,

    I would like investigate and work on this issue. Please assign this to me

    We do not assign issues (see contributing guides) - but feel free to work on it.

  13. SamWheating commented on Jul 1, 2026

    @SamWheating
    Contributor

    It seems like we're seeing the same issue running in 3.2.2 at large scale - seeing lots of idle in transaction queries like this:

    UPDATE task_instance SET last_heartbeat_at='2026-07-01T15:17:36.739110+00:00'::timestamptz, updated_at='2026-07-01T15:17:36.741242+00:00'::timestamptz WHERE task_instance.id = '019f1dfd-0e45-729c-a148-c18a311147fd'::uuid::UUID AND task_instance.state = 'running' AND task_instance.hostname = '<hostname>' AND task_instance.pid = 39

    which then exhaust pgbouncer's pool and eventually cause api-servers, schedulers etc to fail.

    I'll continue to investigate and share updates here, but I don't think that this was fixed by the linked PR.

  14. seanmuth commented on Jul 13, 2026

    @seanmuth
    Contributor

    Confirming this is still present on 3.2.2 (i.e. with #67246 already applied), at scale.

    Adding a concrete reproduction from a large on-prem deployment in case the numbers help narrow the hot path.

    Environment

    • Airflow 3.2.2 (upgraded from 3.1.8), CeleryExecutor
    • ~5,000 DAGs; the DAG involved in the repro has ~350 tasks
    • Postgres metadata DB behind PgBouncer

    Symptom
    Sessions pile up in idle in transaction while still holding task_instance row locks, and the count grows over time until PgBouncer's pool is exhausted and the api-servers/schedulers restart. The blocking chains are dominated by two query shapes:

    1. The heartbeat update (matches @SamWheating's report above):
      UPDATE task_instance
         SET last_heartbeat_at = '…', updated_at = '…'
       WHERE task_instance.id = '…'::uuid
         AND task_instance.state = 'running'
         AND task_instance.hostname = '…'
         AND task_instance.pid = …;
    2. A grid/TI read (matches @mdw00d's report above):
      SELECT task_instance.task_id, task_instance.state, task_instance.dag_version_id,
             task_instance.start_date, task_instance.end_date, dag_version.version_number
        FROM task_instance
        LEFT OUTER JOIN dag_version ON task_instance.dag_version_id = dag_version.id
       WHERE task_instance.dag_id = '<large_dag>'
         AND task_instance.run_id = 'scheduled__…'
       ORDER BY task_instance.task_id;

    Correlated endpoint latencies (captured while clearing tasks on the ~350-task DAG and browsing the grid concurrently):

    Endpoint Result Duration
    POST .../clearTaskInstances 200 ~90s, ~170s, ~175s, ~325s
    POST .../clearTaskInstances 500 ~345s, ~365s, ~375s, ~485s
    GET /ui/grid/structure/<large_dag> (limit=10) 200 ~205s, ~245s, ~250s, ~265s

    Trigger: clearing a large task group while the grid UI is being polled for the same DAG. The idle-in-transaction sessions holding task_instance locks block the heartbeat UPDATEs, workers then time out heartbeating and retry/terminate, and the scheduler stalls on the same table — consistent with the cascade described in the issue.

    Interim mitigation we're validating (not a fix): scoping a tighter idle_in_transaction_session_timeout on the Airflow DB role to forcibly release the leaked locks and keep the pool from exhausting. It stops the api-server restarts but obviously doesn't address the sessions being held open across slow work in the first place.

    Happy to pull more detail (full pg_stat_activity snapshots with wait_event/state, blocking-PID chains) if useful.


    🤖 Generated with Claude Code — Claude Opus 4.8 (1M context)

  15. github-actions commented on Jul 29, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 14 days with no response from the author. It will be closed in next 7 days if no further activity occurs from the issue author.

  16. added
    stale
    on Jul 29, 2026
  17. github-actions commented on Aug 5, 2026

    @github-actions
    Contributor

    This issue has been closed because it has not received response from the issue author.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected_version:3.2area:MetaDBarea:corearea:performancekind:bugneeds-triagepending-responsepriority:highstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions