# mco_statrt_update
RT

> Update the value of a registered runtime statistics counter using a specified operation.

### Prototype

        MCO_RET mco_statrt_update(
            /*IN*/ mco_stat_hdl h,
            /*IN*/ mco_statrt_op_t op,
            /*IN*/ mco_stat_counter_value arg
        );

### Arguments

`h`
Handle of the statistics counter to update (obtained from `mco_statrt_register_counter()` or a built-in constant like `MCO_STATRT_COMMIT_COUNT`).
`op`
Operation to perform on the counter. One of:

- `mco_statrt_op_set` — set counter to `arg`,
- `mco_statrt_op_inc` — increment by 1 (ignores `arg`),
- `mco_statrt_op_add` — add `arg`,
- `mco_statrt_op_sub` — subtract `arg`,
- `mco_statrt_op_min` — set to minimum of current value and `arg`,
- `mco_statrt_op_max` — set to maximum of current value and `arg`.

`arg`
Operand value used by the operation (ignored for `mco_statrt_op_inc`).

### Description

> This function atomically updates the value of a runtime statistics counter according to the specified operation. It is the primary mechanism for instrumenting application code or internal database components with custom metrics.

> If the counter was registered with histogram collection enabled (`collect_histogram = MCO_YES`), the new counter value is automatically appended to the associated histogram after the update.

> The counter must be valid and the statistics subsystem must be initialized (i.e., a database with statistics support must be open).

> Note: This function does not return the updated value. Use `mco_statrt_get()` or `mco_stat_counter_get()` to read the current value.

### Return Codes

`MCO_S_OK`
Counter successfully updated.
`MCO_E_INVALID_HANDLE`
The counter handle is invalid or the statistics subsystem is not initialized.
`MCO_E_ILLEGAL_PARAM`
The specified operation is not supported.

### Example

        // Increment a built-in counter
        mco_statrt_update(MCO_STATRT_COMMIT_COUNT, mco_statrt_op_inc, 0);

        // Update a custom counter with max operation
        mco_stat_hdl latency_counter = /* previously registered */;
        mco_statrt_update(latency_counter, mco_statrt_op_max, measured_latency);

        // Add to an error counter
        mco_statrt_update(my_error_counter, mco_statrt_op_add, 1);

### Files

Header file:
mco.h
Source file:
mcostat.c
Library:
libmcolib.a