If it’s really about a tree (as the graph is more like a graph, although your text is a tree), then the Nested Sets pattern may suit you. Each post you supply with left and right boundaries and number all the subordinate leaves of the tree so that the left border of the repost is always more than the parent post, and the right border, on the contrary, is always less.
post_id | lft | rth 1 | 1 | 14 2 | 2 | 11 3 | 3 | 6 6 | 4 | 5 4 | 7 | 8 7 | 9 | 10 5 | 12 | 13 1 1 <-------------------------------------------> 14 2 2 <----------------------------> 11 3 3 <-----> 6 6 4 <-> 5 4 7 <-> 8 7 9 <-> 10 5 12 <-> 13
Thus, in order to select all the reposts of which post, you need to select posts whose left and right borders are between the left and right borders of the root post. The problem of this pattern is the complexity of the insert. However, it is the fastest in terms of extracting, and most importantly, counting the number of reposts. Divide the right border of the root 14 into two and get the number of posts 7 (this really works if the numbering is solid). Another property - the removal of part of posts does not require renumbering, since the boundaries of the remaining reposts still belong to the set of the original post.
select
for eachparent_post
and if there are a lot of them, then acc. there will be a lot of requests to the database - Vitaly Leonov