【问题标题】:Alternative function of json_build_object() in PostgreSQL 9.2PostgreSQL 9.2 中 json_build_object() 的替代函数
【发布时间】: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;

【问题讨论】:

标签: postgresql triggers sql-update sql-insert postgresql-9.2


【解决方案1】:

它是否适用于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)

【讨论】:

  • row_to_json() 在 9.2 版本中工作。而 json_build_object() 函数在 9.2 版本中不起作用。上面的说法已经奏效了。谢谢..
猜你喜欢
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多