CREATE OR REPLACE FUNCTION drop_now()
RETURNS void AS
$func$
DECLARE
_tbl regclass;
_found int;
BEGIN
FOR _tbl IN
SELECT relid
FROM pg_stat_user_tables
WHERE schemaname = 'public'
AND relname LIKE '%test%'
LOOP
EXECUTE format($f$SELECT 1 FROM %s
WHERE tm < now() - interval '90 min'$f$, _tbl);
GET DIAGNOSTICS _found = ROW_COUNT;
IF _found > 0 THEN
-- EXECUTE 'DROP TABLE ' || _tbl;
RAISE NOTICE 'Dropped table: %', _tbl;
END IF;
END LOOP;
END
$func$ LANGUAGE plpgsql;
Major points
row
is a reserved word in the SQL standard. It's use is allowed in Postgres, but it's still unwise. I make it a habbit to prepend psql variable with an underscore_
to avoid any naming conflicts.You don't don't select the whole row anyway, just the table name in this example. Best use a variable of type
regclass
, thereby avoiding SQL injection by way of illegal table names automatically. Details in this related answer:
Table name as a PostgreSQL function parameterYou don't need
LIMIT
in anEXISTS
expression, which only checks for the existence of any rows. And you don't need meaningful target columns for the same reason. Just writeSELECT 1
orSELECT *
or something.You need dynamic SQL for queries with variable identifiers. Plain SQL does not allow for that. I.e.: build a query string and
EXECUTE
it. Details in this closely related answer:
Dynamic SQL (EXECUTE) as condition for IF statementThe same is true for a
DROP
statement, should you want to run it. I added a comment.