Re: [GENERAL] join from array or cursor

2009-08-22 Thread Greg Stark
On Sun, Aug 23, 2009 at 1:30 AM, John DeSoi wrote: > While it avoids the sort of my method, it appears to be almost 5 times > slower (about 4000 keys in the cursor, Postgres 8.4.0): > > > Function Scan on cursor_pk arr  (cost=0.00..116011.72 rows=1000 width=4) > (actual time=13.561..249.916 rows=43

Re: [GENERAL] join from array or cursor

2009-08-22 Thread John DeSoi
On Aug 21, 2009, at 9:22 AM, Greg Stark wrote: Of course immediately upon hitting send I did think of a way: SELECT (r).* FROM (SELECT (SELECT x FROM x WHERE a=id) AS r FROM unnest(array[1,2]) AS arr(id) ) AS subq; Thanks to all for the interesting insights and discussion. Wh

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Sam Mason
On Fri, Aug 21, 2009 at 01:58:34PM -0400, Merlin Moncure wrote: > On Fri, Aug 21, 2009 at 1:13 PM, Sam Mason wrote: > > On Fri, Aug 21, 2009 at 12:05:51PM -0400, Tom Lane wrote: > >> We might be able to do that based on the row-returning-subselect > >> infrastructure being discussed over here: > >>

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Merlin Moncure
On Fri, Aug 21, 2009 at 1:13 PM, Sam Mason wrote: > On Fri, Aug 21, 2009 at 12:05:51PM -0400, Tom Lane wrote: >> Sam Mason writes: >> > ... PG should instead arrange that the expression >> > "t" is run exactly once and reuse the single result for all columns. >> >> We might be able to do that base

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Sam Mason
On Fri, Aug 21, 2009 at 12:05:51PM -0400, Tom Lane wrote: > Sam Mason writes: > > ... PG should instead arrange that the expression > > "t" is run exactly once and reuse the single result for all columns. > > We might be able to do that based on the row-returning-subselect > infrastructure being

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Tom Lane
Sam Mason writes: > ... PG should instead arrange that the expression > "t" is run exactly once and reuse the single result for all columns. We might be able to do that based on the row-returning-subselect infrastructure being discussed over here: http://archives.postgresql.org/message-id/4087.12

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Sam Mason
On Fri, Aug 21, 2009 at 10:49:52AM -0400, Merlin Moncure wrote: > On Fri, Aug 21, 2009 at 10:17 AM, Sam Mason wrote: > > CREATE TYPE foo AS ( i int, j int ); > > > > SELECT (id((SELECT (1,2)::foo))).*; > > > > or am I missing something obvious? > > I think that what you are bumping in to is th

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Merlin Moncure
On Fri, Aug 21, 2009 at 10:17 AM, Sam Mason wrote: > On Fri, Aug 21, 2009 at 02:22:54PM +0100, Greg Stark wrote: >>  SELECT (r).* >>   FROM (SELECT (SELECT x FROM x WHERE a=id) AS r >>           FROM unnest(array[1,2]) AS arr(id) >>        ) AS subq; > > Shouldn't that second inner SELECT unnecessa

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Sam Mason
On Fri, Aug 21, 2009 at 02:22:54PM +0100, Greg Stark wrote: > SELECT (r).* > FROM (SELECT (SELECT x FROM x WHERE a=id) AS r > FROM unnest(array[1,2]) AS arr(id) >) AS subq; Shouldn't that second inner SELECT unnecessary? I'd be tempted to write: SELECT ((SELECT x FROM x WH

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Merlin Moncure
On Fri, Aug 21, 2009 at 9:22 AM, Greg Stark wrote: > Of course immediately upon hitting send I did think of a way: > >  SELECT (r).* >  FROM (SELECT (SELECT x FROM x WHERE a=id) AS r >          FROM unnest(array[1,2]) AS arr(id) >       ) AS subq; nice use of composite type in select-list subquery

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Greg Stark
On Fri, Aug 21, 2009 at 2:16 PM, Greg Stark wrote: > On Fri, Aug 21, 2009 at 1:16 PM, John DeSoi wrote: >> >> Yes, this is the best I have come up with so far. I have a set returning >> function which returns the key and the index number. The implementation with >> a cursor looks like this: >> >> S

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Greg Stark
On Fri, Aug 21, 2009 at 1:16 PM, John DeSoi wrote: > > Yes, this is the best I have come up with so far. I have a set returning > function which returns the key and the index number. The implementation with > a cursor looks like this: > > SELECT * FROM cursor_pk('c1') c LEFT JOIN foo ON (c.pk = foo

Re: [GENERAL] join from array or cursor

2009-08-21 Thread John DeSoi
On Aug 21, 2009, at 7:26 AM, Sam Mason wrote: It may help to wrap the generate_series call into a function so you don't have to refer to "myPkArray" so many times. Yes, this is the best I have come up with so far. I have a set returning function which returns the key and the index number. T

Re: [GENERAL] join from array or cursor

2009-08-21 Thread Sam Mason
On Thu, Aug 20, 2009 at 11:15:12PM -0400, John DeSoi wrote: > Suppose I have an integer array (or cursor with one integer column) > which represents primary keys of some table. Is there a simple and > efficient way to return the rows of the table corresponding to the > primary key values and

Re: [GENERAL] join from array or cursor

2009-08-20 Thread Scott Bailey
John DeSoi wrote: Suppose I have an integer array (or cursor with one integer column) which represents primary keys of some table. Is there a simple and efficient way to return the rows of the table corresponding to the primary key values and keep them in the same order as the array (or cursor

[GENERAL] join from array or cursor

2009-08-20 Thread John DeSoi
Suppose I have an integer array (or cursor with one integer column) which represents primary keys of some table. Is there a simple and efficient way to return the rows of the table corresponding to the primary key values and keep them in the same order as the array (or cursor)? Seems like i