Re: how to synchronize database operations? - Mailing list pgsql-general

From Tom Lane
Subject Re: how to synchronize database operations?
Date
Msg-id 23685.1029426055@sss.pgh.pa.us
Whole thread Raw
In response to how to synchronize database operations?  (Markus Wagner <magnus@gmx.de>)
Responses Re: how to synchronize database operations?  (Markus Wagner <magnus@gmx.de>)
List pgsql-general
Markus Wagner <magnus@gmx.de> writes:
> Hi,
> when I (re)create my db from within C using libpq I do the following:
> I open "template1" and execute "drop database ..." and "create database ...".
> This worked fine for some time. However, I encountered some synchronization
> problem:

> When recreating, which includes the drop command, I get immediately before the
> create command the error "template1 is accessed by other users". Then, when
> doing the same thing again, there is no db to drop, and the create call
> succeeds.

My guess is that you are closing the connection to the backend that did
the DROP and opening a new one for the CREATE.  The trouble with this is
that it takes a few milliseconds for the old backend to clean up and go
away.  It's quite possible for the new backend to see the old one as
still active, and since it doesn't know what the other one might be
doing to template1, it disallows the CREATE.

If you issue the DROP and the CREATE in the same backend process,
I suspect your problem will go away.

> How can I synchronize that? Just trying to open template1 again and again?

That would probably make it worse ...

            regards, tom lane

pgsql-general by date:

Previous
From: Andrew Sullivan
Date:
Subject: Re: Problem with Now()?
Next
From: Joe Conway
Date:
Subject: Re: Bulk row fetching