@@ -84,3 +84,40 @@ LANGUAGE C;
84
84
85
85
CREATE TABLE IF NOT EXISTSmtm .local_tables (rel_schematext , rel_nametext ,primary key (rel_schema, rel_name));
86
86
87
+ CREATE OR REPLACE FUNCTION mtm .alter_sequences() RETURNSboolean AS
88
+ $$
89
+ DECLARE
90
+ seq_class pg_class%rowtype;
91
+ seq_tuple record;
92
+ node_idint ;
93
+ max_nodesint ;
94
+ new_startint ;
95
+ alteredboolean := false;
96
+ BEGIN
97
+ select current_setting(' multimaster.max_nodes' ) into max_nodes;
98
+ select id," allNodes" into node_idfrom mtm .get_cluster_state ();
99
+ FOR seq_classIN
100
+ SELECT * FROM pg_classWHERE pg_class .relkind = ' S'
101
+ LOOP
102
+ EXECUTE' select * from' || seq_class .relname || ' ;' INTO seq_tuple;
103
+ IFseq_tuple .increment_by != max_nodes THEN
104
+ altered := true;
105
+ RAISE NOTICE' Altering step for sequence % to %.' ,seq_tuple .sequence_name , max_nodes;
106
+ EXECUTE' ALTER SEQUENCE' || seq_class .relname || ' INCREMENT BY' || max_nodes|| ' ;' ;
107
+ END IF;
108
+ IF (seq_tuple .last_value % max_nodes)!= node_id THEN
109
+ altered := true;
110
+ new_start := (seq_tuple .last_value / max_nodes+ 1 )* max_nodes+ node_id;
111
+ RAISE NOTICE' Altering start for sequence % to %.' ,seq_tuple .sequence_name , new_start;
112
+ EXECUTE' ALTER SEQUENCE' || seq_class .relname || ' RESTART WITH' || new_start|| ' ;' ;
113
+ END IF;
114
+ END LOOP;
115
+ IF altered= false THEN
116
+ RAISE NOTICE' All found sequnces have proper params.' ;
117
+ END IF;
118
+ RETURN true;
119
+ END
120
+ $$
121
+ LANGUAGE plpgsql;
122
+
123
+ select mtm .alter_sequences ();