dgraph-io/badger · critical

failed to read block offset from index. Data corrupted

Error message

failed to read block offset from index. Data corrupted

What it means

After parsing the index via flatbuffers, initIndex requires at least one block offset. If index.Offsets fails to populate the BlockOffset struct at position 0, the parsed index is structurally invalid and the table is declared corrupted.

Source

Thrown at table/table.go:496

	}
	if !t.shouldDecrypt() {
		// If there's no encryption, this points to the mmap'ed buffer.
		t._index = index
	}
	t._cheap = &cheapIndex{
		MaxVersion:        index.MaxVersion(),
		KeyCount:          index.KeyCount(),
		UncompressedSize:  index.UncompressedSize(),
		OnDiskSize:        index.OnDiskSize(),
		OffsetsLength:     index.OffsetsLength(),
		BloomFilterLength: index.BloomFilterLength(),
	}

	t.hasBloomFilter = len(index.BloomFilterBytes()) > 0

	var bo fb.BlockOffset
	if !index.Offsets(&bo, 0) {
		return nil, errors.New("failed to read block offset from index. Data corrupted")
	}
	return &bo, nil
}

// KeySplits splits the table into at least n ranges based on the block offsets.
func (t *Table) KeySplits(n int, prefix []byte) []string {
	if n == 0 {
		return nil
	}

	oLen := t.offsetsLength()
	jump := oLen / n
	if jump == 0 {
		jump = 1
	}

	var bo fb.BlockOffset
	var res []string

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Verify encryption keys are correct and unchanged for encrypted tables
  2. Quarantine the corrupt table file and restore from backup
  3. Run disk health checks (SMART) if multiple tables show this error
  4. Rebuild the database via db.Backup/db.Load

Example fix

// before: rotating encryption key without rekeying data

// after: key rotation requires re-opening with same key or full re-import
opt.WithEncryptionKey(sameOriginalKey).WithEncryptionAlgo(aes)
db.Backup(w, 0)
// re-open fresh dir with new key and db.Load(r)
Defensive patterns

Strategy: fallback

Try / catch

db, err := badger.Open(opt)
if err != nil && strings.Contains(err.Error(), "failed to read block offset") {
    // restore the table from backup or rebuild via Backup/Load
}

Prevention

When it happens

Trigger: initIndex reading an index whose flatbuffer bytes are present but do not decode to a TableIndex with >= 1 Offsets — e.g. the wrong byte range was read due to a corrupted footer, or the index bytes were overwritten.

Common situations: Bit rot / bad sectors corrupting index bytes mid-file; decryption of an encrypted table with the wrong key producing garbage; footer values desynced from actual index content.

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/c5fdcb954e376136. Report an issue: GitHub.