1 -- First rename current newest comment time to newest_comment_time_necro
2 -- necro means that time is limited to 2 days, whereas newest_comment_time ignores that.
3 ALTER TABLE post_aggregates RENAME COLUMN newest_comment_time TO newest_comment_time_necro;
5 -- Add the newest_comment_time column
6 ALTER TABLE post_aggregates
7 ADD COLUMN newest_comment_time timestamp NOT NULL DEFAULT now();
9 -- Set the current newest_comment_time based on the old ones
13 newest_comment_time = newest_comment_time_necro;
15 -- Add the indexes for this new column
16 CREATE INDEX idx_post_aggregates_newest_comment_time ON post_aggregates (newest_comment_time DESC);
18 CREATE INDEX idx_post_aggregates_stickied_newest_comment_time ON post_aggregates (stickied DESC, newest_comment_time DESC);
20 -- Forgot to add index w/ stickied first for most comments:
21 CREATE INDEX idx_post_aggregates_stickied_comments ON post_aggregates (stickied DESC, comments DESC);
23 -- Alter the comment trigger to set the newest_comment_time, and newest_comment_time_necro
24 CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
29 IF (TG_OP = 'INSERT') THEN
33 comments = comments + 1,
34 newest_comment_time = NEW.published
36 pa.post_id = NEW.post_id;
37 -- A 2 day necro-bump limit
41 newest_comment_time_necro = NEW.published
43 pa.post_id = NEW.post_id
44 AND published > ('now'::timestamp - '2 days'::interval);
45 ELSIF (TG_OP = 'DELETE') THEN
46 -- Join to post because that post may not exist anymore
50 comments = comments - 1
55 AND pa.post_id = OLD.post_id;