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)