在函数postgres中的每次迭代后提交更改

时间:2019-12-06 17:56:46

标签: postgresql

我正在尝试在此函数内的每次迭代后提交更改,以保存我的进度,以防在工作中停止该函数。我该怎么办?

CREATE OR REPLACE FUNCTION delete_in_loop() 
RETURNS INTEGER AS $$
DECLARE
    counter INTEGER = 0 ;
     i INTEGER = 0 ;
BEGIN
    i = (select COUNT("ID") from "AwsSesNotification"
            where "UTADateCreatedOn" < (now() - interval '3 month'))/1000 + 1 ;
    LOOP 
        EXIT WHEN counter > i ; 
        counter = counter+1;
        delete from "AwsSesNotification"
        where "ID" in(
            select "ID" from "AwsSesNotification"
            where "UTADateCreatedOn" < (now() - interval '3 month')
            limit 1000
        );
        RAISE NOTICE 'Counter: %', counter;
        RAISE NOTICE 'From: %', i;
        PERFORM  pg_sleep(2);
    END LOOP;
    return counter;
END;
$$ LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:0)

这被称为“自主交易”,不幸的是在PostgreSQL中不受支持。 我知道达到类似效果的唯一方法是使用dblink(),实际上是写入具有单独事务上下文的另一个数据库。比正常的写作要慢得多。

最好的问候, 比尼亚尼

相关问题