Skip to content

Writing tables safely

The platform maintains Iceberg tables for you: it compacts small files, rewrites manifests and expires old snapshots. Each of those is a commit. pyiceberg writes with the assumption that nobody committed since it loaded the table, so a write that overlaps one fails:

CommitFailedException: Requirement failed: branch main has changed

Nothing is wrong with your data. Load the table again and redo the write, which is what these helpers do:

from databaas_utils import commit_with_retry
from databaas_utils import writes

writes.append(catalog, "sales.orders", batch)
writes.upsert(catalog, "sales.orders", batch, join_cols=["order_id"])
writes.overwrite(catalog, "sales.orders", batch, overwrite_filter="day = '2026-11-26'")
writes.delete(catalog, "sales.orders", "status = 'void'")

# anything else
commit_with_retry(catalog, "sales.orders", lambda table: table.append(batch))

Up to five tries, waiting 0.5 s, 1 s, 2 s, 4 s (with jitter) between them. Pass attempts=, base_delay= or max_delay= to change that. After the last try the CommitFailedException is raised.

Rules for the function you pass to commit_with_retry:

  • It gets a freshly loaded table on every try and runs again from scratch, so do all your reading inside it and keep it free of side effects other than the write.
  • CommitStateUnknownException is never retried: the commit may have landed, and repeating it could write the rows twice.
  • Other errors are raised straight away.

FileIndex.record already works this way.