2ndQuadrant / pglogical

Logical Replication extension for PostgreSQL 17, 16, 15, 14, 13, 12, 11, 10, 9.6, 9.5, 9.4 (Postgres), providing much faster replication than Slony, Bucardo or Londiste, as well as cross-version upgrades.
http://2ndquadrant.com/en/resources/pglogical/
Other
1.02k stars 154 forks source link

How to sync associated table records #392

Open Nilanth opened 2 years ago

Nilanth commented 2 years ago

How to sync associated table records using replication_set_add_table?

I have tables like Users, Post, and Comments. We need to sync the Comments related to a user.

Table Schema

Users id, name, email

Posts id, name, user_id

Comments id, comment, post_id

I tried with the below query but it doesn't work.

SELECT pglogical.replication_set_add_table(set_name := 'replication_set', relation := 'comments', synchronize_data := true, row_filter := 'post_id in (select id from posts where user_id = 1)');

petere commented 2 years ago

This kind of thing is not supported. A row filter can only look at its own table.

Nilanth commented 2 years ago

This kind of thing is not supported. A row filter can only look at its own table.

Thanks, Do u suggest any other tools for this case?

bonesmoses commented 2 years ago

Sync related tables as whole units. Sync the comments table, and user tables in their entirety. If you need filters like the one you showed here, you need a different tool.

Nilanth commented 2 years ago

@bonesmoses Can you suggest any tool for this?

eulerto commented 1 year ago

Another alternative is to modify your schema to include user_id into the comments table. Hence, you can use a row_filter: user_id = 1.