pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Utility of recursive queries?
3+ messages / 3 participants
[nested] [flat]

* Utility of recursive queries?
@ 2004-04-09 14:33 James Robinson <jlrobins@socialserve.com>
  2004-04-09 16:07 ` Re: Utility of recursive queries? Josh Berkus <josh@agliodbs.com>
  2004-04-09 16:35 ` Re: Utility of recursive queries? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 3+ messages in thread

From: James Robinson @ 2004-04-09 14:33 UTC (permalink / raw)
  To: pgsql-sql

Would recursive queries be the trick to doing things like unwinding a 
linked-list to either the head or tail, with:

	create table list (
		id int primary key,
		parent int references list(id)
	);

	insert into list values (1, null);	-- head of chain in list
	insert into list values (2, 1); -- 1st child
	insert into list values (3, 2); -- second child

Given a reference to id=3, would a recursive query be the trick to 
unrolling the list to discover id=1 as the head using a SQL one-liner? 
Is discovery possible in straight SQL w/o resorting to stored 
procedures (or modifying the table schema to directly point)? And, 
finally, would any potential recursive query implementation be 
noticably more efficient that a straightforward implementation in 
plpgsql, such as:

create or replace function find_head(int) returns int as '
	DECLARE
		cur_par INT;
		prev_par INT;
	BEGIN
		prev_par := $1;
		cur_par := parent from list where id = $1;
		WHILE cur_par is not null LOOP
			prev_par := cur_par;
			cur_par := parent from list where id = prev_par;
		END LOOP;
		return prev_par;
	END;
' language 'plpgsql';


----
James Robinson
Socialserve.com




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Utility of recursive queries?
  2004-04-09 14:33 Utility of recursive queries? James Robinson <jlrobins@socialserve.com>
@ 2004-04-09 16:07 ` Josh Berkus <josh@agliodbs.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Josh Berkus @ 2004-04-09 16:07 UTC (permalink / raw)
  To: James Robinson <jlrobins@socialserve.com>; pgsql-sql

James,

> Would recursive queries be the trick to doing things like unwinding a
> linked-list to either the head or tail, with:

Yes.  Also check out contrib/ltree and contrib/tablefunc in your handy-dandy 
PostgreSQL source code.

-- 
Josh Berkus
Aglio Database Solutions
San Francisco



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Utility of recursive queries?
  2004-04-09 14:33 Utility of recursive queries? James Robinson <jlrobins@socialserve.com>
@ 2004-04-09 16:35 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2004-04-09 16:35 UTC (permalink / raw)
  To: James Robinson <jlrobins@socialserve.com>; +Cc: pgsql-sql

James Robinson <jlrobins@socialserve.com> writes:
> Given a reference to id=3, would a recursive query be the trick to 
> unrolling the list to discover id=1 as the head using a SQL one-liner? 

I think you could do it, but don't have the syntax in my head.

> would any potential recursive query implementation be 
> noticably more efficient that a straightforward implementation in 
> plpgsql

Most likely not ...

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-04-09 16:35 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-04-09 14:33 Utility of recursive queries? James Robinson <jlrobins@socialserve.com>
2004-04-09 16:07 ` Josh Berkus <josh@agliodbs.com>
2004-04-09 16:35 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox