dgraph-io/badger · error

Cannot use GetSequence with managedDB=true.

Error message

Cannot use GetSequence with managedDB=true.

What it means

GetSequence is not supported on managed-database mode because badger manages transaction timestamps itself there, conflicting with sequence leases. Calling db.GetSequence while opt.managedTxns is true panics instead of returning an error.

Source

Thrown at db.go:1429

		var buf [8]byte
		binary.BigEndian.PutUint64(buf[:], lease)
		if err = txn.SetEntry(NewEntry(seq.key, buf[:])); err != nil {
			return err
		}
		seq.leased = lease
		return nil
	})
}

// GetSequence would initiate a new sequence object, generating it from the stored lease, if
// available, in the database. Sequence can be used to get a list of monotonically increasing
// integers. Multiple sequences can be created by providing different keys. Bandwidth sets the
// size of the lease, determining how many Next() requests can be served from memory.
//
// GetSequence is not supported on ManagedDB. Calling this would result in a panic.
func (db *DB) GetSequence(key []byte, bandwidth uint64) (*Sequence, error) {
	if db.opt.managedTxns {
		panic("Cannot use GetSequence with managedDB=true.")
	}
	if db.opt.ReadOnly {
		panic("Cannot use GetSequence in read-only mode.")
	}

	switch {
	case len(key) == 0:
		return nil, ErrEmptyKey
	case bandwidth == 0:
		return nil, ErrZeroBandwidth
	}
	seq := &Sequence{
		db:        db,
		key:       key,
		next:      0,
		leased:    0,
		bandwidth: bandwidth,
	}

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Use Open instead of OpenManaged if you need GetSequence
  2. In managed mode, implement sequences via badger.Sequence alternatives: your own counter key in a transaction (read-modify-write)
  3. Only call GetSequence when !db.opt.managedTxns

Example fix

// before
db, _ := badger.OpenManaged(opt)
seq, _ := db.GetSequence([]byte("k"), 100) // panics
// after
db, _ := badger.Open(opt) // non-managed
seq, _ := db.GetSequence([]byte("k"), 100)
Defensive patterns

Strategy: validation

Validate before calling

if db.opt.managedTxns { return errors.New("GetSequence unavailable in managed mode") }

Try / catch

defer func() {
    if r := recover(); r != nil {
        if strings.Contains(fmt.Sprint(r), "GetSequence with managedDB") { /* fallback counter */ }
    }
}()

Prevention

When it happens

Trigger: Calling db.GetSequence(key, bandwidth) on a DB opened via OpenManaged (or Options with managed txn mode).

Common situations: Mixing OpenManaged (used with Dgraph or manual timestamp control) with application-level sequence generation.

Related errors


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