postgresqlpostgresql-9.6multi-master-replicationpglogical

Different select results when using multimaster via pglogical in PostgreSQL


There are two PostgreSQL 9.6 nodes subscribed to each other via pglogical. If node A inserts a row into the replicated table then node B sees it and vice versa.

However, when I update a row on one node, then subsequent SELECT queries on both nodes will keep returning different results - the current one and some of the previous ones.

Moreover, there are log entries about replication conflicts in the logs of both nodes.

Why does that happen and how do I fix that?

upd: setting pglogical.conflict_resolution to last_update_wins helps. Might consider other options of conflict resolution too


Solution

  • Multi-master replication is difficult.

    There are conflicts that are bound to occur unless your application is aware of and specifically tailored to multi-master replication:

    You will have to fix your application so that it avoids problems like the above, and you will have to manually find and resolve all the conflicts that occurred so far.

    Here is an example of the second case:

    -- node one:
    
    UPDATE person
    SET id = 1234
    WHERE id = 6543;
    
    -- at the same time on node two
    
    DELETE FROM person
    WHERE id = 6543;
    

    Both statements will be replicated to the other node, but do nothing there, because both nodes no longer have a person with id 6543 any more. There will be no replication conflict right away, but node one now has a person that node two doesn't have. It is easy to see how this can lead to replication conflicts later (imagine you insert a row on node one that has a foreign key relationship to person 1234).

    This is why it is in most cases a good idea to consider an architecture that does not include multi-master replication.