Mongo→Postgres conversion does not create the indexes on `legacy_object.expireAt` and `legacy_zset`
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,
checkUpgradeconvertsobjectsinto thelegacy_*tables
How we migrated, in short:
mongoexporttheobjectscollection and load each document into an old-styleobjects (data jsonb)table.- Stub
fun__objects__expireAt, whichcheckUpgradedrops during the conversion. - Start NodeBB (
ghcr.io/nodebb/nodebb:4.15.2) against that database. On init,checkUpgradeconvertsobjectsinto thelegacy_*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 toip:recentthe 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_objectacross the whole table. legacy_objectis about 450k rows for us. Without the index, that delete is a full table scan.- We measured about 170 full scans of
legacy_objecta 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
- The built-in conversion in
checkUpgradedoes NOT create indexes. This looks like the missing piece: - Setup creates both indexes:
- Ben Lubar's standalone converter (now archived) does the same conversion and also creates both indexes:
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:
DELETE FROM "legacy_object"
WHERE "expireAt" IS NOT NULL
AND "expireAt" <= CURRENT_TIMESTAMPThe delete runs before every write, from these two places:
- https://github.com/NodeBB/NodeBB/blob/3c94b9359e33e6426244b5d1aae58b470f8b9bfb/src/database/postgres/helpers.js#L21-L28
- https://github.com/NodeBB/NodeBB/blob/3c94b9359e33e6426244b5d1aae58b470f8b9bfb/src/database/postgres/helpers.js#L46-L57
If the delete only touched the key(s) being written, the missing index would have been moot, e.g.:
DELETE FROM "legacy_object"
WHERE "_key" = $1::TEXT
AND "expireAt" IS NOT NULL
AND "expireAt" <= CURRENT_TIMESTAMPGranted, 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
Source: NodeBB/NodeBB