citusdata / citusdata/citus

mark_tables_colocated may seperate foreign referenced tables into different colocation groups

Open
#1,094 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
12.8k
Forks
794
Avg merge
2d 14h
Merged PRs (30d)
31

Description

If a foreign key is defined to or from a table, we should not allow changing colocation group of this table. Currently we do not have such check.

To replicate the problem;

```sql
set citus.shard_replication_factor to 1;

create table g1t1(c int PRIMARY KEY);
create table g1t2(c int, FOREIGN KEY(c) REFERENCES g1t1(c));
create table g2t1(c);

select create_distributed_table('g1t1', 'c');
select create_distributed_table('g1t2', 'c');
select create_distributed_table('g2t1', 'c', colocate_with => 'none');

select mark_tables_colocated('g2t1', ARRAY['g1t2']);

select logicalrelid, colocationid from pg_dist_partition;
logicalrelid | colocationid
--------------+--------------
g1t1 | 2
g2t1 | 3
g1t2 | 3
```

I have two options in my mind to solve this problem;
- If there is a foreign key relationship in one of the tables we want to change colocation group, error out;
This is easier to implement. We already implemented functions to find out whether a foreign key is defined over a table. However if user changes colocation group of both referenced and referencing tables together, this method will error out unnecessarily.

- If there is a foreign key relationship in one of the tables we want to change colocation group, first scan other tables we want to change colocation group. If that foreign key is defined between two tables we change colocation group, there is no need to error out.
This solution is more involved but it is still moderately easy to implement. We just need to iterate over some loops. This solution also fixes the problem in the previous option.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.