【发布时间】:2022-01-27 00:44:49
【问题描述】:
CREATE OR REPLACE FUNCTION notify_tenant_work_order_status_cud() RETURNS TRIGGER AS $$
DECLARE
row RECORD;
output JSONB;
row_obj JSONB;
payload JSONB;
final_payload JSONB;
BEGIN
-- Checking the Operation Type
IF (TG_OP = 'DELETE') THEN
row = OLD;
ELSE
row = NEW;
END IF;
-- Forming the Output as notification. You can choose you own notification.
output = jsonb_build_object('operation', TG_OP);
row_obj = jsonb_build_object('row', to_jsonb(row));
payload = output || row_obj;
final_payload = jsonb_build_object('id', jsonb_extract_path_text(payload, 'row', 'id'), 'operation', jsonb_extract_path_text(payload, 'operation'));
-- Calling the pg_notify for my_table_update event with output as final_payload
PERFORM pg_notify('tenant_work_order_status_cud'::text, final_payload::text);
-- Returning null because it is an after trigger.
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_tenant_work_order_status_cud AFTER INSERT OR UPDATE OR DELETE ON tenant_work_order_status FOR EACH ROW EXECUTE PROCEDURE notify_tenant_work_order_status_cud();
当对tenant_work_order_status 表执行任何插入、更新和删除操作时,它应该在tenant_work_order_status_cud() 通道上通知。它没有按预期工作。
我们在其他表上有相同的触发器,它工作正常。
任何可能的帮助将不胜感激。
【问题讨论】:
-
定义“它没有按预期工作。”?通知没有发生?确实如此,但信息不正确?其他原因?评论还说
'...pg_notify for my_table_update event...'但pg_notify有'tenant_work_order_status_cud',对吗?添加答案以更新您的问题。 -
问题是有效负载限制为 8000 字节,当有效负载超过 8000 时,将不会在通道上通知。现在只需使用几列作为有效负载即可将有效负载降低到 8000 字节以下。
标签: postgresql triggers plpgsql