DO $$
DECLARE
table_name TEXT;
query_text TEXT;
has_old_data BOOLEAN;
BEGIN
FOR table_name IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'pg_catalog') -- 排除系统模式
LOOP
query_text := format('
SELECT EXISTS (
SELECT 1
FROM %I
WHERE your_date_column < NOW() - INTERVAL ''3 years''
)', table_name);
EXECUTE query_text INTO has_old_data;
IF has_old_data THEN
RAISE NOTICE 'Table % has data older than three years.', table_name;
END IF;
END LOOP;
END $$;