2 ms·
ltree is the one I ended up using. I don't like it much. The way it works allows you to setup a tree but you can't easily move things around within a tree once
by t43562 2y ago
ltree is the one I ended up using. I don't like it much. The way it works allows you to setup a tree but you can't easily move things around within a tree once they're in.
I forgot to say that it's also obviously only a tree, not a graph. It's still useful for some cases.
It has a very good way of querying which is probably fast if you have very deep trees but in the end we didn't need the deep trees so it was chosen for the wrong reasons.
https://www.postgresql.org/docs/current/ltree.html https://www.postgresql.org/docs/current/ltree.html
You essentially have a path field in your record which lists the ids of all the records that are parents of the current one plus the current one.
e.g. if your records are regions of the world and the id is the country name then the path might look like this:
Earth.Europe.Poland
There is support for queries like "what are all the descendants of Europe"
SELECT name FROM regions WHERE path <@ 'Earth.Europe';
Or you can use matching:
SELECT name FROM regions WHERE path ~ '*.Europe.*';