Skip to content

sql: renaming tables into a different parent database allows creating prohibited cross-database references #55709

@jayshrivastava

Description

@jayshrivastava

Describe the problem

Even with sql.cross_db_fks.enabled=false, one can move a table to another database such that there is a cross-database foreign key relation.

To Reproduce

root@:26257/otherdb> show cluster setting sql.cross_db_fks.enabled;
  sql.cross_db_fks.enabled
----------------------------
           false
(1 row)

Time: 0ms total (execution 0ms / network 0ms)

root@:26257/defaultdb> create database otherdb;
CREATE DATABASE

Time: 81ms total (execution 81ms / network 0ms)

root@:26257/defaultdb> create table t1(i int PRIMARY KEY); create table t2(j int, constraint t1_reference foreign key(j) references t1(i));
CREATE TABLE

Note: timings for multiple statements on a single line are not supported. See https://go.crdb.dev/issue-v/48180/v20.2.

root@:26257/defaultdb> alter table t2 rename to otherdb.public.t2;
NOTICE: renaming tables with a qualification is deprecated
HINT: use ALTER TABLE t2 RENAME TO t2 instead
RENAME TABLE

Time: 296ms total (execution 93ms / network 203ms)

root@:26257/defaultdb> use otherdb;
SET

Time: 0ms total (execution 0ms / network 0ms)

root@:26257/otherdb> select * from information_schema.tables where table_name = 't2';
  table_catalog | table_schema | table_name | table_type | is_insertable_into | version
----------------+--------------+------------+------------+--------------------+----------
  otherdb       | public       | t2         | BASE TABLE | YES                |       4
(1 row)

Time: 5ms total (execution 4ms / network 0ms)

Expected behavior
This should only work if sql.cross_db_fks.enabled is set to true.

Environment:

  • CockroachDB version [e.g. 20.2]

Epic: CRDB-1519

Metadata

Metadata

Assignees

Labels

C-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions