问题
I have a PostgreSQL stored procedure which loops over a very large list, and makes changes to some of its members using UPDATE
.
Is there a way to commit these changes per iteration, not at the end of the function execution? It would allow me to run the function for shorts periods of time, making small changes at each run.
Thanks,
Adam
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/2305431/commit-in-postgresql-stored-procedure