Bounded retention mode for remote content #7980

Open
lambadalambda wants to merge 14 commits from feature/bounded-retention into develop

Summary

A common complaint is that the database grows without bound because every remote activity is kept forever. The existing answer, prune_objects, is a batch job that needs a maintenance window and leaves the database growing again afterwards.

This adds a bounded retention mode: remote content is treated as a cache of somebody else's data, with an eviction policy. A cron worker evicts a small batch of old remote threads per run so the database plateaus instead of growing. Local content is never touched.

  • New Pleroma.Retention. Remote threads (activities grouped by context) are evictable when nobody local interacted with them and they have been quiet for longer than remote_post_retention_days, or, over an optional max_objects watermark, once they have been quiet for a day.
  • A thread is pinned if any activity in it is local (posts, replies, favourites, repeats, reactions all carry the thread's context), is bookmarked, is addressed to a local user (DMs, mentions; checked on activities.recipients, so clearing notifications does not unpin), still has a notification for a local user, is quoted by a local post (looked up through the objects_quote_url index, since quotes have their own context), is a report, or if any object in it is referenced by a report. Activities without a context (Follow, Block, chat messages, ...) are never considered. Local objects are guarded by an exact URL prefix check.
  • Selection is an incremental walk, not a full group-by. Flake ids are time-ordered, so each run scans the next batch_size activities after a persisted cursor (new retention_cursors table) up to the id that corresponds to the deadline, and verifies only the threads those activities belong to through the existing (type, context) index. Every activity is visited once, when it becomes old enough. The cursor is persisted per chunk so a killed run resumes. The watermark walk has a cursor of its own.
  • Selection and deletion of a chunk run in one transaction; deletes only touch primary key and ap_id indexes.
  • New Pleroma.Workers.Cron.RetentionWorker, hourly, unique, max_attempts: 1, no-op unless config :pleroma, :retention, enabled: true.
  • prune_objects --keep-threads reuses the same thread rules (full group-by, for the one-off initial cut), and therefore also keeps mentioned and reported threads (noted in the changelog). The hashtag cleanup is shared too; the worker only checks the hashtags of the objects it evicted.
  • Docs in the cheatsheet and the database CLI page, changelog entry.

Measured on a real instance (read-only)

pleroma.soykaf.com, PostgreSQL 15, 312 GB, 107M activities / 74M objects, work_mem 10 MB:

  • The original whole-table group-by (what prune_objects --keep-threads does): seq scan + 3.6 GB on-disk sort, 16 minutes just to select, 36.5M evictable threads found. Fine as a one-off initial cut, not hourly.
  • Cursor walk: range scan of 50k activities 147 ms; verifying ~700 threads with the full pin rules 349 ms warm, 14 s cold. A 50k batch yields roughly 12k threads.
  • The recipients pin adds about 90 ms to a warm verification of 1,818 threads (533 → 621 ms) and kept 14 of them that had no notification left.

Full run on a copy of lain.com

Restored dump (23.1M activities, 13.8M objects, 57 GB), migrated, worker walked in 50k batches until it reached the 90-day deadline, with delete triggers recording every removed row:

  • 4.45M threads evicted (the pin analysis predicted 4.46M evictable), 11.0M objects, 17.6M activities. 57 GB → 14 GB after VACUUM FULL.
  • An empty run (nothing left to evict) takes 77 ms. Runs with evictions averaged 3 min on HDD storage while autovacuum was catching up; the real worker has an hour between batches.
  • Audit after the fixes below: no local activity or object, no post of a pinned thread, no Flag, notification or bookmark removed. The recipients pin alone keeps 75k threads here (this instance has few notifications left).
  • On lain.com, 45 of the 134 remote posts that local users quoted were evicted before quotes pinned their thread.
  • The run found two bugs, fixed in this PR: eviction deleted objects of other threads when a Like/Announce/EmojiReact carried a different context than its object (593 posts in threads with local activity), and local reactions stored with the legacy EmojiReaction type did not pin their thread.

Known limitations, open for discussion

  • The initial backlog through the worker alone would take many runs on an old instance; the docs say to run prune_objects --keep-threads --prune-orphaned-activities once first.
  • Threads that were kept and later lost their pin (removed bookmarks, updated_at bumped by a visibility change) are only revisited on a fresh walk; deleting the cursor row restarts it. A periodic automatic re-walk could be added later.
  • Space freed by eviction is reused, not handed back to the filesystem.
  • Old data where a Create's context differs from its object's (585 of 12.6M Creates on lain.com) can still lose the object while the Create's thread is pinned (3 posts there).
  • Remote activities without a context that point at an evicted object (Update, Remove, Announce) are removed with it, regardless of their age.

Testing

  • mix test test/pleroma/retention_test.exs test/pleroma/workers/cron/retention_worker_test.exs test/mix/tasks/pleroma/database_test.exs (51 tests, 0 failures), plus test/pleroma/docs and test/pleroma/config for the description entry.
  • Run locally with Elixir 1.19 / OTP 28, as the websockex dependency does not compile under Elixir 1.20.
  • Query plans measured on a real 312 GB instance (above); the worker itself has not yet run against one.
## Summary A common complaint is that the database grows without bound because every remote activity is kept forever. The existing answer, `prune_objects`, is a batch job that needs a maintenance window and leaves the database growing again afterwards. This adds a **bounded retention mode**: remote content is treated as a cache of somebody else's data, with an eviction policy. A cron worker evicts a small batch of old remote threads per run so the database plateaus instead of growing. Local content is never touched. - New `Pleroma.Retention`. Remote threads (activities grouped by context) are evictable when nobody local interacted with them and they have been quiet for longer than `remote_post_retention_days`, or, over an optional `max_objects` watermark, once they have been quiet for a day. - A thread is pinned if any activity in it is local (posts, replies, favourites, repeats, reactions all carry the thread's context), is bookmarked, is addressed to a local user (DMs, mentions; checked on `activities.recipients`, so clearing notifications does not unpin), still has a notification for a local user, is quoted by a local post (looked up through the `objects_quote_url` index, since quotes have their own context), is a report, or if any object in it is referenced by a report. Activities without a context (Follow, Block, chat messages, ...) are never considered. Local objects are guarded by an exact URL prefix check. - **Selection is an incremental walk, not a full group-by.** Flake ids are time-ordered, so each run scans the next `batch_size` activities after a persisted cursor (new `retention_cursors` table) up to the id that corresponds to the deadline, and verifies only the threads those activities belong to through the existing `(type, context)` index. Every activity is visited once, when it becomes old enough. The cursor is persisted per chunk so a killed run resumes. The watermark walk has a cursor of its own. - Selection and deletion of a chunk run in one transaction; deletes only touch primary key and ap_id indexes. - New `Pleroma.Workers.Cron.RetentionWorker`, hourly, unique, `max_attempts: 1`, no-op unless `config :pleroma, :retention, enabled: true`. - `prune_objects --keep-threads` reuses the same thread rules (full group-by, for the one-off initial cut), and therefore also keeps mentioned and reported threads (noted in the changelog). The hashtag cleanup is shared too; the worker only checks the hashtags of the objects it evicted. - Docs in the cheatsheet and the database CLI page, changelog entry. ## Measured on a real instance (read-only) pleroma.soykaf.com, PostgreSQL 15, 312 GB, 107M activities / 74M objects, `work_mem` 10 MB: - The original whole-table group-by (what `prune_objects --keep-threads` does): seq scan + 3.6 GB on-disk sort, 16 minutes just to select, 36.5M evictable threads found. Fine as a one-off initial cut, not hourly. - Cursor walk: range scan of 50k activities 147 ms; verifying ~700 threads with the full pin rules 349 ms warm, 14 s cold. A 50k batch yields roughly 12k threads. - The recipients pin adds about 90 ms to a warm verification of 1,818 threads (533 → 621 ms) and kept 14 of them that had no notification left. ## Full run on a copy of lain.com Restored dump (23.1M activities, 13.8M objects, 57 GB), migrated, worker walked in 50k batches until it reached the 90-day deadline, with delete triggers recording every removed row: - 4.45M threads evicted (the pin analysis predicted 4.46M evictable), 11.0M objects, 17.6M activities. 57 GB → 14 GB after `VACUUM FULL`. - An empty run (nothing left to evict) takes 77 ms. Runs with evictions averaged 3 min on HDD storage while autovacuum was catching up; the real worker has an hour between batches. - Audit after the fixes below: no local activity or object, no post of a pinned thread, no Flag, notification or bookmark removed. The recipients pin alone keeps 75k threads here (this instance has few notifications left). - On lain.com, 45 of the 134 remote posts that local users quoted were evicted before quotes pinned their thread. - The run found two bugs, fixed in this PR: eviction deleted objects of *other* threads when a Like/Announce/EmojiReact carried a different context than its object (593 posts in threads with local activity), and local reactions stored with the legacy `EmojiReaction` type did not pin their thread. ## Known limitations, open for discussion - The initial backlog through the worker alone would take many runs on an old instance; the docs say to run `prune_objects --keep-threads --prune-orphaned-activities` once first. - Threads that were kept and later lost their pin (removed bookmarks, `updated_at` bumped by a visibility change) are only revisited on a fresh walk; deleting the cursor row restarts it. A periodic automatic re-walk could be added later. - Space freed by eviction is reused, not handed back to the filesystem. - Old data where a Create's context differs from its object's (585 of 12.6M Creates on lain.com) can still lose the object while the Create's thread is pinned (3 posts there). - Remote activities without a context that point at an evicted object (Update, Remove, Announce) are removed with it, regardless of their age. ## Testing - `mix test test/pleroma/retention_test.exs test/pleroma/workers/cron/retention_worker_test.exs test/mix/tasks/pleroma/database_test.exs` (51 tests, 0 failures), plus `test/pleroma/docs` and `test/pleroma/config` for the description entry. - Run locally with Elixir 1.19 / OTP 28, as the `websockex` dependency does not compile under Elixir 1.20. - Query plans measured on a real 312 GB instance (above); the worker itself has not yet run against one.
Pleroma.Retention treats remote content as a cache: a thread is evicted
once nobody local has interacted with it and it has been quiet for
longer than remote_post_retention_days, or when the objects table is
over an optional row watermark. Local posts are never deleted.

A single grouping scan collects activity and object ids per thread so
the deletes only touch primary key and ap_id indexes.
Adds config :pleroma, :retention (disabled by default) and an hourly
crontab entry that evicts one bounded batch per run.
prune_objects --keep-threads now shares the thread selection with
Pleroma.Retention, which additionally keeps threads that mention a
local user or that are referenced by a report. The hashtag cleanup
is shared as well.
Grouping the whole activities table by thread every run is unusable on
a large instance: on a 312 GB database it plans as a seq scan plus a
50M-row on-disk sort. Instead, walk activities in id order (flake ids
are time-ordered) from a persisted cursor up to the id that corresponds
to the deadline, and verify only the threads those activities belong to
through the (type, context) index. Measured on that instance: 147 ms
for a 50k-activity range scan, 349 ms warm / 14 s cold to verify ~700
threads.

The cursor is persisted per chunk so a killed run resumes. The
watermark walk keeps a cursor of its own so it cannot leave the
age-based walk stranded behind the deadline. Adds the retention_cursors
table.
docs(retention): describe the cursor walk and its limits
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
89e684dc9c
A mention or DM was only pinned by its notification, so clearing
notifications let retention evict a user's direct messages. Check the
activity recipients instead, which does not go away.
Over the watermark the walk went up to the present, so posts that had
just arrived could be evicted while people were reading them.
Each run scanned the whole hashtags table. Collect the hashtag ids of
the objects about to be deleted and check just those.
test(retention): cover the recipients pin in prune_objects and watermark activity
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
5a4ada881f
fix(retention): only delete objects that belong to an evicted thread
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
5d1e331589
Eviction took the object ids of every activity in a thread. A Like or
Announce whose context differs from its object's then deleted a post in
another, possibly pinned thread. Found on a copy of a real instance:
593 posts from threads with local activity in the first 2M objects.
fix(retention): see local reactions of the legacy EmojiReaction type
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
5e425ce9f5
Older data stores emoji reactions as EmojiReaction. The per-thread check
only looked at EmojiReact, so a thread whose only local interaction was
such a reaction was evicted.
lambadalambda changed title from WIP: Bounded retention mode for remote content to Bounded retention mode for remote content 2026-09-25 04:26:30 +00:00
fix(retention): keep threads whose post a local user quoted
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
e93f05a79f
A quote has a context of its own, so the quoted remote thread counted as
untouched and was evicted, breaking the local quote. On lain.com 45 of
134 quoted posts went this way. The objects_quote_url index keeps the
lookup at one probe per post.
Member

Gonna run this on sgsgb now

Gonna run this on sgsgb now
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feature/bounded-retention:feature/bounded-retention
git switch feature/bounded-retention

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch develop
git merge --no-ff feature/bounded-retention
git switch feature/bounded-retention
git rebase develop
git switch develop
git merge --ff-only feature/bounded-retention
git switch feature/bounded-retention
git rebase develop
git switch develop
git merge --no-ff feature/bounded-retention
git switch develop
git merge --squash feature/bounded-retention
git switch develop
git merge --ff-only feature/bounded-retention
git switch develop
git merge feature/bounded-retention
git push origin develop
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
pleroma/pleroma!7980
No description provided.