Link to home
Start Free TrialLog in
Avatar of pvsbandi
pvsbandiFlag for United States of America

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?

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Might we ask why you want to do it in batches?

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.
Avatar of pvsbandi

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
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial