'bundle exec rake db:migrate' taking a long time due to calendar migration

      DELETE
        FROM calendar_events ce
      WHERE
        ce.id IN (SELECT DISTINCT(ce3.id) FROM calendar_events ce2
                  LEFT JOIN calendar_events ce3 ON ce3.user_id = ce2.user_id AND ce3.description = ce2.description
                  WHERE ce2.start_date >= (ce3.start_date - INTERVAL '1 days')
                    AND ce2.start_date <= (ce3.start_date + INTERVAL '1 days')
                    AND ce2.timezone IS NOT NULL
                    AND ce3.timezone IS NULL
                    AND ce3.id != ce2.id
                    AND ce2.post_id IS NULL
                    AND ce3.post_id IS NULL
        )
    SQL

So that looks like at least an O(n^2) operation, right? There are 69,724,384 rows in that table, so that doesn’t look like a very good idea.

@Sikamikanico. If you want the calednar plugin back, I think the thing to do is to just delete all calendar events and you can start over. The other thing we could do would be to run that query on the active server and see if it ever finishes.

The root cause, I think, is a bug in the plugin that duplicated events when a timezone changed.

The calendar_events table is 11GB:

discourse=# SELECT pg_size_pretty( pg_total_relation_size('calendar_events') );
 pg_size_pretty 
----------------
 11 GB
(1 row)

Here are the biggest tables:

       relation       | total_size 
----------------------+------------
 calendar_events      | 11 GB
 post_timings         | 6884 MB
 posts                | 2292 MB
 user_auth_token_logs | 1240 MB
 user_actions         | 1055 MB
(5 rows)

Only calendar_events is clearly bizarre.