Skip site navigation (1) Skip section navigation (2)

final patch - plpgsql: for-in-array

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: final patch - plpgsql: for-in-array
Date: 2010-09-30 12:46:02
Message-ID: AANLkTinqzqUCy++vpkN27=8dr2ADuoz0L9nSRozADt6J@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-hackers
Hello

this patch implement a new iteration construct - iteration over an
array. The sense of this new iteration is:
  * a simple and cleaner syntax
  * a faster execution - this bring down a number of detoast operations

create or replace function subscripts(anyarray, int)
returns int[] as $$
select array(select generate_subscripts($1,$2));
$$ language sql;

create or replace function fora_test()
returns int as $$
declare x int; s int = 0;
   a int[] := array[1,2,3,4,5,6,7,8,9,10];
begin
  for x in array subscripts(a, 1)
  loop
    s := s + a[x];
  end loop;
  return s;
end;
$$ language plpgsql;

create or replace function fora_test()
returns int as $$
declare x int; s int = 0;
begin
  for x in array array[1,2,3,4,5,6,7,8,9,10]
  loop
    s := s + x;
  end loop;
  return s;
end;
$$ language plpgsql;

create or replace function fora_test()
returns int as $$
declare x int; y int;
   a fora_point[] := array[(1,2),(3,4),(5,6)];
begin
  for x, y in array a
  loop
    raise notice 'point=%,%', x, y;
  end loop;
  return 0;
end;
$$ language plpgsql;

Regards

Pavel Stehule

Attachment: for-in-array
Description: application/octet-stream (19.8 KB)

Responses

pgsql-hackers by date

Next:From: David FetterDate: 2010-09-30 13:35:42
Subject: Re: is sync rep stalled?
Previous:From: Andrew GeeryDate: 2010-09-30 12:45:34
Subject: Re: starting to review the Extend NOT NULL representation to pg_constraint patch

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group