def update_execution(liveaction_db, publish=True, set_result_size=False):
    """
    :param set_result_size: True to calculate size of the serialized result field value and set it
                            on the "result_size" database field.
    """
    execution = ActionExecution.get(liveaction__id=str(liveaction_db.id))

    with coordination.get_coordinator().get_lock(str(liveaction_db.id).encode()):
        # Skip execution object update when action is already in completed state.
        if execution.status in action_constants.LIVEACTION_COMPLETED_STATES:
            LOG.debug(
                "[%s] Action is already in completed state: %s. Skipping execution update to state: %s."
                % (execution.id, execution.status, liveaction_db.status)
            )
            return execution

        decomposed = _decompose_liveaction(liveaction_db)

        kw = {}
        for k, v in six.iteritems(decomposed):
            kw["set__" + k] = v

        if liveaction_db.status != execution.status:
            # Note: If the status changes we store this transition in the "log" attribute of action
            # execution
            kw["push__log"] = _create_execution_log_entry(liveaction_db.status)

        if set_result_size:
            # Sadly with the current ORM abstraction there is no better way to achieve updating
            # result_size and we need to serialize the value again - luckily that operation is fast.
            # To put things into perspective - on 4 MB result dictionary it only takes 7 ms which is
            # negligible compared to other DB operations duration (and for smaller results it takes
            # in sub ms range).
            with Timer(key="action.executions.calculate_result_size"):
                result_size = len(
                    ActionExecutionDB.result._serialize_field_value(
                        liveaction_db.result
                    )
                )
                kw["set__result_size"] = result_size

        execution = ActionExecution.update(execution, publish=publish, **kw)
        return execution

    :param set_result_size: True to calculate size of the serialized result field value and set it
                            on the "result_size" database field.
    
st2common/st2common/services/executions.py