Are PL/pgSQL calls atomic? - Mailing list pgsql-sql

From Mark Wright
Subject Are PL/pgSQL calls atomic?
Date
Msg-id 001f01bead45$d72f9100$c62812ac@markw_compaq
Whole thread Raw
Responses Re: [SQL] Are PL/pgSQL calls atomic?
List pgsql-sql
If I call a PL/pgSQL function, and another client makes the same call, does
the second client have to wait for the first to complete execution before it
begins?  If not, is there some sort of mechanism I can use to prevent more
than one call to a function from happening at the same time (something like
a mutex in Win32).

I need to select a row and then mark it as unavailable for other clients to
use.  The table looks like:   create table xyz (id serial, status char default 'N', ...);

My function finds a row by doing:   select id into my_id_variable from xyz where id       = (select min(id) from xyz
wherestatus = 'N');
 

and then marks that row as unavailable by setting status:   update xyz set status = 'Y' where id = my_id_variable;

Obviously, if a second client calls this function before the UPDATE takes
place, it will pick up the same row as the first client, causing rending of
hair and gnashing of teeth.

How do I prevent this?

Mark.
---
Mark Wright
mwright@pro-ns.net
mark_wright@datacard.com




pgsql-sql by date:

Previous
From: "Pham, Thinh"
Date:
Subject: Getting primary key from insert statement
Next
From: "D'Arcy" "J.M." Cain
Date:
Subject: Re: [SQL] Getting primary key from insert statement