How it works…

We have added an extra column needs_vacuum to the first query, resulting in one more variable that we can use to make the VACUUM part conditional.