mongodbwiredtiger

Is db.stats() a blocking call for MongoDB?


While researching how to check the size of a MongoDB, I found this comment:

Be warned that dbstats blocks your database while it runs, so it's not suitable in production. https://jira.mongodb.org/browse/SERVER-5714

Looking at the linked bug report (which is still open), it quotes the Mongo docs as saying:

Command takes some time to run, typically a few seconds unless the .ns file is very large (via use of --nssize). While running other operations may be blocked.

However, when I check the current Mongo docs, I don't find that text. Instead, they say:

The time required to run the command depends on the total size of the database. Because the command must touch all data files, the command may take several seconds to run.

For MongoDB instances using the WiredTiger storage engine, after an unclean shutdown, statistics on size and count may off by up to 1000 documents as reported by collStats, dbStats, count. To restore the correct statistics for the collection, run validate on the collection.

Does this mean the WiredTiger storage engine changed this to a non-blocking call by keeping ongoing stats?


Solution

  • a bit late to the game but I found this question while looking for the answer, and the answer is: Yes until 3.6.12 / 4.0.5 it was acquiring a "shared" lock ("R") which block all write requests during the execution. After that it's now an "intent shared" lock ("r") which doesn't block write requests. Read requests were not impacted.

    Source: https://jira.mongodb.org/browse/SERVER-36437