pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Raghavendra Rao J S V <raghavendrajsv@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Function execution is taking more than 2hrs
Date: Tue, 06 Feb 2018 12:09:25 +0100
Message-ID: <1517915365.2849.14.camel@cybertec.at> (raw)
In-Reply-To: <CAEHH7R7vfZTwE9JBLTOs+Jn1uPQS6ZD9zGCkUGc0akBorjNYuA@mail.gmail.com>
References: <CAEHH7R7vfZTwE9JBLTOs+Jn1uPQS6ZD9zGCkUGc0akBorjNYuA@mail.gmail.com>

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




view thread (2+ messages)

Message-ID: <1517915365.2849.14.camel@cybertec.at>
Permalink:  ../1517915365.2849.14.camel@cybertec.at/
Also on:    postgresql.org/message-id/1517915365.2849.14.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, raghavendrajsv@gmail.com
  Subject: Re: Function execution is taking more than 2hrs
  In-Reply-To: <1517915365.2849.14.camel@cybertec.at>
  References: <CAEHH7R7vfZTwE9JBLTOs+Jn1uPQS6ZD9zGCkUGc0akBorjNYuA@mail.gmail.com> <1517915365.2849.14.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox