Skip to content

Commit

Permalink
Merge #43263
Browse files Browse the repository at this point in the history
43263: sql: enable optimized-driven foreign key checks r=RaduBerinde a=RaduBerinde

Enabling the fk-driven foreign key checks by default.

Release note (sql change, bug fix, performance improvement): foreign
key checks that don't involve cascades are now performed after the
mutation is performed, allowing self-referential FKs or referential
cycles. The execution plans for foreign key checks are now driven by
the optimizer, which can make better planning decisions; in
particular, if there is a suitable duplicated index, the one in the
current locality will be used for FK checks.

Co-authored-by: Radu Berinde <[email protected]>
  • Loading branch information
craig[bot] and RaduBerinde committed Dec 19, 2019
2 parents 14dfb3d + 3078c45 commit 605d8ef
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
18 changes: 9 additions & 9 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1482,25 +1482,25 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation on customers from receipts is preserved.
db.ExpectErr(
t, "foreign key violation.* referenced in table \"receipts\"|update.*violates foreign key constraint on table \"receipts\"",
t, "update.*violates foreign key constraint \"fk_dest_ref_customers\"",
`UPDATE store.customers SET email = concat(id::string, 'nope')`,
)

// FK validation on customers from orders is preserved.
db.ExpectErr(
t, "foreign key violation.* referenced in table \"orders\"|update.*violates foreign key constraint on table \"orders\"",
t, "update.*violates foreign key constraint \"fk_customerid_ref_customers\"",
`UPDATE store.customers SET id = id * 1000`,
)

// FK validation of customer id is preserved.
db.ExpectErr(
t, "foreign key violation.* in customers@primary|insert.*violates foreign key constraint \"fk_customerid_ref_customers\"",
t, "insert.*violates foreign key constraint \"fk_customerid_ref_customers\"",
`INSERT INTO store.orders VALUES (999, NULL, 999)`,
)

// FK validation of self-FK is preserved.
db.ExpectErr(
t, "foreign key violation: value .999. not found in receipts@primary|insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
t, "insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
`INSERT INTO store.receipts VALUES (1, 999, NULL, NULL)`,
)
})
Expand All @@ -1515,7 +1515,7 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation on customers from orders is preserved.
db.ExpectErr(
t, "foreign key violation.* referenced in table \"orders\"|update.*violates foreign key constraint on table \"orders\"",
t, "update.*violates foreign key constraint \"fk_customerid_ref_customers\"",
`UPDATE store.customers SET id = id*100`,
)

Expand Down Expand Up @@ -1556,7 +1556,7 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation of self-FK is preserved.
db.ExpectErr(
t, "foreign key violation: value .999. not found in receipts@primary|insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
t, "insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
`INSERT INTO store.receipts VALUES (-1, 999, NULL, NULL)`,
)
})
Expand All @@ -1574,19 +1574,19 @@ func TestBackupRestoreCrossTableReferences(t *testing.T) {

// FK validation of customer email is preserved.
db.ExpectErr(
t, "foreign key violation.* in customers@customers_email_key|insert.*violates foreign key constraint \"fk_dest_ref_customers\"",
t, "nsert.*violates foreign key constraint \"fk_dest_ref_customers\"",
`INSERT INTO store.receipts VALUES (-1, NULL, '999', 999)`,
)

// FK validation on customers from receipts is preserved.
db.ExpectErr(
t, "foreign key violation.* referenced in table \"receipts\"|delete.*violates foreign key constraint on table \"receipts\"",
t, "delete.*violates foreign key constraint \"fk_dest_ref_customers\"",
`DELETE FROM store.customers`,
)

// FK validation of self-FK is preserved.
db.ExpectErr(
t, "foreign key violation: value .999. not found in receipts@primary|insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
t, "insert.*violates foreign key constraint \"fk_reissue_ref_receipts\"",
`INSERT INTO store.receipts VALUES (-1, 999, NULL, NULL)`,
)
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ var zigzagJoinClusterMode = settings.RegisterBoolSetting(
var optDrivenFKClusterMode = settings.RegisterBoolSetting(
"sql.defaults.experimental_optimizer_foreign_keys.enabled",
"default value for experimental_optimizer_foreign_keys session setting; enables optimizer-driven foreign key checks by default",
false,
true,
)

var insertFastPathClusterMode = settings.RegisterBoolSetting(
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/parallel_stmts_compat
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DELETE FROM kv WHERE k = 1 RETURNING NOTHING
statement ok
INSERT INTO fk VALUES (2)

statement error foreign key violation: values \[2\] in columns \[k\] referenced in table \"fk\"|delete on table "kv" violates foreign key constraint on table "fk"
statement error delete on table "kv" violates foreign key constraint "fk_f_ref_kv" on table "fk"\nDETAIL: Key \(k\)=\(2\) is still referenced from table "fk"
DELETE FROM kv WHERE k = 2 RETURNING NOTHING

statement ok
Expand Down Expand Up @@ -240,7 +240,7 @@ BEGIN
statement ok
DELETE FROM kv WHERE k = 1 RETURNING NOTHING

statement error foreign key violation: values \[2\] in columns \[k\] referenced in table \"fk\"|delete on table "kv" violates foreign key constraint on table "fk"
statement error delete on table "kv" violates foreign key constraint "fk_f_ref_kv" on table "fk"\nDETAIL: Key \(k\)=\(2\) is still referenced from table "fk"
DELETE FROM kv WHERE k = 2 RETURNING NOTHING

statement error current transaction is aborted, commands ignored until end of transaction block
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ enable_insert_fast_path on NULL NULL N
enable_zigzag_join on NULL NULL NULL string
experimental_enable_temp_tables off NULL NULL NULL string
experimental_force_split_at off NULL NULL NULL string
experimental_optimizer_foreign_keys off NULL NULL NULL string
experimental_optimizer_foreign_keys on NULL NULL NULL string
experimental_serial_normalization rowid NULL NULL NULL string
extra_float_digits 0 NULL NULL NULL string
force_savepoint_restart off NULL NULL NULL string
Expand Down Expand Up @@ -1623,7 +1623,7 @@ enable_insert_fast_path on NULL user NULL
enable_zigzag_join on NULL user NULL on on
experimental_enable_temp_tables off NULL user NULL off off
experimental_force_split_at off NULL user NULL off off
experimental_optimizer_foreign_keys off NULL user NULL off off
experimental_optimizer_foreign_keys on NULL user NULL on on
experimental_serial_normalization rowid NULL user NULL rowid rowid
extra_float_digits 0 NULL user NULL 0 2
force_savepoint_restart off NULL user NULL off off
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/show_source
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enable_insert_fast_path on
enable_zigzag_join on
experimental_enable_temp_tables off
experimental_force_split_at off
experimental_optimizer_foreign_keys off
experimental_optimizer_foreign_keys on
experimental_serial_normalization rowid
extra_float_digits 0
force_savepoint_restart off
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/explain_env
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ SET experimental_optimizer_foreign_keys = true
query T
EXPLAIN (OPT, ENV) SELECT * FROM y WHERE u = 3
----
https://cockroachdb.github.io/text/decode.html#eJxUj8tu2kAUhteZp_iVTewqDonYVCAWjjO005oxGk_ToCgaGTOmU4yNxhfZrPIQPCFPUlGI1C7P-b9z-Z61rUxZjBCU6caWSfrr6RG60-myMflKW9S6qtGeKUICQX1JIf3HkKKHQ64aMC4_g0cS_EcY3pKr9tI5V0HEYyl8xiWud9ZsE9tfYy7YzBcLfKcLOA38OHD_R7ONapXVmeowjQRlX_iZbV0IOqWC8oDG6JzkNMf4E31Br1plVh2c9mPf1J-xcPHPWae5ResSd0yIH0oqLh4nxbtds8xNeteD8W80kIilL1ksWRDj5vXtZkxITCWsLu1KW_W7NEWlcrM1NSZ4uL-_5LpIlrlWe7PeJ-u_FCYos-wj7nbamq0u6iRX5a42W7PXVmWl1WZdqI3uqxNfjAmhL_PQZxxONJe3oPzZRUzD02efMBXRDD1-fqWCosEEwzHxPM8jVZoU6AmOh8Px8H48vCMti6q2iSnqEQYPI7wOhvAwGL6RPwEAAP__x3qYDA==
https://cockroachdb.github.io/text/decode.html#eJxUj81O20AUhdfMUxyxwa5iAsqmSpTFYG7aaZ1xNJ5SIoQsx5nQKcFG4x_FrHgIP2GepEqTSmV57_3uOfrujKtsWYwRlvmzK7P81-0NzM7kq8Zu18ahNlWN9kgxFirimqD5TUTo4LGzBkLqz5CxhvwRRQN21p42xymMZaIVF1Lj_NXZl8x151goMedqie-0hNeAJ6H_Ed08p23qzCbdYRYrEl_kkW19KJqRIhlSgp2XHf6EvKV7dGmb2vUOXvsvb8bnIlr-V-s1A7Q-8yeM8UiTOnkcFC9fm9XW5pcdhPxGoUaiuRaJFmGCi4fHiwljCWk4U7q1cenv0hZVurUvtsYU11dXp7spstXWpG_26S17-kthinKzmTBG94uICwkvXugBSN75SCg6VH3CTMVzdPj5lRShwRSjCQuCIGBVnhXoGPZ9v-_f9_078rKoapfZoh5jeD3Gw3CEAMPRI_sTAAD___ayhqM=

statement ok
RESET reorder_joins_limit
Expand Down

0 comments on commit 605d8ef

Please sign in to comment.