如何解决PostgreSQL 9.2中的json_build_object的替代功能
我正在尝试使用在PostgreSQL 9.2中不起作用的函数json_build_object()将对象构建为JSON。我在找出在9.2.json_build_object()中找到json_build_object的替代方法的最佳方法时遇到了麻烦功能是从9.4版本开始添加的。
-- Function: queue_event()
-- DROP FUNCTION queue_event();
CREATE OR REPLACE FUNCTION queue_event()
RETURNS trigger AS
$BODY$
DECLARE
data json;
notification json;
BEGIN
-- Convert the old or new row to JSON,based on the kind of action.
-- Action = DELETE? -> OLD row
-- Action = INSERT or UPDATE? -> NEW row
IF (TG_OP = 'DELETE') THEN
data =row_to_json(OLD);--row_to_json
ELSE
data =row_to_json(NEW);--row_to_json
END IF;
-- Construct the notification as a JSON string.
notification = json_build_object(
'table',TG_TABLE_NAME,'action',TG_OP,'data',data); ---- Alternative to this function in 9.2 whereas in 9.4 version it is working fine.
-- Execute pg_notify(channel,notification)
PERFORM pg_notify('q_event',notification::text);
-- Result is ignored since this is an AFTER trigger
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION queue_event()
OWNER TO postgres;
解决方法
它可以与row_to_json()
和select into
一起使用吗?
select row_to_json(x) into notification
from (values(TG_TABLE_NAME,TG_OP,data)) as x("table",action,data)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。