@@ -34,6 +34,7 @@ CREATE TYPE cmd_status AS ENUM ('waiting', 'canceled', 'failed', 'in progress',
34
34
CREATE TABLE cmd_log (
35
35
id bigserial PRIMARY KEY ,
36
36
cmd_type cmd NOT NULL ,
37
+ cmd_opts TEXT [],
37
38
status cmd_status DEFAULT ' waiting' NOT NULL
38
39
);
39
40
@@ -48,25 +49,17 @@ CREATE TRIGGER cmd_log_inserts
48
49
AFTER INSERT ON cmd_log
49
50
FOR EACH STATEMENT EXECUTE PROCEDURE notify_shardlord();
50
51
51
- -- probably better to keep opts in an array field, but working with arrays from
52
- -- libpq is not very handy
53
- -- opts must be inserted sequentially, we order by them by id
54
- CREATE TABLE cmd_opts (
55
- id bigserial PRIMARY KEY ,
56
- cmd_id bigint REFERENCES cmd_log(id),
57
- opt text
58
- );
59
-
60
52
-- Interface functions
61
53
62
54
-- Add a node. Its state will be reset, all shardman data lost.
63
55
CREATE FUNCTION add_node (connstring text ) RETURNS int AS $$
64
56
DECLARE
65
57
c_id int ;
66
58
BEGIN
67
- INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, ' add_node' )
68
- RETURNING id INTO c_id;
69
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, connstring);
59
+ INSERT INTO @extschema@.cmd_log
60
+ VALUES (DEFAULT, ' add_node' , ARRAY[connstring])
61
+ RETURNING id INTO c_id;
62
+
70
63
RETURN c_id;
71
64
END
72
65
$$ LANGUAGE plpgsql;
@@ -76,12 +69,15 @@ CREATE FUNCTION rm_node(node_id int, force bool default false) RETURNS int AS $$
76
69
DECLARE
77
70
c_id int ;
78
71
BEGIN
79
- INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, ' rm_node' )
80
- RETURNING id INTO c_id;
81
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, node_id);
82
- IF force THEN
83
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, ' force' );
84
- END IF;
72
+ INSERT INTO @extschema@.cmd_log
73
+ VALUES (DEFAULT,
74
+ ' rm_node' ,
75
+ CASE force
76
+ WHEN true THEN ARRAY[node_id::text , ' force' ]
77
+ ELSE ARRAY[node_id::text ]
78
+ END)
79
+ RETURNING id INTO c_id;
80
+
85
81
RETURN c_id;
86
82
END
87
83
$$ LANGUAGE plpgsql;
@@ -95,12 +91,12 @@ CREATE FUNCTION create_hash_partitions(
95
91
DECLARE
96
92
c_id int ;
97
93
BEGIN
98
- INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, ' create_hash_partitions ' )
99
- RETURNING id INTO c_id;
100
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, node_id);
101
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, relation);
102
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, expr) ;
103
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, partitions_count);
94
+ INSERT INTO @extschema@.cmd_log
95
+ VALUES (DEFAULT,
96
+ ' create_hash_partitions ' ,
97
+ ARRAY[node_id:: text , relation, expr, partitions_count:: text ])
98
+ RETURNING id INTO c_id;
99
+
104
100
IF rebalance THEN
105
101
PERFORM shardman .rebalance (relation);
106
102
END IF;
@@ -117,11 +113,10 @@ CREATE FUNCTION move_part(part_name text, dest int, src int DEFAULT NULL)
117
113
DECLARE
118
114
c_id int ;
119
115
BEGIN
120
- INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, ' move_part' )
121
- RETURNING id INTO c_id;
122
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, part_name);
123
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, src);
124
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, dest);
116
+ INSERT INTO @extschema@.cmd_log
117
+ VALUES (DEFAULT, ' move_part' , ARRAY[part_name, src::text , dest::text ])
118
+ RETURNING id INTO c_id;
119
+
125
120
RETURN c_id;
126
121
END $$ LANGUAGE plpgsql;
127
122
@@ -132,10 +127,10 @@ CREATE FUNCTION create_replica(part_name text, dest int) RETURNS int AS $$
132
127
DECLARE
133
128
c_id int ;
134
129
BEGIN
135
- INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, ' create_replica ' )
136
- RETURNING id INTO c_id;
137
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, part_name) ;
138
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, dest);
130
+ INSERT INTO @extschema@.cmd_log
131
+ VALUES (DEFAULT, ' create_replica ' , ARRAY[part_name, dest:: text ])
132
+ RETURNING id INTO c_id;
133
+
139
134
RETURN c_id;
140
135
END $$ LANGUAGE plpgsql;
141
136
@@ -144,9 +139,10 @@ CREATE FUNCTION rebalance(relation text) RETURNS int AS $$
144
139
DECLARE
145
140
c_id int ;
146
141
BEGIN
147
- INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, ' rebalance' )
148
- RETURNING id INTO c_id;
149
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, relation);
142
+ INSERT INTO @extschema@.cmd_log
143
+ VALUES (DEFAULT, ' rebalance' , ARRAY[relation])
144
+ RETURNING id INTO c_id;
145
+
150
146
RETURN c_id;
151
147
END $$ LANGUAGE plpgsql;
152
148
@@ -157,10 +153,10 @@ CREATE FUNCTION set_replevel(relation text, replevel int) RETURNS int AS $$
157
153
DECLARE
158
154
c_id int ;
159
155
BEGIN
160
- INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, ' set_replevel ' )
161
- RETURNING id INTO c_id;
162
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, relation) ;
163
- INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, replevel);
156
+ INSERT INTO @extschema@.cmd_log
157
+ VALUES (DEFAULT, ' set_replevel ' , ARRAY[relation, replevel:: text ])
158
+ RETURNING id INTO c_id;
159
+
164
160
RETURN c_id;
165
161
END $$ LANGUAGE plpgsql STRICT;
166
162
0 commit comments