New in v21.1:
The ALTER DATABASE .. DROP REGION statement drops a region from a multi-region database. While CockroachDB processes an index modification or changing a table to or from a REGIONAL BY ROW table, attempting to drop a region from the database containing that REGIONAL BY ROW table will produce an error. Similarly, while this statement is running, all index modifications and locality changes on REGIONAL BY ROW tables will be blocked.
This is an enterprise-only feature. You can use free trial credits to try it out.
DROP REGION is a subcommand of ALTER DATABASE.
Synopsis
Parameters
| Parameter | Description |
|---|---|
database_name |
The database from which you are dropping a region. |
region_name |
The region being dropped from this database. Allowed values include any region present in SHOW REGIONS FROM DATABASE database_name.You can only drop the primary region from a multi-region database if it's the last remaining region. |
Required privileges
To drop a region from a database, the user must have one of the following:
- Membership to the
adminrole for the cluster. - Membership to the owner role, or the
CREATEprivilege, for the database and allREGIONAL BY ROWtables in the database.
Examples
Setup
Only cluster regions specified at node startup can be used as database regions.
To follow along with the examples below, start a demo cluster with the --global flag to simulate a multi-region cluster:
$ cockroach demo --global --nodes 9 --no-example-database
To see the regions available to the databases in the cluster, use a SHOW REGIONS FROM CLUSTER statement:
SHOW REGIONS FROM CLUSTER;
region | zones
---------------+----------
europe-west1 | {b,c,d}
us-east1 | {b,c,d}
us-west1 | {a,b,c}
(3 rows)
Set the primary region
Suppose you have a database foo in your cluster, and you want to make it a multi-region database.
To add the first region to the database, or to set an already-added region as the primary region, use a SET PRIMARY REGION statement:
ALTER DATABASE foo SET PRIMARY REGION "us-east1";
ALTER DATABASE PRIMARY REGION
Add regions to a database
To add more regions to a database that already has at least one region, use an ADD REGION statement:
ALTER database foo ADD region "us-west1";
ALTER DATABASE ADD REGION
ALTER database foo ADD region "europe-west1";
ALTER DATABASE ADD REGION
View a database's regions
To view the regions associated with a multi-region database, use a SHOW REGIONS FROM DATABASE statement:
SHOW REGIONS FROM DATABASE foo;
database | region | primary | zones
-----------+--------------+---------+----------
foo | us-east1 | true | {b,c,d}
foo | europe-west1 | false | {b,c,d}
foo | us-west1 | false | {a,b,c}
(3 rows)
Drop regions from a database
To drop a region from a multi-region database, use a DROP REGION statement:
ALTER DATABASE foo DROP REGION "us-west1";
ALTER DATABASE DROP REGION
SHOW REGIONS FROM DATABASE foo;
database | region | primary | zones
-----------+--------------+---------+----------
foo | us-east1 | true | {b,c,d}
foo | europe-west1 | false | {b,c,d}
(2 rows)
You can only drop the primary region from a multi-region database if it's the last remaining region.
If you try to drop the primary region when there is more than one region, CockroachDB will return an error:
ALTER DATABASE foo DROP REGION "us-east1";
ERROR: cannot drop region "us-east1"
SQLSTATE: 42P12
HINT: You must designate another region as the primary region using ALTER DATABASE foo PRIMARY REGION <region name> or remove all other regions before attempting to drop region "us-east1"
ALTER DATABASE foo DROP REGION "europe-west1";
ALTER DATABASE DROP REGION
SHOW REGIONS FROM DATABASE foo;
database | region | primary | zones
-----------+----------+---------+----------
foo | us-east1 | true | {b,c,d}
(1 row)
ALTER DATABASE foo DROP REGION "us-east1";
ALTER DATABASE DROP REGION
SHOW REGIONS FROM DATABASE foo;
database | region | primary | zones
-----------+--------+---------+--------
(0 rows)
You cannot drop a region from a database if the databases uses REGION survival goal and there are only three regions configured on the database:
ALTER DATABASE foo SET PRIMARY REGION "us-east1";
ALTER DATABASE PRIMARY REGION
ALTER DATABASE foo ADD REGION "us-west1";
ALTER DATABASE ADD REGION
ALTER DATABASE foo ADD REGION "europe-west1";
ALTER DATABASE ADD REGION
ALTER DATABASE foo DROP REGION "us-west1";
ERROR: at least 3 regions are required for surviving a region failure
SQLSTATE: 22023
HINT: you must add additional regions to the database or change the survivability goal