You can implement trigger on table to check if inserted record is new. Still it is on DB side.
I don't know PHP well enough but I think You can call function e.g. SELECT myschema."InsertWhenNew" ("val1", "val2", "val3"); in the same way as You call INSERTS
Currently the query is a single PHP pg_query_params() call, and it's inside a larger transaction. By the look of it, this requires writing a function to do the job, rather than embedding the logic straight into the query - is this correct?