Line extension through "*" is not supported here

I am in the context of a trigger and trying to get the following snippet to work.

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;

I get the following error

[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

Any suggestion on how to fix a line extension through "*" is not supported here ? Linking to a specific circuit is not an option.

+1
source share
1 answer

Above my head, it should work as follows:

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);

It is better to put all the values ​​using the sentence USING.
And you can write a record to hstore directly withhstore(record) .

0
source

All Articles