Thread: Re: Conditional delete

Re: Conditional delete

From
Qingqing Zhou
Date:


On Mon, 21 Nov 2005, Bartosz Jakubiak wrote:

> Hi.
>
> I'm new with PostgreSQL, but this thing doesn't allow me to sleep:
>
> I wonder if it is possible to execute SQL query which:
> 1. checks out if table exists, and if it is:
> 2. deletes it
> All of it at SQL query level, preferrably in one transaction.
>
> Something like (MSSQL):
> IF EXISTS (SELECT * FROM sysobjects WHERE id =
> object_id(n'properties_branches') AND objectproperty(id, n'isusertable')
> = 1)
> DROP TABLE properties_branches
>
> or (MySQL)
> DROP TABLE IF EXISTS properties_branches;
>

This quite looks like rewrite a script auto generated from SQL Server :-)
Yes, you can do that by querying the system catalog to see if a table
exists (pg_class). However, notice there is possible a race condition
here: if you found one table exists, but when you delete it, it may
already deleted by others.

Regards,
Qingqing