Three of Hanko's Six Missing Indexes Were Already Inside Another Index

Hanko Cloud's Nov 30 sign-in slowdown came from six unindexed foreign key columns. Three of them already sat in a composite index, just not first, and MySQL would have indexed all six on its own.

Share
Abstract grid of two-part index keys sorted by the first key, with a few highlighted second keys scattered across every row.

On November 30, sign-in, session validation and user lookups at Hanko Cloud slowed down for 49 minutes, from 13:58 to 14:47 UTC. Frederic Jahn's postmortem, published the next day, traces it to PostgreSQL running sequential scans on two queries that load a user's related records. One customer reported that some of their users could not sign in.

The fix was six new indexes. The part worth studying is where the gaps were. Three of the six columns had no index at all. The other three were already in a two-column index, in second position.

The three that looked covered

Loading a full user in Hanko eager-loads its associations, and the ORM resolves each one with a WHERE <fk> IN (...) query, according to the fix commit. The commit lists the six columns. emails.user_id, identities.user_id and identities.email_id had no index. The rest were "only covered by" a composite index: webauthn_credentials.user_id by (id, user_id), webauthn_credential_transports.webauthn_credential_id by (name, webauthn_credential_id), and user_custom_claims.user_id by (tenant_id, user_id).

A B-tree on (id, user_id) is sorted by id first. A query that filters only on user_id gets no help finding where that user's rows start. PostgreSQL's page on multicolumn indexes spells out the rule: only constraints on the leading columns limit how much of the index is scanned. Newer releases add skip scan, which can use a later column when the leading one has "so few distinct x values" that most of the index can be skipped. With many distinct leading values, the docs say, "the entire index will have to be scanned, so in most cases the planner will prefer a sequential table scan." A leading id column is close to the worst case, and a sequential scan is what Hanko got.

Table of Hanko's six lookup columns: emails.user_id, identities.user_id and identities.email_id had no index; webauthn_credentials.user_id, webauthn_credential_transports.webauthn_credential_id and user_custom_claims.user_id were only second columns in composite indexes.
The six columns from Hanko's fix commit 231f678 (github.com/teamhanko/hanko).

MySQL would have built these for them

The migration in the commit runs on PostgreSQL only. The commit message gives the reason: MySQL and MariaDB with InnoDB "already have implicit indexes on these foreign key columns."

That is documented MySQL behavior. The MySQL manual says the referencing table must have an index "where the foreign key columns are listed as the first columns," and that such an index "is created on the referencing table automatically if it does not exist." So on MySQL, (id, user_id) would not have satisfied the foreign key on user_id, and the engine would have added the index Hanko was missing.

PostgreSQL makes no such promise. Its constraints chapter says "the declaration of a foreign key constraint does not automatically create an index on the referencing columns," because not every schema needs one. The same schema was indexed on one engine and scanning on the other.

The probe that stayed green

Hanko's status page probe calls /status, which reads signing keys from a small table. That query kept succeeding throughout. Internal monitoring did not alert either. A customer reported the problem at 14:17 UTC, and the status page post went up at 14:33. Traffic fell back by 14:47 and the service recovered with it. The team found the index gaps at 15:14 and deployed the migration at 09:41 UTC on December 1.

Because of what the postmortem calls a monitoring misconfiguration, Hanko says it cannot tell how many projects or users were affected. The follow-ups are latency alerts for the API and database CPU, an audit of the rest of the PostgreSQL schema for similar gaps, and possibly a synthetic check that runs sign-in or session validation.

What I would check after reading it

If you run PostgreSQL behind an ORM that eager-loads by foreign key, list every foreign key column that is not the first column of some index. That check catches all six of Hanko's columns, including the three a person reading the index list would likely pass. The catalog tables pg_constraint and pg_index have everything needed to write it.

One note from the postmortem for self-hosters: the migration builds the indexes without CONCURRENTLY, so writes to each table may pause while its index builds. On large tables, pick the upgrade window with that in mind.

Read more

0 subscribers
0 average monthly readers