There are two optimization being applied here:
Fetch a user’s group id in a seperate query instead of including it as a sub-query. When I tried a subquery, the query plan becomes very inefficient.
Join against the
topic_allowed_groupstable instead of doing an IN against a subquery where we UNION the
topic_ids from the two tables. From my profiling, this enables PG to do a backwards index scan on the