I just learned about using writable common table expressions in postgres-9.1, specifically from this site. http://vibhorkumar.wordpress.com/2011/10/26/upsertmerge-using-writable-cte-in-postgresql-9-1/
WITH upsert as
(update mytable2 m set sales=m.sales+d.sales, status=d.status from mytable d where m.pid=d.pid
RETURNING m.*
)
insert into mytable2 select a.pid, a.sales,'NEW' from mytable a where a.pid not in (select b.pid from upsert b);
I mentioned this to some coworkers and I was asked about the concurrency model/safety that postgres using when doing an operation like this. My first thought was that mytable
gets locked for the entire execution of the statement so that this should be thread safe in all circumstances.
Is that a correct assumption? I don't know a ton about the internal concurrency model of postgres's statement execution. But if anyone wants to go into as much detail as they'd like, that'd be great =]