Skip to content

Airflow 3.2.0 opens too many meta DB connections #65010

Closed

Description

@rbayer666

Under which category would you file this issue?

Airflow Core

Apache Airflow version

3.2.0

What happened and how to reproduce it?

** Issue Description **
After upgrading from Airflow 3.1.8 to 3.2.0 without changing the configuration we encountered the problem that the meta database (running on PostgreSQL) stops accepting connections. Or, if we increase the allowed connections on the database, the sql achemy pool fills up.

The sql_alchemy settings are the default ones:

  • sql_alchemy_pool_enabled = True
  • sql_alchemy_pool_size = 5
  • sql_alchemy_max_overflow = 10

The allowed parallel database connections on the postgresql is set to 100.

We have 1x api server, 1 x scheduler, 1 x triggerer.

Increasing the sql_alchemy pool and the postgresql connections does not solve the issue - it just takes more time until the connection pool is filled up or the database stops accepting connections.

It looks like the database connections are not properly closed and left open. Over time the connections sum up.

What you think should happen instead?

All database connections should be closed when they are no longer needed.

Operating System

Debian GNU/Linux 12 (bookworm)

Deployment

Docker-Compose

Apache Airflow Provider(s)

No response

Versions of Apache Airflow Providers

No response

Official Helm Chart version

Not Applicable

Kubernetes Version

v1.31.13

Helm Chart configuration

No response

Docker Image customizations

No response

Anything else?

No response

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 10, 2026
  2. boring-cyborg commented on Apr 10, 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
    on Apr 10, 2026
  4. alessandro-peyrachia-rnc commented on Apr 10, 2026

    @alessandro-peyrachia-rnc

    Hi, I noticed something similar, we just upgraded a couple hours ago.
    Even with pgbouncer in front of the postgres db, api will fail after a certain time.
    We have 2 api and 2 scheduler, 1 triggerer, 3 workers.

    core:
            parallelism: 128
            max_active_tasks_per_dag: 64
          scheduler:
            max_tis_per_query: 256
          database:
            sql_alchemy_pool_size: 5
            sql_alchemy_max_overflow: 10
            sql_alchemy_pool_recycle: 600
    

    pgbouncer set at

    poolMode: transaction
    parameters:
          max_client_conn: "1000"
          default_pool_size: "30"
          min_pool_size: "10"
          reserve_pool_size: "10"
    

    Affects mainly the api service

  5. dclfan commented on Apr 10, 2026

    @dclfan

    Hi, same experience with the upgrade from 3.1.8 to 3.2.0. We tried on Monday but our test suite was able to replicate this problem in about two hours. We reset everything and tried again, and at around ~2hrs we had DB connection issues that failed all of the jobs in flight. It looked like it recovered on its own if given enough time ~10 min but we had to rollback to 3.1.8. We use pgbouncer, as well.

    pgbouncer settings

    pool_mode = transaction max_client_conn = 1000 default_pool_size = 20 max_db_connections = 75

  6. kaxil commented on Apr 11, 2026

    @kaxil
    Member

    To help narrow down the root cause, could you capture some PostgreSQL diagnostics next time the issue occurs?

    1. Connection state snapshot (most important -- run when connections are high):

    SELECT state, count(*) AS cnt
    FROM pg_stat_activity
    WHERE datname = current_database()
    GROUP BY state
    ORDER BY cnt DESC;

    And the detailed view:

    SELECT state, wait_event_type, wait_event,
           left(query, 100) AS query_prefix,
           age(now(), query_start) AS duration
    FROM pg_stat_activity
    WHERE datname = current_database()
    ORDER BY query_start;

    This tells us whether the stuck connections are active, idle, or idle in transaction.

    2. Lock contention -- are connections waiting on each other?

    SELECT blocked.pid, left(blocked.query, 80) AS blocked_query,
           blocking.pid AS blocking_pid, left(blocking.query, 80) AS blocking_query
    FROM pg_stat_activity blocked
    JOIN pg_locks bl ON bl.pid = blocked.pid AND NOT bl.granted
    JOIN pg_locks gl ON gl.locktype = bl.locktype AND gl.relation = bl.relation AND gl.granted
    JOIN pg_stat_activity blocking ON blocking.pid = gl.pid
    WHERE blocked.datname = current_database();

    3. Log table size -- the log table gets an INSERT on every task state change (new in 3.2.0). If it's large, INSERTs slow down and hold connections longer:

    SELECT relname, n_live_tup, n_dead_tup,
           pg_size_pretty(pg_total_relation_size(oid)) AS total_size,
           last_autovacuum
    FROM pg_stat_user_tables
    WHERE relname = 'log';

    4. Connection growth trend -- run every 5 minutes for ~30 minutes to capture the accumulation pattern:

    SELECT now(), count(*), state
    FROM pg_stat_activity
    WHERE datname = current_database()
    GROUP BY state;

    5. PostgreSQL timeout settings:

    SHOW idle_in_transaction_session_timeout;
    SHOW tcp_keepalives_idle;

    The state column from is the single most useful data point. If we see growing idle in transaction connections, that's a session leak. If we see active connections piling up on INSERT INTO log or SELECT ... FOR UPDATE, that's lock contention from new 3.2.0 query patterns.

  7. added
    affected_version:3.2
    on Apr 11, 2026
  8. dedalozzo commented on Apr 12, 2026

    @dedalozzo
  9. potiuk commented on Apr 13, 2026

    @potiuk
  10. amarbrkic commented on Apr 13, 2026

    @amarbrkic

    Hi, I had similar issues when upgrading to versions 3.1.2 and 3.1.3. I can't guarantee this will solve your issue(s). I solved mine by doing the following:

    1. Fixing my SSL issue. I would recommend checking the logging in your webserver log file, that's where I found my issues.
    2. Changing the file parsing sort mode if you are using mnt on your containers. It seems like parsing multiplies connections, but unfortunately I haven't found out why. I have changed my file_parsing_sort_mode to alphabetical.
  11. LengYue12389 commented on Apr 13, 2026

    @LengYue12389

    Hi, I seem to have encountered the problem of running out of connection pool, and I haven't found the cause yet
    Image

  12. alessandro-peyrachia-rnc commented on Apr 13, 2026

    @alessandro-peyrachia-rnc

    Hi @kaxil ,
    I have gathered the output required, have a look here below.
    I'm available to dig deeper.

    app=> SELECT state, count(*) AS cnt
    FROM pg_stat_activity
    WHERE datname = current_database()
    GROUP BY state
    ORDER BY cnt DESC;
     state  | cnt
    --------+-----
     idle   |  20
     active |   1
    (2 rows)
    
    app=> SELECT state, wait_event_type, wait_event,
           left(query, 100) AS query_prefix,
           age(now(), query_start) AS duration
    FROM pg_stat_activity
    WHERE datname = current_database()
    ORDER BY query_start;
            state        | wait_event_type | wait_event |                                             query_prefix                                             |     duration
    
    ---------------------+-----------------+------------+------------------------------------------------------------------------------------------------------+--------------
    ----
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:04:23.4034
    78
     idle                | Client          | ClientRead | ROLLBACK                                                                                             | 00:04:18.3534
    14
     idle                | Client          | ClientRead | ROLLBACK                                                                                             | 00:04:17.9472
    52
     idle                | Client          | ClientRead | SELECT 1                                                                                             | 00:04:17.6435
    47
     idle                | Client          | ClientRead | ROLLBACK                                                                                             | 00:03:48.3836
    62
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:03:47.8090
    68
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:03:47.7953
    92
     idle                | Client          | ClientRead | SELECT ceil(COUNT(*)::decimal / 32) FROM task_instance WHERE (state='running' OR state='queued') AND | 00:03:47.7107
    23
     idle                | Client          | ClientRead | ROLLBACK                                                                                             | 00:03:47.6354
    18
     idle                | Client          | ClientRead | ROLLBACK                                                                                             | 00:03:47.6234
    39
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:03:43.7063
    49
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:03:43.7055
    76
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:03:21.2596
    13
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:00:31.7996
    02
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:00:02.4373
    78
     idle                | Client          | ClientRead | SELECT ceil(COUNT(*)::decimal / 32) FROM task_instance WHERE (state='running' OR state='queued') AND | 00:00:01.9965
    34
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:00:00.3019
    44
     idle                | Client          | ClientRead | COMMIT                                                                                               | 00:00:00.0795
    6
     active              |                 |            | SELECT state, wait_event_type, wait_event,                                                          +| 00:00:00
                         |                 |            |        left(query, 100) AS query_prefix,                                                            +|
                         |                 |            |        age(now()                                                                                     |
     idle in transaction | Client          | ClientRead | DELETE FROM dag_warning USING dag WHERE dag_warning.dag_id = dag.dag_id AND dag.is_stale = true      | -00:00:00.001
    897
     idle                | Client          | ClientRead |                                                                                                      |
    (21 rows)
    
    app=> SELECT blocked.pid, left(blocked.query, 80) AS blocked_query,
           blocking.pid AS blocking_pid, left(blocking.query, 80) AS blocking_query
    FROM pg_stat_activity blocked
    JOIN pg_locks bl ON bl.pid = blocked.pid AND NOT bl.granted
    JOIN pg_locks gl ON gl.locktype = bl.locktype AND gl.relation = bl.relation AND gl.granted
    JOIN pg_stat_activity blocking ON blocking.pid = gl.pid
    WHERE blocked.datname = current_database();
     pid | blocked_query | blocking_pid | blocking_query
    -----+---------------+--------------+----------------
    (0 rows)
    

    We use cnpg with posgres 18, oids are not enabled by default

    app=> SELECT relname, n_live_tup, n_dead_tup,
           pg_size_pretty(pg_total_relation_size(oid)) AS total_size,
           last_autovacuum
    FROM pg_stat_user_tables
    WHERE relname = 'log';
    ERROR:  column "oid" does not exist
    LINE 2:        pg_size_pretty(pg_total_relation_size(oid)) AS total_...
    
    app=> SELECT now(), count(*), state
    FROM pg_stat_activity
    WHERE datname = current_database()
    GROUP BY state;
                  now              | count | state
    -------------------------------+-------+--------
     2026-04-13 08:48:38.289699+00 |     1 | active
     2026-04-13 08:48:38.289699+00 |    20 | idle
    (2 rows)
    
    app=> SHOW idle_in_transaction_session_timeout;
    SHOW tcp_keepalives_idle;
     idle_in_transaction_session_timeout
    -------------------------------------
     0
    (1 row)
    
     tcp_keepalives_idle
    ---------------------
     7200
    (1 row)
    

    I run the queries directly on the db, not through pgbouncer

    What is unusual is the behaviour when job gets queued.
    This morning we added a batch of around 200 jobs, using a pool of 20 slots
    The db showed this behaviour here, which was not present before

    Image

    Let me know how can I help further,
    Thanks

  13. ashb commented on Apr 13, 2026

    @ashb
    Member

    I think I've managed to reproduce this locally.

    Investigating.

  14. 6 remaining items

  15. ashb commented on Apr 13, 2026

    @ashb
    Member

    PR and unit tests to follow shortly, and then we can start a vote for 3.2.1

  16. ashb commented on Apr 13, 2026

    @ashb
    Member

    My fix def gets most of the occurrences. It sounds like Jed could have another case.

  17. self-assigned this
    on Apr 13, 2026
  18. added this to the Airflow 3.2.1 milestone on Apr 13, 2026
  19. rbayer666 commented on Apr 14, 2026

    @rbayer666
    Author

    Wow, thank you all for your effort!

  20. potiuk commented on Apr 14, 2026

    @potiuk
    Member

    Also for posterity - #65179 follow up that would avoid holding the connection too long (resource hog).

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

Metadata

Metadata

Assignees

Labels

affected_version:3.2area:MetaDBarea:corekind:bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions