Can logical decoding be used to replicate a single table?

I am researching logical decoding , and I was able to create a slot and replicate all the transactions in the database to another using the streaming replication protocol , and it works very well.

But I need to replicate only one table, not all the tables in the database.

So my question is: does logical decoding filter the stream for a single table?

My current tip is to create a custom logical output decoding plugin , am I mistaken?

Update

I built an output plugin based on contrib/test decoding from postgresql sources, and that was a good way. However, this was not useful for real use cases, so I decided to take some other projects as links to fork and update.

Wal2json was the best for me, so I decided to develop it and add a table filter as an option, rather than hard-coded table names.

Here is the fork and this is a set of changes ,

How to use

First create a slot with the wal2json plugin:

pg_recvlogical -d postgres --slot test_slot --create-slot -P wal2json

Then start getting the stream

pg_recvlogical -d postgres --slot test_slot --start -o limit-to = table_foo, table_bar -f -

Now we are ready to receive updates only on table_foo and table_bar .


It was a really good call, I am not a c developer, and I know that the code needs some optimization, but at the moment it works better than expected.

+6
source share
1 answer

According to the documentation, you can implement your own synchronous replication solutions by implementing thread replication :

  • CREATE_REPLICATION_SLOT word_name LOGICAL options
  • DROP_REPLICATION_SLOT slot_name
  • START_REPLICATION SLOT slot_name LOGIC options

In addition to the interface above, you also need to implement the Logical Decoding Output plugin. In this plugin interface, you need to configure a Change Callback operation that listens for all DML operations:

The required change_cb callback is called for each individual row, the change within the transaction, maybe it is INSERT, UPDATE, or DELETE. Even if the original command changed several lines at once, the callback will be called individually for each line.

This is the function in which you want to check a specific table for replication. Also keep in mind that Change Callback does NOT handle UNLOGGED and TEMP , but I think this is not a serious limitation.

+1
source

All Articles