dgraph-io/dgraph · error
count length < 4 for key: %q, parsed key: %+v
Error message
count length < 4 for key: %q, parsed key: %+v
What it means
Error returned by x/keys.go while parsing a key when fewer than 4 bytes remain for the count field. The raw key and parsed structure are reported via %q and %+v. Indicates a truncated or malformed key; fix by correcting or regenerating the key data.
Source
Thrown at x/keys.go:601
k = k[8:]
p.StartUid = binary.BigEndian.Uint64(k)
case ByteIndex:
if !p.HasStartUid {
p.Term = string(k)
break
}
if len(k) < 8 {
return p, errors.Errorf("StartUid length < 8 for key: %q, parsed key: %+v", key, p)
}
term := k[:len(k)-8]
startUid := k[len(k)-8:]
p.Term = string(term)
p.StartUid = binary.BigEndian.Uint64(startUid)
case ByteCount, ByteCountRev:
if len(k) < 4 {
return p, errors.Errorf("count length < 4 for key: %q, parsed key: %+v", key, p)
}
p.Count = binary.BigEndian.Uint32(k)
if !p.HasStartUid {
break
}
if len(k) != 12 {
return p, errors.Errorf("StartUid length != 8 for key: %q, parsed key: %+v", key, p)
}
k = k[4:]
p.StartUid = binary.BigEndian.Uint64(k)
default:
// Some other data type.
return p, errors.Errorf("Invalid data type")
}
return p, nilView on GitHub (pinned to 759e242be6)
Solutions
- Verify the key payload is >= 4 bytes with hex.Dump
- Rebuild the key with the count-key construction helper
- Audit the code path that wrote the key for truncation
- Restore affected data from a backup if on-disk corruption
Example fix
// before: count key without 4-byte count
key := []byte{byte(x.ByteCount)}
p, err := x.Parse(key) // count length < 4
// after
key := x.CountKey(attr, term, startUid) // includes 4-byte count
p, err := x.Parse(key) Defensive patterns
Strategy: validation
Validate before calling
if len(key) < 5 { return fmt.Errorf("count key too short: %d", len(key)) }
ok, err := x.IsDropOpKey(key) Type guard
func isMinCountKey(key []byte) bool { return len(key) >= 5 } Prevention
- Build count keys with the library helper including the 4-byte count
- Checksum keys on write paths to catch truncation early
- Validate fixtures in tests for exact key lengths
When it happens
Trigger: Calling Parse/IsDropOpKey with a ByteCount or ByteCountRev key whose payload is shorter than 4 bytes.
Common situations: Corrupted count index entries in storage, hand-built keys missing the 4-byte count, or truncated data from a faulty migration/backup restore.
Related errors
- Invalid UID with value 0 for key: %v
- StartUid length != 8 for key: %q, parsed key: %+v
- StartUid length < 8 for key: %q, parsed key: %+v
- Invalid data type
- could not parse key %s
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/cafb748d37940a47.
Report an issue: GitHub.