[SERVER-14833] Creating descending index on _id can corrupt namespace Created: 08/Aug/14  Updated: 27/Oct/15  Resolved: 11/Aug/14

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: 2.4.10
Fix Version/s: 2.4.11

Type: Bug Priority: Critical - P2
Reporter: David Hows Assignee: J Rassi
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to DOCS-4046 descending _id index -> ascending Closed
related to SERVER-14849 Creating descending _id index should ... Backlog
related to SERVER-14848 Port index_id_desc.js to v2.6 and mas... Closed
Tested
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

db.foo.ensureIndex({_id:-1});
db.foo.ensureIndex({_id:-1});

Participants:

 Description   
Issue Status as of Aug 21, 2014

ISSUE SUMMARY
It is possible to create multiple _id indexes on a collection using ensureIndex({_id:-1}).

db.foo.ensureIndex({_id:-1})
db.foo.ensureIndex({_id:-1})
db.foo.ensureIndex({_id:-1})
db.foo.getIndexes().length 
4

Although having multiple _id indexes on a collection is not intended behavior, the collection may be usable in this state. However, an attempt to subsequently drop such a collection corrupts the namespace:

db.foo.drop()
Fri Aug  8 16:43:48.593 drop failed: {
    "nIndexesWas" : 4,
    "errmsg" : "exception: drop: dropIndexes for collection failed - consider trying repair  cause: BSONObj size: 0 (0x00000000) is invalid. Size must be between 0 and 16793600(16MB) First element: EOO",
    "code" : 12503,
    "ok" : 0
} at src/mongo/shell/collection.js:383

USER IMPACT
The affected namespace is corrupted upon collection drop, and cannot be used until it is repaired.

WORKAROUNDS
Create _id indexes with ensureIndex({_id: 1}) instead of ensureIndex({_id: -1}). If the indexes have already been created and the affected collection needs to be dropped, run db.repairDatabase() afterwards to fix the namespace corruption.

AFFECTED VERSIONS
MongoDB 2.4 production releases are affected by this issue.

FIX VERSION
The fix is included in the 2.4.11 production release.

RESOLUTION DETAILS
Reject ensureIndex({{_id:-1}) requests if the _id index already exists.

Original description

Affects only 2.4 branch (issue not in 2.6.3 nor in master as of 7 Aug 2014 GMT)

You can insert the same descending _id index multiple times, as they are inserting as ascending each time.

Create a descending _id index:

db.foo.ensureIndex({_id:-1});
db.foo.ensureIndex({_id:-1});
db.foo.ensureIndex({_id:-1});

Check the count

> db.foo.getIndexes().length 
4

We have created the same index 3 times.

Attempting to drop the collection will corrupt the namespace

> db.foo.drop();
Fri Aug  8 16:43:48.593 drop failed: {
	"nIndexesWas" : 4,
	"errmsg" : "exception: drop: dropIndexes for collection failed - consider trying repair  cause: BSONObj size: 0 (0x00000000) is invalid. Size must be between 0 and 16793600(16MB) First element: EOO",
	"code" : 12503,
	"ok" : 0
} at src/mongo/shell/collection.js:383



 Comments   
Comment by Githook User [ 13/Aug/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-14833 fts_index3.js build correct index, minor comment fixes
Branch: v2.4
https://github.com/mongodb/mongo/commit/bd14042812b9bab796a83672f95b2d0e2e6b4252

Comment by Githook User [ 12/Aug/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-14833 Fix issue where 98eb2f1d broke text index spec rewrite
Branch: v2.4
https://github.com/mongodb/mongo/commit/89d34fd3eb9b21dab1243363e14f5f5ef6a430ff

Comment by Githook User [ 11/Aug/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-14833 {_id:-1} index builds should no-op if _id index exists
Branch: v2.4
https://github.com/mongodb/mongo/commit/98eb2f1dd5e9577db16f2f109b0d8b5b9d56552c

Generated at Thu Feb 08 03:36:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.