CREATE FUNCTION log_roll_test()
RETURNS trigger
LANGUAGE plpgsql AS
$func$
BEGIN
EXECUTE format('INSERT INTO %I SELECT ($1).*' -- !
, to_char(NEW.updt_ts, '"log"WW')) -- child table name
USING NEW; -- !
RETURN NULL;
END
$func$;
You cannot reference NEW
inside the query string. NEW
is visible in the function body, but not inside EXECUTE
environment. The best solution is to pass values in the USING
clause.
I also substituted the equivalent to_char(NEW.updt_ts, '"log"WW')
for the table name. to_char()
is faster and simpler here.