我在将触发器中的 bytea 数据从一个表复制到另一个表时遇到问题。
原始查询如下所示:
INSERT INTO "mytable" ("dataid","data")
VALUES
(123456,'\x3008d1a10a000000050000000000000000000000fb27000009090504010804060a08020b020c040000000000000000000000000001000000'::bytea);
在“插入前”触发器中,尝试将此值插入另一个表时出现语法错误。原因是触发器内部的值 NEW.data 已经转义,看起来像
0\010\321\241\012\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000\373\'000\000\011\011\005\004\001\010\004\006\012\010\002\013\002\014\004\000\000\000\000\000\000\000\000\000\000\000\000\000\001\000\000\000
有没有办法检索 NEW.dataid 实际值(在类型转换之前或作为字符串)?
ps 我无法在生产数据库上更改原始查询或将 bytea_output 转换或设置为十六进制
触发功能码:
BEGIN
PERFORM dblink('logsyellow', '
INSERT INTO datastorage(dataid,data)
VALUES(' || new.dataid || ','||new.data||')
');
RETURN new;
END;
我得到的错误:
PL/pgSQL function fn_replicate_data() line 4 at PERFORM
2014-07-08 13:24:35 GMT OPERATOR: INSERT INTO mytable ( dataid, data ) VALUES ( 123456, '\x3008d1a10a000000050000000000000000000000fb27000009090504010804060a08020b020c040000000000000000000000000001000000'::bytea );
2014-07-08 13:24:35 GMT ERROR: syntax error (at near "\")
2014-07-08 13:24:35 GMT CONTEXT: Error occurred on dblink connection named "unnamed": could not execute command.
SQL-operator: "SELECT DBLINK_EXEC('logsyellow','INSERT INTO datastorage(dataid,data) VALUES('||new.dataid||','''||new.data||''') ')"