Skip site navigation (1) Skip section navigation (2)

Question when to use BEGIN / END

From: Rikard Bosnjakovic <rikard(dot)bosnjakovic(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Question when to use BEGIN / END
Date: 2009-11-30 09:32:18
Message-ID: (view raw, whole thread or download thread mbox)
Lists: 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 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

- Rikard


pgsql-novice by date

Next:From: Jean-Yves F. BarbierDate: 2009-11-30 14:48:33
Subject: circular references restore
Previous:From: Jean-Yves F. BarbierDate: 2009-11-29 14:30:57
Subject: Re: bytea and text

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group