> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Regional Tables

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

In a <InternalLink path="multiregion-overview">multi-region deployment</InternalLink>, <InternalLink path="multiregion-overview">regional table locality</InternalLink> is a good choice for tables with the following requirements:

* Read and write latency must be low.
* Rows in the table, and all latency-sensitive queries, can be tied to specific regions.

Tables with regional table locality can survive zone or region failures, depending on the database-level <InternalLink path="multiregion-overview#survival-goals">survival goal</InternalLink> setting.

For new clusters using the <InternalLink path="multiregion-overview">multi-region SQL abstractions</InternalLink>, Cockroach Labs recommends lowering the <InternalLink path="cockroach-start">`--max-offset`</InternalLink> setting to `250ms`. This setting is especially helpful for lowering the write latency of <InternalLink path="table-localities#global-tables">global tables</InternalLink>. Nodes can run with different values for `--max-offset`, but only for the purpose of updating the setting across the cluster using a rolling upgrade.

## Before you begin

### Fundamentals

Multi-region patterns require thinking about the following questions:

* What are your <InternalLink path="multiregion-overview#survival-goals">survival goals</InternalLink>?  Do you need to survive a <InternalLink path="multiregion-survival-goals#survive-zone-failures">zone failure</InternalLink>? Do you need to survive a <InternalLink path="multiregion-survival-goals#survive-region-failures">region failure</InternalLink>?
* What are the <InternalLink path="multiregion-overview">table localities</InternalLink> that will provide the performance characteristics you need for each table's data?
  * Do you need low-latency reads and writes from a single region? Do you need that single region to be configurable at the <InternalLink path="table-localities#regional-by-row-tables">row level</InternalLink>? Or will <InternalLink path="table-localities#regional-tables">a single optimized region for the entire table</InternalLink> suffice?
  * Do you have a "read-mostly" <InternalLink path="table-localities#global-tables">table of reference data that is rarely updated</InternalLink>, but that must be read with low latency from all regions?

For more information about CockroachDB multi-region capabilities, review the following pages:

* <InternalLink path="multiregion-overview">Multi-Region Capabilities Overview</InternalLink>
* <InternalLink path="choosing-a-multi-region-configuration">How to Choose a Multi-Region Configuration</InternalLink>
* <InternalLink path="multiregion-survival-goals#when-to-use-zone-vs-region-survival-goals">When to use `ZONE` vs. `REGION` Survival Goals</InternalLink>
* <InternalLink path="table-localities#when-to-use-regional-vs-global-tables">When to use `REGIONAL` vs. `GLOBAL` Tables</InternalLink>

In addition, reviewing the following information will be helpful:

* The concept of <InternalLink path="cockroach-start#locality">locality</InternalLink>, which CockroachDB uses to place and balance data based on how you define survival goal and table locality settings.
* The recommendations in our <InternalLink path="recommended-production-settings">Production Checklist</InternalLink>, including our <InternalLink path="recommended-production-settings#hardware">hardware recommendations</InternalLink>. Afterwards, perform a proof of concept to size hardware for your use case.
* <InternalLink path="performance-best-practices-overview">SQL Performance Best Practices</InternalLink>

### Cluster setup

Each <InternalLink path="topology-patterns#multi-region">multi-region pattern</InternalLink> assumes the following setup:

<img src="https://mintcdn.com/cockroachlabs/3pzu7mzfAVB_Ztt9/images/v25.1/topology-patterns/topology_multi-region_hardware.png?fit=max&auto=format&n=3pzu7mzfAVB_Ztt9&q=85&s=68c1362fe77c906f33f2401f21acb6ec" alt="Multi-region hardware setup" width="960" height="540" data-path="images/v25.1/topology-patterns/topology_multi-region_hardware.png" />

#### Hardware

* 3 regions
* Per region, 3+ AZs with 3+ VMs evenly distributed across them
* Region-specific app instances and load balancers
  * Each load balancer redirects to CockroachDB nodes in its region.
  * When CockroachDB nodes are unavailable in a region, the load balancer redirects to nodes in other regions.

#### Cluster startup

Start each node with the <InternalLink path="cockroach-start#locality">`--locality`</InternalLink> flag specifying its region and AZ combination. For example, the following command starts a node in the `west1` AZ of the `us-west` region:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach start \
--locality=region=us-west,zone=west1 \
--certs-dir=certs \
--advertise-addr=<node1 internal address \
--join=<node1 internal address:26257,<node2 internal address:26257,<node3 internal address:26257 \
--cache=.25 \
--max-sql-memory=.25 \
--background
```

## Configuration

### Summary

To use this pattern, set the <InternalLink path="multiregion-overview">table locality</InternalLink> to either [`REGIONAL BY TABLE`](#regional-tables) or [`REGIONAL BY ROW`](#regional-by-row-tables).

#### Regional tables

In a *regional* table, access to the table will be fast in the table's home region and slower in other regions. In other words, CockroachDB optimizes access to data in a regional table from a single region. By default, a regional table's home region is the <InternalLink path="multiregion-overview#database-regions">database's primary region</InternalLink>, but that can be changed to use any region in the database. Regional tables work well when your application requires low-latency reads and writes for an entire table from a single region.

For instructions showing how to set a table's locality to `REGIONAL BY TABLE` and configure its home region, see <InternalLink path="alter-table">`ALTER TABLE ... SET LOCALITY`</InternalLink>.

By default, all tables in a multi-region database are *regional* tables that use the database's primary region. Unless you know your application needs different performance characteristics than regional tables provide, there is no need to change this setting.

#### Regional by row tables

In a *regional by row* table, individual rows are optimized for access from different home regions. Each row's home region is specified in a hidden <InternalLink path="alter-table">`crdb_region` column</InternalLink>, and is by default the region of the <InternalLink path="architecture/life-of-a-distributed-transaction#gateway">gateway node</InternalLink> from which the row is inserted. The `REGIONAL BY ROW` setting automatically divides a table and all of <InternalLink path="table-localities#indexes-on-regional-by-row-tables">its indexes</InternalLink> into <InternalLink path="partitioning">partitions</InternalLink> that use `crdb_region` as the prefix.

Use regional by row tables when your application requires low-latency reads and writes at a row level where individual rows are primarily accessed from a single region. For an example of a table in a multi-region cluster that can benefit from the `REGIONAL BY ROW` setting, see the `users` table from the <InternalLink path="movr">MovR application</InternalLink>, which could store users' data in specific regions for better performance.

To take advantage of regional by row tables:

* Use unique key lookups or queries with <InternalLink path="limit-offset">`LIMIT`</InternalLink> clauses to enable <InternalLink path="cost-based-optimizer#locality-optimized-search-in-multi-region-clusters">locality optimized searches</InternalLink> that prioritize rows in the gateway node's region. If there is a possibility that the results of the query all live in local rows, CockroachDB will first search for rows in the gateway node's region. The search only continues in remote regions if rows in the local region did not satisfy the query.

* Use <InternalLink path="foreign-key#rules-for-creating-foreign-keys">foreign keys</InternalLink> that reference the <InternalLink path="alter-table">`crdb_region` column</InternalLink> in <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables, unless <InternalLink path="alter-table#turn-on-auto-rehoming-for-regional-by-row-tables">auto-rehoming is enabled</InternalLink> for those tables.

* <InternalLink path="alter-table#turn-on-auto-rehoming-for-regional-by-row-tables">Turn on auto-rehoming for regional by row tables</InternalLink>. A row's home region will be automatically set to the gateway region of any <InternalLink path="update">`UPDATE`</InternalLink> or <InternalLink path="upsert">`UPSERT`</InternalLink> statements that write to those rows.

For instructions showing how to set a table's locality to `REGIONAL BY ROW` and configure the home regions of its rows, see <InternalLink path="alter-table">`ALTER TABLE ... SET LOCALITY`</InternalLink>.

For more information on regional by row tables, see the [Cockroach Labs blog post](https://www.cockroachlabs.com/blog/regional-by-row/).

<InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink> are not compatible with databases containing <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables. CockroachDB does not prevent you from defining secondary regions on databases with regional by row tables, but the interaction of these features is not supported.

Therefore, Cockroach Labs recommends that you avoid defining secondary regions on databases that use regional by row table configurations.

### Steps

<Note>
  By default, all tables in a multi-region database are [regional tables](#regional-tables).  Therefore, the steps below show how to set up [regional by row tables](#regional-by-row-tables).
</Note>

1. Create a database and set it as the default database:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   CREATE DATABASE test;
   ```

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   USE test;
   ```

   [This cluster is already deployed across three regions](#cluster-setup). Therefore, to make this database a "multi-region database", issue the following SQL statement to <InternalLink path="alter-database#set-the-primary-region">set the primary region</InternalLink>:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER DATABASE test PRIMARY REGION "us-east";
   ```

<Note>
  Every multi-region database must have a primary region.  For more information, see <InternalLink path="multiregion-overview#database-regions">Database regions</InternalLink>.
</Note>

1. Issue the following <InternalLink path="alter-database#add-region">`ADD REGION`</InternalLink> statements to add the remaining regions to the database:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER DATABASE test ADD REGION "us-west";
   ```

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER DATABASE test ADD REGION "us-central";
   ```

2. Create a `users` table:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   CREATE TABLE users (
       id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
       city STRING NOT NULL,
       first_name STRING NOT NULL,
       last_name STRING NOT NULL,
       address STRING NOT NULL
   );
   ```

   By default, all tables in a multi-region cluster default to the [`REGIONAL BY TABLE`](#regional-tables) locality setting.  To verify this, issue a <InternalLink path="show-create">`SHOW CREATE`</InternalLink> on the `users` table you just created:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SHOW CREATE TABLE users;
   ```

   ```
     table_name |                        create_statement
   -------------+------------------------------------------------------------------
     users      | CREATE TABLE public.users (
                |     id UUID NOT NULL DEFAULT gen_random_uuid(),
                |     city STRING NOT NULL,
                |     first_name STRING NOT NULL,
                |     last_name STRING NOT NULL,
                |     address STRING NOT NULL,
                |     CONSTRAINT "primary" PRIMARY KEY (id ASC),
                |     FAMILY "primary" (id, city, first_name, last_name, address)
                | ) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION
   ```

3. Set the table's locality to [`REGIONAL BY ROW`](#regional-by-row-tables) using the <InternalLink path="alter-table#set-locality">`ALTER TABLE ... SET LOCALITY`</InternalLink> statement:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER TABLE users SET LOCALITY REGIONAL BY ROW;
   ```

   ```
   NOTICE: LOCALITY changes will be finalized asynchronously; further schema changes on this table may be restricted until the job completes
   ALTER TABLE SET LOCALITY
   ```

4. Identify which rows need to be optimized for access from which regions. Issue <InternalLink path="update">`UPDATE`</InternalLink> statements that modify the automatically created <InternalLink path="alter-table">`crdb_region`</InternalLink> column. Issue the statements below to associate each row with a home region that depends on its `city` column:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   UPDATE users SET crdb_region = 'us-central'   WHERE city IN ('chicago', 'milwaukee', 'dallas');
   UPDATE users SET crdb_region = 'us-east'      WHERE city IN ('washington dc', 'boston', 'new york');
   UPDATE users SET crdb_region = 'us-west'      WHERE city IN ('los angeles', 'san francisco', 'seattle');
   ```

   By default, the region column will get auto-assigned on insert; this is also known as "auto-homing".  For more information about how the `crdb_region` column works, see <InternalLink path="alter-table">`ALTER TABLE ... SET LOCALITY REGIONAL BY ROW`</InternalLink>.

Note that the <InternalLink path="architecture/sql-layer">SQL engine</InternalLink> will avoid sending requests to nodes in other regions when it can instead read a value from a unique column that is stored locally. This capability is known as <InternalLink path="cost-based-optimizer#locality-optimized-search-in-multi-region-clusters">*locality optimized search*</InternalLink>.

<Tip>
  A good way to check that your <InternalLink path="multiregion-overview">table locality settings</InternalLink> are having the expected effect is by monitoring how the performance metrics of a workload change as the settings are applied to a running cluster.  For a tutorial showing how table localities can improve performance metrics across a multi-region cluster, see <InternalLink path="demo-low-latency-multi-region-deployment">Low Latency Reads and Writes in a Multi-Region Cluster</InternalLink>.
</Tip>

## Characteristics

### Latency

For [`REGIONAL BY TABLE` tables](#regional-tables), you get low latency for single-region writes and reads, as well as multi-region stale reads.

For [`REGIONAL BY ROW` tables](#regional-by-row-tables), you get low-latency consistent multi-region reads & writes for rows which are homed in specific regions, and low-latency multi-region stale reads from all other regions.

### Resiliency

Because the `test` database does not specify a <InternalLink path="multiregion-overview#survival-goals">survival goal</InternalLink>, it uses the default <InternalLink path="multiregion-survival-goals#survive-zone-failures">`ZONE` survival goal</InternalLink>.  With the default settings, an entire availability zone (AZ) can fail without interrupting access to the database.

For more information about how to choose a database survival goal, see <InternalLink path="multiregion-survival-goals#when-to-use-zone-vs-region-survival-goals">When to use `ZONE` vs. `REGION` survival goals</InternalLink>.

## Alternatives

* If rows in the table **cannot** be tied to specific geographies, reads must be up-to-date for business reasons or because the table is referenced by <InternalLink path="foreign-key">foreign keys</InternalLink>, and the table is rarely modified, consider the <InternalLink path="global-tables">`GLOBAL` Table Locality Pattern</InternalLink>.
* If your application can tolerate historical reads in some cases, consider the <InternalLink path="topology-follower-reads">Follower Reads pattern</InternalLink>.

## Tutorial

For a step-by-step demonstration showing how CockroachDB's multi-region capabilities (including [`REGIONAL BY ROW` tables](#regional-by-row-tables)) give you low-latency reads in a distributed cluster, see the tutorial on <InternalLink path="demo-low-latency-multi-region-deployment">Low Latency Reads and Writes in a Multi-Region Cluster</InternalLink>.

## Demo video

If you'd prefer to watch a video on Regional Tables, check out the following video:

<iframe width="560" height="315" src="https://www.youtube.com/embed/3gmF_yXj3q8" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

## See also

* <InternalLink path="multiregion-overview">Multi-Region Capabilities Overview</InternalLink>
* <InternalLink path="choosing-a-multi-region-configuration">How to Choose a Multi-Region Configuration</InternalLink>
* <InternalLink path="multiregion-survival-goals#when-to-use-zone-vs-region-survival-goals">When to Use `ZONE` vs. `REGION` Survival Goals</InternalLink>
* <InternalLink path="table-localities#when-to-use-regional-vs-global-tables">When to Use `REGIONAL` vs. `GLOBAL` Tables</InternalLink>
* <InternalLink path="demo-low-latency-multi-region-deployment">Low Latency Reads and Writes in a Multi-Region Cluster</InternalLink>
* <InternalLink path="migrate-to-multiregion-sql">Migrate to Multi-Region SQL</InternalLink>
* <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink>
* <InternalLink path="alter-database#set-secondary-region">`ALTER DATABASE ... SET SECONDARY REGION`</InternalLink>
* <InternalLink path="alter-database#drop-secondary-region">`ALTER DATABASE ... DROP SECONDARY REGION`</InternalLink>
* <InternalLink path="topology-patterns">Topology Patterns Overview</InternalLink>
  * Single-region patterns
    * <InternalLink path="topology-development">Development</InternalLink>
    * <InternalLink path="topology-basic-production">Basic Production</InternalLink>
  * Multi-region patterns
    * <InternalLink path="regional-tables">`REGIONAL` Tables</InternalLink>
    * <InternalLink path="global-tables">`GLOBAL` Tables</InternalLink>
    * <InternalLink path="topology-follower-reads">Follower Reads</InternalLink>
    * <InternalLink path="topology-follow-the-workload">Follow-the-Workload</InternalLink>
