Re: Weird "template1" errors on CREATE DATABASE - Mailing list pgsql-general

From Tom Lane
Subject Re: Weird "template1" errors on CREATE DATABASE
Date
Msg-id 3892.1050676844@sss.pgh.pa.us
Whole thread Raw
In response to Weird "template1" errors on CREATE DATABASE  (Ken Williams <ken@mathforum.org>)
Responses Re: Weird "template1" errors on CREATE DATABASE  (Ken Williams <ken@mathforum.org>)
List pgsql-general
Ken Williams <ken@mathforum.org> writes:
> I'm getting some weird errors when I run some Perl code that does a
> CREATE DATABASE query.  Sometimes it dies with the error 'ERROR:
> CREATE DATABASE: source database "template1" is being accessed by other
> users'.  Other times it works fine.

> The thing is, this server is running on my laptop, and no other code is
> accessing the database except the code I'm running right now.
> Sometimes it happens the very first time I connect to the database
> (using "template1" as advised in the docs) and try to create a
> database.  So there couldn't *possibly* be anyone else accessing the
> "template1" database.

I have seen reports like this before, and AFAICT the issue is simply
that your previous connection to template1 hasn't finished cleaning
itself up yet.  It takes a finite amount of time for a backend to shut
down.  Also, it's not unheard-of for client-side code to be holding a
connection open for longer than the programmer realizes, especially
when the connection closure is triggered indirectly through garbage
collection.  Perl is one language where that applies.

Throwing a fractional-second delay in before you try to do the CREATE
DATABASE will probably make the problem go away.  Alternatively, fix
your client-side code so that it's not opening a fresh connection for
each SQL command, as I surmise it's doing now.

            regards, tom lane


pgsql-general by date:

Previous
From: Andrew Sullivan
Date:
Subject: Re: Using index for "like 'ABC%'" type query
Next
From: Tom Lane
Date:
Subject: default locale considered harmful? (was Re: Using index for "like 'ABC%'" type query)