DROP IF ...

From: CG <cgg007(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: DROP IF ...
Date: 2005-05-24 17:44:54
Message-ID: 20050524174454.67174.qmail@web32501.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

PostgreSQL 7.4 ...

I'm trying to find a way to drop a table via SQL if it contains 0 rows. Here
was my thought:

CREATE OR REPLACE FUNCTION dropif(text, bool)
RETURNS bool AS
'DECLARE
tblname ALIAS FOR $1;
condition ALIAS FOR $2;
BEGIN
IF (condition) THEN
EXECUTE(\'DROP TABLE "\' || tblname || \'";\');
END IF;
RETURN \'t\'::bool;
END;'
LANGUAGE 'plpgsql' VOLATILE;

... then ...

BEGIN;
CREATE TABLE testtbl (i int4);
SELECT dropif('testtbl',(SELECT count(*)::int4 FROM testtbl) = 0);

ERROR: relation 286000108 is still open
CONTEXT: PL/pgSQL function "dropif" line 6 at execute statement

... It makes sense. The select is still open when the table is going to be
dropped. I need a different strategy.

Please advise!

CG

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-05-24 18:12:25 Re: [SQL] could not devise a query plan
Previous Message Andrew Hammond 2005-05-24 16:10:54 Re: Transaction in plpgslq