Re: General Postgres performance tips when using ARRAY

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: bubba postgres <bubba(dot)postgres(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: General Postgres performance tips when using ARRAY
Date: 2011-05-26 02:29:08
Message-ID: BANLkTimiZ8NwxEo5ub_HFAe6wun1K_OagA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, May 25, 2011 at 9:17 PM, bubba postgres
<bubba(dot)postgres(at)gmail(dot)com> wrote:
>
> So, what are the gotcha's around manipulating Arrays in stored procs?
> It seems reasonable that an array_cat /etc would cause the creation of a new
> array, but does mutating an existing array also create a copy?

Never, ever, if at all possible, build arrays with array_cat, ||
operator, etc. Try not to work with arrays iteratively. It will be
very slow. You have better options:

1. subquery array constructor:
array_var := array(select bar from foo where ...);

2. array_agg()
select into array_var array_agg(bar) from foo where ...

3. values array constructor:
array_var := array[1, 2, 3];

don't forget, in recent postgres, you can make arrays of composite
types as well, and also nest:
complex_type := array(select row(bar, array(select baz from bat where
..)) from foo);

For expanding arrays prefer unnest() and check out the coming 9.1
foreach feature
(http://www.postgresql.org/docs/9.1/static/plpgsql-control-structures.html#PLPGSQL-FOREACH-ARRAY)

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2011-05-26 02:36:31 Re: Connecting to Postgres using Windows 7
Previous Message Andy Lurie 2011-05-26 02:15:57 OHI to Sponsor PG West 2011