Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ej189-0007HJ-PF for pgsql-general@arkaria.postgresql.org; Tue, 06 Feb 2018 11:09:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ej188-0003Rj-EP for pgsql-general@arkaria.postgresql.org; Tue, 06 Feb 2018 11:09:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ej188-0003Ra-6k for pgsql-general@lists.postgresql.org; Tue, 06 Feb 2018 11:09:40 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ej184-000810-Oj for pgsql-general@lists.postgresql.org; Tue, 06 Feb 2018 11:09:39 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 4916542A0D; Tue, 6 Feb 2018 12:09:35 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id hVYfiTToQ_R3; Tue, 6 Feb 2018 12:09:28 +0100 (CET) Message-ID: <1517915365.2849.14.camel@cybertec.at> Subject: Re: Function execution is taking more than 2hrs From: Laurenz Albe To: Raghavendra Rao J S V , pgsql-general@lists.postgresql.org Date: Tue, 06 Feb 2018 12:09:25 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Raghavendra Rao J S V wrote: > Device id column logic has changed in my organization. > Therefore, I need to modify all the old device id values to new device id value > of the tables which contains the device id column. > Old device id and new device id columns are mapped in “old_new_deviceids” table. > > There are twenty tables which contains device id column. > Therefore I need to modify the device id’s in all those twenty tables based on “old_new_deviceids” table . > Each and every table will contains around 2Lakhs records. That is a sign of a bad design. Any value that can change should only occur once in the database. 200000 rows is not a lot; you should stick with international measures to make yourself understood. > I have created a dynamic procedure using EXECUTE FORMAT ,which accepts table name and column name as input parameter as below. > > CREATE OR REPLACE FUNCTION pop_endpoints_with_new_deviceid() > RETURNS void > AS $$ [...] > PERFORM pop_new_deviceid_for_table(‘Table_Name1','deviceid'); [...] > $$ LANGUAGE plpgsql; > > > CREATE OR REPLACE FUNCTION pop_new_deviceid_for_table(p_table varchar,p_column varchar) > RETURNS void > AS $$ [...] > v_select:='SELECT distinct t2.deviceid_old,t2.deviceid_new > FROM '|| p_table ||' t1,endpoint_deviceids_barediscovery t2 > WHERE t1.'||p_column||'=t2.deviceid_old [...] > FOR rec IN EXECUTE v_select LOOP [...] > EXECUTE FORMAT('UPDATE %I set %I = %s where %I=%s',p_table,p_column,rec.deviceid_new,p_column,rec.deviceid_old); [...] > END LOOP; [...] > $$ LANGUAGE plpgsql; [...] > Sometimes “select pop_endpoints_with_new_deviceid()” is taking just 5 minutes > and some times more than 2hrs 25 minutes. how to narrow down the issue The problem is clear. Rather than changing all rows with a single UPDATE statement, you perform one UPDATE per row. > How to tack the time taken by each function in postgres? You could use pg_stat_statements with pg_stat_statements.track = all or use PL Profiler: https://bitbucket.org/openscg/plprofiler Yours, Laurenz Albe