@@ -36,14 +36,14 @@ CREATE TABLE tables (
36
36
CREATE TABLE partitions (
37
37
part_name text PRIMARY KEY ,
38
38
node_id int NOT NULL REFERENCES nodes(id) ON DELETE CASCADE , -- node on which partition lies
39
- relation text NOT NULL
39
+ relation text NOT NULL REFERENCES tables(relation) ON DELETE CASCADE
40
40
);
41
41
42
42
-- Partition replicas
43
43
CREATE TABLE replicas (
44
44
part_name text NOT NULL REFERENCES partitions(part_name) ON DELETE CASCADE ,
45
45
node_id int NOT NULL REFERENCES nodes(id) ON DELETE CASCADE , -- node on which partition lies
46
- relation text NOT NULL ,
46
+ relation text NOT NULL REFERENCES tables(relation) ON DELETE CASCADE ,
47
47
PRIMARY KEY (part_name,node_id)
48
48
);
49
49
@@ -581,29 +581,41 @@ BEGIN
581
581
END
582
582
$$ LANGUAGE plpgsql;
583
583
584
- -- Remove table from all nodes. All table partitions are removed, but replicas
585
- -- and logical stuff not.
584
+ -- Remove table from all nodes.
586
585
CREATE FUNCTION rm_table (rel regclass)
587
586
RETURNS void AS $$
588
587
DECLARE
589
588
rel_name text = rel::text ;
590
- node shardman .nodes ;
591
- drops text = ' ' ;
589
+ node_id int ;
590
+ pname text ;
591
+ drop1 text = ' ' ;
592
+ drop2 text = ' ' ;
592
593
BEGIN
593
594
IF shardman .redirect_to_shardlord (format(' rm_table(%L)' , rel_name))
594
595
THEN
595
596
RETURN;
596
597
END IF;
597
598
598
599
-- Drop table at all nodes
599
- FOR node IN SELECT * FROM shardman .nodes
600
+ FOR node_id IN SELECT id FROM shardman .nodes
600
601
LOOP
601
- drops := format(' %s%s:DROP TABLE %I CASCADE;' ,
602
- drops, node .id , rel_name);
602
+ -- Drop parent table. It will also delete all its partitions.
603
+ drop1 := format(' %s%s:DROP TABLE %I CASCADE;' ,
604
+ drop1, node_id, rel_name);
605
+ -- Drop replicas and stub tables (which are replaced with foreign tables)
606
+ FOR pname IN SELECT part_name FROM shardman .partitions WHERE relation= rel_name
607
+ LOOP
608
+ drop2 := format(' %s%s:DROP TABLE IF EXISTS %I CASCADE;' ,
609
+ drop2, node_id, pname);
610
+ END LOOP;
603
611
END LOOP;
604
612
605
613
-- Broadcast drop table commands
606
- PERFORM shardman .broadcast (drops);
614
+ PERFORM shardman .broadcast (drop1);
615
+ PERFORM shardman .broadcast (drop2);
616
+
617
+ -- Update metadata
618
+ DELETE FROM shardman .tables WHERE relation= rel_name;
607
619
END
608
620
$$ LANGUAGE plpgsql;
609
621
0 commit comments