From fd9ca3ee17a0080ca50086ac1d0ccddad213e0f8 Mon Sep 17 00:00:00 2001 From: skylar Date: Sun, 3 Aug 2025 04:03:22 -0500 Subject: [PATCH] add database prune sql script --- database_prune.sql | 39 +++++++++++++++++++++++++++++++++++++++ deletes.md | 10 +++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 database_prune.sql diff --git a/database_prune.sql b/database_prune.sql new file mode 100644 index 0000000..0933ff3 --- /dev/null +++ b/database_prune.sql @@ -0,0 +1,39 @@ +\c pleroma +do $$ +declare +maxA integer := (SELECT count(*) from activities WHERE inserted_at < now() - interval '4 months'); +maxO integer := (SELECT count(*) from objects WHERE inserted_at < now() - interval '4 months'); +maxN integer := (SELECT count(*) from notifications WHERE inserted_at < now() - interval '4 months'); +counter integer := 0; +begin +RAISE NOTICE 'Marked for deletion: % activities', maxA; +while counter < maxA loop +DELETE FROM activities WHERE id = any (array(SELECT id FROM activities WHERE inserted_at < now() - interval '4 months' LIMIT 1000)); +COMMIT; +PERFORM pg_sleep(3); +counter := counter + 1000; +end loop; +RAISE NOTICE 'Marked for deletion: % objects', maxO; +counter := 0; +while counter < maxO loop +DELETE FROM deliveries WHERE object_id = any (array(SELECT id FROM objects WHERE inserted_at < now() - interval '4 months' LIMIT 10000)); +COMMIT; +PERFORM pg_sleep(3); +counter := counter + 1000; +end loop; +counter := 0; +while counter < maxO loop +DELETE FROM objects WHERE id = any (array(SELECT id FROM objects WHERE inserted_at < now() - interval '4 months' LIMIT 1000)); +COMMIT; +PERFORM pg_sleep(3); +counter := counter + 1000; +end loop; +RAISE NOTICE 'Marked for deletion: % notifications', maxN; +counter := 0; +while counter < maxO loop +DELETE FROM notifications WHERE id = any (array(SELECT id FROM notifications WHERE inserted_at < now() - interval '4 months' LIMIT 1000)); +COMMIT; +PERFORM pg_sleep(3); +counter := counter + 1000; +end loop; +end$$; \ No newline at end of file diff --git a/deletes.md b/deletes.md index 6001589..b6f8d32 100644 --- a/deletes.md +++ b/deletes.md @@ -1,4 +1,4 @@ -Deletes are the danger zone, deleting activities from the table can and will introduce weird problems. For example, when a new activity federates in referencing a deleted activity, it'll cause notifications to temporarily break for the affected user(s). Pleroma will look in the table for the activity it's referencing, that'll time out, and the request to load notifications into pleroma-fe will 502 and they'll just be blank. +Deletes are the danger zone, deleting activities from the table can and will introduce weird problems. For example, when a new activity federates in referencing a deleted activity, it'll cause notifications to temporarily break for the affected user(s). Pleroma will look in the table for the activity it's referencing, that'll fail, and users will see one or more 500 errors on screen. But if it's a choice between killing the instance entirely due to storage constraints or deleting activities and accepting the risk of errors, we can delete them. Before you delete anything, take a look at how to clean up pg_repack remnants [here](https://git.yandere.love/skylar/pleromer-stuff/src/branch/main/repack.md). @@ -52,3 +52,11 @@ counter := counter + 1; end loop; end$$; ``` + +To prune the database on a recurring basis, adjust this [SQL file](https://git.yandere.love/skylar/pleromer-stuff/src/branch/main/database_prune.sql) to your desired retention period for activities, objects, and notifications, then execute it with: + +``` +sudo -Hu postgres psql -f /path/to/database_prune.sql +``` + +If you've named your database something other than "pleroma", that will need to be changed as well. \ No newline at end of file