# ltree — Postgres extension

> Tree paths such as categories and org charts.

Source: https://scribase.com/extensions/ltree

Category: Data types. Version: 1.3. Optional, enabled per project.

## About

ltree stores a position in a tree as a dotted path (shop.clothing.shirts) and indexes it, so "everything under this node" and "all ancestors of this node" are single indexed queries instead of recursive CTEs.

## Enable it

```sql
create extension if not exists ltree with schema extensions;
```

## Example

```sql
create table categories (id serial primary key, path ltree not null);
create index on categories using gist (path);

insert into categories (path) values ('shop'), ('shop.clothing'), ('shop.clothing.shirts');

-- the subtree under shop.clothing
select path from categories where path <@ 'shop.clothing';
```

Upstream: https://www.postgresql.org/docs/17/ltree.html