0

Is it possible to fire triggers only on a read-only Postgres replicated database?

I want triggers to fire only on the replicated database - not on the master (to avoid load). This is to track - say - all updates/deletes.

Would enabling replica trigger be the way to go? On the primary/master, the triggers remain disabled, but in the replicated db, we can have the triggers fired?

The wal2json plugin is an option, but triggers seem a safer bet.

Laurenz Albe
62k4 gold badges57 silver badges93 bronze badges
asked Feb 11, 2020 at 17:25

1 Answer 1

1

You can do that with logical replication, but not with streaming (physical) replication, because in the latter case the standby server is read-only.

For logical replication, you have to define the trigger as replica trigger:

CREATE TRIGGER tgname ... ON TABLE atable ...;
ALTER TABLE atable ENABLE REPLICA TRIGGER tgname;
David S
2844 silver badges16 bronze badges
answered Feb 11, 2020 at 18:08
4
  • if there is a streaming replication already set from master, can logical replication be enabled on the same? can logical replication be enabled on a async replica instead of the master ? Commented Feb 11, 2020 at 18:36
  • Yes to the first question, no to the second. Commented Feb 11, 2020 at 18:38
  • wouldnt adding logical replication to the master add more load to it ? It is better off then having the triggers in the master itself? Commented Feb 11, 2020 at 20:00
  • 1
    Logical replication is way cheaper than a trigger. Commented Feb 11, 2020 at 20:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.