Skip to content

Drop redundant leading-column indexes and index dead_letters.retried_message_id #115

Done

Description

@cardmagic

Problem

On origin/main (64e552ad5dfe0c5292b0198d8664802a4bfd9f4e), the create-tables migration in db/migrate/20260805000000_create_solid_objects_tables.rb uses Rails t.references for message and instance foreign keys. That helper adds a plain index on the column. The same migration then adds a unique index whose leading column is that same column. The result is two b-trees for one access path: every insert and delete maintains both, and any query that used the plain index can use the unique one instead.

This was confirmed by running SolidObjects::SchemaBootstrap.install from lib/solid_objects/schema_bootstrap.rb against that revision and reading SQLite PRAGMA index_list / PRAGMA index_info. The names below are under the default solid_objects_ table prefix.

A second, separate gap showed up while measuring the MessagePruner change in #113. dead_letters.retried_message_id has a foreign key (fk_so_dead_letters_retry, ON DELETE SET NULL) and no index on SQLite or PostgreSQL. MySQL creates an index for the foreign key automatically. Each deleted message makes the database find dead letters whose retried_message_id points at it; without an index that is a scan of dead_letters.

Measured on a copy of the synthetic benchmark database (SQLite 3.54.1, 1M messages), deleting 5 batches of 1000 messages inside a rolled-back transaction:

Dead letters in the table Wall time for 5 delete batches
1,000 0.98 s
51,000 22.3 s

The candidate-selection query was not the bottleneck in that measurement (the NOT EXISTS guard on retried_message_id stayed under 40 ms either way). A dead-letter table that is allowed to grow is the next pruner bottleneck for existing installs. #113 changed only code, tests, and the changelog; it did not add this migration.

Goal

Maintenance: query performance and correctness for existing Ruby installs. This ships in the next minor release, not a patch, because existing installs have to run the migration.

Scope

One new reversible migration in db/migrate/ (up and down). Do not rewrite already-shipped migrations. New installs reach the same end state because they apply the full set through SchemaBootstrap.install or bin/rails solid_objects:install:migrations.

1. Drop the plain index only where a unique index already leads with that column

Drop these six plain indexes and keep the unique index that already covers them:

Table Plain index to drop Unique index that covers it
ready_messages index_solid_objects_ready_messages_on_message_id idx_so_ready_message (message_id)
claimed_messages index_solid_objects_claimed_messages_on_message_id idx_so_claimed_message (message_id)
dead_letters index_solid_objects_dead_letters_on_message_id idx_so_dead_letters_message (message_id)
broadcasts index_solid_objects_broadcasts_on_message_id idx_so_broadcasts_observable (message_id, observable_name)
ready_messages index_solid_objects_ready_messages_on_instance_id idx_so_ready_instance_sequence (instance_id, sequence)
claimed_messages index_solid_objects_claimed_messages_on_instance_id idx_so_claimed_instance (instance_id)

Do not drop index_solid_objects_dead_letters_on_instance_id. It is the only index on dead_letters.instance_id. There is no unique index with instance_id as its leading column, so dropping it would leave fk_so_dead_letters_instance without a covering index, which MySQL requires.

Do not drop index_solid_objects_effects_on_message_id. It is the only message_id index on effects.

2. Add an index on dead_letters.retried_message_id

Add one index on that column, with an explicit stable name consistent with the existing idx_so_dead_letters_* names. On MySQL the foreign key may already have an auto-created index; the migration must not leave two indexes on the same column (if_not_exists or the equivalent). The end state on every adapter is that retried_message_id is indexed.

3. Run the migration on every CI adapter, and check MySQL foreign keys

The migration must run, up and down, on every adapter in the CI matrix in .github/workflows/ci.yml: SQLite, PostgreSQL 18, and MySQL 8.4 with both mysql2 and trilogy. MySQL may refuse to drop a plain index that a foreign key is still using. After the drops, a MySQL check must confirm that every foreign key still has a covering index.

4. Schema test that keeps the duplicates from coming back

Add a schema test that fails on any non-unique index whose leading column(s) are already the leading column(s) of a unique index on the same table, except for an explicit allowlist of exactly two index names:

  • index_solid_objects_messages_on_instance_id (covered by idx_so_messages_instance_sequence)
  • index_solid_objects_reminders_on_instance_id (covered by idx_so_reminders_name)

The test must include a comment that these two are known and left for a possible follow-up. It must also fail if an allowlisted index no longer exists, so the list cannot go stale.

The test must not fail merely because two different unique indexes share a leading column. idx_so_messages_instance_sequence (instance_id, sequence) and idx_so_messages_idempotency (instance_id, idempotency_key) are a legitimate pair and must still pass.

5. CHANGELOG note under Unreleased

Add a note under the existing ## Unreleased section in CHANGELOG.md for the next minor release. It must tell existing installs to copy and run the migration with bin/rails solid_objects:install:migrations and their usual db:migrate (the same path documented in docs/operations.md and used for the reminder-history migration in 1.1.0). The release PR itself is not part of this issue.

Acceptance

  • After the new migration, the six plain indexes named above are absent, and the six unique indexes named above are still present, on SQLite, PostgreSQL, and MySQL (mysql2 and trilogy).
  • dead_letters.retried_message_id is indexed on all three databases, with a single index on that column, not two.
  • index_solid_objects_effects_on_message_id and index_solid_objects_dead_letters_on_instance_id are still present.
  • There is exactly one new migration in db/migrate/. It is reversible: down restores the six dropped indexes and removes the index this migration added when that index is present. Already-shipped migrations are unchanged.
  • The migration is exercised on every CI adapter listed above. The MySQL jobs include a check that every foreign key still has a covering index after the drops.
  • A schema test keeps an explicit allowlist of exactly two index names, index_solid_objects_messages_on_instance_id (covered by idx_so_messages_instance_sequence) and index_solid_objects_reminders_on_instance_id (covered by idx_so_reminders_name), with a comment that they are known and left for a possible follow-up. It fails on any other non-unique index whose leading column(s) are already the leading column(s) of a unique index on the same table, and it also fails if an allowlisted index no longer exists. Two unique indexes that merely share a leading column still pass.
  • CHANGELOG.md under ## Unreleased tells existing installs to run the migration before they rely on the new indexes, and names this as a minor-release change.
  • A fresh SchemaBootstrap.install and an upgrade of an already-installed schema both end in that same index set.

Not planned

  • The JavaScript keyset-paging change, and any other JavaScript schema or index work.
  • Any other index or schema change. In particular, this issue does not drop index_solid_objects_messages_on_instance_id (covered by idx_so_messages_instance_sequence) or index_solid_objects_reminders_on_instance_id (covered by idx_so_reminders_name). Those show the same leading-column pattern on current main and are allowlisted in the schema test rather than dropped; they can be a follow-up, and they are not in this migration.
  • Edits to already-shipped migrations, or any change to docs/, README, or AGENTS.md. The documentation change in this issue is the CHANGELOG.md Unreleased note.
  • The release PR, a version bump, or a tag. Merge and release still need Lucas.
  • Edits or comments on other issues.

JavaScript counterpart

The same duplicate plain-plus-unique pattern is not present in cardmagic/solid-objects-js on origin/main (ef6a15e17afb97cc433f3cefe4c0fc1eca3901aa). In src/schema.ts, ready_messages and claimed_messages use message_id TEXT PRIMARY KEY rather than a plain index beside a separately declared unique index. dead_letters uses message_id TEXT NOT NULL UNIQUE and already creates ${prefix}dead_letters_retried_message on retried_message_id. This issue does not add JavaScript work.

Activity

  1. added
    ready-to-implement
    cloving-working
    and removed
    ready-to-implement
    on Dec 2, 2026
  2. removed
    cloving-working
    on Dec 2, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions