简体   繁体   English

Postgres通知不使用逻辑复制

[英]Postgres Notify not working with logical replication

I am replicating data from Postgres 10.4 to another Postgres 10.4 instance using logical replication. 我正在使用逻辑复制将Postgres 10.4中的数据复制到另一个Postgres 10.4实例。

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. 该表有一个触发器,它执行另一个函数(返回一个触发器)来为下游监听器调用NOTIFY。

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. 我发现了日志记录的详细程度,除了我添加到函数中的日志语句以验证它们正在运行之外,没有看到与Notify相关的任何内容。

Another report from someone on Stack Overflow: Notify From Trigger On PG Logical Replicated Table Stack Overflow上的某人报告: PG逻辑复制表上的触发通知

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 . 更新:看起来这是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. 根据PostgreSQL邮件列表上的这篇文章 ,默认情况下,逻辑复制不会导致触发器在副本上触发,因为表通常具有“本地”复制角色,而在逻辑副本上,数据将以“副本”角色插入。

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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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