Re: How can I do conditional 'drop table' in Postgres

From: Shachar Shemesh <psql(at)shemesh(dot)biz>
To: Dragan Matic <mlists(at)panforma(dot)co(dot)yu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How can I do conditional 'drop table' in Postgres
Date: 2004-05-07 15:29:23
Message-ID: 409BAB53.6010508@shemesh.biz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dragan Matic wrote:

> if exists (select * from sysobjects where id =
> object_id(N'[dbo].[pp_fisk]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
> drop table [dbo].[pp_fisk]
> GO
>
> For instance, this is a valid script in Ms SQL, it will drop table
> pp_fisk only if it exists, is there a way to do something similar in
> Postgres? Tnx in advance.
>
> Dragan

I'm working from memory here, so exact syntax might be incorrect.

perform select * from pg_table where ....;
if found then
drop table ...
end if

This applies to plpgsql only. Please pay attention, however, that if you
are going to be dropping and recreating tables from this function, you
must have all queries relating this table use "execute". Otherwise, the
table's OID is going to be cached the first time code referencing this
table (in "from") is run, and subsequent runs in the same session will
not find the table (even if you create a new table with the same name).
If that is a problem for you, consider replacing "drop table" with
"delete from table", which will delete all elements form the table, but
leave the table itself.

Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting
http://www.lingnu.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ed L. 2004-05-07 16:32:05 Re: Interpreting vacuum verbosity
Previous Message Tom Lane 2004-05-07 15:11:08 Re: My database hurts "ERROR: duplicate key violates unique constraint"