Skip to content

Commit 6425d69

Browse files
committed
alter_sequences() function
1 parent 74fa53c commit 6425d69

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

multimaster--1.0.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,40 @@ LANGUAGE C;
8484

8585
CREATE TABLE IF NOT EXISTS mtm.local_tables(rel_schema text, rel_name text, primary key(rel_schema, rel_name));
8686

87+
CREATE OR REPLACE FUNCTION mtm.alter_sequences() RETURNS boolean AS
88+
$$
89+
DECLARE
90+
seq_class pg_class%rowtype;
91+
seq_tuple record;
92+
node_id int;
93+
max_nodes int;
94+
new_start int;
95+
altered boolean := false;
96+
BEGIN
97+
select current_setting('multimaster.max_nodes') into max_nodes;
98+
select id, "allNodes" into node_id from mtm.get_cluster_state();
99+
FOR seq_class IN
100+
SELECT * FROM pg_class WHERE pg_class.relkind = 'S'
101+
LOOP
102+
EXECUTE 'select * from ' || seq_class.relname || ';' INTO seq_tuple;
103+
IF seq_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();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy