[SQL] unnesting of array of different size explodes memory

2011-04-13 Thread Andreas Gaab
Hi, I need to sum up the elements of two arrays. Most arrays were of the same size, thus I just unnested the arrays in one table and grouped the results in a loop for every row. When two arrays of different size (1200 and 1300) were processed, the memory usage exploded and the query was killed

Re: [SQL] unnesting of array of different size explodes memory

2011-04-13 Thread Tom Lane
Andreas Gaab writes: > As I now understand, the following query leads to 12 results, not just 4 (or > 3...): > SELECT unnest(ARRAY[1,2,3]), unnest(ARRAY[4,5,6,7]); > Why could postgres use as much memory till the kernel complained when > unnesting 1200 and 1300 elements resulting in 1.6e6 rows

Re: [SQL] pass in array to function for use by where clause? how optimize?

2011-04-13 Thread Tom Lane
Anish Kejariwal writes: > (select store_id, avg(sales) sales > from store > where group_id in(select $1[i] from generate_subscripts($1, 1) g(i)) Seems like a pretty brute-force way to deal with the array. Try where group_id = any($1) Not sure if it'll be a great deal faster, but it's at least

Re: [SQL] pass in array to function for use by where clause? how optimize?

2011-04-13 Thread Rob Sargent
On 04/13/2011 09:09 AM, Tom Lane wrote: Anish Kejariwal writes: (select store_id, avg(sales) sales from store where group_id in(select $1[i] from generate_subscripts($1, 1) g(i)) Seems like a pretty brute-force way to deal with the array. Try where group_id = any($1) Not sure if it'll be

Re: [SQL] pass in array to function for use by where clause? how optimize?

2011-04-13 Thread Tom Lane
Rob Sargent writes: > On 04/13/2011 09:09 AM, Tom Lane wrote: >> Anish Kejariwal writes: >>> (select store_id, avg(sales) sales >>> from store >>> where group_id in(select $1[i] from generate_subscripts($1, 1) g(i)) >> Seems like a pretty brute-force way to deal with the array. Try >> where gro

[SQL] Re: [SQL] unnesting of array of different size explodes memory

2011-04-13 Thread Charlie
Another approach to consider is using strings. E.g. The function assembles the underlying query as a string and returns the results of executing the string. Passing the groups in a string may be straightforward in your application, or not. There was a thread in the novice forum last year abou

Re: [SQL] pass in array to function for use by where clause? how optimize?

2011-04-13 Thread Anish Kejariwal
Thanks Tom! I've never used the any function before. It totally fixed the issue. It's easier to code, makes more sense, and it fixed my performance issue. My guess is that the optimizer does not how to deal with generate_subscripts?? Thanks!! Anish On Wed, Apr 13, 2011 at 9:15 AM, Tom Lane

[SQL] strange update behavior

2011-04-13 Thread Volkmar Herbst GCI
I did encounter a quite strange behavior during update which I did not understand: The following query updated table parcelle with only one entry for all rows: update cadastre.parcelle set code_unique = t2.code_unique from cadastre.parcelle t1, cadastre.propriete t2 where t1.uoid =