There should be semicolons before 'ELSE' and before 'END IF' (semicolons are required after all statements except that which opens a block. See documentation). You still may wish to add exception handling, as INSERT may fail if unique index is used on material column and several sessions will try to add the same material at the same time.
There is a lot of similar questions here (look here for example) or google for 'postgres upsert'.