Re: is there a select for update insert if not exist type command?

From: Chris Travers <chris(dot)travers(at)gmail(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: Evan Rempel <erempel(at)uvic(dot)ca>, pgsql-general(at)postgresql(dot)org
Subject: Re: is there a select for update insert if not exist type command?
Date: 2012-06-10 02:43:46
Message-ID: CAKt_ZfuoUW1mrFv74Ye3jH9UFOqCx-QJ2GdJEfcMjyiZf0uC+w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin 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

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Edson Richter 2012-06-10 02:59:57 Re: is there a select for update insert if not exist type command?
Previous Message Evan Rempel 2012-06-10 02:40:12 Re: is there a select for update insert if not exist type command?

Browse pgsql-general by date

  From Date Subject
Next Message Edson Richter 2012-06-10 02:59:57 Re: is there a select for update insert if not exist type command?
Previous Message Evan Rempel 2012-06-10 02:40:12 Re: is there a select for update insert if not exist type command?