1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# collection index backfill
the collection index only has entries for accounts that have created records since live indexing was deployed. the backfill imports historical data from a source relay that already has a complete `listReposByCollection` endpoint.
## how it works
the backfiller runs as a background thread, triggered via admin API. it:
1. **discovers collections** from two sources (unioned):
- [lexicon garden](https://lexicon.garden/llms.txt) `llms.txt` — ~700 known NSIDs, parsed from markdown links
- collection-index scan — collections already observed from the live firehose (repo-to-collection prefix scan)
2. **inserts progress rows** into postgres (`backfill_progress` table) for each collection. existing rows are skipped (`ON CONFLICT DO NOTHING`), so re-triggering is safe.
3. **pages through each collection** sequentially, calling `com.atproto.sync.listReposByCollection` on the source relay (default: `bsky.network`) with `limit=1000`. each DID in the response is added to the collection index via `addCollection`. cursor and imported count are persisted after each page for resumability.
4. **marks complete** when a page returns no cursor (no more results).
## progress tracking
```sql
CREATE TABLE backfill_progress (
collection TEXT PRIMARY KEY,
source TEXT NOT NULL,
cursor TEXT NOT NULL DEFAULT '',
imported_count BIGINT NOT NULL DEFAULT 0,
completed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
```
- cursor = last pagination cursor from source relay
- imported_count = total DIDs added for this collection
- completed_at = null while in progress, set when done
- if the process crashes or restarts, it resumes from the saved cursor
## admin API
requires bearer token auth (`RELAY_ADMIN_PASSWORD`).
### trigger backfill
```
POST /admin/backfill-collections?source=bsky.network
```
returns 200 with collection count if started, 409 if already running. only one backfill can run at a time.
### check status
```
GET /admin/backfill-collections
```
returns JSON:
```json
{
"running": true,
"total": 1269,
"completed": 621,
"in_progress": 648,
"total_imported": 13628818,
"collections": [
{"collection": "app.bsky.feed.post", "source": "bsky.network", "imported": 28000000, "completed": true},
{"collection": "app.bsky.feed.like", "source": "bsky.network", "imported": 6732000, "completed": false, "cursor": "did:plc:..."}
]
}
```
## using the script
the relay repo has a convenience script at `scripts/backfill-status`:
```bash
# check progress (summary with recent incomplete)
./scripts/backfill-status status
# trigger a new backfill
./scripts/backfill-status start [source]
# full JSON output
./scripts/backfill-status full
```
requires `ZLAY_ADMIN_PASSWORD` and `ZLAY_DOMAIN` in `.env`.
## performance characteristics
- collections are processed sequentially (one at a time)
- 100ms pause between pages to avoid hammering the source relay
- one HTTP client is reused across all pages for a given collection
- large collections like `app.bsky.feed.like` (~30M+ DIDs) take 1-2 hours each
- small/niche collections complete in seconds
- full backfill of ~1269 collections takes several hours, dominated by the 5-6 largest `app.bsky.*` collections
## re-running
safe to trigger again after completion — existing progress rows are preserved, completed collections are skipped. useful if new collections appear (e.g. new lexicons published to the network).
## source code
- `src/internal/collection_index/backfill.zig` — Backfiller struct with all backfill logic
- `src/internal/event_log.zig` — backfill_progress table creation (in `init()`)
- `src/main.zig` — admin route handlers
## cleanup
```
POST /admin/cleanup-collection-index?scope=inactive-accounts
POST /admin/cleanup-collection-index?scope=invalid-collections
GET /admin/cleanup-collection-index
```
`inactive-accounts` (the default) removes every pair of accounts that are not
active. `invalid-collections` removes every pair filed under a collection name
that is not an NSID; the index no longer accepts such names, so this is only
needed for an index written before 2026-11-13. One cleanup runs at a time; the
status reports `running` and the `removed` count.