dgraph-io/badger · error

ERROR: compression level(%v) must be greater than zero

Error message

ERROR: compression level(%v) must be greater than zero

What it means

badger.ParseCompressionFlag parses a badger.compression superflag string of the form "type[,level]" (e.g. "zstd:3"). This error is returned when the level component is parsed successfully but is <= 0, which is invalid because compression levels must be positive.

Source

Thrown at options.go:235

	// of performance reasons, 1KB would be a good option too, allowing
	// values smaller than 1KB to be collocated with the keys in the LSM tree.
	return DefaultOptions(path).WithValueThreshold(maxValueThreshold /* 1 MB */)
}

// parseCompression returns badger.compressionType and compression level given compression string
// of format compression-type:compression-level
func parseCompression(cStr string) (options.CompressionType, int, error) {
	cStrSplit := strings.Split(cStr, ":")
	cType := cStrSplit[0]
	level := 3

	var err error
	if len(cStrSplit) == 2 {
		level, err = strconv.Atoi(cStrSplit[1])
		y.Check(err)
		if level <= 0 {
			return 0, 0,
				fmt.Errorf("ERROR: compression level(%v) must be greater than zero", level)
		}
	} else if len(cStrSplit) > 2 {
		return 0, 0, fmt.Errorf("ERROR: Invalid badger.compression argument")
	}
	switch cType {
	case "zstd":
		return options.ZSTD, level, nil
	case "snappy":
		return options.Snappy, 0, nil
	case "none":
		return options.None, 0, nil
	}
	return 0, 0, fmt.Errorf("ERROR: compression type (%s) invalid", cType)
}

// generateSuperFlag generates an identical SuperFlag string from the provided Options.
func generateSuperFlag(options Options) string {
	superflag := ""

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Set the level to a positive integer (e.g. zstd:1 or zstd:3)
  2. Omit the level entirely (e.g. badger.compression=zstd) to use the library default
  3. Use badger.compression=none to disable compression
  4. Validate the flag string in config pipelines before applying it to badger

Example fix

// before
badger.compression=zstd:0
// after
badger.compression=zstd:3
Defensive patterns

Strategy: validation

Validate before calling

func validateCompressionFlag(v string) error {
    parts := strings.Split(v, ":")
    if len(parts) > 2 { return fmt.Errorf("invalid badger.compression: %s", v) }
    if len(parts) == 2 {
        lvl, err := strconv.Atoi(parts[1])
        if err != nil || lvl <= 0 { return fmt.Errorf("compression level must be > 0: %s", v) }
    }
    return nil
}

Try / catch

opts, err := badger.DefaultOptions(dir).FromSuperFlag(cfg)
if err != nil && strings.Contains(err.Error(), "must be greater than zero") {
    return fmt.Errorf("fix badger.compression in config: %w", err)
}

Prevention

When it happens

Trigger: Passing a superflag like badger.compression=zstd:0 or badger.compression=snappy:-1 into Options.FromSuperFlag / configuration (CLI flag, env, config file).

Common situations: Hand-edited config files; templates that substitute a zero default; tools generating flags programmatically with an uninitialized level variable.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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