RefLib

Reference library for AI

Back

postgresql_16:9.6.23.1 Definitions

PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > ltree — hierarchical tree-like data type > Definitions

A *label* is a sequence of alphanumeric characters, underscores, and hyphens. Valid alphanumeric character ranges are dependent on the database locale. For example, in C locale, the characters A-Za-z0-9_- are allowed. Labels must be no more than 1000 characters long.

Examples: 42, Personal_Services

A *label path* is a sequence of zero or more labels separated by dots, for example L1.L2.L3, representing a path from the root of a hierarchical tree to a particular node. The length of a label path cannot exceed 65535 labels.

Example: Top.Countries.Europe.Russia

The ltree module provides several data types:

Both star symbols and simple words can be quantified to restrict how many labels they can match: *{*n*} Match exactly *n* labels *{*n*,} Match at least *n* labels *{*n*,*m*} Match at least *n* but not more than *m* labels *{,*m*} Match at most *m* labels — same as *{0,*m*} foo{*n*,*m*} Match at least *n* but not more than *m* occurrences of foo foo{,} Match any number of occurrences of foo, including zero In the absence of any explicit quantifier, the default for a star symbol is to match any number of labels (that is, {,}) while the default for a non-star item is to match exactly once (that is, {1}).

There are several modifiers that can be put at the end of a non-star lquery item to make it match more than just the exact match: @ Match case-insensitively, for example a@ matches A * Match any label with this prefix, for example foo* matches foobar % Match initial underscore-separated words The behavior of % is a bit complicated. It tries to match words rather than the entire label. For example foo_bar% matches foo_bar_baz but not foo_barbaz. If combined with *, prefix matching applies to each word separately, for example foo_bar%* matches foo1_bar2_baz but not foo1_br2_baz.

Also, you can write several possibly-modified non-star items separated with | (OR) to match any of those items, and you can put ! (NOT) at the start of a non-star group to match any label that doesn't match any of the alternatives. A quantifier, if any, goes at the end of the group; it means some number of matches for the group as a whole (that is, some number of labels matching or not matching any of the alternatives).

Here's an annotated example of lquery: Top.*{0,2}.sport*@.!football|tennis{1,}.Russ*|Spain a. b. c. d. e. This query will match any label path that:

1. begins with the label Top 2. and next has zero to two labels before 3. a label beginning with the case-insensitive prefix sport 4. then has one or more labels, none of which match football nor tennis 5. and then ends with a label beginning with Russ or exactly matching Spain.

Here's an example ltxtquery: Europe & Russia*@ & !Transportation This will match paths that contain the label Europe and any label beginning with Russia (case-insensitive), but not paths containing the label Transportation. The location of these words within the path is not important. Also, when % is used, the word can be matched to any underscore-separated word within a label, regardless of position.

Note: ltxtquery allows whitespace between symbols, but ltree and lquery do not.

Back