pvsbandi
asked on
Postgres - DML in batches
I want to know if there's a way in Postgres Plpgsql, to perform an update or a delete in batches (say, a batch of 1000) till the end of the last record.
Could someone please share an example, if a better solution is feasible?
ASKER
Thank you! yes, I understand the negative impacts. i woild like to see how it can be accomplished, if there's a way.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I know the CTE way of doing it. But I was manually triggering it till the end of the record.
How to automate the repetition in a loop?
>>How to automate the repetition in a loop?
Use plpgsql: Create a procedure or anonymous code block:
https://www.postgresql.org/docs/current/plpgsql-control-structures.html
Use plpgsql: Create a procedure or anonymous code block:
https://www.postgresql.org/docs/current/plpgsql-control-structures.html
To get the number of rows affected by the update or delete, look at the returning clause:
https://www.postgresql.org/docs/current/plpgsql-statements.html
You can see it in action here:
https://stackoverflow.com/questions/4038616/get-count-of-records-affected-by-insert-or-update-in-postgresql
https://www.postgresql.org/docs/current/plpgsql-statements.html
You can see it in action here:
https://stackoverflow.com/questions/4038616/get-count-of-records-affected-by-insert-or-update-in-postgresql
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
MOST of the time, this isn't really faster and can consume more resources.
There is also to issue of committing. If you commit per batch, what if one fails? Then you can have data that isn't as complete as you desire.