This example shows how you convert two tables with standard sequences used as primary keys and a parent-child relationship by a foreign key constraint to use the MMR-ready sequence, You can then use them in a multi-master replication system.
The tables are defined as follows:
CREATE TABLE MMR_seq_tbl ( id SERIAL PRIMARY KEY, field VARCHAR2(20) ); CREATE TABLE MMR_seq_child_tbl ( id SERIAL PRIMARY KEY, field VARCHAR2(20), parent_id INTEGER CONSTRAINT MMR_seq_tbl_fk REFERENCES MMR_seq_tbl(id) );
Note
Observe the foreign key constraint between columns MMR_seq_child_tbl.parent_id and MMR_seq_tbl.id.
The tables are populated with an initial set of rows:
INSERT INTO MMR_seq_tbl (field) VALUES ('MMRnode_a - Row 1'); INSERT INTO MMR_seq_tbl (field) VALUES ('MMRnode_a - Row 2'); INSERT INTO MMR_seq_tbl (field) VALUES ('MMRnode_a - Row 3'); INSERT INTO MMR_seq_child_tbl (field, parent_id) VALUES ('MMRnode_a - Row 1-1', 1); INSERT INTO MMR_seq_child_tbl (field, parent_id) VALUES ('MMRnode_a - Row 1-2', 1); INSERT INTO MMR_seq_child_tbl (field, parent_id) VALUES ('MMRnode_a - Row 2-1', 2); INSERT INTO MMR_seq_child_tbl (field, parent_id) VALUES ('MMRnode_a - Row 2-2', 2); INSERT INTO MMR_seq_child_tbl (field, parent_id) VALUES ('MMRnode_a - Row 3-1', 3); INSERT INTO MMR_seq_child_tbl (field, parent_id) VALUES ('MMRnode_a - Row 3-2', 3);
The resulting table content is the following:
edb=# SELECT * FROM MMR_seq_tbl;
id | field ----+------------------- 1 | MMRnode_a - Row 1 2 | MMRnode_a - Row 2 3 | MMRnode_a - Row 3 (3 rows)
edb=# SELECT * FROM MMR_seq_child_tbl;
id | field | parent_id ----+---------------------+----------- 1 | MMRnode_a - Row 1-1 | 1 2 | MMRnode_a - Row 1-2 | 1 3 | MMRnode_a - Row 2-1 | 2 4 | MMRnode_a - Row 2-2 | 2 5 | MMRnode_a - Row 3-1 | 3 6 | MMRnode_a - Row 3-2 | 3 (6 rows)
Prior to performing the conversion, obtain the current, maximum sequence value of the sequence to convert to an MMR-ready sequence. In this example, the value is 6, as seen in the id column of table MMR_seq_child_tbl.
This value is needed to set a newly created sequence to use for the MMR-ready sequence. This value must be a large enough starting value to avoid auniqu eness conflict with the converted sequence values of the existing rows.
Converting existing standard sequence values
To convert the existing sequence values in columns MMR_seq_tbl.id, MMR_seq_child_tbl.id, and MMR_seq_child_tbl.parent_id:
Permit deferred updates to the foreign key constraint.
ALTER TABLE MMR_seq_child_tbl ALTER CONSTRAINT MMR_seq_tbl_fk DEFERRABLE INITIALLY DEFERRED;
Create the function to perform the sequence conversion.
CREATE OR REPLACE FUNCTION MMR_sequence_convert ( old_seq_value bigint ) RETURNS bigint LANGUAGE sql AS $function$ SELECT ( (SELECT current_setting('cluster.unique_db_id'))::bigint << 52)::bigint + $1; $function$;
Change the sequence columns to data type BIGINT so they are large enough for the MMR-ready sequence.
ALTER TABLE MMR_seq_tbl ALTER COLUMN id SET DATA TYPE BIGINT; ALTER TABLE MMR_seq_child_tbl ALTER COLUMN id SET DATA TYPE BIGINT; ALTER TABLE MMR_seq_child_tbl ALTER COLUMN parent_id SET DATA TYPE BIGINT;
Set the unique database identifier used by the MMR-ready sequence.