Package: pg-batch
PostgreSQL equivalent ofhttps://github.com/gabfl/mysql-batch.Updating or deleting a large amount of rows in PostgreSQL will create
locks that will paralyze other queries running in parallel.This tool will run UPDATE and DELETE queries in small batches to limit
locking. If a large number of rows has to be updated or deleted, it is
also possible to limit the number of rows selected at once.Installationpip3 install pg_batchUPDATE exampleYou can run this example with the schema available insample_table/schema.sqlThe following example will be identical to the following update:UPDATEbatch_testSETdate=NOW()WHEREnumber>30ANDdateisNULL;This is the equivalent to process this update with batches of 20 rows:pg_batch--hostlocalhost\--userpostgres\--passwordsecret_password\--database"test"\--table"batch_test"\--write_batch_size20\--where"number > 30 AND date IS NULL"\--set"date = NOW()"Output sample:*Selectingdata...query:SELECTidasidFROMbatch_testWHEREnumber>30ANDdateISNULLANDid>0ORDERBYidLIMIT10000*Preparingtomodify70rows...*Updating20rows...query:UPDATEbatch_testSETdate=NOW()WHEREidIN(31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50)*Startupdating?[Y/n]*Updating20rows...query:UPDATEbatch_testSETdate=NOW()WHEREidIN(51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70)*Updating20rows...query:UPDATEbatch_testSETdate=NOW()WHEREidIN(71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90)*Updating10rows...query:UPDATEbatch_testSETdate=NOW()WHEREidIN(91,92,93,94,95,96,97,98,99,100)*Selectingdata...query:SELECTidasidFROMbatch_testWHEREnumber>30ANDdateISNULLANDid>100ORDERBYidLIMIT10000*Nomorerowstomodify!*ProgramexitedDELETE exampleThe following example will be identical to the following delete:DELETEFROMbatch_testWHEREnumber>30ANDdateisNULL;This is the equivalent to process this delete with batches of 20 rows:pg_batch--hostlocalhost\--userpostgres\--passwordsecret_password\--database"test"\--table"batch_test"\--write_batch_size20\--where"number > 30 AND date IS NULL"\--action"delete"Output sample:*Selectingdata...query:SELECTidasidFROMbatch_testWHEREnumber>30ANDdateISNULLANDid>0ORDERBYidLIMIT10000*Preparingtomodify70rows...*Deleting20rows...query:DELETEFROMbatch_testWHEREidIN(31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50)*Startdeleting?[Y/n]*Deleting20rows...query:DELETEFROMbatch_testWHEREidIN(51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70)*Deleting20rows...query:DELETEFROMbatch_testWHEREidIN(71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90)*Deleting10rows...query:DELETEFROMbatch_testWHEREidIN(91,92,93,94,95,96,97,98,99,100)*Selectingdata...query:SELECTidasidFROMbatch_testWHEREnumber>30ANDdateISNULLANDid>100ORDERBYidLIMIT10000*Nomorerowstomodify!*ProgramexitedUsageusage:pg_batch[-h][-HHOST][-PPORT]-UUSER[-pPASSWORD]-dDATABASE-tTABLE[-idPRIMARY_KEY]-wWHERE[-sSET][-rbzREAD_BATCH_SIZE][-wbzWRITE_BATCH_SIZE][-SSLEEP][-a{update,delete}][-n]optionalarguments:-h,--helpshowthishelpmessageandexit-HHOST,--hostHOSTPostgreSQLserverhost-PPORT,--portPORTPostgreSQLserverport-UUSER,--userUSERPostgreSQLuser-pPASSWORD,--passwordPASSWORDPostgreSQLpassword-dDATABASE,--databaseDATABASEPostgreSQLdatabasename-tTABLE,--tableTABLEPostgreSQLtable-idPRIMARY_KEY,--primary_keyPRIMARY_KEYNameoftheprimarykeycolumn-wWHERE,--whereWHERESelectWHEREclause-sSET,--setSETUpdateSETclause-rbzREAD_BATCH_SIZE,--read_batch_sizeREAD_BATCH_SIZESelectbatchsize-wbzWRITE_BATCH_SIZE,--write_batch_sizeWRITE_BATCH_SIZEUpdate/deletebatchsize-SSLEEP,--sleepSLEEPSleepaftereachbatch-a{update,delete},--action{update,delete}Action('update'or'delete')-n,--no_confirmDon'taskforconfirmationbeforetorunthewritequeriesLicenseThis program is under MIT license (view license).