retention: Fix OOM issues when deleting large numbers of transactions.
retention: Fix OOM issues when deleting large numbers of transactions.

For unknown reasons, deleting 10,000s of ArchiveTransaction objects
results in rapidly growing memory in the job making the request in the
Django process, eventually leading to an OOM kill.

I don't understand why Django behaves that way; I would have expected
the failure mode to instead be a serious load problem on the database
server, but perhaps the way Django's internal deletion logic handles
cascading the deletes to many millions of ArchiveMessages and other
ForeignKey objects requires tracking a lot of data in memory.

The solution is the same in any case, which is to batch the deletions
to execute a reasonable number of them at once.  Doing a single
ArchiveTransaction at a time would likely result in huge numbers of
database queries in a loop, which performs very poorly.  So we balance
by batching deletions in groups of 100 ArchiveTransactions; testing
this in production, I saw no spike of memory usage materially beyond
that of a normal Django process, and each bulk-deletion transaction
takes several seconds to process (meaning per-transaction overhead is
negligible).

hackerkid/zulip,synicalsyntax/zulip,eeshangarg/zulip,hackerkid/zulip,andersk/zulip,rht/zulip,showell/zulip,zulip/zulip,brainwane/zulip,hackerkid/zulip,eeshangarg/zulip,brainwane/zulip,andersk/zulip,punchagan/zulip,shubhamdhama/zulip,andersk/zulip,showell/zulip,andersk/zulip,synicalsyntax/zulip,shubhamdhama/zulip,hackerkid/zulip,synicalsyntax/zulip,shubhamdhama/zulip,eeshangarg/zulip,synicalsyntax/zulip,brainwane/zulip,kou/zulip,brainwane/zulip,kou/zulip,punchagan/zulip,timabbott/zulip,zulip/zulip,punchagan/zulip,synicalsyntax/zulip,zulip/zulip,rht/zulip,showell/zulip,hackerkid/zulip,synicalsyntax/zulip,timabbott/zulip,shubhamdhama/zulip,showell/zulip,punchagan/zulip,showell/zulip,eeshangarg/zulip,zulip/zulip,eeshangarg/zulip,brainwane/zulip,kou/zulip,rht/zulip,brainwane/zulip,zulip/zulip,showell/zulip,rht/zulip,punchagan/zulip,kou/zulip,shubhamdhama/zulip,hackerkid/zulip,punchagan/zulip,kou/zulip,andersk/zulip,rht/zulip,shubhamdhama/zulip,showell/zulip,hackerkid/zulip,zulip/zulip,rht/zulip,timabbott/zulip,rht/zulip,kou/zulip,timabbott/zulip,eeshangarg/zulip,synicalsyntax/zulip,zulip/zulip,brainwane/zulip,andersk/zulip,timabbott/zulip,timabbott/zulip,kou/zulip,eeshangarg/zulip,punchagan/zulip,andersk/zulip,timabbott/zulip,shubhamdhama/zulip