0

I have two tables A and B and A has foreign key to B in Postgresql database . I want to replicate table A to another database without replicating table B because that takes too much time and is not necessary; Only a small portion of table B has been referenced by table A. Is there any replication system in which we can replicate table A and only rows of table B(referenced table) which has been referenced?

1 Answers1

0

If it's just about the two (or a few) tables I would create a VIEW on B in your master DB:

CREATE VIEW b_for_a
SELECT *  -- or list columns explicitly, depends on your plans
FROM   b
WHERE  EXISTS (SELECT 1 FROM a WHERE a.b_id  = b.b_id);

And sync with dblink (executed on the slave side). Code example:

Also consider:

Or use foreign tables using postgres_fdw in Postgres 9.3+.

The remaining big question is how and when to trigger the sync. If your app is not time critical, you could schedule cron jobs at opportune times. And if your tables are small you could simply truncate and copy the whole table. If you need to replicate changes more often or if the tables grow bigger or if you can't lock target tables freely, it gets more complex. Triggers, LISTEN / NOTIFY or even dblink_get_notify(), ...

Hand-knit solutions need more work and attention to details than one of the established replication systems. If things get complex, rather consider one of the established replication systems. Like Slony or Buccardo which offer per-table granularity.

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228