Restore fails due to disk space on migration because of 70M calendar events

OK. I’m restoring on a production site. It’s migrating.

Here’s a query that’s been running for 30 minutes:

discourse=# SELECT pid, usename, state, query, query_start
FROM pg_stat_activity
WHERE state = 'active';
 pid |  usename  | state  |                                                    query                                                     |          query_start
-----+-----------+--------+--------------------------------------------------------------------------------------------------------------+-------------------------------
 519 | postgres  | active | SELECT pid, usename, state, query, query_start                                                              +| 2026-02-14 17:58:35.473337+00
     |           |        | FROM pg_stat_activity                                                                                       +|
     |           |        | WHERE state = 'active';                                                                                      |
 308 | discourse | active | DELETE                                                                                                      +| 2026-02-14 17:26:08.12598+00
     |           |        |   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                                                                       +|
     |           |        |   )                                                                                                         +|
     |           |        |                                                                                                              |
(2 rows)

That’s from this:

This shows that the latest migration is later, but I guess that’s because that migration is from a plugin that just got added;.

discourse=# SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1;
    version
----------------
 20250507013646
(1 row)

There are a lot of calendar events!!! 69_724_384!

discourse=# select count(*) from calendar_events;
  count
----------
 69724384
(1 row)

So I guess that’s the problem.

. . . but they don’t even have discourse_calendar installed on the source site!?

But they DO have that number of events in calendar_events on the source table. . . .

But there are only 4 post_custom_fields with a calendar event. ?

So there are a bunch of events that are not connected to a topic or post. Standard holidays like Christmas, new year’s, boxing day.

Here’s the fix

delete from  calendar_events where topic_id=0 and post_id is null and post_number is null;

And that left 99 events.

Maybe SiteSetting.delete_expired_event_posts_after got set to 0, and even though they weren’t really using the plugin it just created all of these events?

And yup! SiteSetting.delete_expired_event_posts_after is set to -1.

Oh, but -1 is the default. So maybe that’s the issue?

No. That’s event POSTS. I don’t quite know how there were 70 Million events.

https://github.com/discourse/discourse/blob/main/plugins/discourse-calendar/config/settings.yml#L11-L13