Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1spWUl-0025uT-H8 for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 17:23:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1spWUk-000BIu-7v for pgsql-general@arkaria.postgresql.org; Sat, 14 Sep 2024 17:23:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1spWUj-000BIm-PW for pgsql-general@lists.postgresql.org; Sat, 14 Sep 2024 17:23:37 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1spWUf-001BNk-Sc for pgsql-general@lists.postgresql.org; Sat, 14 Sep 2024 17:23:37 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5c3d209db94so3753924a12.3 for ; Sat, 14 Sep 2024 10:23:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1726334613; x=1726939413; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=g02YxK1nFlotHB4GRJMvd9GWVeiOqj9CH3eggDElAXU=; b=UyU6+xc1RB8KMjMuyglTJZ+xxfHC2YZ9y1NWNgrjz95XXgAN0iiXRN9TXYHZ5q93nP sCU+h26hHuGFwp0rSuLnTaCyWhKR5NWD5zg2zzP5P5qXApDEYYUVR5LZbGjHaA6g08pA 4fSYegFDORJV0bVnOKMZz078PjpVIFIvvW5N3xNRArl68jS1NdrNbgJy0YTo6dbfle8e o2EPgxvSWW4JCE1fYGw2/nsVs4bWzj9dFSZ01d623ipDc07bQ10nQC/WcR3AkeLTYxNn jBIWaD7/wcHIoWZ/E4w5tHC7PG50BbouaFrcI12ORLojfLMg+HnGzeFJkEb+E7Dn6J6H /sHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726334613; x=1726939413; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=g02YxK1nFlotHB4GRJMvd9GWVeiOqj9CH3eggDElAXU=; b=FntxQCsfZP7MT1J5cMkb+0DjEW1NIdhKsEXKU2Vwtsu3O1xc+0muv4PeG4aCc7AoDP 4zALycE5Q2xLMbuDLD0iEuT7xn4TkBvU/ehqYDXuE4QIcT/A77OHdP0eiSshpbX3iO5o t/sWR5tXGNVZPrA1L7Q7rCexN7XbchbgosE7QvFi5aoTxS2ZxZSIZfquv1N8aTFq9DrE o9qByGWxzadXMHoF2y5etUTLUcbufkXUH7+Ef5X98uz/SmIl/2TH55xdmKsgqKmjaP97 +6wgcd3DJlyTgnRJr+Jdj3LbgOO/ZTJbk2n3KogG+Ih3nsqysWr0v4uR9CeiAMHlZHr1 4PnQ== X-Gm-Message-State: AOJu0Yy0nepmeTHX+57/d4AgeTqOYtlhYfjfcwgxVPQ6agb8j4bUnsA7 XZF0gmLkK3VNnLNp7cgPYazlpgChhfSG79NbbQ/IKoYc+Zifk0i/F0zMh13ZyUQ5JqGUXet0C0Z X X-Google-Smtp-Source: AGHT+IHY+4Rs0Imki4e5TvIHcXxYN4Lq70okTCTv/xlzUwXwuEPpnDqsT7pUaK3M4ZRv4+xeTXjugg== X-Received: by 2002:a17:907:6d28:b0:a8d:1655:a423 with SMTP id a640c23a62f3a-a9029678c66mr1167454766b.56.1726334612347; Sat, 14 Sep 2024 10:23:32 -0700 (PDT) Received: from dynamic-pd01.res.v6.highway.a1.net ([2001:871:260:35ac:ea40:e2b4:803a:2aad]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a90610f4443sm101868366b.75.2024.09.14.10.23.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 10:23:32 -0700 (PDT) Message-ID: <37bc4217078946546b62c6ca2f2e458b798b1f36.camel@cybertec.at> Subject: Re: update faster way From: Laurenz Albe To: yudhi s Cc: pgsql-general Date: Sat, 14 Sep 2024 19:23:31 +0200 In-Reply-To: References: <1955d0e6cafd643520d282a74d9956340983074e.camel@cybertec.at> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 2024-09-14 at 16:10 +0530, yudhi s wrote: > > However, the strain on your system's resources and particularly the row > > locks will impair normal database work. > >=20 > > Essentially, you can either take an extended down time or perform the u= pdates > > in very small chunks with a very low "lock_timeout" over a very long pe= riod > > of time.=C2=A0 If any of the batches fails because of locking conflicts= , it has > > to be retried. > >=20 > > Investigate with EXPLAIN (ANALYZE) why the updates take that long.=C2= =A0 It could > > be a lame disk, tons of (unnecessary?) indexes or triggers, but it migh= t as > > well be the join with the lookup table, so perhaps there is room for > > improvement (more "work_mem" for a hash join?) >=20 > We have mostly insert/update happen on current day/live partition. So > considering that, if we will run batch updates(with batch size of 1000) f= rom > five different sessions in parallel on different historical partition, at= any > time they will lock 5000 rows and then commit. And also those rows will n= ot > collide with each other. So do you think that approach can anyway cause l= ocking > issues? The updates won't lock with each other. I thought that other database acti= vity might modify rows in these partitions. If that is not the case, you don't = need to worry about locks. In that case I would also choose a much higher batch size. You should make sure to back off every now and then and VACUUM the partitio= n, so that you avoid excessive table bloat. > We will ensure the update of live partition occurs when we have least act= ivity. > So in that way we will not need extended down time. Please correct me if = wrong. That sounds right. > Never used lock_timeout though, but in above case do we need lock_timeout= ? It can be useful if your updating process is blocked by a lock from the application. Setting the parameter to a low value will keep your update from hanging for a long time and will throw an error instead. Erroring out early reduces the danger of a deadlock. > Regarding batch update with batch size of 1000, do we have any method exi= sts > in postgres (say like forall statement in Oracle) which will do the batch= dml. > Can you please guide me here, how we can do it in postgres. I would certainly not perform the update row for row in PL/pgSQL code. Perhaps something like this: DO $$DECLARE i bigint; BEGIN FOR i IN 1..1000000 by 100000 LOOP UPDATE tab SET ... WHERE id >=3D i AND id < i + 100000; COMMIT; END LOOP; END;$$; VACUUM tab; Then repeat for the next million rows, and so on. > And yes will need to see what happens in the update using explain analyze= . > And I was trying to see, if we can run explain analyze without doing > actual update , but seems that is not possible.=C2=A0 You can do it in a transaction and roll the transaction back. Yours, Laurenz Albe