Committing to DB at the end of an activity and Activity Idempotency

In Activity Definition | Temporal Platform Documentation , it is said:

Idempotence for Activities is also important due to a particular edge case inherent in distributed computing. Consider a scenario in which a Worker polls the Temporal Service, accepts the Activity Task, and begins executing the Activity. The Activity function completes successfully, but the Worker crashes just before it notifies the Temporal Service. In this case, the Event History won’t reflect the successful completion of the Task, so the Activity will be retried. If the Activity is not idempotent, this could have negative consequences, such as duplicate charges in a payment processing scenario.

So then, let’s say I have the following activity with a DB operation using SQLAlchemy :

from sqlalchemy.ext.asyncio import async_sessionmaker

@activity.defn
async def db_operation() -> UUID:
    async with async_sessionmaker(...) as session:
        model = SomeModel(id=42) # SQLAlchemy ORM model
        session.add(model)
        await session.commit()
        return model.id

Committing the changes to DB at the end of the activity seems like the ~most one can do to make this function as granular as possible, but there’s still the time between the commit and the notification to the Temporal Service (the return statement and completion processing).

According to the paragraph from the docs, this limitation is unavoidable and one really needs to make sure the DB operation (or whatever) is idempotent.

I just want to check my understanding: does this make sense? Is there a better way to do DB operations as an activity in the Python SDK (using e.g. SQLAlchemy)? Thank you :folded_hands: