bytebase/bytebase · error

failed to get index schema of collection %s

Error message

failed to get index schema of collection %s

What it means

Raised during MongoDB schema sync: the helper getIndexes, which runs the listIndexes command against a collection, failed while building the TableMetadata for that collection. The wrapped error typically carries a driver failure — collection dropped mid-sync, insufficient privileges, or a connection loss to the MongoDB instance — aborting the whole SyncDBSchema for the database.

Source

Thrown at backend/plugin/db/mongodb/sync.go:175

		}
		dataSize64, err := convertEmptyInterfaceToInt64(dataSize)
		if err != nil {
			slog.Debug("Failed to convert dataSize to int64", slog.Any("dataSize", dataSize))
		}

		totalIndexSize, ok := commandResult["totalIndexSize"]
		if !ok {
			return nil, errors.New("cannot get totalIndexSize from collStats command result")
		}
		totalIndexSize64, err := convertEmptyInterfaceToInt64(totalIndexSize)
		if err != nil {
			slog.Debug("Failed to convert totalIndexSize to int64", slog.Any("totalIndexSize", totalIndexSize))
		}

		// Get collection indexes.
		indexes, err := getIndexes(ctx, collection)
		if err != nil {
			return nil, errors.Wrapf(err, "failed to get index schema of collection %s", collectionName)
		}
		schemaMetadata.Tables = append(schemaMetadata.Tables, &storepb.TableMetadata{
			Name:      collectionName,
			RowCount:  count,
			DataSize:  dataSize64,
			IndexSize: totalIndexSize64,
			Indexes:   indexes,
		})
	}

	for _, viewName := range viewNames {
		schemaMetadata.Views = append(schemaMetadata.Views, &storepb.ViewMetadata{Name: viewName})
	}

	return &storepb.DatabaseSchemaMetadata{
		Name:    d.databaseName,
		Schemas: []*storepb.SchemaMetadata{schemaMetadata},
	}, nil

View on GitHub (pinned to 1870550677)

Solutions

  1. Grant the sync user listIndexes (built-in read role includes it) on the target database.
  2. Confirm the collection still exists: db.<name>.getIndexes() as the sync user; re-run sync if it was dropped mid-scan.
  3. Read the wrapped error for the precise driver/MongoDB code to distinguish auth vs connectivity vs namespace issues.
  4. Exclude or skip the offending namespace if it is a view or system collection not needed in metadata.

Example fix

// before
role limited to {find: targetdb}
// after
db.grantRolesToUser("bytebase_user", [{ role: "read", db: "targetdb" }])  // includes listIndexes
Defensive patterns

Strategy: retry

Validate before calling

// pre-check index listing permission
if _, err := db.RunCommand(ctx, bson.D{{Key:"listIndexes", Value:"probeCollection"}}).Raw(); err != nil {
  return fmt.Errorf("sync user lacks listIndexes: %w", err)
}

Try / catch

_, err := syncDBSchema(ctx)
if err != nil {
  var idxErr *IndexSchemaError
  if errors.As(err, &idxErr) {
    slog.Error("index sync failed", slog.String("collection", idxErr.Collection), slog.Any("err", err))
    // check collection still exists, then retry
  }
}

Prevention

When it happens

Trigger: ListIndexes on a specific collection fails — typically the sync user lacks listIndexes privilege on that collection, the collection was dropped concurrently, or the server connection failed at that point.

Common situations: Least-privilege user with find-only access (listIndexes denied); a concurrent migration dropping the collection while sync runs; special namespaces (views, system.*) where index listing is restricted.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/1e5897e109345e89. Report an issue: GitHub.