citusdata / citusdata/citus

( multi_alter_table_add_foreign_key_without_name.sql) With citus_set_coordinator_host, foreign key constraint can be created when one of the tables is not a citus table.

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

Description

Repro steps (derived from multi_alter_table_add_foreign_key_without_name.sql):

1. Create a cluster
` citus_dev make test --size=2
`
2. Note that coordinator is in metadata by checking
`select * from pg_dist_node;`

3.
```
CREATE TABLE referenced_local_table(id int PRIMARY KEY, other_column int);
CREATE TABLE reference_table(id int, referencing_column int);
SELECT create_reference_table('reference_table');
create_reference_table
------------------------

(1 row)

ALTER TABLE reference_table ADD FOREIGN KEY (referencing_column) REFERENCES referenced_local_table(id);
NOTICE: local tables that are added to metadata automatically by citus, but not chained with reference tables via foreign keys might be automatically converted back to postgres tables
HINT: Executing citus_add_local_table_to_metadata($$public.referenced_local_table$$) prevents this for the given relation, and all of the connected relations
ALTER TABLE

```

This scenario is expected to fail to create foreign key.

If the coordinator is removed, it fails as expected.

```
SELECT master_remove_node('localhost', 9700);
master_remove_node
--------------------

(1 row)

CREATE TABLE referenced_local_table(id int PRIMARY KEY, other_column int);
CREATE TABLE reference_table(id int, referencing_column int);
SELECT create_reference_table('reference_table');
CREATE TABLE
CREATE TABLE
create_reference_table
------------------------

(1 row)

ALTER TABLE reference_table ADD FOREIGN KEY (referencing_column) REFERENCES referenced_local_table(id);
ERROR: referenced table "referenced_local_table" must be a distributed table or a reference table
DETAIL: To enforce foreign keys, the referencing and referenced rows need to be stored on the same node.
HINT: You could use SELECT create_reference_table('referenced_local_table') to replicate the referenced table to all nodes or consider dropping the foreign key

```

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.