BadgerDB crashed when manual transaction commit

txn := c.db.NewTransaction(true)
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Seek(prefixBytes); it.ValidForPrefix(prefixBytes); it.Next() {
    k := it.Item().Key()
    // Prefix deleting
    err := txn.Delete(k)
    if err == badger.ErrTxnTooBig {
	  log.Warn("txn too big, deleting using new txn")
	  _ = txn.Commit()
	  txn = c.db.NewTransaction(true)
	  _ = txn.Delete(k)
    } else if err != nil {
	    log.Warn("Error when deleting table:", err)
err = txn.Commit()

Program crashed when executing the last txn.Commit()
Badger version: v3.2103.2
Go version: 1.19
Hardware: MacOS M1 Pro

Hi @darks,

The program crashes because when you are calling txn.Commit(), you have an iterator on the same transaction that you are using for the for loop. You cannot commit a transaction while an iterator is not yet closed.

What you probably want to do instead is create a separate txn for iterator over the data in badger and another (and more) transactions for deleting data into badger. We also have a WriteBatch API that you could use instead to achieve the same result. badger package - - Go Packages

Hope that helps.

Problem solved, thank you

1 Like