【问题标题】:Trigger function in postgressql not working, after INSERT,UPDATE,DELETE operations在 INSERT、UPDATE、DELETE 操作之后,postgresql 中的触发函数不起作用
【发布时间】: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


【解决方案1】:

确保payload不超过8000字节,pg-channel只能承载小于8000字节。

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2021-04-24
    • 2019-11-22
    • 2018-09-07
    • 1970-01-01
    • 2011-12-02
    相关资源
    最近更新 更多