Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/current/_includes/v26.2/faq/auto-generate-unique-ids.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ SELECT * FROM users;
(3 rows)
~~~

For performance, CockroachDB automatically skips uniqueness checks for `gen_random_uuid()` due to the near-zero probability of UUID collisions. This optimization is controlled by the [`sql.optimizer.uniqueness_checks_for_gen_random_uuid.enabled`]({% link {{ page.version.version }}/cluster-settings.md %}) cluster setting, which defaults to `false`.
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated

#### Use `uuid_v4()`

Alternatively, you can use the [`BYTES`]({% link {{ page.version.version }}/bytes.md %}) column with the `uuid_v4()` function as the default value:
Expand Down Expand Up @@ -122,4 +124,6 @@ SELECT * FROM users3;

Upon insert or upsert, the `unique_rowid()` function generates a default value from the timestamp and ID of the node executing the insert. Such time-ordered values are likely to be globally unique except in cases where a very large number of IDs (100,000+) are generated per node per second. Also, there can be gaps and the order is not completely guaranteed.

In multi-region deployments with [`REGIONAL BY ROW`]({% link {{ page.version.version }}/table-localities.md %}#regional-by-row-tables) tables, uniqueness checks can add latency due to cross-partition validation. You can improve performance by setting the `skip_unique_checks` index storage parameter, but **only** if the application can guarantee uniqueness. For more information, refer to [`skip_unique_checks`]({% link {{ page.version.version }}/with-storage-parameter.md %}#skip-unique-checks).
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated

To understand the differences between the `UUID` and `unique_rowid()` options, see the [SQL FAQs]({% link {{ page.version.version }}/sql-faqs.md %}#what-are-the-differences-between-uuid-sequences-and-unique_rowid). For further background on UUIDs, see [What is a UUID, and Why Should You Care?](https://www.cockroachlabs.com/blog/what-is-a-uuid/).
23 changes: 12 additions & 11 deletions src/current/_includes/v26.2/misc/index-storage-parameters.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
| Parameter name | Description | Data type | Default value
|---------------------+----------------------|-----|------|
| `bucket_count` | The number of buckets into which a [hash-sharded index]({% link {{ page.version.version }}/hash-sharded-indexes.md %}) will split. | Integer | The value of the `sql.defaults.default_hash_sharded_index_bucket_count` [cluster setting]({% link {{ page.version.version }}/cluster-settings.md %}). |
| <a name="storage-parameter-shard-columns"></a> `shard_columns` | The first key column or columns to use when computing the shard column for a [hash-sharded index]({% link {{ page.version.version }}/hash-sharded-indexes.md %}). The value must match a prefix of the index key columns. If this parameter is omitted, CockroachDB hashes all index key columns. | Tuple of column identifiers or a single column identifier | All index key columns |
| `geometry_max_x` | The maximum X-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if you are using a custom [SRID]({% link {{ page.version.version }}/architecture/glossary.md %}#srid). | | Derived from SRID bounds, else `(1 << 31) -1`. |
| `geometry_max_y` | The maximum Y-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if you are using a custom [SRID]({% link {{ page.version.version }}/architecture/glossary.md %}#srid). | | Derived from SRID bounds, else `(1 << 31) -1`. |
| `geometry_min_x` | The minimum X-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if the default bounds of the SRID are too large/small for the given data, or SRID = 0 and you wish to use a smaller range (unfortunately this is currently not exposed, but is viewable on <https://epsg.io/3857>). By default, SRID = 0 assumes `[-min int32, max int32]` ranges. | | Derived from SRID bounds, else `-(1 << 31)`. |
| `geometry_min_y` | The minimum Y-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if you are using a custom [SRID]({% link {{ page.version.version }}/architecture/glossary.md %}#srid). | | Derived from SRID bounds, else `-(1 << 31)`. |
| `s2_level_mod` | `s2_max_level` must be divisible by `s2_level_mod`. `s2_level_mod` must be between `1` and `3`. | Integer | `1` |
| `s2_max_cells` | The maximum number of S2 cells used in the covering. Provides a limit on how much work is done exploring the possible coverings. Allowed values: `1-30`. You may want to use higher values for odd-shaped regions such as skinny rectangles. Used in [spatial indexes]({% link {{ page.version.version }}/spatial-indexes.md %}). | Integer | `4` |
| `s2_max_level` | The maximum level of S2 cell used in the covering. Allowed values: `1-30`. Setting it to less than the default means that CockroachDB will be forced to generate coverings using larger cells. Used in [spatial indexes]({% link {{ page.version.version }}/spatial-indexes.md %}). | Integer | `30` |
| Parameter name | Description | Data type | Default value | Can be [altered after index creation]({% link {{ page.version.version }}/alter-index.md %}#modify-index-storage-parameters)?
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated
|---------------------+----------------------|-----|------|-----|
| `bucket_count` | The number of buckets into which a [hash-sharded index]({% link {{ page.version.version }}/hash-sharded-indexes.md %}) will split. | Integer | The value of the `sql.defaults.default_hash_sharded_index_bucket_count` [cluster setting]({% link {{ page.version.version }}/cluster-settings.md %}). | No |
| <a name="storage-parameter-shard-columns"></a> `shard_columns` | The first key column or columns to use when computing the shard column for a [hash-sharded index]({% link {{ page.version.version }}/hash-sharded-indexes.md %}). The value must match a prefix of the index key columns. If this parameter is omitted, CockroachDB hashes all index key columns. | Tuple of column identifiers or a single column identifier | All index key columns | No |
| <a name="skip-unique-checks"></a> `skip_unique_checks` | Disables [unique constraint checks]({% link {{ page.version.version }}/unique.md %}) for indexes with [implicit partition]({% link {{ page.version.version }}/alter-table.md %}#using-implicit-vs-explicit-index-partitioning-in-regional-by-row-tables) columns, including indexes on [`REGIONAL BY ROW`]({% link {{ page.version.version }}/regional-tables.md %}) tables. This avoids expensive cross-partition uniqueness checks in multi-region deployments. Can only be set on unique indexes with implicit partitioning. **Warning:** This should only be used if the application can guarantee uniqueness. Safe use cases include: externally generated UUIDs, or columns using [`unique_rowid()`]({% link {{ page.version.version }}/sql-faqs.md %}#how-do-i-auto-generate-unique-row-ids-in-cockroachdb) or [`unordered_unique_rowid()`]({% link {{ page.version.version }}/sql-faqs.md %}#how-do-i-auto-generate-unique-row-ids-in-cockroachdb) as defaults (provided you do not also manually insert values into those columns). Note that, by default, the optimizer does not perform uniqueness checks on columns using [`gen_random_uuid()`]({% link {{ page.version.version }}/uuid.md %}). Incorrectly applying this parameter when uniqueness is not guaranteed could result in logically duplicate keys in different partitions of a unique index. | Boolean | `false` | Yes |
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated
| `geometry_max_x` | The maximum X-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if you are using a custom [SRID]({% link {{ page.version.version }}/architecture/glossary.md %}#srid). | | Derived from SRID bounds, else `(1 << 31) -1`. | No |
| `geometry_max_y` | The maximum Y-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if you are using a custom [SRID]({% link {{ page.version.version }}/architecture/glossary.md %}#srid). | | Derived from SRID bounds, else `(1 << 31) -1`. | No |
| `geometry_min_x` | The minimum X-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if the default bounds of the SRID are too large/small for the given data, or SRID = 0 and you wish to use a smaller range (unfortunately this is currently not exposed, but is viewable on <https://epsg.io/3857>). By default, SRID = 0 assumes `[-min int32, max int32]` ranges. | | Derived from SRID bounds, else `-(1 << 31)`. | No |
| `geometry_min_y` | The minimum Y-value of the [spatial reference system]({% link {{ page.version.version }}/architecture/glossary.md %}#spatial-reference-system) for the object(s) being covered. This only needs to be set if you are using a custom [SRID]({% link {{ page.version.version }}/architecture/glossary.md %}#srid). | | Derived from SRID bounds, else `-(1 << 31)`. | No |
| `s2_level_mod` | `s2_max_level` must be divisible by `s2_level_mod`. `s2_level_mod` must be between `1` and `3`. | Integer | `1` | No |
| `s2_max_cells` | The maximum number of S2 cells used in the covering. Provides a limit on how much work is done exploring the possible coverings. Allowed values: `1-30`. You may want to use higher values for odd-shaped regions such as skinny rectangles. Used in [spatial indexes]({% link {{ page.version.version }}/spatial-indexes.md %}). | Integer | `4` | No |
| `s2_max_level` | The maximum level of S2 cell used in the covering. Allowed values: `1-30`. Setting it to less than the default means that CockroachDB will be forced to generate coverings using larger cells. Used in [spatial indexes]({% link {{ page.version.version }}/spatial-indexes.md %}). | Integer | `30` | No |

The following parameters are included for PostgreSQL compatibility and do not affect how CockroachDB runs:

Expand Down
112 changes: 111 additions & 1 deletion src/current/v26.2/alter-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ Additional parameters are documented for the respective [subcommands](#subcomman

Subcommand | Description |
-----------|-------------|
[`CONFIGURE ZONE`](#configure-zone) | [Replication Controls]({% link {{ page.version.version }}/configure-replication-zones.md %}) for an index. |
[`CONFIGURE ZONE`](#configure-zone) | [Replication Controls]({% link {{ page.version.version }}/configure-replication-zones.md %}) for an index. |
[`PARTITION BY`](#partition-by) | Partition, re-partition, or un-partition an index.
[`RENAME TO`](#rename-to) | Change the name of an index.
[`SCATTER`](#scatter) | Make 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. |
[`SET`](#set) | Modify alterable [storage parameters]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) on an existing index.
[`RESET`](#reset) | Remove alterable [storage parameters]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) from an existing index, resetting it to its default value.
[`SPLIT AT`](#split-at) | Force a [range split]({% link {{ page.version.version }}/architecture/distribution-layer.md %}#range-splits) at the specified row in the index.
[`UNSPLIT AT`](#unsplit-at) | Remove a range split enforcement in the index.
[`VISIBILITY`](#visibility) | Set the visibility of an index between a range of `0.0` and `1.0`.
Expand Down Expand Up @@ -145,6 +147,51 @@ Parameter | Description

For usage, see [Synopsis](#synopsis).

### `SET`

`ALTER INDEX ... SET (storage_parameter)` modifies an alterable [index storage parameter]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) on an existing index.

{{site.data.alerts.callout_info}}
Most [index storage parameters]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) can only be set at index creation time and cannot be modified afterward.
{{site.data.alerts.end}}
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated

For examples, see [Modify index storage parameters](#modify-index-storage-parameters).

#### Required privileges

The user must have the `CREATE` [privilege]({% link {{ page.version.version }}/security-reference/authorization.md %}#managing-privileges) on the table.

#### Parameters

Parameter | Description
----------|-------------
`storage_parameter` | The name of the [index storage parameter]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) to modify.
`value` | The new value for the storage parameter.

For usage, see [Synopsis](#synopsis).

### `RESET`

`ALTER INDEX ... RESET (storage_parameter)` removes an alterable [index storage parameter]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) from an existing index, resetting it to its default value.

{{site.data.alerts.callout_info}}
Most [index storage parameters]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) can only be set at index creation time and cannot be modified afterward.
{{site.data.alerts.end}}

For examples, see [Modify index storage parameters](#modify-index-storage-parameters).

#### Required privileges

The user must have the `CREATE` [privilege]({% link {{ page.version.version }}/security-reference/authorization.md %}#managing-privileges) on the table.

#### Parameters

Parameter | Description
----------|-------------
`storage_parameter` | The name of the [index storage parameter]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) to reset to its default value.

For usage, see [Synopsis](#synopsis).

### `SPLIT AT`

`ALTER INDEX ... SPLIT AT` forces a [range split]({% link {{ page.version.version }}/architecture/distribution-layer.md %}#range-splits) at a specified row in the index.
Expand Down Expand Up @@ -512,6 +559,69 @@ SELECT range_id, start_pretty, end_pretty, split_enforced_until FROM crdb_intern

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 [merge the data]({% link {{ page.version.version }}/architecture/distribution-layer.md %}#range-merges) in the table as needed.

### Modify index storage parameters

Most [index storage parameters]({% link {{ page.version.version }}/with-storage-parameter.md %}#index-parameters) can only be set when creating an index and cannot be changed afterward. However, some index storage parameters can be modified on an existing index using `ALTER INDEX ... SET` or `ALTER INDEX ... RESET`.
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated

This example demonstrates enabling and disabling the `skip_unique_checks` parameter on a unique index on a [`REGIONAL BY ROW`]({% link {{ page.version.version }}/regional-tables.md %}) table.
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated

Set up a multi-region database and create a `REGIONAL BY ROW` table with a unique index:

{% include_cached copy-clipboard.html %}
~~~ sql
CREATE DATABASE multi_region_db PRIMARY REGION "us-east1" REGIONS "us-west1", "europe-west1";
USE multi_region_db;

CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email STRING UNIQUE,
name STRING
) LOCALITY REGIONAL BY ROW;
~~~

View the index on the `email` column:

{% include_cached copy-clipboard.html %}
~~~ sql
SHOW CREATE TABLE users;
~~~

~~~
table_name | create_statement
-------------+----------------------------------------------------------------------------------------------------
users | CREATE TABLE public.users (
| id UUID NOT NULL DEFAULT gen_random_uuid(),
| email STRING NULL,
| name STRING NULL,
| crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
| CONSTRAINT users_pkey PRIMARY KEY (id ASC),
| UNIQUE INDEX users_email_key (email ASC)
| ) LOCALITY REGIONAL BY ROW
(1 row)
~~~

The `users_email_key` index is a unique index that is implicitly partitioned by the `crdb_region` column. By default, CockroachDB performs cross-partition uniqueness checks for this index (`skip_unique_checks = false`).
Comment thread
bsanchez-the-roach marked this conversation as resolved.
Outdated

#### Set a storage parameter

To disable the cross-partition uniqueness checks, set `skip_unique_checks` to `true`:

{% include_cached copy-clipboard.html %}
~~~ sql
ALTER INDEX users_email_key SET (skip_unique_checks = true);
~~~

#### Reset a storage parameter

To re-enable cross-partition uniqueness checks, reset the parameter to its default value:

{% include_cached copy-clipboard.html %}
~~~ sql
ALTER INDEX users_email_key RESET (skip_unique_checks);
~~~

This restores the default behavior where CockroachDB enforces uniqueness across all partitions of the index (`skip_unique_checks = false`).

### Set index visibility

#### Set an index to be not visible
Expand Down
Loading
Loading