【发布时间】:2020-09-02 12:04:33
【问题描述】:
我正在尝试使用函数 json_build_object() 将对象构建为 JSON,该函数在 PostgreSQL 9.2 中不起作用。我无法找出在 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;
【问题讨论】:
-
你知道 Postgres 9.2 是 no longer supported 吗?您应该尽快计划升级。
标签: postgresql triggers sql-update sql-insert postgresql-9.2