IT lexicon › Database › Materialized path

Materialized path

Database På svenska → Updated: 2026-05-29

A way to store tree structures in a relational database where each node saves the full path from the root as a string, e.g. "/1/4/9/". It makes subtree and ancestor queries into simple string matches.

Mechanic: each row has a path column that encodes the path from the root (with a delimiter character). Win: "fetch the whole subtree under node 4" becomes WHERE path LIKE '/1/4/%' (fast with a regular index); "fetch all ancestors" is done by splitting the string. Much simpler than recursive queries for reading. Price: moving a subtree requires updating all affected paths, and the path string has a length limit. PostgreSQL has a dedicated ltree type with operators and a GiST index for exactly this. One of several patterns for modeling hierarchies in SQL, alongside adjacency list, nested set, and closure table. Best when reading subtrees is common and moves are rare. Related to adjacency list and nested set.

← Back to the lexicon