Re: is there a select for update insert if not exist type command? - Mailing list pgsql-general

From Chris Travers
Subject Re: is there a select for update insert if not exist type command?
Date
Msg-id CAKt_ZfuoUW1mrFv74Ye3jH9UFOqCx-QJ2GdJEfcMjyiZf0uC+w@mail.gmail.com
Whole thread Raw
In response to Re: is there a select for update insert if not exist type command?  ("David Johnston" <polobo@yahoo.com>)
List pgsql-general
Depending on the version of Pg there are two possible solutions to
this problem.  The first (old solution) that really only works well
one row at a time is to do a stored procedure that does something
like:

update foo set bar = baz where id = in_id

if not found
   insert into foo (bar) values (baz)
end if;

The newer way, which can be done in SQL with Pg 9.1 is to use writable
common table expressions.  See
http://vibhorkumar.wordpress.com/2011/10/26/upsertmerge-using-writable-cte-in-postgresql-9-1/
for an example by Vibhor Kumar.

Best Wishes,
Chris Travers

pgsql-general by date:

Previous
From: Evan Rempel
Date:
Subject: Re: is there a select for update insert if not exist type command?
Next
From: Edson Richter
Date:
Subject: Re: is there a select for update insert if not exist type command?