ReplaceQueries don't work properly with PostgreSQL

PostgreSQL doesn't have an equivalent of REPLACE INTO. At the moment the query we have will only perform an insert if there isn't a matching row - but this isn't quite the same as it doesn't update any existing rows.

The consensus seems to be we need to use either multiple queries or a PL/pgSQL function - either way it is going to be messy.

Unfortunately that means we will run two queries, but there is no other way. Also, we can pass them to the database in just one go, which means we'll get rid of the slowest part anyway, so things should be fine.

#384: Change REPLACE queries to only run once.
This change also splits out the UPDATE / INSERT queries for PostgreSQL into two separate queries (instead of sending them to the database together).
The hook: the INSERT query is only executed if necessary (no rows were affected during UPDATE).