我处于触发器环境中,并尝试让以下代码片段起作用。
execute format('insert into %I (user_name, action, new_values, query)
values (''%I'', ''i'', hstore(($1).*), current_query())',
tg_table_name::text || '_audit', current_user::text)
using new;
我收到以下错误
[SQL]insert into book (title, n_pages) values ('PG is Great', 250); [Err] ERROR: row expansion via "*" is not supported here LINE 2: values ('u1', 'i', hstore(($1).*), current_q... ^ QUERY: insert into book_audit (user_name, action, new_values, query) values ('u1', 'i', hstore(($1).*), current_query()) CONTEXT: PL/pgSQL function "if_modified_func" line 8 at EXECUTE statement
此处不支持有关如何通过“*”修复行扩展的任何建议? 耦合到特定模式不是一种选择。
答案 0 :(得分:0)
从我的头脑中,它应该像这样工作:
EXECUTE format('
INSERT INTO %I (user_name, action, new_values, query)
SELECT $1, ''i'', $2, current_query()'
, tg_table_name::text || '_audit')
USING current_user, hstore(NEW);
最好使用USING
子句提供所有值
你可以cast a record to hstore directly with hstore(record)
。