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 

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
      joining column's datatypes do not match

Reply via email to