1

I have an issue with catching all the data with AFTER INSERT trigger on my PostgreSQL 16 logical replication. I simplified my tables into

create table customer (id serial, email text);
create table customer_address (id integer, city text);
create table customer_inserts (id integer, email text, city text);

The insert that is happening on primary server is basically something like this:

with customer_insert as 
  (insert into customer (email) 
  select '[email protected]'
  returning id)
insert into customer_address (id, city)
select id, 'New York'
from customer_insert;

And on replica I need to catch data from both of the tables into the 3rd one but it seems that the trigger does not see the row from customer_address table. If I use some other table, where row was already there before - it works, but with left join to customer_address the column from there is null.

Here the full script with trigger: https://dbfiddle.uk/Mvo7oHRg

I used ALTER TABLE customer ENABLE REPLICA TRIGGER customer_trg; - so I know the trigger generally works but not with join to other table with data inserted in same transaction.

I even made additonal columns in both of them with now() as default to see if there is difference, but in most cases (not all!) it's the same.

Is it a nature of logical replication that data is not replicated in the same transactions as on primary? Or is there any trick with the triggers I should use?

On the primary te same trigger is working like a charm - but the objective is to make it work on replica.

4
  • From here ALTER TABLE did you read and implement this: Triggers configured as ENABLE REPLICA will only fire if the session is in “replica” mode ? Also this The effect of this mechanism is that in the default configuration, triggers do not fire on replicas. This is useful because if a trigger is used on the origin to propagate data between tables, then the replication system will also replicate the propagated data; so the trigger should not fire a second time on the replica, because that would lead to duplication. Commented Dec 19, 2024 at 23:31
  • Yes I did, I mentioned it in the question Commented Dec 20, 2024 at 7:38
  • 1) I don't see where you indicate the setting for session_replication_role. 2) I just realized it is not clear whether the trigger is on the table on the publisher or subscriber side? Commented Dec 20, 2024 at 16:18
  • Are these tables published in the same publication on the primary, and are they subscribed to in the same subscription on standby? If it's 1:1, does synchronous_commit=remote_apply change anything? @AdrianKlaver That setting is set by the replication system. OP's trigger is correctly configured to fire in that context. The trigger in question is on the subscriber. Same one set up on the publisher "works like a charm" - mentioned at the end. Commented Dec 20, 2024 at 17:40

0

Your Answer

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.