Skip to main content
The ALTER INDEX applies a to an index.
This schema change statement is registered as a job. You can view long-running jobs with .

Required privileges

Refer to the respective subcommands.

Synopsis

alter_index syntax diagram

Parameters

ParameterDescription
index_nameThe name of the you want to change.
IF EXISTSAlter the index only if an index table_index_name exists; if one does not exist, do not return an error.
Additional parameters are documented for the respective subcommands.

Subcommands

SubcommandDescription
CONFIGURE ZONE for an index.
PARTITION BYPartition, re-partition, or un-partition an index.
RENAME TOChange the name of an index.
SCATTERMake a best-effort attempt to redistribute replicas and leaseholders for the ranges of a table or index. Note that this statement does not return an error even if replicas are not moved.
SPLIT ATForce a at the specified row in the index.
UNSPLIT ATRemove a range split enforcement in the index.
VISIBILITYSet the visibility of an index between a range of 0.0 and 1.0.
[NOT] VISIBLEMake an index visible or not visible to the .

CONFIGURE ZONE

ALTER INDEX ... CONFIGURE ZONE is used to add, modify, reset, or remove replication zones for an index. To view details about existing replication zones, use . For more information about replication zones, see . You can use replication zones to control the number and location of replicas for specific sets of data, both when replicas are first added and when they are rebalanced to maintain cluster equilibrium. For examples, see Replication Controls. For instructions showing how to troubleshoot replication zones that may be misconfigured, see .

Required privileges

The user must be a member of the or have been granted or privileges. To configure , the user must be a member of the admin role.

Parameters

ParameterDescription
variableThe name of the to change.
valueThe value of the to change.
DISCARDRemove a replication zone.
For usage, see Synopsis.

PARTITION BY

ALTER INDEX ... PARTITION BY is used to partition, re-partition, or un-partition a secondary index. After defining partitions, is used to control the replication and placement of partitions. Similar to , partitions can improve query performance by limiting the numbers of rows that a query must scan. In the case of , partitioning can limit a query scan to data in a specific region. For examples, see .
Most users should not need to use partitioning directly. Instead, they should use CockroachDB’s built-in , which automatically handle geo-partitioning and other low-level details.
The is different from the conventional primary key: The unique identifier in the primary key must be prefixed with all columns you want to partition and subpartition the table on, in the order in which you want to nest your subpartitions. If the primary key in your existing table does not meet the requirements, you can change the primary key with . For examples, see Define partitions.

Required privileges

The user must have the CREATE on the table.

Parameters

ParameterDescription
name_listList of columns you want to define partitions on (in the order they are defined in the primary key).
list_partitionsName of list partition followed by the list of values to be included in the partition.
range_partitionsName of range partition followed by the range of values to be included in the partition.
For usage, see Synopsis.

RENAME TO

ALTER INDEX ... RENAME TO changes the name of an index.
It is not possible to rename an index referenced by a view. For more details, see .
For examples, see Rename indexes.

Required privileges

The user must have the CREATE on the table.

Parameters

ParameterDescription
index_new_nameThe you want to use for the index, which must be unique to its table and follow these .
For usage, see Synopsis.

SCATTER

ALTER INDEX ... SCATTER runs a specified set of ranges for a table or index through the queue. If many ranges have been created recently, the replication queue may transfer some leases to other replicas to balance load across the cluster. Note that this statement makes a best-effort attempt to redistribute replicas and leaseholders for the ranges of an index. It does not return an error even if replicas are not moved.
SCATTER has the potential to result in data movement proportional to the size of the table or index being scattered, thus taking additional time and resources to complete.
For examples, refer to Scatter indexes.

Required privileges

The user must have the INSERT on the table or index.

Parameters

ParameterDescription
table_nameThe name of the table that you want to scatter.
table_index_nameThe name of the index that you want to scatter.
expr_listA list of in the form of the primary key of the table or the specified index.
For usage, see Synopsis.

SPLIT AT

ALTER INDEX ... SPLIT AT forces a at a specified row in the index. CockroachDB breaks data into ranges. By default, CockroachDB attempts to keep ranges below . To do this, the system will automatically if it grows larger than this limit. For most use cases, this automatic range splitting is sufficient, and you should never need to worry about when or where the system decides to split ranges. However, there are reasons why you may want to perform manual splits on the ranges that store tables or indexes:
  • When a table only consists of a single range, all writes and reads to the table will be served by that range’s . If a table only holds a small amount of data but is serving a large amount of traffic, load distribution can become unbalanced and a can occur. Splitting the table’s ranges manually can allow the load on the table to be more evenly distributed across multiple nodes. For tables consisting of more than a few ranges, load will naturally be distributed across multiple nodes and this will not be a concern.
  • When a table is created, it will only consist of a single range. If you know that a new table will immediately receive significant write traffic, you may want to preemptively split the table based on the expected distribution of writes before applying the load. This can help avoid reduced workload performance that results when automatic splits are unable to keep up with write traffic and a occurs.
For examples, see Split and unsplit indexes.

Required privileges

The user must have the INSERT on the table or index.

Parameters

ParameterDescription
select_stmtA that produces one or more rows at which to split the index.
a_exprThe expiration of the split enforcement on the index. This can be a , , , or .
For usage, see Synopsis.

UNSPLIT AT

ALTER INDEX ... UNSPLIT AT removes a split enforcement on a , at a specified row in the index. Removing a split enforcement from a table or index (“unsplitting”) allows CockroachDB to merge ranges as needed, to help improve your cluster’s performance. For more information, see . For examples, see Split and unsplit indexes.

Required privileges

The user must have the INSERT on the table or index.

Parameters

ParameterDescription
select_stmtA that produces one or more rows at which to unsplit an index.
ALLRemove all split enforcements for an index.
For usage, see Synopsis.

VISIBILITY

ALTER INDEX ... VISIBILITY specifies the visibility of an index between a range of 0.0 and 1.0.
  • VISIBILITY 0.0 means that an index is not visible to the . This is equivalent to NOT VISIBLE.
  • VISIBILITY 1.0 means that an index is visible to the optimizer. This is equivalent to VISIBLE.
  • Any value between 0.0 and 1.0 means that an index is visible to the specified fraction of queries. This is known as a partially visible index. For the purposes of , partially visible indexes are treated as . If a partially visible index can be used to improve a query plan, the optimizer will recommend making it fully visible. For an example, refer to .

[NOT] VISIBLE

ALTER INDEX ... VISIBLE and ALTER INDEX ... NOT VISIBLE determines whether the index is visible to the . By default, indexes are visible. If an index is NOT VISIBLE, queries will not read from the index unless it is specifically selected with an or the property is overridden with the . In order to keep NOT VISIBLE indexes up to date, queries will still write to the index as they insert and update data in the table. This allows you to create an index and check for query plan changes without affecting production queries. For an example, refer to Set an index to be not visible. Note the following considerations for index visibility:
  • Primary indexes must be visible.
  • Queries may still read from NOT VISIBLE, indexes to enforce .
  • Queries may still read from NOT VISIBLE indexes to perform foreign key cascades and enforce .
  • When defining a , you cannot use the NOT VISIBLE syntax to make the corresponding index not visible. Instead, use ALTER INDEX ... NOT VISIBLE after creating the UNIQUE constraint.
For examples, refer to Set index visibility.

Aliases

In CockroachDB, the following are aliases for NOT VISIBLE:
  • INVISIBLE

Examples

Configure replication zones

Setup

The following examples use MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. For more information about the MovR example application and dataset, see . To follow along, run with the --geo-partitioned-replicas flag. This command opens an interactive SQL shell to a temporary, 9-node in-memory cluster with the movr database.

Create a replication zone for an index

The can take advantage of replication zones for secondary indexes when optimizing queries.
The on a table will automatically use the replication zone for the table. You can also add distinct replication zones for secondary indexes. To control replication for a specific secondary index, use the ALTER INDEX ... CONFIGURE ZONE statement to define the relevant values (other values will be inherited from the parent zone).
To get the name of a secondary index, which you need for the CONFIGURE ZONE statement, use the or statements.

Edit a replication zone

Remove a replication zone

When you discard a zone configuration, the objects it was applied to will then inherit a configuration from an object “the next level up”; e.g., if the object whose configuration is being discarded is a table, it will use its parent database’s configuration.You cannot DISCARD any zone configurations on multi-region tables, indexes, or partitions if the created the zone configuration.

Troubleshoot replication zones

For instructions showing how to troubleshoot replication zones that may be misconfigured, see .

Define partitions

Define a list partition on an index

Suppose we have a table called students_by_list, a secondary index on the table called name_idx, and the primary key of the table is defined as (country, id). We can define partitions on the index by list:

Define a range partition on an index

Suppose we have a table called students_by_range, with a secondary index called name_idx, and the primary key of the table is defined as (expected_graduation_date, id). We can define partitions on the index by range:

Define subpartitions on an index

Suppose we have a table named students, with a secondary index called name_idx, and the primary key is defined as (country, expected_graduation_date, id). We can define partitions and subpartitions on the index:

Repartition an index

Unpartition an index

Rename indexes

Rename an index

Scatter indexes

Before scattering, you can view the current replica and leaseholder distribution for an index:
After scattering, recheck the leaseholder distribution:

Split and unsplit indexes

Setup

The following examples use MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. For more information about the MovR example application and dataset, see . To follow along, run with the --geo-partitioned-replicas flag. This command opens an interactive SQL shell to a temporary, 9-node in-memory cluster with the movr database.

Split an index

Add a new secondary to the rides table, on the revenue column:
Then split the table ranges by secondary index values:

Set the expiration on a split enforcement

For an example, see .

Unsplit an index

Add a new secondary to the rides table, on the revenue column, and then split the table ranges by secondary index values as described in Split an index. To remove the split enforcements, run the following:
You can see the split’s expiration date in the split_enforced_until column. The table also contains information about ranges in your CockroachDB cluster, including the split_enforced_until column.
The table is still split into ranges at 25.00, 50.00, and 75.00, but the split_enforced_until column is now NULL for all ranges in the table. The split is no longer enforced, and CockroachDB can in the table as needed.

Set index visibility

Set an index to be not visible

Start the on a 3-node CockroachDB demo cluster with a larger data set.
  1. Show the indexes on the rides table. In the second-to-last column, visible, you can see that all indexes have the value t (true).
  2. Explain a query that filters on revenue. Since there is no index on the revenue column, the query performs a full scan.
  3. Create the recommended index.
  4. Display the indexes on the rides table to verify the newly created index rides_revenue_idx.
  5. Explain the query behavior after creating the index. The query now uses the rides_revenue_idx index and scans many fewer rows.
  6. Alter the index to be not visible to the optimizer, specifying the NOT VISIBLE clause.
  7. Display the table indexes and verify that the index visibility for rides_revenue_idx is f (false).
  8. Explain the query behavior after making the index not visible to the optimizer. With the index not visible, the optimizer reverts to full scan and recommends that you make the index visible.

Set an index as partially visible

Using the rides_revenue_idx created in the preceding example:
  1. Set the visibility of the index to 0.5.
  2. Display the table indexes and verify that the index visibility for rides_revenue_idx is 0.5.
  3. Explain the query behavior after making the index partially visible to the optimizer. For the purposes of index recommendations, a partially visible index is treated as not visible. The optimizer recommends that you make this index fully visible.

See also