litedb-org/LiteDB · error · LiteException

0

0

Error message

This collection has no more space for new indexes

What it means

Thrown by CollectionPage.InsertCollectionIndex when a collection cannot accept another standard index. Two conditions trigger it: the collection already has 255 indexes (the byte slot limit), or the serialized length of the new index entry plus existing entries exceeds P_INDEXES_COUNT — the fixed byte region reserved for index metadata inside the single collection header page. Each CollectionIndex occupies space proportional to its name and expression string.

Source

Thrown at LiteDB/Engine/Pages/CollectionPage.cs:203

        public VectorIndexMetadata GetVectorIndexMetadata(string name)
        {
            return _vectorIndexes.TryGetValue(name, out var metadata) ? metadata : null;
        }

        /// <summary>
        /// Insert new index inside this collection page
        /// </summary>
        public CollectionIndex InsertCollectionIndex(string name, string expr, bool unique)
        {
            if (_indexes.ContainsKey(name) || _vectorIndexes.ContainsKey(name))
            {
                throw LiteException.IndexAlreadyExist(name);
            }

            var totalLength = this.GetSerializedLength(CollectionIndex.GetLength(name, expr), 0);

            if (_indexes.Count == 255 || totalLength >= P_INDEXES_COUNT) throw new LiteException(0, $"This collection has no more space for new indexes");

            var slot = (byte)(_indexes.Count == 0 ? 0 : (_indexes.Max(x => x.Value.Slot) + 1));

            var index = new CollectionIndex(slot, 0, name, expr, unique);

            _indexes[name] = index;

            this.IsDirty = true;

            return index;
        }

        public (CollectionIndex Index, VectorIndexMetadata Metadata) InsertVectorIndex(string name, string expr, ushort dimensions, VectorDistanceMetric metric)
        {
            if (_indexes.ContainsKey(name) || _vectorIndexes.ContainsKey(name))
            {
                throw LiteException.IndexAlreadyExist(name);
            }

View on GitHub (pinned to f906a5f850)

Solutions

  1. Drop unused indexes before creating new ones (db.GetCollection(name).DropIndex(...)).
  2. Shorten index names and/or expression strings to fit in the reserved header region.
  3. If you genuinely need more than 255 indexes, reconsider the data model — LiteDB collections are capped by design.
  4. Audit existing indexes with GetIndexes() before adding more.

Example fix

// before — too many indexes
col.EnsureIndex("idx_256", "$ field256"); // throws

// after — drop unused, then add
col.CreateIndex("idx_256", "$ field256");
// or remove a stale one first:
col.DropIndex("idx_old");
Defensive patterns

Strategy: validation

Validate before calling

const int MAX_INDEXES = 255;

public void CreateIndexSafely(ILiteCollection<BsonDocument> col, string name, string expr, bool unique)
{
    var existing = col.GetIndexes().Count();
    if (existing >= MAX_INDEXES)
        throw new InvalidOperationException($"Collection '{col.Name}' already has {existing} indexes (max {MAX_INDEXES}). Drop one first.");
    col.CreateIndex(name, BsonExpression.Create(expr), unique);
}

Type guard

static bool CanAddIndex(ILiteCollection<BsonDocument> col) =>
    col.GetIndexes().Count() < 255;

Try / catch

try
{
    col.EnsureIndex(name, expr);
}
catch (LiteException ex) when (ex.Message.Contains("no more space for new indexes"))
{
    // Drop a stale index or report to the user that the collection is at capacity.
    throw new InvalidOperationException("Collection has reached its index limit (255) or header space is full.", ex);
}

Prevention

When it happens

Trigger: Calling EnsureIndex / CreateIndex (or db.Execute with CREATE INDEX) on a collection that is at the 255-index hard cap, or whose header page has insufficient free bytes for the new index metadata due to long index names/expressions.

Common situations: Programmatically generating many indexes (e.g. one per dynamic field); using very long index names or complex expression strings that consume the reserved byte budget; accumulating indexes over time without cleanup.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/3982142c16874766. Report an issue: GitHub.