Exact home timelines with a gated bounded fast query #7994

Open
lambadalambda wants to merge 2 commits from perf/home-timeline-bounded into develop

Summary

Two topical commits:

  • Exact home selection: scope gin_fuzzy_search_limit = 0 to home-timeline database selection. PostgreSQL's fuzzy GIN limit can otherwise omit matching activities and make repeated pages differ. Keep the configured limit for search and all other callers; rendering remains outside the transaction.
  • Gated fast path: try a bounded home query only with at least 128 unique effective recipient addresses (including list memberships) and no followed hashtags. These are conservative starting thresholds from the read-only query experiments, not a claim of universal speedup.

Query and fallback behavior

  • Select from the first 8,192 activities in the request's pagination direction and ID range.
  • Reuse the complete existing filter pipeline outside the candidate limit; change only the positive recipient-overlap predicate to EXISTS (SELECT … FROM unnest(recipients) … ANY($recipients)). Preserve the self-post OR, list behavior, blocks, mutes, visibility, object filters, and typed preloads.
  • In the same SQL statement/snapshot, count up to 8,193 candidate IDs. A full page can return immediately; a partial or empty page is accepted only when that metadata proves the requested range was exhausted. This avoids an unnecessary full-query fallback for quiet since_id polls.
  • Hydrate only selected page IDs (at most 40) through the shared filter/preload pipeline. Concurrent deletes or visibility changes may shorten a page, but cannot introduce an unselected activity.
  • Give the speculative queries a 250 ms PostgreSQL timeout budget, reducing the hydration timeout by elapsed time. Recover a canceled query using a savepoint, then run the original exact query. Also fall back when a truncated window cannot fill the page.
  • Roll back read-only savepoints even on success to restore prior GIN/timeout settings, including nested transactions and SQL Sandbox. Non-cancellation errors propagate.

No global GIN configuration change, migration, index addition, or deployment.

Verification

TDD regression tests, independent correctness/architecture reviews for both commits, formatting and git diff --check.

mix test   test/pleroma/web/activity_pub/activity_pub_test.exs   test/pleroma/web/activity_pub/home_timeline_test.exs   test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs   test/pleroma/pagination_test.exs

175 tests, 0 failures, 1 skipped, using Elixir 1.17.3 / OTP 26 and an isolated PostgreSQL 18.6 database.

Coverage includes settings restoration, real SQL timeout recovery in selection and hydration, unrelated-error propagation, gate boundaries, exact 8,192/8,193 candidate boundaries, sparse fallback, partial/empty polling, pagination bounds/order/limits, shared filters, preloads/thread-mute fields, and list/exclusive-list behavior.

A local EXPLAIN (ANALYZE, BUFFERS) on a synthetic 90,060-activity fixture confirms an activity-ID index-only metadata scan capped at 8,193 and an ordered activity-ID source scan capped at 8,192 below the filters. The full-page fixture stopped after 20 source activities; selection took about 4.4 ms locally. This is a query-shape check, not a production speedup claim.

## Summary Two topical commits: - **Exact home selection:** scope `gin_fuzzy_search_limit = 0` to home-timeline database selection. PostgreSQL's fuzzy GIN limit can otherwise omit matching activities and make repeated pages differ. Keep the configured limit for search and all other callers; rendering remains outside the transaction. - **Gated fast path:** try a bounded home query only with **at least 128 unique effective recipient addresses** (including list memberships) and **no followed hashtags**. These are conservative starting thresholds from the read-only query experiments, not a claim of universal speedup. ## Query and fallback behavior - Select from the first **8,192 activities** in the request's pagination direction and ID range. - Reuse the complete existing filter pipeline outside the candidate limit; change only the positive recipient-overlap predicate to `EXISTS (SELECT … FROM unnest(recipients) … ANY($recipients))`. Preserve the self-post OR, list behavior, blocks, mutes, visibility, object filters, and typed preloads. - In the same SQL statement/snapshot, count up to **8,193 candidate IDs**. A full page can return immediately; a partial or empty page is accepted only when that metadata proves the requested range was exhausted. This avoids an unnecessary full-query fallback for quiet `since_id` polls. - Hydrate only selected page IDs (at most 40) through the shared filter/preload pipeline. Concurrent deletes or visibility changes may shorten a page, but cannot introduce an unselected activity. - Give the speculative queries a **250 ms PostgreSQL timeout budget**, reducing the hydration timeout by elapsed time. Recover a canceled query using a savepoint, then run the original exact query. Also fall back when a truncated window cannot fill the page. - Roll back read-only savepoints even on success to restore prior GIN/timeout settings, including nested transactions and SQL Sandbox. Non-cancellation errors propagate. No global GIN configuration change, migration, index addition, or deployment. ## Verification TDD regression tests, independent correctness/architecture reviews for both commits, formatting and `git diff --check`. ```sh mix test test/pleroma/web/activity_pub/activity_pub_test.exs test/pleroma/web/activity_pub/home_timeline_test.exs test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs test/pleroma/pagination_test.exs ``` **175 tests, 0 failures, 1 skipped**, using Elixir 1.17.3 / OTP 26 and an isolated PostgreSQL 18.6 database. Coverage includes settings restoration, real SQL timeout recovery in selection and hydration, unrelated-error propagation, gate boundaries, exact 8,192/8,193 candidate boundaries, sparse fallback, partial/empty polling, pagination bounds/order/limits, shared filters, preloads/thread-mute fields, and list/exclusive-list behavior. A local `EXPLAIN (ANALYZE, BUFFERS)` on a synthetic 90,060-activity fixture confirms an activity-ID index-only metadata scan capped at 8,193 and an ordered activity-ID source scan capped at 8,192 below the filters. The full-page fixture stopped after 20 source activities; selection took about 4.4 ms locally. This is a query-shape check, not a production speedup claim.
perf: gate bounded home timeline selection with exact fallback
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
810734db2e
Author
Owner

Two things here:

  1. a correctness fix for timelines. the fuzzy gin limit also applies to general gin index usage, not just search.
  2. a query improvement for actually active accounts, gated so that it doesn't fail catastrophically for some cases (like, users who don't follow anyone, or who only follow dead accounts). speeds up the timeline for active accounts on soykaf by around 2x.
Two things here: 1. a correctness fix for timelines. the fuzzy gin limit also applies to general gin index usage, not just search. 2. a query improvement for actually active accounts, gated so that it doesn't fail catastrophically for some cases (like, users who don't follow anyone, or who only follow dead accounts). speeds up the timeline for active accounts on soykaf by around 2x.
lambadalambda changed title from WIP: Exact home timelines with a gated bounded fast query to Exact home timelines with a gated bounded fast query 2026-09-25 12:37:38 +00:00
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 perf/home-timeline-bounded:perf/home-timeline-bounded
git switch perf/home-timeline-bounded

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 perf/home-timeline-bounded
git switch perf/home-timeline-bounded
git rebase develop
git switch develop
git merge --ff-only perf/home-timeline-bounded
git switch perf/home-timeline-bounded
git rebase develop
git switch develop
git merge --no-ff perf/home-timeline-bounded
git switch develop
git merge --squash perf/home-timeline-bounded
git switch develop
git merge --ff-only perf/home-timeline-bounded
git switch develop
git merge perf/home-timeline-bounded
git push origin develop
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
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!7994
No description provided.