Re: seemingly slow for-loop in plpgsql - Mailing list pgsql-sql

From Tom Lane
Subject Re: seemingly slow for-loop in plpgsql
Date
Msg-id 7518.1220395649@sss.pgh.pa.us
Whole thread Raw
In response to seemingly slow for-loop in plpgsql  ("Claus Guttesen" <kometen@gmail.com>)
Responses Re: seemingly slow for-loop in plpgsql
List pgsql-sql
"Claus Guttesen" <kometen@gmail.com> writes:
> create or replace function update_hashcode() returns setof duplicates as
> $body$
> declare
>   d duplicates%rowtype;
>   h text;
> begin
>   for d in select * from duplicates where length(hashcode) = 33 loop
>     h := rtrim(d.hashcode, E'\n');
>     update duplicates set hashcode = h where id = d.id;
>     return next d;
>   end loop;
> end
> $body$
> language 'plpgsql' ;

Why in the world are you using a for-loop for this at all?  It would be
tremendously faster as a single SQL command:

update duplicates set hashcode = rtrim(hashcode, E'\n') where length(hashcode) = 33;
        regards, tom lane


pgsql-sql by date:

Previous
From: "Claus Guttesen"
Date:
Subject: seemingly slow for-loop in plpgsql
Next
From: Achilleas Mantzios
Date:
Subject: Re: order of rows in update