by np422 · 12y ago · 0 comments
WITH upsert AS (
UPDATE some_table
SET attrib = 'foo'
WHERE id = 123
RETURNING *
) INSERT INTO some_table ( id , attrib )
SELECT 123 as id, 'foo' as attrib
WHERE 123 NOT IN (SELECT id FROM upsert );If this is standard upsert query in your app, you should turn it into a procedure and parameterize, catch exceptions anyway.
Or you could mess around with transaction isolation.