Hierarchies with Postgres (2013)
monkeyandcrow.com
monkeyandcrow.com
[1] https://www.postgresql.org/docs/current/static/ltree.html
Most of the operations described in the article are performed by using the && (overlaps) operator, which can make use of a GIN index (though the article doesn't mention this), so probably the article's approach will work OK.
Ltree seems like it would be good for representing a named categorical hierarchy.
I'd love to see how ltree performs though.
What testing it receives I don't know, but I'm quite prepared to suppose that nobody's found a bug in it since 2006.
When you need a custom extension to cover your use case, maybe you should use a different tool altogether. I have nothing against Postgres though, its a fantastic DB and I use it everyday.
But there are better tools out there in the market which can be used for these sort of data structures.
I had versioning problem to solve recently, and recursive CTE was my first choice (did take some time to wrap my head around but was also performant enough), https://github.com/recipehub/recipehub-service/blob/master/d...
The indexed arrays work well for doing lots of queries of descendants, though are harder to manage.
One nice thing is that this approach worked well with ActiveRecord, while at the time CTE support was a bit awkward.
I'd love to see some people benchmark other approaches.
There are always better tools but that doesn't mean that it is "wrong". For example, S3 is generally a better tool than Postgres for storing files but there are several legitimate use cases where storing the files in the DB is better than S3.
For example, if you're using a database without window functions, obtaining the entire tree in one query is impossible. Also, deleting nodes without removing the subtree is complicated compared to other approaches like Path Enumeration, Nested Sets and Closure Tables.
In the application I'm currently writing (with PostgreSQL 9.5) I opted to use the adjacency list approach, mainly due to its simplicity; nothing other than the parent_id field has to be maintained.
This one looks simpler than using Nested Sets which is a plus, if you have a seldom changing tree with lots of reads then this approach is probably a good one.
Depends on if you're concerned with ordering as well as parentage.