I played with ltree , which is the PostgreSQL Contributor module, to find out if this is good for thread comments. You create a column in your table that saves the path and creates the ltree index on it. Then you can execute the following queries:
ltreetest=
I have not played with him enough to determine how well it works with things like inserts, updates, or deletes. I assume the deletion will look like this:
DELETE FROM test WHERE path ~ '*.Astronomy.*';
I think a table with comments might look like this:
CREATE SEQUENCE comment_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 78616 CACHE 1; CREATE TABLE comments ( comment_id int PRIMARY KEY, path ltree, comment text ); CREATE INDEX comments_path_idx ON comments USING gist (path);
The rough (and untested) insert looks like this:
CREATE FUNCTION busted_add_comment(text the_comment, int parent_comment_id) RETURNS void AS $BODY$ DECLARE INT _new_comment_id;
Or something. Basically, a path is simply a hierarchy consisting of all primary keys.
source share