Skip to content

Instantly share code, notes, and snippets.

@lkmill
Last active February 14, 2020 14:50
Show Gist options
  • Save lkmill/22d6b089fabb211008658168619696aa to your computer and use it in GitHub Desktop.
Save lkmill/22d6b089fabb211008658168619696aa to your computer and use it in GitHub Desktop.
ancestor_topics
delete from links_topics where (link_id, topic_id) in (
select
j_parent.link_id,
j_parent.topic_id
from (
select
lt.link_id,
lt.topic_id,
t.path
from links_topics as lt
inner join topics as t on lt.topic_id = t.id
) as j_child
inner join (
select
lt.link_id,
lt.topic_id,
t.path
from links_topics as lt
inner join topics as t on lt.topic_id = t.id
) as j_parent on j_parent.link_id = j_child.link_id and j_parent.path @> subpath(j_child.path, 0, nlevel(j_child.path) - 1)
);
select
j_parent.link_id,
j_parent.topic_id as parent_path,
j_parent.path as parent_path,
j_child.topic_id as child_path,
j_child.path as child_path
from (
select
lt.link_id,
lt.topic_id,
t.path
from links_topics as lt
inner join topics as t on lt.topic_id = t.id
) as j_child
inner join (
select
lt.link_id,
lt.topic_id,
t.path
from links_topics as lt
inner join topics as t on lt.topic_id = t.id
) as j_parent on j_parent.link_id = j_child.link_id and j_parent.path @> subpath(j_child.path, 0, nlevel(j_child.path) - 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment