db: Switch to statement rather than row trigger
Created by: ryanslade
The trig_soft_delete_orphan_repo_by_external_service_repo was previously triggered per row when an external_service_repos row was deleted.
We have switched this to run after the entire statement affecting external_service_repos has run. Running per row led to a quadratic number of statments being run.
Onc significant side effect of this change is that after any row is deleted in external_service_repos ANY orphaned repos will be soft deleted rather than repos only affected by the deleted row. The reason for this is that after statement triggers don't have access to what changed in Postgre 9.6 which we need to support. From 10 we could use transition tables.
Given that whenver we add a repo we should ALWAYS also add an entry in the external_service_repos table this should not pose an issue.
Performance:
In testing locally, deleting a an external service with a large number of repos dropped from around 20s to 8s. When the number of repos owned by the external service was doubled, the runtime also doubled showing that we are now O(n).
Closes: https://github.com/sourcegraph/sourcegraph/issues/16069