仅供参考:
CREATE OR REPLACE FUNCTION SD_RebuildIndex() RETURNS void AS $$
DECLARE
var_table_name VARCHAR(150);
var_index_name VARCHAR(150);
var_reindex_sql VARCHAR(200);
var_analyze_sql VARCHAR(200);
c_index_name CURSOR FOR select relname,indexrelname from pg_stat_user_indexes order by indexrelname;
BEGIN
OPEN c_index_name;
FETCH c_index_name INTO var_table_name,var_index_name;
WHILE FOUND LOOP
var_reindex_sql:= 'REINDEX INDEX ' || var_index_name || ';';
var_analyze_sql:= 'ANALYZE ' || var_table_name || ';';
EXECUTE var_reindex_sql;
EXECUTE var_analyze_sql;
FETCH c_index_name INTO var_table_name,var_index_name;
END LOOP;
CLOSE c_index_name;
END;
$$ LANGUAGE plpgsql;