The maintenance notebook is three lines, it ran fine for a month, and now it fails most nights with a concurrent modification error. Nothing changed in the notebook. What changed is that the streaming job feeding the same table got busier.
-- nightly maintenance notebook (Spark SQL), 02:00 OPTIMIZE silver_events ; VACUUM silver_events ; DESCRIBE HISTORY silver_events LIMIT 5 ;
Enter fullscreen mode
Exit fullscreen mode
Why it only breaks sometimes
If your background is SQL Server, this is the assumption that bites. SQL Agent jobs serialise against a table using locks: the second writer waits. Delta does not. Delta uses optimistic concurrency β both writers proceed, and the one that commits second checks whether the files it read are still current. If they are not, it fails rather than corrupting the table.
So a nightly OPTIMIZE rewriting files while a streaming MERGE is writing into the same files is not a queue. It is a race, and the loser raises a concurrent modification error.β¦