1

Thank you for help or suggestion offered.

I am trying to build my own multi-master replication on Postgresql 10 in Windows, for a situation which cannot use any of the current 3rd party tools for PG multimaster replication, which can also involve another DB platform in a subscriber group (Sybase ADS). I have the following logic to create bidirectional replication, partially inspired by Bucardo's logic, between 1 publisher and 2 subscribers:

  1. When INSERT, UPDATE, or DELETE is made on Source table, Source table Trigger adds row to created meta table on Source DB that will act as a replication transaction to be performed on the 2 subscriber DBs which subcribe to it.

  2. A NOTIFY signal will be sent to a service, or script written in Python or some scripting language will monitor for changes in the metatable or trigger execution and be able to do a table compare or script the statement to run on each subscriber database.

***I believe that triggers on the subscribers will need to be paused to keep them from pushing their received statements to their subscribers, i.e. if node A and node B both subscribe to each other's table A, then an update to node A's table A should replicate to node B's table A without then replicating back to table A in a bidirectional "ping-pong storm".

  1. There will be a final compare between tables and the transaction will be closed. Re-enable triggers on subscribers if they were paused/disabled when pushing transactions from step 2 addendum.

This will hopefully be able to be done bidirectionally, in order of timestamp, in FIFO order, unless I can figure out a to create child processes to run the synchronizations concurrently.

For this, I am trying to figure out the best way to setup the service logic---essentially Step 2 above, which has apparently been done using a daemon in Linux, but I have to work in Windows, making it run as, or resembling, a service/agent---or come up with a reasonably easy and efficient design to send the source DBs statements to the subscribers DBs.

Does anyone see that this plan is faulty or may not work?

1 Answer 1

0

Disclaimer: I don't know anything about Postgresql but have done plenty of custom replication.

The main problem with bidirectional replication is merge issues.

If the same key is used in both systems with different attributes, which one gets to push their change? If you nominate a master it's easier. Then the slave just gets overwritten every time.

How much latency can you handle? It's much easier to take the 'notify' part out and just have a five minute windows task scheduler job that inspects log tables and pushes data around.

In other words, this kind of pattern:

  1. Change occurs in a table. A database trigger on that table notes the change and writes the PK of the table to a change log table. A ReplicationBatch column in the log table is set to NULL by default

  2. A windows scheduled task inspects all change log tables to find all changes that happened since the last run and 'reserves' these records by setting their replication state to a replication batch number

i.e. you run a UPDATE LogTable Set ReplicationBatch=BatchNumber WHERE ReplicationState IS NULL

  1. All records that have been marked are replicated

you run a SELECT * FROM LogTable WHERE ReplicationState=RepID to get the records to be processed

  1. When complete, the reserved records are marked as complete so the next time around only subsequent changes are replicated. This completion flag might be in the log table or it might be in a ReplicaionBatch number table

The main point is that you need to reserve records for replication, so that as you are replicating them out, additional log records can be added in from the source without messing up the batch

Then periodically you clear out the log tables.

2
  • Thank you very much for a thorough and relatively "friendly" answer to a broad question by SO standards. For latency, it's usually pretty mild, maybe on average 30 row changes to a source table per day, then pushed out gradually in real time. sometimes a subscriber in a group falls offline for a few hours, which seems to be the biggest quirk, but I'm not sure there's priority logic for one "master" to push to the subscriber once it comes back, but I added a priority column in my "groups" table to hold the subscriptions, so I'm still wrestling with that design for merge issues. Sep 4, 2018 at 6:46
  • for some sort of conflict resolution logic, I was thinking of incorporating the timestamps down to the finest digit and have the queue of replication rows write FIFO, without stressing performance, even though I'm happy to use other logic to rule out the earlier or "losing" rows in a scenario. Another issue that doesn't come up much at the moment, that I'd test out as the clusters and activity grow Sep 4, 2018 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.