Skip to content

Two-node active-active lab with pgEdge

Start two local Mimus nodes with Spock 5.0.15, establish bidirectional replication, and verify delta_apply conflict merging.

Initialize two writable nodes

With Spock 5.0.15 installed under /usr/mimus-19, initialize two local instances on ports 5541 (node_a) and 5542 (node_b) with logical WAL and commit timestamps enabled:

for p in 5541 5542; do
  initdb -D "/tmp/mimus-${p}" --auth-local=trust --auth-host=trust
  cat >> "/tmp/mimus-${p}/postgresql.conf" <<EOF
port = ${p}
wal_level = logical
track_commit_timestamp = on
output_plugin_libraries = 'pgoutput,spock_output'
max_worker_processes = 16
max_replication_slots = 10
max_wal_senders = 10
shared_preload_libraries = 'spock'
spock.conflict_resolution = 'last_update_wins'
spock.save_resolutions = on
spock.enable_ddl_replication = off
EOF
  pg_ctl -D "/tmp/mimus-${p}" -l "/tmp/mimus-${p}.log" start
  createdb -p "${p}" lab
done

Create nodes, tables, and bidirectional subscriptions

Create the same table with delta_apply enabled on both nodes, register each Spock node, and wire up subscriptions in both directions:

-- Run on port 5541 (node_a)
CREATE EXTENSION spock;
SELECT spock.node_create('node_a', 'host=127.0.0.1 port=5541 dbname=lab');

CREATE TABLE public.accounts (
    id      integer PRIMARY KEY,
    balance integer NOT NULL
);
ALTER TABLE public.accounts ALTER COLUMN balance
    SET (log_old_value = true, delta_apply_function = spock.delta_apply);
INSERT INTO public.accounts VALUES (1, 100);
SELECT spock.repset_add_table('default', 'public.accounts');
-- Run on port 5542 (node_b)
CREATE EXTENSION spock;
SELECT spock.node_create('node_b', 'host=127.0.0.1 port=5542 dbname=lab');

CREATE TABLE public.accounts (
    id      integer PRIMARY KEY,
    balance integer NOT NULL
);
ALTER TABLE public.accounts ALTER COLUMN balance
    SET (log_old_value = true, delta_apply_function = spock.delta_apply);
SELECT spock.repset_add_table('default', 'public.accounts');

SELECT spock.sub_create(
    'sub_from_a', 'host=127.0.0.1 port=5541 dbname=lab',
    replication_sets := ARRAY['default'],
    synchronize_structure := false,
    synchronize_data := true, forward_origins := '{}'
);
SELECT spock.sub_wait_for_sync('sub_from_a');

Then add the reverse subscription on node_a (5541) with synchronize_data := false:

SELECT spock.sub_create(
    'sub_from_b', 'host=127.0.0.1 port=5542 dbname=lab',
    replication_sets := ARRAY['default'],
    synchronize_structure := false,
    synchronize_data := false, forward_origins := '{}'
);
SELECT spock.sub_wait_for_sync('sub_from_b');

Verify concurrent delta updates

Apply independent balance adjustments on each node (+30 on node_a, -15 on node_b) and verify that both converge to 115:

psql -X -p 5541 -d lab -c "UPDATE public.accounts SET balance = balance + 30 WHERE id = 1;"
psql -X -p 5542 -d lab -c "UPDATE public.accounts SET balance = balance - 15 WHERE id = 1;"
sleep 1
psql -X -p 5541 -d lab -c "SELECT 'node_a' AS node, id, balance FROM public.accounts;"
psql -X -p 5542 -d lab -c "SELECT 'node_b' AS node, id, balance FROM public.accounts;"

For production conflict arbitration, monitoring, and standby failover rules, consult the pgEdge manual.

Scan to open this page
https://mimus.pgsty.com/docs/tutorials/active-active-nodes/