Postgres Notify not working with logical replication

泪湿孤枕 提交于 2019-12-10 16:14:42

问题


I am replicating data from Postgres 10.4 to another Postgres 10.4 instance using logical replication.

The subscriber has several triggers that log events to a single table. This table has a trigger that executes another function (that returns a trigger) to call NOTIFY for a downstream listener.

Trigger on the audit table looks like this:

CREATE TRIGGER queue_insert
    AFTER INSERT ON schema_name.table_name FOR EACH ROW
     EXECUTE PROCEDURE notify_downstream()
GO

Notify downstream definition:

CREATE OR REPLACE FUNCTION schema_name.notify_downstream () RETURNS trigger AS
'
declare
message character varying;
begin

raise log ''notify running!'';

message := ''
{ "id": 'edited for brevity' }

'';
execute ''notify chan_name, '''''' || message || '''''''';

raise log ''Value: %'', message;
return new;
end;
'
LANGUAGE 'plpgsql'
GO

Using the logging, I'm able to prove that this is firing. I can also see that there is data using:

select pg_notification_queue_usage()

The problem is that none of the listeners get the message until I insert into the table (read as: outside of logical replication) to make the trigger fire, then the listener receives all of the messages that notify should have sent from logical replication.

All of this worked well until we moved to logical replication (we had a home grown solution that was retired and I don't know anything about it).

I receive no errors or strange messages that could give me any clues. I turned up verbosity of the logging as well and see nothing related to Notify other than the log statements I added to the functions to verify that they are running.

Another report from someone on Stack Overflow: Notify From Trigger On PG Logical Replicated Table

Question: How do I debug this issue? How do I get the listeners to receive the messages without manually inserting a row to have them appear all of a sudden?


回答1:


Update: It looks like this is a bug with PostgreSQL 10.4, and at least up to at least 11.4. There's an experimental patch available here.


According to this post on the PostgreSQL mailing list it looks like by default logical replication won't cause triggers to fire on replicas because tables generally have the "local" replication role and on logical replicas the data gets inserted with the "replica" role.

It looks like you can alter your table to always fire triggers, including on replication by doing the following (see the documentation here):

ALTER TABLE my_table ENABLE ALWAYS TRIGGER my_trigger;



来源:https://stackoverflow.com/questions/51457034/postgres-notify-not-working-with-logical-replication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!