<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
One of my developers asked me for a Postgres equivalent to Oracle's
"CONNECT BY" clause. Sure enough, there is a module called "tablefunc"
which contains several overloaded incarnations of the "connectby"
function. I installed some well known tables, usually used to
demonstrate the "CONNECT BY" <br>
oracle clause. The relevant table is this one:<br>
<br>
<blockquote><font color="#3333ff"><tt>scott=> select * from emp;</tt><br>
<tt> empno | ename | job | mgr | hiredate | sal |
comm | </tt><br>
<tt>deptno </tt><br>
<tt>-------+--------+-----------+------+---------------------+------+------</tt><br>
<tt>+--------</tt><br>
<tt> 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450
| </tt><br>
<tt>| 10</tt><br>
<tt> 7839 | KING | PRESIDENT | | 1981-11-17 00:00:00 | 5000
| </tt><br>
<tt>| 10</tt><br>
<tt> 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300
| </tt><br>
<tt>| 10</tt><br>
<tt> 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800
| </tt><br>
<tt>| 20</tt><br>
<tt> 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600
| 300 </tt><br>
<tt>| 30</tt><br>
<tt> 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250
| 500 </tt><br>
<tt>| 30</tt><br>
<tt> 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975
| </tt><br>
<tt>| 20</tt><br>
<tt> 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 |
1400 </tt><br>
<tt>| 30</tt><br>
<tt> 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850
| </tt><br>
<tt>| 30</tt><br>
<tt> 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000
| </tt><br>
<tt>| 20</tt><br>
<tt> 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500
| 0 </tt><br>
<tt>| 30</tt><br>
<tt> 7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100
| </tt><br>
<tt>| 20</tt><br>
<tt> 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950
| </tt><br>
<tt>| 30</tt><br>
<tt> 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000
| </tt><br>
<tt>| 20</tt><br>
<tt>(14 rows)</tt><br>
</font></blockquote>
<br>
Some of you might even recognize this table, it is frequently used in
the Oracle courses. True enough, connectby function works flawlessly:<br>
<br>
<blockquote><font color="#3333ff"><tt>scott=> select empno,mgr,level<br>
from connectby('emp','empno','mgr','7839',0)<br>
as t(empno int,mgr int,level int)<br>
scott-> order by level;<br>
empno | mgr | level <br>
-------+------+-------<br>
7839 | | 0<br>
7782 | 7839 | 1<br>
7566 | 7839 | 1<br>
7698 | 7839 | 1<br>
7900 | 7698 | 2<br>
7934 | 7782 | 2<br>
7788 | 7566 | 2<br>
7902 | 7566 | 2<br>
7654 | 7698 | 2<br>
7844 | 7698 | 2<br>
7499 | 7698 | 2<br>
7521 | 7698 | 2<br>
7369 | 7902 | 3<br>
7876 | 7788 | 3<br>
(14 rows)<br>
Time: 3.218 ms<br>
scott=> <br>
</tt></font></blockquote>
<br>
My question, however is the following: why is "connectby" needed at
all? Postgres supports ANSI standard recursive "WITH" clause which
makes the following query possible:<br>
<blockquote><font color="#3333ff"><tt>scott=> with recursive
e(empno,mgr,level) as (<br>
select empno,mgr,0 from emp where empno=7839<br>
union<br>
select emp.empno,emp.mgr,e.level+1<br>
from emp,e<br>
where emp.mgr=e.empno)<br>
select * from e;<br>
empno | mgr | level <br>
-------+------+-------<br>
7839 | | 0<br>
7782 | 7839 | 1<br>
7566 | 7839 | 1<br>
7698 | 7839 | 1<br>
7934 | 7782 | 2<br>
7499 | 7698 | 2<br>
7521 | 7698 | 2<br>
7654 | 7698 | 2<br>
7788 | 7566 | 2<br>
7844 | 7698 | 2<br>
7900 | 7698 | 2<br>
7902 | 7566 | 2<br>
7369 | 7902 | 3<br>
7876 | 7788 | 3<br>
(14 rows)<br>
<br>
Time: 2.661 ms<br>
scott=> </tt></font><br>
</blockquote>
<br>
It even executes faster than the fancy module, calling a C library
function. Does anybody here use the connectby function and if so, why?
Granted, it's somewhat cleaner to write the "connectby", but not much.
Aesthetics alone does not justify its existence. My postgres version is
8.4.3.<br>
<br>
<pre class="moz-signature" cols="72">--
Mladen Gogala
Sr. Oracle DBA
1500 Broadway
New York, NY 10036
(212) 329-5251
<a class="moz-txt-link-freetext" href="http://www.vmsinfo.com">http://www.vmsinfo.com</a>
The Leader in Integrated Media Intelligence Solutions
</pre>
</body>
</html>