Skip to content

Commit c111e95

Browse files
committed
Respect foreign-key dependencies when copying databases
1 parent 14215e0 commit c111e95

5 files changed

Lines changed: 148 additions & 21 deletions

File tree

‎src/execution/operator/set/physical_union.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,15 @@ void PhysicalUnion::BuildPipelines(Pipeline &current, MetaPipeline &meta_pipelin
8383
auto dependency_type =
8484
order_matters ? PipelineDependencyType::REQUIRED : PipelineDependencyType::OPTIONAL_DEPENDENCY;
8585
dependencies = meta_pipeline.AddDependenciesFrom(union_pipeline, union_pipeline, false, dependency_type);
86+
if (!allow_out_of_order) {
87+
// CreateUnionPipeline already orders this pipeline after current. Include current when
88+
// propagating that ordering to the child sink pipelines as well.
89+
dependencies.push_back(current.shared_from_this());
90+
}
8691
// we also add dependencies if the LHS child can saturate all available threads
8792
// in that case, we recursively make all RHS children depend on the LHS.
8893
// This prevents breadth-first plan evaluation
89-
if (can_saturate_threads) {
94+
if (!allow_out_of_order || can_saturate_threads) {
9095
last_child_ptr = meta_pipeline.GetLastChild();
9196
}
9297
}
@@ -96,8 +101,10 @@ void PhysicalUnion::BuildPipelines(Pipeline &current, MetaPipeline &meta_pipelin
96101
children[i].get().BuildPipelines(union_pipeline, meta_pipeline);
97102

98103
if (last_child_ptr) {
99-
// the pointer was set, set up the dependencies
100-
meta_pipeline.AddRecursiveDependencies(dependencies, *last_child_ptr);
104+
// Explicitly ordered unions also order their children's sink pipelines.
105+
auto dependency_mode =
106+
!allow_out_of_order ? RecursiveDependencyMode::FORCE : RecursiveDependencyMode::RESPECT_PARALLELISM;
107+
meta_pipeline.AddRecursiveDependencies(dependencies, *last_child_ptr, dependency_mode);
101108
}
102109
}
103110
}

‎src/planner/binder/statement/bind_copy_database.cpp‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ unique_ptr<LogicalOperator> Binder::BindCopyDatabaseSchema(Catalog &from_databas
5151

5252
unique_ptr<LogicalOperator> Binder::BindCopyDatabaseData(Catalog &source_catalog,
5353
const Identifier &target_database_name) {
54-
auto source_schemas = source_catalog.GetSchemas(context);
55-
56-
// We can just use ExtractEntries here because the order doesn't matter
57-
ExportEntries entries;
58-
PhysicalExport::ExtractEntries(context, source_schemas, entries);
54+
auto entries = PhysicalExport::GetNaiveExportOrder(context, source_catalog);
5955

6056
vector<unique_ptr<LogicalOperator>> insert_nodes;
61-
for (auto &table_ref : entries.tables) {
57+
for (auto &table_ref : entries) {
58+
if (table_ref.get().type != CatalogType::TABLE_ENTRY) {
59+
continue;
60+
}
6261
auto &table = table_ref.get().Cast<TableCatalogEntry>();
6362
// generate the insert statement
6463
InsertStatement insert_stmt;

‎src/planner/binder/statement/bind_export.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ string SanitizeExportIdentifier(const Identifier &str) {
4646
return result;
4747
}
4848

49-
bool ReferencedTableIsOrdered(const Identifier &referenced_table, catalog_entry_vector_t &ordered) {
49+
bool ReferencedTableIsOrdered(const TableCatalogEntry &referencing_table, const Identifier &referenced_table,
50+
catalog_entry_vector_t &ordered) {
5051
for (auto &entry : ordered) {
5152
auto &table_entry = entry.get().Cast<TableCatalogEntry>();
52-
if (table_entry.name == referenced_table) {
53+
if (&table_entry.ParentSchema() == &referencing_table.ParentSchema() && table_entry.name == referenced_table) {
5354
// The referenced table is already ordered
5455
return true;
5556
}
@@ -79,7 +80,7 @@ void ScanForeignKeyTable(catalog_entry_vector_t &ordered, catalog_entry_vector_t
7980
// This table references a table, don't move it yet
8081
move_to_ordered = false;
8182
break;
82-
} else if (!ReferencedTableIsOrdered(fk.info.table, ordered)) {
83+
} else if (!ReferencedTableIsOrdered(table_entry, fk.info.table, ordered)) {
8384
// The table that it references isn't ordered yet
8485
move_to_ordered = false;
8586
break;

‎test/sql/copy_database/copy_database_fk.test‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
# description: Test COPY DATABASE with foreign key constraint
33
# group: [copy_database]
44

5-
# FIXME - this is not working right now - we need to correctly take dependencies into account for COPY FROM DATABASE
6-
mode skip instable
5+
statement ok
6+
SET threads = 4
7+
8+
statement ok
9+
ATTACH ':memory:' AS source
10+
11+
statement ok
12+
USE source
713

814
statement ok
915
CREATE TABLE pk_integers(i INTEGER PRIMARY KEY)
@@ -17,12 +23,25 @@ INSERT INTO pk_integers VALUES (1), (2), (3)
1723
statement ok
1824
INSERT INTO fk_integers VALUES (1), (2)
1925

20-
# now export the db
26+
# Copy the schema and data.
2127
statement ok
2228
ATTACH ':memory:' AS db1
2329

2430
statement ok
25-
COPY FROM DATABASE memory TO db1
31+
COPY FROM DATABASE source TO db1
32+
33+
query I
34+
SELECT i FROM db1.pk_integers ORDER BY i
35+
----
36+
1
37+
2
38+
3
39+
40+
query I
41+
SELECT j FROM db1.fk_integers ORDER BY j
42+
----
43+
1
44+
2
2645

2746
statement ok
2847
USE db1
@@ -31,13 +50,15 @@ USE db1
3150
statement error
3251
INSERT INTO fk_integers VALUES (4)
3352
----
53+
Violates foreign key constraint
3454

3555
statement ok
3656
INSERT INTO fk_integers VALUES (3)
3757

3858
statement error
3959
DELETE FROM pk_integers WHERE i=3
4060
----
61+
Violates foreign key constraint
4162

4263
statement ok
4364
DELETE FROM fk_integers WHERE j=3
@@ -48,18 +69,17 @@ DELETE FROM pk_integers WHERE i=3
4869
statement error
4970
UPDATE pk_integers SET i=5 WHERE i=2
5071
----
72+
Violates foreign key constraint
5173

5274
statement error
53-
UPDATE fk_integers SET i=4 WHERE j=2
54-
----
55-
56-
statement error
57-
UPDATE fk_integers SET i=4 WHERE j=2
75+
UPDATE fk_integers SET j=4 WHERE j=2
5876
----
77+
Violates foreign key constraint
5978

6079
statement error
6180
DROP TABLE pk_integers;
6281
----
82+
this table is main key table of the table "fk_integers"
6383

6484
statement ok
6585
DROP TABLE fk_integers;
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# name: test/sql/copy_database/copy_database_fk_order.test
2+
# description: Copy foreign-key dependency chains across schemas in dependency order
3+
# group: [copy_database]
4+
5+
foreach preserve_order true false
6+
7+
foreach threads 1 4
8+
9+
statement ok
10+
SET preserve_insertion_order = {preserve_order}
11+
12+
statement ok
13+
SET threads = {threads}
14+
15+
statement ok
16+
ATTACH ':memory:' AS source
17+
18+
statement ok
19+
ATTACH ':memory:' AS target
20+
21+
statement ok
22+
CREATE SCHEMA source.first
23+
24+
statement ok
25+
CREATE SCHEMA source.second
26+
27+
# A same-named table in another schema cannot satisfy the dependency on second.parent.
28+
statement ok
29+
CREATE TABLE source.first.parent(i INTEGER PRIMARY KEY)
30+
31+
statement ok
32+
CREATE TABLE source.second.root(i INTEGER PRIMARY KEY)
33+
34+
statement ok
35+
CREATE TABLE source.second.parent(i INTEGER PRIMARY KEY REFERENCES source.second.root(i))
36+
37+
statement ok
38+
CREATE TABLE source.second.child(i INTEGER REFERENCES source.second.parent(i))
39+
40+
statement ok
41+
INSERT INTO source.first.parent VALUES (42)
42+
43+
statement ok
44+
INSERT INTO source.second.root SELECT i FROM range(10000) t(i)
45+
46+
statement ok
47+
INSERT INTO source.second.parent SELECT i FROM range(10000) t(i)
48+
49+
statement ok
50+
INSERT INTO source.second.child SELECT i FROM range(10000) t(i)
51+
52+
statement ok
53+
INSERT INTO source.second.child VALUES (NULL)
54+
55+
# Splitting schema and data copy must preserve the same dependency order.
56+
statement ok
57+
COPY FROM DATABASE source TO target (SCHEMA)
58+
59+
statement ok
60+
COPY FROM DATABASE source TO target (DATA)
61+
62+
query I
63+
SELECT i FROM target.first.parent
64+
----
65+
42
66+
67+
query II
68+
SELECT count(*), sum(i) FROM target.second.root
69+
----
70+
10000 49995000
71+
72+
query II
73+
SELECT count(*), sum(i) FROM target.second.parent
74+
----
75+
10000 49995000
76+
77+
query II
78+
SELECT count(*), sum(i) FROM target.second.child
79+
----
80+
10001 49995000
81+
82+
statement error
83+
INSERT INTO target.second.child VALUES (10000)
84+
----
85+
Violates foreign key constraint
86+
87+
statement error
88+
DELETE FROM target.second.parent WHERE i = 42
89+
----
90+
Violates foreign key constraint
91+
92+
statement ok
93+
DETACH target
94+
95+
statement ok
96+
DETACH source
97+
98+
endloop
99+
100+
endloop

0 commit comments

Comments
 (0)