Make the search allowlist maintenance script online and batched
Problem
scripts/maintenance/nip_rs_search_allowlist.sql changes which event kinds get a full-text index. It does this in one transaction:
ALTER TABLE events DROP COLUMN search_tsvALTER TABLE events ADD COLUMN search_tsv ... GENERATED ALWAYS AS (...) STOREDCREATE INDEX idx_events_search_tsv ON events USING GIN (search_tsv)
This rewrites every events partition and rebuilds the partitioned GIN index while it holds ACCESS EXCLUSIVE. All event reads and writes stop until the transaction commits. The script header says so, and it tells the operator to use a maintenance window.
docs/database.md does not allow this shape:
- "A change MUST NOT rewrite or fully scan a large table under a write-blocking lock. That includes ... changes to the generated
events.search_tsvcolumn. Use the expand/contract forms above." - Rule 3: a
scripts/maintenance/script "MUST work in bounded batches that each commit on their own, and MUST end with a validation query". - Rule 6: changes to partitioned tables (
events) need their own reviewed procedure.
The problem is older than the emoji work. PR #8783 adds kind 30180 to the script's kind list and documents that a database built from migrations/0008_fresh_install_search_allowlist.sql must run the script before the emoji migration rollout. So the next run of this script is now planned, which makes the problem more urgent.
Impact
- A run on a large production database blocks all event traffic for the full rewrite. The time grows with table size.
- The rewrite needs free space for a second copy of the heap, TOAST, and index files, plus WAL.
- Each future change to the kind list repeats the full rewrite.
Proposal
Replace the script with an expand/contract procedure that never rewrites events under a write-blocking lock. One possible shape:
- Expand: add a plain nullable
tsvectorcolumn (not generated). The relay write path fills it for the allowlisted kinds. - Backfill: a batched
scripts/maintenance/script (or a definition for the backfill framework indocs/data-backfills.md, if it has shipped) that fills the new column partition by partition, commits per batch, is idempotent, and ends with a validation query. - Index: build the GIN index per partition with
CREATE INDEX CONCURRENTLY, and attach the partition indexes to anON ONLYparent index. - Switch:
buzz-searchreads the new column. - Contract: drop the old generated column in a later release.
With this shape, a change to the kind list is a code change plus a bounded backfill, not a table rewrite.
Other options a reviewer may prefer: a per-kind partial expression index instead of a stored column, or keeping the deny-list expression from schema/tables/public/events.sql and retiring the allowlist path.
Open questions
- Which deployments run on a database built from migration 0008, or have already run this script? Block's hosted relay is the important case for the emoji rollout.
- Does the deployed relay depend on
search_tsvbeing a generated column, for example inbuzz-searchqueries or in the desired-state schema checks?
References
scripts/maintenance/nip_rs_search_allowlist.sqlmigrations/0008_fresh_install_search_allowlist.sqldocs/database.md(Rule 3, Rule 6, and the online-change rules)- PR #8783 (kind 30180 custom emoji entries) and its review thread on the FTS allowlist
🤖 Generated with Claude Code