I have problems with copying bytea data in trigger from one table to another.
The original query looks like:
INSERT INTO "mytable" ("dataid","data")
VALUES
(123456,'\x3008d1a10a000000050000000000000000000000fb27000009090504010804060a08020b020c040000000000000000000000000001000000'::bytea);
Inside "before insert" trigger, I get syntax error when trying to insert this value to another table. The reason is that inside the trigger the value NEW.data is already escaped and looks like
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
Is there a way to retrive NEW.dataid actual value ( before type cast or as a string ) ?
p.s. I cannot change original query or convert or set bytea_output to hex on production db
Trigger function code:
BEGIN
PERFORM dblink('logsyellow', '
INSERT INTO datastorage(dataid,data)
VALUES(' || new.dataid || ','||new.data||')
');
RETURN new;
END;
Error I get:
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||''') ')"