DROP IF ... - Mailing list pgsql-sql

From CG
Subject DROP IF ...
Date
Msg-id 20050524174454.67174.qmail@web32501.mail.mud.yahoo.com
Whole thread Raw
Responses Re: DROP IF ...  ("Thomas F. O'Connell" <tfo@sitening.com>)
List 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 


pgsql-sql by date:

Previous
From: Andrew Hammond
Date:
Subject: Re: Transaction in plpgslq
Next
From: Tony Wasson
Date:
Subject: Re: Help: Function for splitting VARCHAR column and migrating its data to 2 new tables