dgraph-io/badger · error
Cannot ban namespace in read-only mode.
Error message
Cannot ban namespace in read-only mode.
What it means
BanNamespace panics when the DB is opened in read-only mode. Banning a namespace writes a marker entry (under bannedNsKey) to the database, which requires write access; read-only handles cannot perform that write, so the library panics to prevent an invalid state.
Source
Thrown at db.go:1956
if db.opt.NamespaceOffset < 0 {
return nil
}
if len(key) <= db.opt.NamespaceOffset+8 {
return nil
}
if db.bannedNamespaces.has(y.BytesToU64(key[db.opt.NamespaceOffset:])) {
return ErrBannedKey
}
return nil
}
// BanNamespace bans a namespace. Read/write to keys belonging to any of such namespace is denied.
func (db *DB) BanNamespace(ns uint64) error {
if db.opt.NamespaceOffset < 0 {
return ErrNamespaceMode
}
if db.opt.ReadOnly {
panic("Cannot ban namespace in read-only mode.")
}
db.opt.Infof("Banning namespace: %d", ns)
// First set the banned namespaces in DB and then update the in-memory structure.
key := y.KeyWithTs(append(bannedNsKey, y.U64ToBytes(ns)...), 1)
entry := []*Entry{{
Key: key,
Value: nil,
}}
req, err := db.sendToWriteCh(entry)
if err != nil {
return err
}
if err := req.Wait(); err != nil {
return err
}
db.bannedNamespaces.add(ns)
return nil
}View on GitHub (pinned to 2a001d466f)
Solutions
- Open a writable DB instance to call BanNamespace
- Guard the call: skip or return an error when the DB options are read-only
- Persist banned namespaces through your own config store and apply them only on writable instances
Example fix
// before
if err := roDB.BanNamespace(ns); err != nil { ... }
// after
if !roDB.IsClosed() && !opts.ReadOnly {
if err := db.BanNamespace(ns); err != nil { ... }
} Defensive patterns
Strategy: validation
Validate before calling
if opts.ReadOnly { return errors.New("BanNamespace requires a writable DB") } Prevention
- Expose admin operations only on writable DB instances
- Check opt.ReadOnly before invoking namespace admin APIs
- Remember BanNamespace persists state — it can never be read-only
When it happens
Trigger: Calling db.BanNamespace(ns) on a DB opened with badger.Options.ReadOnly = true. Note it also panics-free returns ErrNamespaceMode if NamespaceOffset < 0 — the read-only panic is hit only when namespace mode is configured.
Common situations: Multi-tenant apps where a reader replica instance is asked to ban a tenant; a shared DB wrapper opened read-only for queries but also wired to admin handlers; forgetting that namespace bans are persisted writes, not in-memory-only flags.
Related errors
- Cannot use GetSequence in read-only mode.
- Cannot flatten in read-only mode.
- Attempting to drop data in read-only mode.
- ErrReadOnlyTxn
- ErrNamespaceMode
AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05).
Data as JSON: /api/errors/a625a8b9f43c731b.
Report an issue: GitHub.