Run SQL statements in PL/pgSQL only if a row doesn't exist
问题 I want to do something like this in a PL/pgSQL function in Postgres 9.6: INSERT INTO table1 (id, value) VALUES (1, 'a') ON CONFLICT DO NOTHING; --- If the above statement didn't insert a new row --- because id of 1 already existed, --- then run the following statements INSERT INTO table2 (table1_id, value) VALUES (1, 'a'); UPDATE table3 set (table1_id, time) = (1, now()); However, I don't know how to determine whether the first INSERT actually inserted a new row, or whether the the ON