> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/appendixes/contrib/ltree.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.22. ltree — 階層式樹狀資料型別

### F.22. ltree — 階層式樹狀資料型別 [#](#ltree) <a href="#ltree" id="ltree"></a>

[F.22.1. 定義](#ltree-definitions)

[F.22.2. 運算子與函式](https://docs.postgresql.tw/19/appendixes/contrib/ltree/ltree-ops-funcs.md#ltree-ops-funcs)

[F.22.3. 索引](#ltree-indexes)

[F.22.4. 範例](#ltree-example)

[F.22.5. 轉換](#ltree-transforms)

[F.22.6. 作者](#ltree-authors)

此模組實作資料型別 `ltree`，用以表示儲存在階層式樹狀結構中資料的標籤。它提供廣泛的標籤樹搜尋功能。

此模組被視為「受信任」，亦即具有目前資料庫 `CREATE` 權限的非超級使用者可以安裝它。

#### F.22.1. 定義 [#](#ltree-definitions) <a href="#ltree-definitions" id="ltree-definitions"></a>

*標籤*是由英數字元、底線與連字號組成的序列。有效的英數字元範圍取決於資料庫地區設定。例如，在 C 地區設定中，允許字元 `A-Za-z0-9_-`。標籤長度不可超過 1000 個字元。

範例：`42`、`Personal_Services`

*標籤路徑*是零個或多個以點分隔的標籤序列，例如 `L1.L2.L3`，表示從階層樹根節點到特定節點的路徑。標籤路徑不可超過 65535 個標籤。

範例：`Top.Countries.Europe.Russia`

`ltree` 模組提供數種資料型別：

* `ltree` 儲存標籤路徑。
* `lquery` 表示類似正規表示式的模式，用於比對 `ltree` 值。簡單單字會比對路徑中的該標籤。星號符號（`*`）會比對零個或多個標籤。它們可使用點連結成必須符合整個標籤路徑的模式。例如：

  ```

  foo         比對完全相同的標籤路徑 foo
  *.foo.*     比對任何包含標籤 foo 的標籤路徑
  *.foo       比對任何最後一個標籤為 foo 的標籤路徑
  ```

  星號符號與簡單單字都可加上量詞，以限制可比對的標籤數：

  ```

  *{n}        比對恰好 n 個標籤
  *{n,}       比對至少 n 個標籤
  *{n,m}      比對至少 n 個、至多 m 個標籤
  *{,m}       比對至多 m 個標籤，等同於 *{0,m}
  foo{n,m}    比對至少 n 次、至多 m 次出現的 foo
  foo{,}      比對任意次數出現的 foo，包括零次
  ```

  沒有明確量詞時，星號符號預設比對任意數量的標籤（亦即 `{,}`），非星號項目的預設值則是恰好比對一次（亦即 `{1}`）。

  可在非星號的 `lquery` 項目結尾加上數種修飾字，使其不僅限於完全比對：

  ```

  @           不區分大小寫比對，例如 a@ 符合 A
  *           比對具有此前綴的任何標籤，例如 foo* 符合 foobar
  %           比對標籤開頭以底線分隔的單字
  ```

  `%` 的行為稍微複雜。它嘗試比對單字而非整個標籤。例如，`foo_bar%` 符合 `foo_bar_baz`，但不符合 `foo_barbaz`。若與 `*` 組合，前綴比對會分別套用於每個單字；例如，`foo_bar%*` 符合 `foo1_bar2_baz`，但不符合 `foo1_br2_baz`。

  此外，可以用 `|`（OR）分隔多個可帶有修飾字的非星號項目，以比對其中任一項目；也可在非星號群組開頭放置 `!`（NOT），以比對不符合任何選項的標籤。量詞（若有）放在群組結尾，表示整個群組的比對次數，也就是有多少個標籤符合任一選項，或不符合任何選項。

  以下是加註解的 `lquery` 範例：

  ```

  Top.*{0,2}.sport*@.!football|tennis{1,}.Russ*|Spain
  a.  b.     c.      d.                   e.
  ```

  此查詢會比對符合下列條件的任何標籤路徑：

  1. 以標籤 `Top` 開始；
  2. 接著在下列標籤之前有零至兩個標籤；
  3. 以不區分大小寫的前綴 `sport` 開始的標籤；
  4. 然後有一個或多個不符合 `football` 與 `tennis` 的標籤；
  5. 最後以 `Russ` 開頭的標籤或完全符合 `Spain` 的標籤結束。
* `ltxtquery` 表示類似全文檢索的模式，用於比對 `ltree` 值。`ltxtquery` 值包含單字，結尾可能帶有修飾字 `@`、`*`、`%`；修飾字的意義與 `lquery` 相同。單字可用 `&`（AND）、`|`（OR）、`!`（NOT）及括號組合。它與 `lquery` 的主要差異在於，`ltxtquery` 比對單字時不考慮其在標籤路徑中的位置。

  以下是 `ltxtquery` 範例：

  ```

  Europe & Russia*@ & !Transportation
  ```

  此範例會比對含有標籤 `Europe` 與任何以 `Russia` 開頭（不區分大小寫）之標籤的路徑，但不比對含有標籤 `Transportation` 的路徑。這些單字在路徑中的位置並不重要。此外，使用 `%` 時，單字可比對標籤內任一以底線分隔的單字，與位置無關。

注意：`ltxtquery` 允許符號之間有空白，但 `ltree` 與 `lquery` 不允許。

#### F.22.2. 運算子與函式 [#](#ltree-ops-funcs) <a href="#ltree-ops-funcs" id="ltree-ops-funcs"></a>

完整內容請見 [F.22.2. 運算子與函式](https://docs.postgresql.tw/19/appendixes/contrib/ltree/ltree-ops-funcs.md#ltree-ops-funcs)。

#### F.22.3. 索引 [#](#ltree-indexes) <a href="#ltree-indexes" id="ltree-indexes"></a>

`ltree` 支援數種索引，可加速下列對應運算子的操作：

* `ltree` 的 B-tree 索引： `<`, `<=`, `=`, `>=`, `>`
* `ltree` 的雜湊索引： `=`
* `ltree` 的 GiST 索引（`gist_ltree_ops` 運算子類別）： `<`, `<=`, `=`, `>=`, `>`, `@>`, `<@`, `@`, `~`, `?`

  `gist_ltree_ops` GiST 運算子類別以位元映射簽章近似表示一組路徑標籤。其選用的整數參數 `siglen` 決定簽章長度，單位為位元組。預設簽章長度為 8 位元組。長度必須是 `int` 對齊大小（大多數機器為 4 位元組）的正整數倍，且不可超過 2024 位元組。較長的簽章能使搜尋更精確（掃描較小比例的索引與較少的 heap 頁面），代價是索引較大。

  使用預設 8 位元組簽章長度建立此類索引的範例：

  ```

  CREATE INDEX path_gist_idx ON test USING GIST (path);
  ```

  使用 100 位元組簽章長度建立此類索引的範例：

  ```

  CREATE INDEX path_gist_idx ON test USING GIST (path gist_ltree_ops(siglen=100));
  ```
* `ltree[]` 的 GiST 索引（`gist__ltree_ops` 運算子類別）： `ltree[] <@ ltree`, `ltree @> ltree[]`, `@`, `~`, `?`

  `gist__ltree_ops` GiST 運算子類別的運作方式類似於 `gist_ltree_ops`，也接受簽章長度作為參數。`gist__ltree_ops` 的 `siglen` 預設值為 28 位元組。

  使用預設 28 位元組簽章長度建立此類索引的範例：

  ```

  CREATE INDEX path_gist_idx ON test USING GIST (array_path);
  ```

  使用 100 位元組簽章長度建立此類索引的範例：

  ```

  CREATE INDEX path_gist_idx ON test USING GIST (array_path gist__ltree_ops(siglen=100));
  ```

  注意：此索引類型是有損的。

#### F.22.4. 範例 [#](#ltree-example) <a href="#ltree-example" id="ltree-example"></a>

此範例使用下列資料（也可在原始碼發行套件的 `contrib/ltree/ltreetest.sql` 檔案中取得）：

```

CREATE TABLE test (path ltree);
INSERT INTO test VALUES ('Top');
INSERT INTO test VALUES ('Top.Science');
INSERT INTO test VALUES ('Top.Science.Astronomy');
INSERT INTO test VALUES ('Top.Science.Astronomy.Astrophysics');
INSERT INTO test VALUES ('Top.Science.Astronomy.Cosmology');
INSERT INTO test VALUES ('Top.Hobbies');
INSERT INTO test VALUES ('Top.Hobbies.Amateurs_Astronomy');
INSERT INTO test VALUES ('Top.Collections');
INSERT INTO test VALUES ('Top.Collections.Pictures');
INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy');
INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy.Stars');
INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy.Galaxies');
INSERT INTO test VALUES ('Top.Collections.Pictures.Astronomy.Astronauts');
CREATE INDEX path_gist_idx ON test USING GIST (path);
CREATE INDEX path_idx ON test USING BTREE (path);
CREATE INDEX path_hash_idx ON test USING HASH (path);
```

現在，資料表 `test` 已填入描述下列階層結構的資料：

```

                        Top
                     /   |  \
             Science Hobbies Collections
                 /       |              \
        Astronomy   Amateurs_Astronomy Pictures
           /  \                            |
Astrophysics  Cosmology                Astronomy
                                        /  |    \
                                 Galaxies Stars Astronauts
```

我們可以查詢繼承關係：

```

ltreetest=> SELECT path FROM test WHERE path <@ 'Top.Science';
                path
------------------------------------
 Top.Science
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
(4 rows)
```

以下是一些路徑比對範例：

```

ltreetest=> SELECT path FROM test WHERE path ~ '*.Astronomy.*';
                     path
-----------------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
 Top.Collections.Pictures.Astronomy
 Top.Collections.Pictures.Astronomy.Stars
 Top.Collections.Pictures.Astronomy.Galaxies
 Top.Collections.Pictures.Astronomy.Astronauts
(7 rows)

ltreetest=> SELECT path FROM test WHERE path ~ '*.!pictures@.Astronomy.*';
                path
------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
(3 rows)
```

以下是一些全文檢索範例：

```

ltreetest=> SELECT path FROM test WHERE path @ 'Astro*% & !pictures@';
                path
------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
 Top.Hobbies.Amateurs_Astronomy
(4 rows)

ltreetest=> SELECT path FROM test WHERE path @ 'Astro* & !pictures@';
                path
------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
(3 rows)
```

使用函式建構路徑：

```

ltreetest=> SELECT subpath(path,0,2)||'Space'||subpath(path,2) FROM test WHERE path <@ 'Top.Science.Astronomy';
                 ?column?
------------------------------------------
 Top.Science.Space.Astronomy
 Top.Science.Space.Astronomy.Astrophysics
 Top.Science.Space.Astronomy.Cosmology
(3 rows)
```

我們可以建立 SQL 函式，在路徑的指定位置插入標籤，以簡化此操作：

```

CREATE FUNCTION ins_label(ltree, int, text) RETURNS ltree
    AS 'select subpath($1,0,$2) || $3 || subpath($1,$2);'
    LANGUAGE SQL IMMUTABLE;

ltreetest=> SELECT ins_label(path,2,'Space') FROM test WHERE path <@ 'Top.Science.Astronomy';
                ins_label
------------------------------------------
 Top.Science.Space.Astronomy
 Top.Science.Space.Astronomy.Astrophysics
 Top.Science.Space.Astronomy.Cosmology
(3 rows)
```

#### F.22.5. 轉換 [#](#ltree-transforms) <a href="#ltree-transforms" id="ltree-transforms"></a>

`ltree_plpython3u` 擴充套件為 PL/Python 實作 `ltree` 型別的轉換。若已安裝此擴充套件，並在建立函式時指定此轉換，`ltree` 值就會對應為 Python 串列。（不過，目前尚不支援反向轉換。）

#### F.22.6. 作者 [#](#ltree-authors) <a href="#ltree-authors" id="ltree-authors"></a>

所有工作均由 Teodor Sigaev（`<teodor@stack.net>`）與 Oleg Bartunov（`<oleg@sai.msu.su>`）完成。更多資訊請參閱 <http://www.sai.msu.su/~megera/postgres/gist/>。作者感謝 Eugeny Rodichev 提供有益的討論，並歡迎提供意見與錯誤回報。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ltree.html)（核對日期：2026-09-11）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/appendixes/contrib/ltree.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
