buzz icon indicating copy to clipboard operation
buzz copied to clipboard

Make the search allowlist maintenance script online and batched

Open blakemcanally opened this issue 1 month ago • 0 comments

Problem

scripts/maintenance/nip_rs_search_allowlist.sql changes which event kinds get a full-text index. It does this in one transaction:

  1. ALTER TABLE events DROP COLUMN search_tsv
  2. ALTER TABLE events ADD COLUMN search_tsv ... GENERATED ALWAYS AS (...) STORED
  3. CREATE 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_tsv column. 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:

  1. Expand: add a plain nullable tsvector column (not generated). The relay write path fills it for the allowlisted kinds.
  2. Backfill: a batched scripts/maintenance/ script (or a definition for the backfill framework in docs/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.
  3. Index: build the GIN index per partition with CREATE INDEX CONCURRENTLY, and attach the partition indexes to an ON ONLY parent index.
  4. Switch: buzz-search reads the new column.
  5. 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_tsv being a generated column, for example in buzz-search queries or in the desired-state schema checks?

References

  • scripts/maintenance/nip_rs_search_allowlist.sql
  • migrations/0008_fresh_install_search_allowlist.sql
  • docs/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

blakemcanally avatar Oct 30 '26 22:10 blakemcanally