Question when to use BEGIN / END - Mailing list pgsql-novice

From Rikard Bosnjakovic
Subject Question when to use BEGIN / END
Date
Msg-id d9e88eaf0911300132o4887c7edv6458da164aa8686f@mail.gmail.com
Whole thread Raw
Responses Re: Question when to use BEGIN / END  (Josh Kupershmidt <schmiddy@gmail.com>)
Re: Question when to use BEGIN / END  (Mladen Gogala <mladen.gogala@vmsinfo.com>)
List pgsql-novice
I have a case where I want to forbid a race condition. Basically, I have this:

$q = "CREATE VIEW foo AS ( SELECT * FROM foo WHERE [...different
conditions every call ...]  )";
@pg_query($db, $q);
$q = "SELECT * FROM foo";
$res = pg_query($db, $q);
$row = pg_fetch_object($res);
... change table foo...
$q = "DROP VIEW foo";
@pg_query($db, $q);

It is of utmost importance that there is not another client doing the
same thing while the above is running. When the DROP VIEW is done,
another client can start its work but not until then.

I'm reading about BEGIN/END on
http://www.postgresql.org/docs/7.4/interactive/sql-begin.html but I'm
not sure this is the solution. What I'm wondering is this: If I
prepend BEGIN on CREATE VIEW above, and append END after DROP VIEW,
can I still access the table foo in between? Or is BEGIN/END only
supposed to be used on queries that do INSERT or UPDATE, i.e. no
data-reading?


--
- Rikard

pgsql-novice by date:

Previous
From: "Jean-Yves F. Barbier"
Date:
Subject: Re: bytea and text
Next
From: "Jean-Yves F. Barbier"
Date:
Subject: circular references restore