COMMIT in PostgreSQL stored procedure [duplicate]

会有一股神秘感。 提交于 2019-12-06 01:32:12

No, it's currently not supported to open or close transactions inside a stored procedure, no.

If it did, btw, committing after each iteration would make things a lot slower. You'd have to at least commit in batches of 10,000 or 100,000 updates. And as was said in the comments, the real win is of course not to run this ISAM style but to figure out some way to write it as a single query.

There is a cost per statement, so if you can write your function to do less statements, you're better off...

FOR all IN (select * from TABLE1)
LOOP
    FOR some IN (select * from)
    LOOP
        INSERT INTO TABLE2 VALUES (all.id, some.id)
    END LOOP
END LOOP

Replace the whole loop with a single INSERT statement:

INSERT INTO TABLE2 SELECT all.id, some.id FROM all, some WHERE...

But beware of the size of the list you are going to update. We had a similar problem, we have to create a lot of tables dynamically and insert a lot of data in them. Firstly we create a stored procedure and loops over a list of months and years and create a single table for every month, but it collapse in a single stored procedure. So, we create the stored procedure but we don't loops in there, instead we loops outside the stored procedure and it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!