dgraph-io/badger · error

error during flatten in StreamWriter: %w

Error message

error during flatten in StreamWriter: %w

What it means

When preparing an incremental StreamWriter, the library detects that data spans all levels down to L0 (prevLevel==0) and calls db.Flatten(3) to compact everything to the max level before resuming incremental writes. If that flatten/compaction operation fails, the underlying error is wrapped with this message and returned.

Source

Thrown at stream_writer.go:121

	isEmptyDB := true
	for _, level := range sw.db.Levels() {
		if level.NumTables > 0 {
			sw.prevLevel = level.Level
			isEmptyDB = false
			break
		}
	}
	if isEmptyDB {
		// If DB is empty, we should allow doing incremental stream write.
		return nil
	}
	if sw.prevLevel == 0 {
		// It seems that data is present in all levels from Lmax to L0. If we call flatten
		// on the tree, all the data will go to Lmax. All the levels above will be empty
		// after flatten call. Now, we should be able to use incremental stream writer again.
		if err := sw.db.Flatten(3); err != nil {
			return fmt.Errorf("error during flatten in StreamWriter: %w", err)
		}
		sw.prevLevel = len(sw.db.Levels()) - 1
	}
	return nil
}

// Write writes KVList to DB. Each KV within the list contains the stream id which StreamWriter
// would use to demux the writes. Write is thread safe and can be called concurrently by multiple
// goroutines.
func (sw *StreamWriter) Write(buf *z.Buffer) error {
	if buf.LenNoPadding() == 0 {
		return nil
	}

	// closedStreams keeps track of all streams which are going to be marked as done. We are
	// keeping track of all streams so that we can close them at the end, after inserting all
	// the valid kvs.
	closedStreams := make(map[uint32]struct{})

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Inspect the wrapped %w cause with errors.Unwrap/errors.Is and fix it (e.g., free disk space, repair the DB)
  2. Free disk capacity and ensure the compaction workers can run, then retry PrepareIncremental()
  3. If corruption is suspected, run badger repair or restore from the last good backup before retrying

Example fix

// before
err := sw.PrepareIncremental() // "error during flatten in StreamWriter: ..."
// after
err := sw.PrepareIncremental()
if err != nil {
    cause := errors.Unwrap(err) // inspect flatten failure, e.g. disk full
    // free space / repair db, then retry
}
Defensive patterns

Strategy: retry

Validate before calling

// Ensure ample free disk space and no other compaction-stopping condition before PrepareIncremental.
if !hasFreeDiskSpace(lsmDir, minRequiredBytes) { return errors.New("insufficient disk space for flatten") }

Try / catch

err := sw.PrepareIncremental()
if err != nil {
    var cause error
    for e := err; e != nil; e = errors.Unwrap(e) { cause = e }
    // inspect cause (e.g. disk full / I/O error), remediate, then retry once
}

Prevention

When it happens

Trigger: PrepareIncremental() on a DB where tables are present from Lmax up to L0 and the LSM tree cannot be flattened because compaction fails (e.g., disk full, I/O errors, table corruption).

Common situations: Disk out of space or slow/failing storage during compaction; corrupted SST files from an earlier crash; running PrepareIncremental on a DB in a degraded/compacting state.

Related errors


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