Skip to content

Commit

Permalink
opt: remove uniqueness checks for gen_random_uuid()
Browse files Browse the repository at this point in the history
This commit removes uniqueness checks for UUID columns that are set
to gen_random_uuid(), because uniqueness can be sufficiently guaranteed
by randomness. The probability of collision when using gen_random_uuid()
is extremely low. If users still want the checks, however, they can set
sql.optimizer.uniqueness_checks_for_gen_random_uuid.enabled, a new cluster
setting, to true. By default the setting is false.

Fixes #57790

Release justification: This commit is a low-risk update to new functionality.

Release note (sql change): Added a new cluster setting
sql.optimizer.uniqueness_checks_for_gen_random_uuid.enabled, which controls
creation of uniqueness checks for UUID columns set to gen_random_uuid().
When enabled, uniqueness checks will be added for the UUID column if it
has a unique constraint that cannot be enforced by an index. When disabled,
no uniqueness checks are planned for these columns, and uniqueness is
assumed due to the near-zero collision probability of gen_random_uuid()
  • Loading branch information
rytaft committed Feb 26, 2021
1 parent 4d24f8a commit 472774f
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ sql.metrics.statement_details.plan_collection.period duration 5m0s the time unti
sql.metrics.statement_details.threshold duration 0s minimum execution time to cause statement statistics to be collected. If configured, no transaction stats are collected.
sql.metrics.transaction_details.enabled boolean true collect per-application transaction statistics
sql.notices.enabled boolean true enable notices in the server/client protocol being sent
sql.optimizer.uniqueness_checks_for_gen_random_uuid.enabled boolean false if enabled, uniqueness checks may be planned for mutations of UUID columns updated with gen_random_uuid(); otherwise, uniqueness is assumed due to near-zero collision probability
sql.spatial.experimental_box2d_comparison_operators.enabled boolean false enables the use of certain experimental box2d comparison operators
sql.stats.automatic_collection.enabled boolean true automatic statistics collection mode
sql.stats.automatic_collection.fraction_stale_rows float 0.2 target fraction of stale rows per table that will trigger a statistics refresh
Expand Down
1 change: 1 addition & 0 deletions docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<tr><td><code>sql.metrics.statement_details.threshold</code></td><td>duration</td><td><code>0s</code></td><td>minimum execution time to cause statement statistics to be collected. If configured, no transaction stats are collected.</td></tr>
<tr><td><code>sql.metrics.transaction_details.enabled</code></td><td>boolean</td><td><code>true</code></td><td>collect per-application transaction statistics</td></tr>
<tr><td><code>sql.notices.enabled</code></td><td>boolean</td><td><code>true</code></td><td>enable notices in the server/client protocol being sent</td></tr>
<tr><td><code>sql.optimizer.uniqueness_checks_for_gen_random_uuid.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if enabled, uniqueness checks may be planned for mutations of UUID columns updated with gen_random_uuid(); otherwise, uniqueness is assumed due to near-zero collision probability</td></tr>
<tr><td><code>sql.spatial.experimental_box2d_comparison_operators.enabled</code></td><td>boolean</td><td><code>false</code></td><td>enables the use of certain experimental box2d comparison operators</td></tr>
<tr><td><code>sql.stats.automatic_collection.enabled</code></td><td>boolean</td><td><code>true</code></td><td>automatic statistics collection mode</td></tr>
<tr><td><code>sql.stats.automatic_collection.fraction_stale_rows</code></td><td>float</td><td><code>0.2</code></td><td>target fraction of stale rows per table that will trigger a statistics refresh</td></tr>
Expand Down
21 changes: 19 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/unique
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,19 @@ CREATE TABLE uniq_computed_pk (
)

statement ok
CREATE TABLE other (k INT, v INT, w INT NOT NULL, x INT, y INT)
CREATE TABLE uniq_uuid (
id1 UUID,
id2 UUID,
UNIQUE WITHOUT INDEX (id1),
UNIQUE WITHOUT INDEX (id2)
)

statement ok
CREATE TABLE other (k INT, v INT, w INT NOT NULL, x INT, y INT, u UUID)

# Insert some data into the other table.
statement ok
INSERT INTO other VALUES (10, 10, 1, 1, 1)
INSERT INTO other VALUES (10, 10, 1, 1, 1, '8597b0eb-7b89-4857-858a-fabf86f6a3ac')


# -- Tests with INSERT --
Expand Down Expand Up @@ -361,6 +369,15 @@ i s d c_i_expr c_s c_d c_d_expr
1 a 1.0 bar a 1.0 1.0
2 b 2.0 bar b 2.0 2.0

# Insert a couple of rows into a table with UUID columns.
statement ok
INSERT INTO uniq_uuid (id1, id2) SELECT gen_random_uuid(), '8597b0eb-7b89-4857-858a-fabf86f6a3ac'

# We can catch uniqueness violations on UUID columns set to a value other than
# gen_random_uuid().
statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_id2"\nDETAIL: Key \(id2\)=\('8597b0eb-7b89-4857-858a-fabf86f6a3ac'\) already exists\.
INSERT INTO uniq_uuid (id1, id2) SELECT gen_random_uuid(), u FROM other


# -- Tests with UPDATE --
subtest Update
Expand Down
Loading

0 comments on commit 472774f

Please sign in to comment.