This issue has been migrated from #13417.
In purge_history we check how many events we need to delete, but if that number is 0 (which can happen frequently on inactive rooms with room retention enabled) we still proceed to perform all of the database operation as if there was anything to purge. We could just skip all the way down to dropping the temporary table, clearing caches and returning.