Skip to content

PostgreSQL: index for the AT_TIMESTAMP stream seek needs a data migration #467

Open

Description

@LeeroyHannigan

The AT_TIMESTAMP seek on PostgreSQL runs SELECT MIN(sequence_number) FROM stream_records WHERE shard_id = $1 AND created_at >= $2. The planner takes the minimum off the (shard_id, sequence_number) primary key and applies created_at as a heap filter; the global created_at index cannot yield the minimum sequence without a sort, so it is not chosen. With sequence and creation time correlated, a seek near the tip filters the whole shard.

Measured on one shard of 100,000 records with created_at spread over 24 hours: a seek at the start answers in 0.04 ms (first row qualifies), a mid-range seek in 9.46 ms (49,999 rows removed by filter), a seek near the tip in 19.21 ms (99,899 rows removed). Linear in shard size beyond that.

A covering index (shard_id, created_at, sequence_number) gives the minimum directly. It needs a data migration file under crates/storage-postgres/data_migrations/ and an entry in migrations::DATA_MIGRATIONS, which means existing deployments run extenddb migrate before the upgraded binary serves, and the catalog-converge and backup tests that enumerate the migration list need the new file added. That upgrade step is why the index is not part of the feature change; GetShardIterator runs about once per consumer resume, so the cost is tolerable until a hot shard holds millions of records.

MongoDB and SQLite got their (shard_id, created_at) index in the feature change itself, since both create indexes idempotently at startup.

Activity

  1. added
    backend:postgresql
    on Oct 27, 2026
  2. LeeroyHannigan commented on Nov 14, 2026

    @LeeroyHannigan
    CollaboratorAuthor

    #468 no longer adds the (shard_id, created_at) index on SQLite and MongoDB. Main moved both schemas into numbered catalog migrations, so the index now needs a migration on SQLite and MongoDB as well as PostgreSQL.

    Until then the AT_TIMESTAMP seek uses the existing (shard_id, sequence_number) index on all three. Cassandra needs no index, because its seek reads one shard's sequence range in one partition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend:postgresql

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions