Re: CREATE DATABASE/DROP DATABASE race conditions

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Florian Weimer <Weimer(at)CERT(dot)Uni-Stuttgart(dot)DE>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: CREATE DATABASE/DROP DATABASE race conditions
Date: 2002-07-14 18:44:55
Message-ID: 20096.1026672295@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Florian Weimer <Weimer(at)CERT(dot)Uni-Stuttgart(dot)DE> writes:
> \connect template1 fw
> CREATE DATABASE aaa;
> \connect aaa fw;
> CREATE TABLE a (a text);
> \connect template1 fw
> DROP DATABASE aaa;

> sometimes results in:

> You are now connected to database template1 as user fw.
> CREATE DATABASE aaa;
> CREATE DATABASE
> You are now connected to database aaa as user fw.
> CREATE TABLE a (a text);
> CREATE
> You are now connected to database template1 as user fw.
> DROP DATABASE aaa;
> psql:/tmp/t.sql:6: ERROR: DROP DATABASE: database "aaa" is being accessed by other users

Well, this is not what you asserted to begin with. The reason the above
fails is that it takes a nonzero amount of time for a backend to exit
after it detects client disconnect. The "other user" being complained
of is simply your own old backend that had been used for the CREATE
TABLE command. (It doesn't help any that psql doesn't drop the old
connection till it's successfully established a new one; so the normal
backend startup time doesn't offer any offsetting delay in this
scenario.)

It might be possible to tweak the FE/BE protocol to allow this to be
handled more carefully. Right now the normal case is

client sends X
client closes connection

backend closes connection

backend cleans up

but we could do

client sends X

client waits to see EOF

backend cleans up

backend closes connection

client closes connection

The backend code change would be trivial (instead of explicitly closing
the socket, just let it be closed by the kernel when the process exits).
The frontend change would be a little less trivial, and would be wanted
only by a few clients anyway. Not sure how to handle that; maybe create
a variant version of PQfinish ...

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Mats Lofkvist 2002-07-15 12:59:19 Re: 7.2.1 backend crash (convert_string_datum, locale)
Previous Message Florian Weimer 2002-07-14 17:53:08 Re: CREATE DATABASE/DROP DATABASE race conditions