GitHub user bufubaoni added a comment to the discussion: Airflow scheduler 
crashes with a psycopg2.errors.GroupingError due to an incorrect SQL query when 
detecting orphaned datasets

I believe there were some unexpected situations that led to data migration, but 
the corresponding indexes were not restored, including but not limited to the 
`dataset_pkey` index on the dataset table. In my actual maintenance experience, 
the database was reset, and only the table data was restored, but the indexes 
were not correctly restored.
This manifested as the scheduler being unable to start. The fix referenced 
[this pull request](https://github.com/apache/airflow/pull/40351).
However, I recently discovered that this SQL query can run correctly with the 
proper indexes in place:

```
SELECT 
    dataset.id, 
    dataset.uri,
    dataset.extra,
    dataset.created_at, 
    dataset.updated_at, 
    dataset.is_orphaned
FROM 
    dataset 
LEFT OUTER JOIN 
    dag_schedule_dataset_reference 
ON 
    dataset.id = dag_schedule_dataset_reference.dataset_id 
LEFT OUTER JOIN 
    task_outlet_dataset_reference 
ON 
    dataset.id = task_outlet_dataset_reference.dataset_id
WHERE 
    NOT dataset.is_orphaned 
GROUP BY 
    dataset.id
HAVING 
    COUNT(dag_schedule_dataset_reference.dag_id) = 0 
    AND COUNT(task_outlet_dataset_reference.dag_id) = 0;
```
But it fails to run correctly when the indexes are missing. This also explains 
why the code, which has never been modified, suddenly started having 
issues—it's such an edge case. It's almost certain that we encountered the same 
problem.

GitHub link: 
https://github.com/apache/airflow/discussions/51886#discussioncomment-13511037

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to