citusdata / citusdata/citus

Low-level foreign key APIs ignore inherited foreign keys, creating some edge cases

Open
#5,351 0 comments 1 reaction 0 assignees View on GitHub
bug dev_reported
Dominant language
C
Stars
12.8k
Forks
794
Avg merge
2d 14h
Merged PRs (30d)
31

Description

For various reasons, Citus only checks for non-inherited foreign keys, such as [`GetForeignKeyOid()`](https://github.com/citusdata/citus/blob/52879fdc96bf97aa5cdeea276eab2969f19cbef4/src/backend/distributed/commands/foreign_constraint.c#L1041-L1052).

It mostly works fine because almost all operations need to involve the parent table anyway. However, certain operations, like `alter_table_set_access_method` does not require the parent to be accessed:

```SQL
CREATE TABLE parent(a int) PARTITION BY RANGE (a);
CREATE TABLE child PARTITION OF parent FOR VALUES FROM (3) TO (5);
SELECT create_distributed_table('parent', 'a');

CREATE TABLE ref (a int PRIMARY KEY);
SELECT create_reference_table('ref');

alter table parent add constraint fkey FOREIGN KEY (a) REFERENCES ref(a);

select alter_table_set_access_method('child', 'columnar');
NOTICE: creating a new table for public.child
NOTICE: moving the data of public.child
NOTICE: dropping the old public.child
NOTICE: renaming the new table to public.child
ERROR: Foreign keys and AFTER ROW triggers are not supported for columnar tables
HINT: Consider an AFTER STATEMENT trigger instead.
CONTEXT: SQL statement "ALTER TABLE public.parent ATTACH PARTITION public.child FOR VALUES FROM (3) TO (5);"
Time: 264.079 ms
```

However, it should have been like below, where we throw the error properly:

```SQL
select alter_table_set_access_method('child', 'columnar');

ERROR: cannot complete operation because table child has a foreign key
Time: 1.338 ms
```

Note: Foreign keys to reference table relies on a different code-path, so all works fine in that front

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.