Expression index on Sequence length so SV routing by alt length stays a hashed subplan

Open
#2,635 5 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, python

Research direction

Read annotation/annotation_pipeline_routing.py to locate the routing predicate, then inspect Sequence.Meta.indexes and the migrations that create indexes concurrently. Add an expression index on length(seq) and run ANALYZE on snpdb_sequence; verify with EXPLAIN that the subquery stays a hashed SubPlan and uses a bitmap index scan. The issue reports local measurements but does not name a test file.

Written by the indexing model from the issue text.

Description

Annotation

🤖 Written by Claude

Follow-up to #1708 (PR #2428), which left open "the cost of the Length(seq) subquery on a database with many long sequences".

Problem

_explicit_annotated_as_structural_q (annotation/annotation_pipeline_routing.py) routes an explicit variant whose alt is long to the SV pipeline with:

alt_id IN (SELECT id FROM snpdb_sequence WHERE length(seq) >= 1000)

It sits inside an OR, so Postgres can't turn it into a join and plans it as a SubPlan. That has two regimes:

  • Hashed SubPlan: one sequential scan of snpdb_sequence per query (per parallel worker), building a hash of the matching ids. ~20 ms at 100k sequences, ~0.2 s at 1M. Fine.
  • Materialised SubPlan: there are no statistics for length(seq), so the planner assumes a third of the rows match. A SubPlan is only hashed when estimated rows × 32 bytes fits in work_mem × hash_mem_multiplier (4 MB × 2 on Postgres 15+, × 1 before). Past ~786k sequences (15+) or ~393k (13/14) at default settings the planner stops hashing, materialises the matching ids and scans that list for every variant the OR reaches, i.e. nearly every explicit variant with a short ref.

Measured on a developer database (100k-variant pk slice, 758 sequences ≥ 1 kb, parallelism off, cliff forced with work_mem = 512kB):

plan time
hashed SubPlan 40 ms
materialised SubPlan 1,418 ms

Materialised cost scales as variants × long sequences. With the 11,592 long sequences counted in #1708, a 25k-variant annotation run's query would take ~5 s and anything applying the predicate to a whole build (annotation/backfill_columns.py, a whole-build count) goes from seconds to about an hour at 17M variants. The scheduler runs the predicate at least twice per annotation run (the count in annotation_scheduler_task.py and the dump).

Only a VariantAnnotationVersion with explicit_structural_variant_min_size set uses the predicate, so deployments whose versions predate #2428 aren't affected until the next version is created.

Fix

An expression index on length(seq), with an ANALYZE so the planner gets statistics for the expression. Tested locally:

  • Built in 38 ms / 776 kB on 107k rows; on 1M rows it detoasts every sequence once, so expect under a few minutes.
  • After ANALYZE the row estimate is 769 rather than 34,752, so the SubPlan stays hashed even at work_mem = 512kB.
  • The subquery itself becomes a bitmap index scan: 0.25 ms instead of 20 ms.

Index added as Sequence.Meta.indexes, created concurrently in the migration so VCF import isn't blocked writing sequences, followed by ANALYZE snpdb_sequence.

To check a deployment is over the cliff: EXPLAIN the SV routing query; SubPlan with a Materialize under it, rather than hashed SubPlan, is the slow plan.

Dominant language
Python
Stars
31
Forks
4
Avg merge
4h 52m
Merged PRs (30d)
183

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SACGF/variantgrid

All issues in SACGF/variantgrid

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.