ramitkataria commented on code in PR #77247:
URL: https://github.com/apache/airflow/pull/77247#discussion_r4624451016


##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -847,8 +847,23 @@ def _select_task_instances_to_queue(
             timer.start()
 
             try:
-                locked_query = with_row_locks(query, of=TI, session=session, 
skip_locked=True)
-                task_instances_to_examine = session.scalars(locked_query).all()
+                # The ranking subquery reads the statement's snapshot, but a 
locking read returns the latest
+                # version of the rows it locks, which can have left SCHEDULED 
since. The state is read as a
+                # column so that the check sees the locked row whatever TIs 
the session already holds. It is
+                # not checked in the outer WHERE: when the statistics show no 
SCHEDULED rows, PostgreSQL
+                # then joins through the ti_state index instead of the primary 
key.
+                locked_query = with_row_locks(

Review Comment:
   Non-blocking question: did you consider `populate_existing=True` here, as 
the other locked TI query in this file does 
(`session.scalars(locked_query.execution_options(populate_existing=True))`)? It 
would refresh every column of a TI the session already holds, so the check 
could be a plain `ti.state`, and `pool`, `priority_weight` and `try_number` 
would be the locked values too. I can see it overwriting unflushed changes on 
those TIs being a reason not to. If that is why, maybe add a brief comment?



-- 
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