A trigger that is defined as
AFTER INSERT...FOR EACH ROW will queue up info all the inserted rows and then fire the trigger for each one at the end of the statement. So if you insert millions of records with a single statement, that queue will take up a lot of memory.
BEFORE INSERT does not do this, it executes the trigger function for each row immediately before each one is inserted, and doesn’t queue up anything. If possible, rewrite to a BEFORE trigger.