如何将新数据存储为 JSON PostgreSQL 中的触发器?
How store as JSON the new data inside a trigger in PostgreSQL?
我尝试按照以下说明进行操作:
https://wiki.postgresql.org/wiki/Audit_trigger
Auditing values as JSON
For PostgreSQL 9.2, or 9.1 with the fantastic json_91 addon, you can
log the old and new values in the table as structured json instead of
flat text, giving you much more power to query your audit history.
Just change the types of v_old_data, v_new_data, original_data and
new_data from TEXT to json, then replace ROW(OLD.) and ROW(NEW.)
with row_to_json(OLD) and row_to_json(NEW) respectively.
但是这让我出错:
CREATE OR REPLACE FUNCTION add_log (name text, Action TEXT, data jsonb, OUT RETURNS BOOLEAN)
AS $$
BEGIN
RETURNS = true;
END;
$$
LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION log_city() RETURNS TRIGGER AS
$$
DECLARE
v_new_data jsonb;
BEGIN
IF (TG_OP = 'UPDATE') THEN
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
v_new_data := row_to_jsonb(NEW);
EXECUTE add_log('City', 'City.New', v_new_data);
RETURN NEW;
END IF;
RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$$ LANGUAGE plpgsql;
INSERT INTO Location (city, state, country) values ('a', 'b' , 'c')
它说:
ERROR: function row_to_jsonb(location) does not exist
如果我输入 v_new_data := row_to_jsonb(ROW(NEW));
然后我得到:
ERROR: function row_to_jsonb(record) does not exist
documentation 中指出
Table 9-42 shows the functions that are available for creating json
and jsonb values. (There are no equivalent functions for jsonb, of the
row_to_json and array_to_json functions. However, the to_jsonb
function supplies much the same functionality as these functions
would.)
因此必须使用row_to_json
。 row_to_jsonb 不存在,但 row_to_json
也会为 JSONB
类型生成所需的结果。
我尝试按照以下说明进行操作:
https://wiki.postgresql.org/wiki/Audit_trigger
Auditing values as JSON
For PostgreSQL 9.2, or 9.1 with the fantastic json_91 addon, you can log the old and new values in the table as structured json instead of flat text, giving you much more power to query your audit history. Just change the types of v_old_data, v_new_data, original_data and new_data from TEXT to json, then replace ROW(OLD.) and ROW(NEW.) with row_to_json(OLD) and row_to_json(NEW) respectively.
但是这让我出错:
CREATE OR REPLACE FUNCTION add_log (name text, Action TEXT, data jsonb, OUT RETURNS BOOLEAN)
AS $$
BEGIN
RETURNS = true;
END;
$$
LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION log_city() RETURNS TRIGGER AS
$$
DECLARE
v_new_data jsonb;
BEGIN
IF (TG_OP = 'UPDATE') THEN
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
v_new_data := row_to_jsonb(NEW);
EXECUTE add_log('City', 'City.New', v_new_data);
RETURN NEW;
END IF;
RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$$ LANGUAGE plpgsql;
INSERT INTO Location (city, state, country) values ('a', 'b' , 'c')
它说:
ERROR: function row_to_jsonb(location) does not exist
如果我输入 v_new_data := row_to_jsonb(ROW(NEW));
然后我得到:
ERROR: function row_to_jsonb(record) does not exist
documentation 中指出
Table 9-42 shows the functions that are available for creating json and jsonb values. (There are no equivalent functions for jsonb, of the row_to_json and array_to_json functions. However, the to_jsonb function supplies much the same functionality as these functions would.)
因此必须使用row_to_json
。 row_to_jsonb 不存在,但 row_to_json
也会为 JSONB
类型生成所需的结果。