Skip to content

Commit e323711

Browse files
authored
Document modifying the region column or expression (#20271)
Fixes DOC-14702 Summary of changes: - Update `CREATE TABLE`, `ALTER TABLE`, and computed columns docs with instructions for modifying the region column or its expression NB. Changes ported to v25.1, v25.3, v25.4
1 parent 18cb677 commit e323711

15 files changed

+135
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
The following instructions show how to change the mapping of the [`crdb_internal_region`]({% link {{ page.version.version }}/alter-table.md %}#crdb_region) column that determines row locality for a [regional by row table]({% link {{ page.version.version }}/regional-tables.md %}#regional-by-row-tables) where the [column was already defined with `REGIONAL BY ROW AS {column}`]{% if page.name == "create-table.md" %}(#create-a-table-with-a-regional-by-row-locality-using-a-custom-region-column){% elsif page.name == "alter-table.md" %}(#rename-crdb_region){% else %}({% link {{ page.version.version }}/create-table.md %}#create-a-table-with-a-regional-by-row-locality-using-a-custom-region-column){% endif %}. This method [alters the computed column's expression]({% link {{ page.version.version }}/computed-columns.md %}#alter-the-formula-for-a-computed-column).
2+
3+
1. [Add a new region column]({% link {{ page.version.version }}/alter-table.md %}#add-column) of the same type (`crdb_internal_region`) with the updated scalar expression for the computed column:
4+
5+
{% include_cached copy-clipboard.html %}
6+
~~~ sql
7+
ALTER TABLE app.public.users ADD COLUMN region_new crdb_internal_region AS ({new_expression}) STORED;
8+
~~~
9+
10+
1. Atomically [swap the column names]({% link {{ page.version.version }}/alter-table.md %}#rename-column) so the new computed column takes the original name:
11+
12+
{% include_cached copy-clipboard.html %}
13+
~~~ sql
14+
ALTER TABLE app.public.users RENAME COLUMN region TO region_prev, RENAME COLUMN region_new TO region;
15+
~~~
16+
17+
1. Point the table locality at the new computed column using [`ALTER TABLE ... SET LOCALITY`]({% link {{ page.version.version }}/alter-table.md %}#set-locality):
18+
19+
{% include_cached copy-clipboard.html %}
20+
~~~ sql
21+
ALTER TABLE app.public.users SET LOCALITY REGIONAL BY ROW AS region;
22+
~~~
23+
24+
1. After verifying the changes have occurred (using a query like `SELECT region, * FROM app.public.users WHERE ...`), [drop the previous computed column]({% link {{ page.version.version }}/alter-table.md %}#drop-column):
25+
26+
{% include_cached copy-clipboard.html %}
27+
~~~ sql
28+
ALTER TABLE app.public.users DROP COLUMN region_prev;
29+
~~~
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
The following instructions show how to change the mapping of the [`crdb_internal_region`]({% link {{ page.version.version }}/alter-table.md %}#crdb_region) column that determines row locality for a [regional by row table]({% link {{ page.version.version }}/regional-tables.md %}#regional-by-row-tables) where the [column was already defined with `REGIONAL BY ROW AS {column}`]{% if page.name == "create-table.md" %}(#create-a-table-with-a-regional-by-row-locality-using-a-custom-region-column){% elsif page.name == "alter-table.md" %}(#rename-crdb_region){% else %}({% link {{ page.version.version }}/create-table.md %}#create-a-table-with-a-regional-by-row-locality-using-a-custom-region-column){% endif %}. This method [alters the computed column's expression]({% link {{ page.version.version }}/computed-columns.md %}#alter-the-formula-for-a-computed-column).
2+
3+
1. [Add a new region column]({% link {{ page.version.version }}/alter-table.md %}#add-column) of the same type (`crdb_internal_region`) with the updated scalar expression for the computed column:
4+
5+
{% include_cached copy-clipboard.html %}
6+
~~~ sql
7+
ALTER TABLE app.public.users ADD COLUMN region_new crdb_internal_region AS ({new_expression}) STORED;
8+
~~~
9+
10+
1. Atomically [swap the column names]({% link {{ page.version.version }}/alter-table.md %}#rename-column) so the new computed column takes the original name:
11+
12+
{% include_cached copy-clipboard.html %}
13+
~~~ sql
14+
ALTER TABLE app.public.users RENAME COLUMN region TO region_prev, RENAME COLUMN region_new TO region;
15+
~~~
16+
17+
1. Point the table locality at the new computed column using [`ALTER TABLE ... SET LOCALITY`]({% link {{ page.version.version }}/alter-table.md %}#set-locality):
18+
19+
{% include_cached copy-clipboard.html %}
20+
~~~ sql
21+
ALTER TABLE app.public.users SET LOCALITY REGIONAL BY ROW AS region;
22+
~~~
23+
24+
1. After verifying the changes have occurred (using a query like `SELECT region, * FROM app.public.users WHERE ...`), [drop the previous computed column]({% link {{ page.version.version }}/alter-table.md %}#drop-column):
25+
26+
{% include_cached copy-clipboard.html %}
27+
~~~ sql
28+
ALTER TABLE app.public.users DROP COLUMN region_prev;
29+
~~~
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
The following instructions show how to change the mapping of the [`crdb_internal_region`]({% link {{ page.version.version }}/alter-table.md %}#crdb_region) column that determines row locality for a [regional by row table]({% link {{ page.version.version }}/regional-tables.md %}#regional-by-row-tables) where the [column was already defined with `REGIONAL BY ROW AS {column}`]{% if page.name == "create-table.md" %}(#create-a-table-with-a-regional-by-row-locality-using-a-custom-region-column){% elsif page.name == "alter-table.md" %}(#rename-crdb_region){% else %}({% link {{ page.version.version }}/create-table.md %}#create-a-table-with-a-regional-by-row-locality-using-a-custom-region-column){% endif %}. This method [alters the computed column's expression]({% link {{ page.version.version }}/computed-columns.md %}#alter-the-formula-for-a-computed-column).
2+
3+
1. [Add a new region column]({% link {{ page.version.version }}/alter-table.md %}#add-column) of the same type (`crdb_internal_region`) with the updated scalar expression for the computed column:
4+
5+
{% include_cached copy-clipboard.html %}
6+
~~~ sql
7+
ALTER TABLE app.public.users ADD COLUMN region_new crdb_internal_region AS ({new_expression}) STORED;
8+
~~~
9+
10+
1. Atomically [swap the column names]({% link {{ page.version.version }}/alter-table.md %}#rename-column) so the new computed column takes the original name:
11+
12+
{% include_cached copy-clipboard.html %}
13+
~~~ sql
14+
ALTER TABLE app.public.users RENAME COLUMN region TO region_prev, RENAME COLUMN region_new TO region;
15+
~~~
16+
17+
1. Point the table locality at the new computed column using [`ALTER TABLE ... SET LOCALITY`]({% link {{ page.version.version }}/alter-table.md %}#set-locality):
18+
19+
{% include_cached copy-clipboard.html %}
20+
~~~ sql
21+
ALTER TABLE app.public.users SET LOCALITY REGIONAL BY ROW AS region;
22+
~~~
23+
24+
1. After verifying the changes have occurred (using a query like `SELECT region, * FROM app.public.users WHERE ...`), [drop the previous computed column]({% link {{ page.version.version }}/alter-table.md %}#drop-column):
25+
26+
{% include_cached copy-clipboard.html %}
27+
~~~ sql
28+
ALTER TABLE app.public.users DROP COLUMN region_prev;
29+
~~~

src/current/v25.2/alter-table.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,12 @@ To ensure that the uniqueness constraint is enforced properly across regions whe
13141314

13151315
{% include {{page.version.version}}/sql/locality-optimized-search.md %}
13161316

1317+
<a name="modify-rbr-region-column"></a>
1318+
1319+
#### Modify the region column or its expression
1320+
1321+
{% include {{ page.version.version }}/sql/modify-region-column-or-its-expression.md %}
1322+
13171323
#### Using `DEFAULT gen_random_uuid()` in `REGIONAL BY ROW` tables
13181324

13191325
To auto-generate unique row identifiers in `REGIONAL BY ROW` tables, use the [`UUID`]({% link {{ page.version.version }}/uuid.md %}) column with the `gen_random_uuid()` [function]({% link {{ page.version.version }}/functions-and-operators.md %}#id-generation-functions) as the [default value]({% link {{ page.version.version }}/default-value.md %}):

src/current/v25.2/computed-columns.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ For more information, see [`ADD COLUMN`]({% link {{ page.version.version }}/alte
101101

102102
{% include {{ page.version.version }}/computed-columns/alter-computed-column.md %}
103103

104+
{{site.data.alerts.callout_info}}
105+
If the computed column controls row locality in a [`REGIONAL BY ROW`]({% link {{ page.version.version }}/table-localities.md %}#regional-by-row-tables) table (for example, using a custom [`crdb_internal_region`]({% link {{ page.version.version }}/alter-table.md %}#crdb_region) column with `REGIONAL BY ROW AS`), you can change its expression by following the steps in [Modify the region column or its expression]({% link {{ page.version.version }}/alter-table.md %}#modify-rbr-region-column).
106+
{{site.data.alerts.end}}
107+
104108
## See also
105109

106110
- [Scalar Expressions]({% link {{ page.version.version }}/scalar-expressions.md %})

src/current/v25.2/create-table.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,10 @@ For example:
863863

864864
CockroachDB will then assign a region to each row, based on the value of the `region` column. In this example, the `region` column is computed from the value of the `city` column.
865865

866+
#### Modify the region column or its expression
867+
868+
{% include {{ page.version.version }}/sql/modify-region-column-or-its-expression.md %}
869+
866870
### Create a table with an identity column
867871

868872
[Identity columns](#identity-columns) define a sequence from which to populate a column when a new row is inserted.

src/current/v25.2/table-localities.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Table locality settings are used for optimizing latency under different read and
2929

3030
{% include {{page.version.version}}/sql/regional-by-row-table-description.md %}
3131

32+
To change the column or expression used to determine row locality for a regional by row table defined with `REGIONAL BY ROW AS {column}`, refer to [Modify the region column or its expression]({% link {{ page.version.version }}/alter-table.md %}#modify-rbr-region-column).
33+
3234
### Indexes on `REGIONAL BY ROW` tables
3335

3436
{% include {{page.version.version}}/sql/indexes-regional-by-row.md %}

src/current/v25.3/alter-table.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,12 @@ ALTER TABLE rides ADD COLUMN region crdb_internal_region AS (
26182618

26192619
{% include {{page.version.version}}/sql/locality-optimized-search.md %}
26202620

2621+
<a name="modify-rbr-region-column"></a>
2622+
2623+
#### Modify the region column or its expression
2624+
2625+
{% include {{ page.version.version }}/sql/modify-region-column-or-its-expression.md %}
2626+
26212627
#### Infer a row's home region from a foreign key
26222628

26232629
{{site.data.alerts.callout_info}}

src/current/v25.3/computed-columns.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ For more information, see [`ADD COLUMN`]({% link {{ page.version.version }}/alte
101101

102102
{% include {{ page.version.version }}/computed-columns/alter-computed-column.md %}
103103

104+
{{site.data.alerts.callout_info}}
105+
If the computed column controls row locality in a [`REGIONAL BY ROW`]({% link {{ page.version.version }}/table-localities.md %}#regional-by-row-tables) table (for example, using a custom [`crdb_internal_region`]({% link {{ page.version.version }}/alter-table.md %}#crdb_region) column with `REGIONAL BY ROW AS`), you can change its expression by following the steps in [Modify the region column or its expression]({% link {{ page.version.version }}/alter-table.md %}#modify-rbr-region-column).
106+
{{site.data.alerts.end}}
107+
104108
## See also
105109

106110
- [Scalar Expressions]({% link {{ page.version.version }}/scalar-expressions.md %})

src/current/v25.3/create-table.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,10 @@ For example:
863863

864864
CockroachDB will then assign a region to each row, based on the value of the `region` column. In this example, the `region` column is computed from the value of the `city` column.
865865

866+
#### Modify the region column or its expression
867+
868+
{% include {{ page.version.version }}/sql/modify-region-column-or-its-expression.md %}
869+
866870
### Create a table with an identity column
867871

868872
[Identity columns](#identity-columns) define a sequence from which to populate a column when a new row is inserted.

0 commit comments

Comments
 (0)