citus icon indicating copy to clipboard operation
citus copied to clipboard

Citus local tables is broken with SET DEFAULT on foreign keys

Open onderkalaci opened this issue 3 years ago • 0 comments

The root of the problem is that we do not create DEFAULT values on the shards


CREATE TABLE PKTABLE (tid int, id int, PRIMARY KEY (tid, id));

CREATE TABLE FKTABLE (
  tid int,
  fk_id_del_set_default int DEFAULT 0,
  FOREIGN KEY (tid, fk_id_del_set_default) REFERENCES PKTABLE ON DELETE SET DEFAULT
);

SELECT citus_add_local_table_to_metadata('PKTABLE', true);


Such that the shard DOES NOT have the default value:

\d fktable
                       Table "public.fktable"
┌───────────────────────┬─────────┬───────────┬──────────┬─────────┐
│        Column         │  Type   │ Collation │ Nullable │ Default │
├───────────────────────┼─────────┼───────────┼──────────┼─────────┤
│ tid                   │ integer │           │          │         │
│ fk_id_del_set_default │ integer │           │          │ 0       │
└───────────────────────┴─────────┴───────────┴──────────┴─────────┘
Foreign-key constraints:
    "fktable_tid_fk_id_del_set_default_fkey" FOREIGN KEY (tid, fk_id_del_set_default) REFERENCES pktable(tid, id) ON DELETE SET DEFAULT

[local] onderkalaci@postgres:5432-90200=# \d fktable_102319 
                   Table "public.fktable_102319"
┌───────────────────────┬─────────┬───────────┬──────────┬─────────┐
│        Column         │  Type   │ Collation │ Nullable │ Default │
├───────────────────────┼─────────┼───────────┼──────────┼─────────┤
│ tid                   │ integer │           │          │         │
│ fk_id_del_set_default │ integer │           │          │         │
└───────────────────────┴─────────┴───────────┴──────────┴─────────┘
Foreign-key constraints:
    "fktable_tid_fk_id_del_set_default_fkey_102319" FOREIGN KEY (tid, fk_id_del_set_default) REFERENCES pktable_102318(tid, id) ON DELETE SET DEFAULT

As a result of this, the ON DELETE SET DEFAUL is broken:

INSERT INTO PKTABLE VALUES (1,1);
INSERT INTO FKTABLE VALUES
  (1, 1);
DELETE FROM PKTABLE WHERE id = 1;
SELECT * FROM FKTABLE ORDER BY id;

-- OPPPS: DEFAULT is ignored
table fktable ;
┌─────┬───────────────────────┐
│ tid │ fk_id_del_set_default │
├─────┼───────────────────────┤
│     │                       │
└─────┴───────────────────────┘
(1 row)

Should have been:

table fktable ;
┌─────┬───────────────────────┐
│ tid │ fk_id_del_set_default │
├─────┼───────────────────────┤
│     │                     0 │
└─────┴───────────────────────┘
(1 row)


onderkalaci avatar Sep 12 '22 09:09 onderkalaci