Skip to content

Deleting external services triggers too many queries

Created by: ryanslade

When we delete an external service the following happens:

  • The external service is soft deleted
  • Each row in external_service_repos related to the external service is deleted. (trig_delete_external_service_ref_on_external_service_repos)
  • Each deletion of the row in external_service_repos triggers a soft deletion of the associated repo if it no longer has any reference in the external_service_repos table (trig_soft_delete_orphan_repo_by_external_service_repo)
  • Each deletion from the repo table triggers another deletion in the external_service_repos table (trig_delete_repo_ref_on_external_service_repos)

The number of queries executed is O(n³), where n is the number of repos owned by an external service.

These triggers make sense when we delete a single repo or row in external_service_repos but are unacceptable when we delete a large number of rows in one operation. We may need to delete these triggers or look into using FOR EACH STATEMENT rather than FOR EACH ROW

Related to: https://github.com/sourcegraph/customer/issues/139