postgresql_16:9.6.23.2 Operators and Functions
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > ltree — hierarchical tree-like data type > Operators and Functions
Type ltree has the usual comparison operators =, <>, <, >, <=, >=. Comparison sorts in the order of a tree traversal, with the children of a node sorted by label text. In addition, the specialized operators shown in (see ltree-op-table) are available.
| Operator Description | | --- | | ltree @> ltree boolean Is left argument an ancestor of right (or equal)? | | ltree <@ ltree boolean Is left argument a descendant of right (or equal)? | | ltree ~ lquery boolean lquery ~ ltree boolean Does ltree match lquery? | | ltree ? lquery[] boolean lquery[] ? ltree boolean Does ltree match any lquery in array? | | ltree @ ltxtquery boolean ltxtquery @ ltree boolean Does ltree match ltxtquery? | | ltree \|\| ltree ltree Concatenates ltree paths. | | ltree \|\| text ltree text \|\| ltree ltree Converts text to ltree and concatenates. | | ltree[] @> ltree boolean ltree <@ ltree[] boolean Does array contain an ancestor of ltree? | | ltree[] <@ ltree boolean ltree @> ltree[] boolean Does array contain a descendant of ltree? | | ltree[] ~ lquery boolean lquery ~ ltree[] boolean Does array contain any path matching lquery? | | ltree[] ? lquery[] boolean lquery[] ? ltree[] boolean Does ltree array contain any path matching any lquery? | | ltree[] @ ltxtquery boolean ltxtquery @ ltree[] boolean Does array contain any path matching ltxtquery? | | ltree[] ?@> ltree ltree Returns first array entry that is an ancestor of ltree, or NULL if none. | | ltree[] ?<@ ltree ltree Returns first array entry that is a descendant of ltree, or NULL if none. | | ltree[] ?~ lquery ltree Returns first array entry that matches lquery, or NULL if none. | | ltree[] ?@ ltxtquery ltree Returns first array entry that matches ltxtquery, or NULL if none. |
The operators <@, @>, @ and ~ have analogues ^<@, ^@>, ^@, ^~, which are the same except they do not use indexes. These are useful only for testing purposes.
The available functions are shown in (see ltree-func-table).
| Function Description Example(s) | | --- | | subltree ( ltree, start integer, end integer ) ltree Returns subpath of ltree from position start to position end-1 (counting from 0). subltree('Top.Child1.Child2', 1, 2) Child1 | | subpath ( ltree, offset integer, len integer ) ltree Returns subpath of ltree starting at position offset, with length len. If offset is negative, subpath starts that far from the end of the path. If len is negative, leaves that many labels off the end of the path. subpath('Top.Child1.Child2', 0, 2) Top.Child1 | | subpath ( ltree, offset integer ) ltree Returns subpath of ltree starting at position offset, extending to end of path. If offset is negative, subpath starts that far from the end of the path. subpath('Top.Child1.Child2', 1) Child1.Child2 | | nlevel ( ltree ) integer Returns number of labels in path. nlevel('Top.Child1.Child2') 3 | | index ( a ltree, b ltree ) integer Returns position of first occurrence of b in a, or -1 if not found. index('0.1.2.3.5.4.5.6.8.5.6.8', '5.6') 6 | | index ( a ltree, b ltree, offset integer ) integer Returns position of first occurrence of b in a, or -1 if not found. The search starts at position offset; negative offset means start -offset labels from the end of the path. index('0.1.2.3.5.4.5.6.8.5.6.8', '5.6', -4) 9 | | text2ltree ( text ) ltree Casts text to ltree. | | ltree2text ( ltree ) text Casts ltree to text. | | lca ( ltree , ltree , ... ) ltree Computes longest common ancestor of paths (up to 8 arguments are supported). lca('1.2.3', '1.2.3.4.5.6') 1.2 | | lca ( ltree[] ) ltree Computes longest common ancestor of paths in array. lca(array['1.2.3'::ltree,'1.2.3.4']) 1.2 |