#14926·NodeBB

Mongo→Postgres conversion does not create the indexes on `legacy_object.expireAt` and `legacy_zset`

Author: DanVanAttaCreated Sep 27, 2026Updated Sep 27, 2026
Labelsbug

NodeBB version

4.15.2

NodeBB git hash

No response

NodeJS version

No response

Installed NodeBB plugins

No response

Database type

No response

Database version

Postgres 18

Exact steps to cause this issue

  • DB migration from Mongo to Postgres
  • Start NodeBB (ghcr.io/nodebb/nodebb:4.15.2)
  • On init, checkUpgrade converts objects into the legacy_* tables

How we migrated, in short:

  1. mongoexport the objects collection and load each document into an old-style objects (data jsonb) table.
  2. Stub fun__objects__expireAt, which checkUpgrade drops during the conversion.
  3. Start NodeBB (ghcr.io/nodebb/nodebb:4.15.2) against that database. On init, checkUpgrade converts objects into the legacy_* tables, without the indexes.

The full shell history of the migration, including data fixups specific to our data: https://gist.github.com/DanVanAtta/1fdd5fd7a5c27dfe22610b1db6e01caa

What you expected

Postgres indexes to be created along with the legacy_* tables

What happened instead

No Postgres indexes created

Anything else?

Hi! Our forum, https://forums.triplea-game.org, runs on NodeBB. Yesterday a scraper gave us an impromptu load test and took the site down at about 70 requests/s [1]. We were on 4.15.2 with PostgreSQL 18.

We recently migrated from MongoDB to Postgres under that same NodeBB version. We loaded the Mongo export into an objects table and started NodeBB, which converted the 'objects' into the legacy_* tables (checkUpgrade in postgres.js). That conversion does not create the indexes on legacy_object.expireAt or legacy_zset (_key, score), and the missing expireAt index turned out to be important.

Why did the missing index hurt?

  • Unique-visitor counting was on (logIPs). NodeBB writes a visitor's IP to ip:recent the first time it sees that IP each hour.
  • The scraper rotated across ~27k IP addresses, so a steady share of its requests were an IP's first visit of the hour, and each of those was a write.
  • Before every write, NodeBB's Postgres adapter deletes expired rows from legacy_object across the whole table.
  • legacy_object is about 450k rows for us. Without the index, that delete is a full table scan.
  • We measured about 170 full scans of legacy_object a minute, roughly 3 a second. Across all the other load, CPU became pegged at 100%, everything started to queue up, nginx ran out of connections, and the site went from slow to down.

Suggested fix (1): create the indexes during database conversion

Since createIndices uses CREATE INDEX IF NOT EXISTS, calling it on startup would also cover restored databases.

Suggested fix (2): only delete expired rows for the key being written

The delete of expired rows is fairly simple:

sql
  DELETE FROM "legacy_object"
   WHERE "expireAt" IS NOT NULL
     AND "expireAt" <= CURRENT_TIMESTAMP

The delete runs before every write, from these two places:

If the delete only touched the key(s) being written, the missing index would have been moot, e.g.:

sql
  DELETE FROM "legacy_object"
   WHERE "_key" = $1::TEXT 
     AND "expireAt" IS NOT NULL
     AND "expireAt" <= CURRENT_TIMESTAMP

Granted, with the expireAt index the full-table delete is fairly cheap. Still, this change would turn a query on the hot path from an index range scan into a primary-key lookup. Clean up of all expired keys would potentially need a periodic sweep of the whole table.

[1] Write up of the scraping incident: https://github.com/triplea-game/forums/discussions/5