简体   繁体   中英

Postgres Notify not working with logical replication

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?

Update: It looks like this is a bug with PostgreSQL 10.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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM