INSERT OR UPDATE for PostgreSQL
The easiest way I found to simulate a INSERT OR UPDATE using PostgreSQL (or pretty much any other RDB) is using the INSERT INTO … SELECT syntax:
INSERT INTO myTable (myKey) SELECT myKeyValue WHERE myKeyValue NOT IN (SELECT myKey FROM myTable);
UPDATE myTable SET myUpdateCol = myUpdateColValue WHERE myKey = myKeyValue;
This way you don’t need any complex trigger or transaction savepoint operations, using just plain SQL.