Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 699ff30

Browse files
committed
Fix rm_table command
1 parent 42057f9 commit 699ff30

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

pg_shardman--1.0.sql

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ CREATE TABLE tables (
3636
CREATE TABLE partitions (
3737
part_name text PRIMARY KEY,
3838
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
4040
);
4141

4242
-- Partition replicas
4343
CREATE TABLE replicas (
4444
part_name text NOT NULL REFERENCES partitions(part_name) ON DELETE CASCADE,
4545
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,
4747
PRIMARY KEY (part_name,node_id)
4848
);
4949

@@ -581,29 +581,41 @@ BEGIN
581581
END
582582
$$ LANGUAGE plpgsql;
583583

584-
-- Remove table from all nodes. All table partitions are removed, but replicas
585-
-- and logical stuff not.
584+
-- Remove table from all nodes.
586585
CREATE FUNCTION rm_table(rel regclass)
587586
RETURNS void AS $$
588587
DECLARE
589588
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 = '';
592593
BEGIN
593594
IF shardman.redirect_to_shardlord(format('rm_table(%L)', rel_name))
594595
THEN
595596
RETURN;
596597
END IF;
597598

598599
-- Drop table at all nodes
599-
FOR node IN SELECT * FROM shardman.nodes
600+
FOR node_id IN SELECT id FROM shardman.nodes
600601
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;
603611
END LOOP;
604612

605613
-- 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;
607619
END
608620
$$ LANGUAGE plpgsql;
609621

0 commit comments

Comments
 (0)