CONFIGURE ZONE

Use CONFIGURE ZONE to add, modify, reset, and remove replication zones. To view details about existing replication zones, see SHOW ZONE CONFIGURATIONS.

In CockroachDB, 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.

Note:

Adding replication zones for rows and secondary indexes is an enterprise-only feature.

Synopsis

alter_zone_range_stmt ::=

ALTERRANGErange_nameCONFIGUREZONEUSINGvariable=COPYFROMPARENTvalue,variable=valueCOPYFROMPARENTDISCARD

alter_zone_database_stmt ::=

ALTERDATABASEdatabase_nameCONFIGUREZONEUSINGvariable=COPYFROMPARENTvalue,variable=valueCOPYFROMPARENTDISCARD

alter_zone_table_stmt ::=

ALTERPARTITIONpartition_nameOFTABLEtable_nameCONFIGUREZONEUSINGvariable=COPYFROMPARENTvalue,variable=valueCOPYFROMPARENTDISCARD

alter_zone_index_stmt ::=

ALTERINDEXtable_name@index_nameCONFIGUREZONEUSINGvariable=COPYFROMPARENTvalue,variable=valueCOPYFROMPARENTDISCARD

Required privileges

Currently, only the root user can configure replication zones.

Parameters

ParameterDescription
range_nameThe name of the system range for which to show replication zone configurations.
database_nameThe name of the database for which to show replication zone configurations.
table_nameThe name of the table for which to show replication zone configurations.
partition_nameThe name of the partition for which to show replication zone configurations.
index_nameThe name of the index for which to show replication zone configurations.
variableThe name of the variable to change.
valueThe value of the variable to change.
DISCARDRemove a replication zone.

Variables

VariableDescription
range_min_bytesThe minimum size, in bytes, for a range of data in the zone. When a range is less than this size, CockroachDB will merge it with an adjacent range.Default: 1048576 (1MiB)
range_max_bytesThe maximum size, in bytes, for a range of data in the zone. When a range reaches this size, CockroachDB will spit it into two ranges.Default: 67108864 (64MiB)
gc.ttlsecondsThe number of seconds overwritten values will be retained before garbage collection. Smaller values can save disk space if values are frequently overwritten; larger values increase the range allowed for AS OF SYSTEM TIME queries, also know as Time Travel Queries.It is not recommended to set this below 600 (10 minutes); doing so will cause problems for long-running queries. Also, since all versions of a row are stored in a single range that never splits, it is not recommended to set this so high that all the changes to a row in that time period could add up to more than 64MiB; such oversized ranges could contribute to the server running out of memory or other problems.Default: 90000 (25 hours)
num_replicasThe number of replicas in the zone.Default: 3For the system database and .meta, .liveness, and .system ranges, the default value is 5.
constraintsAn array of required (+) and/or prohibited (-) constraints influencing the location of replicas. See Types of Constraints and Scope of Constraints for more details.To prevent hard-to-detect typos, constraints placed on store attributes and node localities must match the values passed to at least one node in the cluster. If not, an error is signalled.Default: No constraints, with CockroachDB locating each replica on a unique node and attempting to spread replicas evenly across localities.
lease_preferences An ordered list of required and/or prohibited constraints influencing the location of leaseholders. Whether each constraint is required or prohibited is expressed with a leading + or -, respectively. Note that lease preference constraints do not have to be shared with the constraints field. For example, it's valid for your configuration to define a lease_preferences field that does not reference any values from the constraints field. It's also valid to define a lease_preferences field with no constraints field at all. If the first preference cannot be satisfied, CockroachDB will attempt to satisfy the second preference, and so on. If none of the preferences can be met, the lease will be placed using the default lease placement algorithm, which is to base lease placement decisions on how many leases each node already has, trying to make all the nodes have around the same amount.Each value in the list can include multiple constraints. For example, the list [[+zone=us-east-1b, +ssd], [+zone=us-east-1a], [+zone=us-east-1c, +ssd]] means "prefer nodes with an SSD in us-east-1b, then any nodes in us-east-1a, then nodes in us-east-1c with an SSD." For a usage example, see Constrain leaseholders to specific datacenters.Default: No lease location preferences are applied if this field is not specified.

Note:

If a value is not set, new zone configurations will inherit their values from their parent zone (e.g., a partition zone inherits from the table zone), which is not necessarily .default.

If a variable is set to COPY FROM PARENT (e.g., range_max_bytes = COPY FROM PARENT), the variable will copy its value from its parent replication zone. The COPY FROM PARENT value is a convenient shortcut to use so you do not have to look up the parent's current value. For example, the range_max_bytes and range_min_bytes variables must be set together, so when editing one value, you can use COPY FROM PARENT for the other. Note that if the variable in the parent replication zone is changed after the child replication zone is copied, the change will not be reflected in the child zone.

Viewing schema changes

This schema change statement is registered as a job. You can view long-running jobs with SHOW JOBS.

Examples

Edit a replication zone

  1. > ALTER TABLE t CONFIGURE ZONE USING range_min_bytes = 0, range_max_bytes = 90000, gc.ttlseconds = 89999, num_replicas = 4, constraints = '[-region=west]';
  1. CONFIGURE ZONE 1

Edit the default replication zone

To edit the default replication zone, use the ALTER RANGE … CONFIGURE ZONE statement to define the values you want to change (other values will remain the same):

  1. > ALTER RANGE default CONFIGURE ZONE USING num_replicas = 5, gc.ttlseconds = 100000;
  1. CONFIGURE ZONE 1
  1. > SHOW ZONE CONFIGURATION FOR RANGE default;
  1. zone_name | config_sql
  2. +-----------+------------------------------------------+
  3. .default | ALTER RANGE default CONFIGURE ZONE USING
  4. | range_min_bytes = 1048576,
  5. | range_max_bytes = 67108864,
  6. | gc.ttlseconds = 100000,
  7. | num_replicas = 5,
  8. | constraints = '[]',
  9. | lease_preferences = '[]'
  10. (1 row)

Create a replication zone for a database

To control replication for a specific database, use the ALTER DATABASE … CONFIGURE ZONE statement to define the values you want to change (other values will not be affected):

  1. > ALTER DATABASE test CONFIGURE ZONE USING num_replicas = 5, gc.ttlseconds = 100000;
  1. CONFIGURE ZONE 1
  1. > SHOW ZONE CONFIGURATION FOR DATABASE test;
  1. zone_name | config_sql
  2. +-----------+------------------------------------------+
  3. test | ALTER DATABASE test CONFIGURE ZONE USING
  4. | range_min_bytes = 1048576,
  5. | range_max_bytes = 67108864,
  6. | gc.ttlseconds = 100000,
  7. | num_replicas = 5,
  8. | constraints = '[]',
  9. | lease_preferences = '[]'
  10. (1 row)

Create a replication zone for a table

To control replication for a specific table, use the ALTER TABLE … CONFIGURE ZONE statement to define the values you want to change (other values will not be affected):

  1. > ALTER TABLE customers CONFIGURE ZONE USING num_replicas = 5, gc.ttlseconds = 100000;
  1. CONFIGURE ZONE 1
  1. > SHOW ZONE CONFIGURATION FOR TABLE customers;
  1. zone_name | config_sql
  2. +----------------+--------------------------------------------+
  3. test.customers | ALTER TABLE customers CONFIGURE ZONE USING
  4. | range_min_bytes = 1048576,
  5. | range_max_bytes = 67108864,
  6. | gc.ttlseconds = 100000,
  7. | num_replicas = 5,
  8. | constraints = '[]',
  9. | lease_preferences = '[]'
  10. (1 row)

Create a replication zone for a secondary index

Tip:

New in v19.1: The Cost-based Optimizer can take advantage of replication zones for secondary indexes when optimizing queries. For more information, see Cost-based optimizer - preferring the nearest index.

Note:

This is an enterprise-only feature.

The secondary indexes on a table will automatically use the replication zone for the table. However, with an enterprise license, you can 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 values you want to change (other values will not be affected).

Tip:

To get the name of a secondary index, which you need for the CONFIGURE ZONE statement, use the SHOW INDEX or SHOW CREATE TABLE statements.

  1. > ALTER INDEX tpch.frequent_customers CONFIGURE ZONE USING num_replicas = 5, gc.ttlseconds = 100000;
  1. CONFIGURE ZONE 1
  1. > SHOW ZONE CONFIGURATION FOR INDEX tpch.customer@frequent_customers;
  1. zone_name | config_sql
  2. +----------------------------------+--------------------------------------------------------------------------+
  3. tpch.customer@frequent_customers | ALTER INDEX tpch.public.customer@frequent_customers CONFIGURE ZONE USING
  4. | range_min_bytes = 1048576,
  5. | range_max_bytes = 67108864,
  6. | gc.ttlseconds = 100000,
  7. | num_replicas = 5,
  8. | constraints = '[]',
  9. | lease_preferences = '[]'
  10. (1 row)

Create a replication zone for a table or secondary index partition

Note:

This is an enterprise-only feature.

To control replication for table partitions, use the ALTER PARTITION … CONFIGURE ZONE statement to define the values you want to change (other values will not be affected):

  1. > ALTER PARTITION north_america OF TABLE customers CONFIGURE ZONE USING num_replicas = 5, constraints = '[-region=EU]';
  1. CONFIGURE ZONE 1
  1. > SHOW ZONE CONFIGURATION FOR PARTITION north_america OF TABLE customers;
  1. zone_name | config_sql
  2. +------------------------------+-------------------------------------------------------------------------------+
  3. test.customers.north_america | ALTER PARTITION north_america OF INDEX customers@primary CONFIGURE ZONE USING
  4. | range_min_bytes = 1048576,
  5. | range_max_bytes = 67108864,
  6. | gc.ttlseconds = 100000,
  7. | num_replicas = 5,
  8. | constraints = '[-region=EU]',
  9. | lease_preferences = '[]'
  10. (1 row)

Tip:

Since the syntax is the same for defining a replication zone for a table or index partition (e.g., database.table.partition), give partitions names that communicate what they are partitioning, e.g., north_america_table vs north_america_idx1.

Create a replication zone for a system range

In addition to the databases and tables that are visible via the SQL interface, CockroachDB stores internal data in what are called system ranges. CockroachDB comes with pre-configured replication zones for some of these ranges:

Zone NameDescription
.metaThe "meta" ranges contain the authoritative information about the location of all data in the cluster.These ranges must retain a majority of replicas for the cluster as a whole to remain available and historical queries are never run on them, so CockroachDB comes with a pre-configured .meta replication zone with num_replicas set to 5 to make these ranges more resilient to node failure and a lower-than-default gc.ttlseconds to keep these ranges smaller for reliable performance.If your cluster is running in multiple datacenters, it's a best practice to configure the meta ranges to have a copy in each datacenter.
.livenessThe "liveness" range contains the authoritative information about which nodes are live at any given time.These ranges must retain a majority of replicas for the cluster as a whole to remain available and historical queries are never run on them, so CockroachDB comes with a pre-configured .liveness replication zone with num_replicas set to 5 to make these ranges more resilient to node failure and a lower-than-default gc.ttlseconds to keep these ranges smaller for reliable performance.
.systemThere are system ranges for a variety of other important internal data, including information needed to allocate new table IDs and track the status of a cluster's nodes.These ranges must retain a majority of replicas for the cluster as a whole to remain available, so CockroachDB comes with a pre-configured .system replication zone with num_replicas set to 5 to make these ranges more resilient to node failure.
.timeseriesThe "timeseries" ranges contain monitoring data about the cluster that powers the graphs in CockroachDB's Admin UI. If necessary, you can add a .timeseries replication zone to control the replication of this data.

Warning:

Use caution when editing replication zones for system ranges, as they could cause some (or all) parts of your cluster to stop working.

To control replication for one of the above sets of system ranges, use the ALTER RANGE … CONFIGURE ZONE statement to define the values you want to change (other values will not be affected):

  1. > ALTER RANGE meta CONFIGURE ZONE USING num_replicas = 7;
  1. CONFIGURE ZONE 1
  1. > SHOW ZONE CONFIGURATION FOR RANGE meta;
  1. zone_name | config_sql
  2. +-----------+---------------------------------------+
  3. .meta | ALTER RANGE meta CONFIGURE ZONE USING
  4. | range_min_bytes = 1048576,
  5. | range_max_bytes = 67108864,
  6. | gc.ttlseconds = 3600,
  7. | num_replicas = 7,
  8. | constraints = '[]',
  9. | lease_preferences = '[]'
  10. (1 row)

Reset a replication zone

  1. > ALTER TABLE t CONFIGURE ZONE USING DEFAULT;
  1. CONFIGURE ZONE 1

Remove a replication zone

  1. > ALTER TABLE t CONFIGURE ZONE DISCARD;
  1. CONFIGURE ZONE 1

See also

Was this page helpful?
YesNo