Skip to content

Use loose foreign key for deployments.cluster_id

What does this MR do and why?

Replaces the foreign key constraint for deployments.cluster_id with a loose foreign key.

Some clusters have a very large number of associated deployments, which can prevent the cluster from being deleted as the statement timeout is reached while executing foreign key actions. A loose foreign key defers the foreign key action, allowing the cluster to be deleted without error.

Migration output

main: == 20220531024142 TrackClustersDeletions: migrating ===========================
main: -- execute("CREATE TRIGGER clusters_loose_fk_trigger\nAFTER DELETE ON clusters REFERENCING OLD TABLE AS old_table\nFOR EACH STATEMENT\nEXECUTE FUNCTION insert_into_loose_foreign_keys_deleted_records();\n")
main:    -> 0.0016s
main: == 20220531024142 TrackClustersDeletions: migrated (0.0025s) ==================

main: == 20220531024143 RemoveClustersDeploymentsClusterIdFk: migrating =============
main: -- foreign_keys(:deployments)
main:    -> 0.0039s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("LOCK clusters, deployments IN ACCESS EXCLUSIVE MODE")
main:    -> 0.0003s
main: -- foreign_keys(:deployments)
main:    -> 0.0016s
main: -- remove_foreign_key(:deployments, :clusters, {:name=>"fk_289bba3222"})
main:    -> 0.0033s
main: == 20220531024143 RemoveClustersDeploymentsClusterIdFk: migrated (0.0108s) ====
main: == 20220531024143 RemoveClustersDeploymentsClusterIdFk: reverting =============
main: -- transaction_open?()
main:    -> 0.0000s
main: -- foreign_keys(:deployments)
main:    -> 0.0103s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0002s
main: -- execute("ALTER TABLE deployments VALIDATE CONSTRAINT fk_289bba3222;")
main:    -> 0.0120s
main: -- execute("RESET statement_timeout")
main:    -> 0.0006s
main: == 20220531024143 RemoveClustersDeploymentsClusterIdFk: reverted (0.0286s) ====

main: == 20220531024142 TrackClustersDeletions: reverting ===========================
main: -- execute("DROP TRIGGER IF EXISTS clusters_loose_fk_trigger ON clusters")
main:    -> 0.0011s
main: == 20220531024142 TrackClustersDeletions: reverted (0.0018s) ==================

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #360696 (closed)

Edited by Tiger Watson

Merge request reports

Loading