All Products
Search
Document Center

Tablestore:Use conditional updates

Last Updated:Aug 12, 2026

Use the Tablestore SDK for Go to specify conditions for a row write, update, or deletion so that the operation is performed only when the target row meets the conditions.

Prerequisites

Install the Tablestore SDK for Go and initialize the client.

Description

A conditional update can contain a row existence condition, a column condition, or both. Call SetCondition and SetColumnCondition on PutRowChange, UpdateRowChange, or DeleteRowChange. You can also add these row change objects to a BatchWriteRow request. If the conditions are not met, the server returns an error and does not modify data.

  • A row existence condition checks whether the target row exists. You can ignore row existence, require the row to exist, or require the row not to exist.

  • A column condition compares one column value or combines multiple column conditions by using logical operators. A single-column condition can also reference a primary key column. Column-to-column and constant-to-constant comparisons are not supported. An update can contain up to 10 column conditions.

The following sample updates the status column only if the target row exists and the value of the score column is 30.

primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("id", "row1")

change := &tablestore.UpdateRowChange{
    TableName:  "example_table",
    PrimaryKey: primaryKey,
}
change.PutColumn("status", "processed")
change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
change.SetColumnCondition(
    tablestore.NewSingleColumnCondition("score", tablestore.CT_EQUAL, int64(30)),
)

_, err := client.UpdateRow(&tablestore.UpdateRowRequest{UpdateRowChange: change})
if err != nil {
    log.Fatal(err)
}

Parameters

Row existence condition

Call SetCondition to specify a RowExistenceExpectation value.

Name

Type

Description

expectation (required)

RowExistenceExpectation

The row existence condition. Valid values: RowExistenceExpectation_IGNORE, which does not check whether the row exists; RowExistenceExpectation_EXPECT_EXIST, which requires the row to exist; and RowExistenceExpectation_EXPECT_NOT_EXIST, which requires the row not to exist.

Single-column condition

Call NewSingleColumnCondition(columnName, comparator, value) to create a single-column condition. SingleColumnCondition contains the following parameters.

Name

Type

Description

ColumnName (required)

*string

The column to evaluate. This can be an attribute column or primary key column.

Comparator (required)

*ComparatorType

The comparison operator. Valid values: CT_EQUAL, CT_NOT_EQUAL, CT_GREATER_THAN, CT_GREATER_EQUAL, CT_LESS_THAN, and CT_LESS_EQUAL.

ColumnValue (required)

interface{}

The constant value to compare against.

FilterIfMissing (optional)

bool

Specifies whether the condition is not met if the row does not contain the column. Default value: false, which means that the condition is met.

LatestVersionOnly (optional)

bool

Specifies whether to evaluate only the latest version. Default value: false, which means that the condition is met if any version meets the condition.

Composite condition

Call NewCompositeColumnCondition(lo) to create a composite condition, and call AddFilter to add subconditions. A subcondition can be a single-column condition or a nested composite condition. CompositeColumnValueFilter contains the following parameters.

Name

Type

Description

Operator (required)

LogicalOperator

The logical operator. LO_AND and LO_OR require at least two subconditions. LO_NOT accepts only one subcondition.

Filters (required)

[]ColumnFilter

The subconditions to combine.

Examples

Update data by using a composite condition

The following sample performs an update only if score >= 60 and status is pending.

condition := tablestore.NewCompositeColumnCondition(tablestore.LO_AND)
condition.AddFilter(
    tablestore.NewSingleColumnCondition("score", tablestore.CT_GREATER_EQUAL, int64(60)),
)
condition.AddFilter(
    tablestore.NewSingleColumnCondition("status", tablestore.CT_EQUAL, "pending"),
)

change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
change.SetColumnCondition(condition)