connectby(text relname, text keyid_fld, text parent_keyid_fld
[, text orderby_fld], text start_with, int max_depth
[, text branch_delim])
Inputs
relname
Name of the source relation
keyid_fld
Name of the key field
parent_keyid_fld
Name of the key_parent field
orderby_fld
If optional ordering of siblings is desired:
Name of the field to order siblings
start_with
root value of the tree input as a text value regardless of keyid_fld type
max_depth
zero (0) for unlimited depth, otherwise restrict level to this depth
branch_delim
If optional branch value is desired, this string is used as the delimiter.
When not provided, a default value of ‘~’ is used for internal
recursion detection only, and no “branch” field is returned.
Outputs
Returns setof record, which must defined with a column definition
in the FROM clause of the SELECT statement, e.g.:
SELECT * FROM connectby(‘connectby_tree’, ‘keyid’, ‘parent_keyid’, ‘row2’, 0, ‘~’)
AS t(keyid text, parent_keyid text, level int, branch text);
- or -
SELECT * FROM connectby(‘connectby_tree’, ‘keyid’, ‘parent_keyid’, ‘row2’, 0)
AS t(keyid text, parent_keyid text, level int);
SELECT * FROM connectby(‘connectby_tree’, ‘keyid’, ‘parent_keyid’, ‘pos’, ‘row2’, 0, ‘~’)
AS t(keyid text, parent_keyid text, level int, branch text, pos int);
- or -
SELECT * FROM connectby(‘connectby_tree’, ‘keyid’, ‘parent_keyid’, ‘pos’, ‘row2’, 0)
AS t(keyid text, parent_keyid text, level int, pos int);
Notes
1. keyid and parent_keyid must be the same data type
2. The column definition must include a third column of type INT4 for
the level value output
3. If the branch field is not desired, omit both the branch_delim input
parameter and the branch field in the query column definition. Note
that when branch_delim is not provided, a default value of ‘~’ is used
for branch_delim for internal recursion detection, even though the branch
field is not returned.
4. If the branch field is desired, it must be the fourth column in the query
column definition, and it must be type TEXT.
5. The parameters representing table and field names must include double
quotes if the names are mixed-case or contain special characters.
6. If sorting of siblings is desired, the orderby_fld input parameter and
a name for the resulting serial field (type INT32) in the query column
definition must be given.
Example usage
CREATE TABLE connectby_tree(keyid text, parent_keyid text, pos int);
INSERT INTO connectby_tree VALUES(‘row1’,NULL, 0);
INSERT INTO connectby_tree VALUES(‘row2’,‘row1’, 0);
INSERT INTO connectby_tree VALUES(‘row3’,‘row1’, 0);
INSERT INTO connectby_tree VALUES(‘row4’,‘row2’, 1);
INSERT INTO connectby_tree VALUES(‘row5’,‘row2’, 0);
INSERT INTO connectby_tree VALUES(‘row6’,‘row4’, 0);
INSERT INTO connectby_tree VALUES(‘row7’,‘row3’, 0);
INSERT INTO connectby_tree VALUES(‘row8’,‘row6’, 0);
INSERT INTO connectby_tree VALUES(‘row9’,‘row5’, 0);
– with branch, without orderby_fld
SELECT * FROM connectby(‘connectby_tree’, ‘keyid’, ‘parent_keyid’, ‘row2’, 0, ‘~’)
AS t(keyid text, parent_keyid text, level int, branch text);
keyid | parent_keyid | level | branch
-------±-------------±------±--------------------
row2 | | 0 | row2
row4 | row2 | 1 | row2~row4
row6 | row4 | 2 | row2~row4~row6
row8 | row6 | 3 | row2~row4~row6~row8
row5 | row2 | 1 | row2~row5
row9 | row5 | 2 | row2~row5~row9
(6 rows)