vincbeck opened a new pull request, #77547:
URL: https://github.com/apache/airflow/pull/77547

   ## Why
   
   Every running task writes `task_instance.last_heartbeat_at` every 5 s. 
Because that column has its own index
   (`ti_heartbeat`), PostgreSQL cannot apply these writes as HOT updates 
(in-place row updates that skip the indexes):
   each heartbeat adds an entry to every index on `task_instance`. At 70,000 
running tasks that is 14,000 such writes
   per second, with the index bloat and WAL (the database's write log) that 
come with them.
   
   ## What changes
   
   - Migration `0158_3_5_0_drop_ti_heartbeat_index.py` drops `ti_heartbeat`; 
the downgrade recreates it. Both skip the
     step if it is already done, so deployments that dropped the index by hand 
can still upgrade.
   - A heartbeat now changes no indexed column (`updated_at` is not indexed 
either), so PostgreSQL can apply it as a
     HOT update. A new test keeps it that way.
   - The only query that filters on the column is the scheduler's 
heartbeat-timeout check, which usually picks the
     `state` index anyway. Nothing else (REST API, UI, providers, `db clean`) 
filters or sorts on it.
   
   ## Results
   
   `main` is `bada0686ef`. On AWS: 2 schedulers, real `AwsEcsExecutor` on 
Fargate, RDS PostgreSQL 14, 10,000 running
   tasks. On a laptop: 4 schedulers, real `CeleryExecutor`, PostgreSQL 17, 
200,000 tasks of 2 to 6 minutes; and a
   replay of 14,000 heartbeats per second on 70,000 running task instances.
   
   | | main | this PR | change |
   |---|---:|---:|---:|
   | HOT updates (AWS, ECS) | 1% | 91% | **+90 points** |
   | WAL written (AWS, ECS) | 896-910 MB | 269 MB | **-70%** |
   | WAL written (laptop, Celery) | 31.3 GiB | 5.9 GiB | **-81%** |
   | PostgreSQL CPU, at least 60,000 running (laptop, Celery) | 2.02 cores | 
1.29 cores | **-36%** |
   | Average heartbeat latency (laptop, replay) | 3,934 ms (falling behind) | 
1.0 ms | **database keeps up** |
   
   The end-to-end times do not change (1,334 s against 1,355 s on the laptop): 
the tasks run for minutes, so the
   scheduler is not the limit there. On MySQL the gain is small: it saves one 
secondary index write per heartbeat.
   
   ## Trade-offs
   
   - The heartbeat-timeout check gets slower when finished attempts do not 
outnumber running ones. With 70,000
     running and no finished task instances on PostgreSQL 14: 3 ms on `main`, 
33 ms here, once every 10 s per
     scheduler (about 0.3% of its time). With 500,000 finished, the plans are 
the same (68 against 72 ms).
   - On PostgreSQL, `DROP INDEX` takes an `ACCESS EXCLUSIVE` lock on 
`task_instance`. The drop is quick, but it waits
     behind open transactions and blocks heartbeats while it waits (a worker 
tolerates 30 failed heartbeats in a row).
   - The downgrade rebuilds the index with a plain `CREATE INDEX`, which blocks 
writes to `task_instance` for a time
     proportional to the table size.
   
   ## Tests
   
   New tests check that a heartbeat writes no indexed column (fails on `main`), 
the migration up and down (also with
   the index changed by hand), and the offline SQL. The migration, scheduler 
and heartbeat endpoint tests pass on
   SQLite, PostgreSQL and MySQL.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Opus 5.5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to