PostgreSQL – Insert RECORD into a Table in a Trigger Function

plpgsqlpostgresqlrecordtriggerupdate

I would like to insert a RECORD data type variable (NEW variable) into a table in a trigger. What would the SQL look like?
The following attempts were unsuccessful:

EXECUTE 'INSERT INTO my_table VALUES ' || NEW;
EXECUTE 'INSERT INTO my_table VALUES ' || NEW.*;
EXECUTE 'INSERT INTO my_table SELECT * FROM ' || NEW;

Best Answer

You must expand the record.

I've also added a column list to the insert and used the much safer EXECUTE ... USING form.

EXECUTE 'INSERT INTO my_table (col1, col2, col3) VALUES (?, ?, ?)' USING NEW.col1, NEW.col2, NEW.col3;

Please explain your use case in more detail if this is doesn't solve the issue.