Skip to content

Writes

databaas_utils.writes

Iceberg writes that survive a concurrent commit.

pyiceberg asserts that the branch still points at the snapshot it saw when the table was loaded, so any commit that lands in between, such as the platform's table maintenance rewriting files, fails the write with CommitFailedException. The write itself is still valid; the fix is to load the table again and redo the whole operation, which is what these helpers do.

Only CommitFailedException is retried. CommitStateUnknownException means the commit may have landed, so repeating it could write twice.

commit_with_retry

commit_with_retry(
    catalog: Catalog,
    identifier: str,
    operation: Callable[[Table], T],
    *,
    attempts: int = COMMIT_ATTEMPTS,
    base_delay: float = BASE_DELAY,
    max_delay: float = MAX_DELAY,
) -> T

Run a write against a freshly loaded table, repeating it on a commit conflict.

operation receives the table and is called again, with a newly loaded table, each time the commit conflicts, so it must do all of its reading inside the call and be safe to run again from scratch.

Parameters:

Name Type Description Default
catalog Catalog

The catalog to load the table from.

required
identifier str

Fully qualified namespace.table.

required
operation Callable[[Table], T]

The write, e.g. lambda t: t.append(batch).

required
attempts int

Total tries, including the first.

COMMIT_ATTEMPTS
base_delay float

Seconds before the second try; doubles per retry, with jitter.

BASE_DELAY
max_delay float

Upper bound for one wait, in seconds.

MAX_DELAY

Returns:

Type Description
T

Whatever operation returns.

Raises:

Type Description
CommitFailedException

After attempts conflicts.

Example

commit_with_retry(catalog, "sales.orders", lambda t: t.append(batch))

append

append(
    catalog: Catalog,
    identifier: str,
    data: Table,
    **retry: Any,
) -> None

table.append(data), retried on a commit conflict. See commit_with_retry.

overwrite

overwrite(
    catalog: Catalog,
    identifier: str,
    data: Table,
    overwrite_filter: BooleanExpression | str | None = None,
    **retry: Any,
) -> None

table.overwrite(data, overwrite_filter), retried on a commit conflict.

upsert

upsert(
    catalog: Catalog,
    identifier: str,
    data: Table,
    join_cols: list[str] | None = None,
    **retry: Any,
) -> Any

table.upsert(data, join_cols), retried on a commit conflict.

Returns:

Type Description
Any

pyiceberg's UpsertResult from the attempt that succeeded.

delete

delete(
    catalog: Catalog,
    identifier: str,
    delete_filter: BooleanExpression | str,
    **retry: Any,
) -> None

table.delete(delete_filter), retried on a commit conflict.