From 78fc703ebd7bd865ca6dd688d7bc5329063b1fdd Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 26 Jun 2025 03:40:21 -0400 Subject: [PATCH] Optimizing person_content_combined migration. (#5819) --- .../up.sql | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/migrations/2024-12-05-233704_add_person_content_combined_table/up.sql b/migrations/2024-12-05-233704_add_person_content_combined_table/up.sql index 4cb912b01..15744d7db 100644 --- a/migrations/2024-12-05-233704_add_person_content_combined_table/up.sql +++ b/migrations/2024-12-05-233704_add_person_content_combined_table/up.sql @@ -5,9 +5,7 @@ CREATE TABLE person_content_combined ( id serial PRIMARY KEY, published timestamptz NOT NULL, post_id int UNIQUE REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE, - comment_id int UNIQUE REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE, - -- Make sure only one of the columns is not null - CHECK (num_nonnulls (post_id, comment_id) = 1) + comment_id int UNIQUE REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE ); CREATE INDEX idx_person_content_combined_published ON person_content_combined (published DESC, id DESC); @@ -32,19 +30,11 @@ FROM CREATE TABLE person_saved_combined ( id serial PRIMARY KEY, saved timestamptz NOT NULL, - person_id int NOT NULL REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE, - post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE, - comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE, - UNIQUE (person_id, post_id), - UNIQUE (person_id, comment_id), - -- Make sure only one of the columns is not null - CHECK (num_nonnulls (post_id, comment_id) = 1) + person_id int NOT NULL REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE, + post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE, + comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE ); -CREATE INDEX idx_person_saved_combined_published ON person_saved_combined (saved DESC, id DESC); - -CREATE INDEX idx_person_saved_combined ON person_saved_combined (person_id); - -- Updating the history INSERT INTO person_saved_combined (saved, person_id, post_id, comment_id) SELECT @@ -67,7 +57,20 @@ FROM WHERE saved IS NOT NULL; +CREATE INDEX idx_person_saved_combined_published ON person_saved_combined (saved DESC, id DESC); + +CREATE INDEX idx_person_saved_combined ON person_saved_combined (person_id); + +ALTER TABLE person_saved_combined + ALTER CONSTRAINT person_saved_combined_person_id_fkey NOT DEFERRABLE, + ALTER CONSTRAINT person_saved_combined_post_id_fkey NOT DEFERRABLE, + ALTER CONSTRAINT person_saved_combined_comment_id_fkey NOT DEFERRABLE, + ADD CONSTRAINT person_saved_combined_person_id_post_id_key UNIQUE (person_id, post_id), + ADD CONSTRAINT person_saved_combined_person_id_comment_id_key UNIQUE (person_id, comment_id), + ADD CONSTRAINT person_saved_combined_check CHECK (num_nonnulls (post_id, comment_id) = 1); + ALTER TABLE person_content_combined ALTER CONSTRAINT person_content_combined_post_id_fkey NOT DEFERRABLE, - ALTER CONSTRAINT person_content_combined_comment_id_fkey NOT DEFERRABLE; + ALTER CONSTRAINT person_content_combined_comment_id_fkey NOT DEFERRABLE, + ADD CONSTRAINT person_content_combined_check CHECK (num_nonnulls (post_id, comment_id) = 1);