未公開トピックを含めるようバッジクエリを修正しますか?

こんにちは、非公開(UNLISTED)の投稿に返信したユーザーにバッジを付与するバッジクエリを作成しようとしています。

残念ながら、通常のクエリは何らかの理由で「非公開」の投稿を無視してしまい、バッジが付与されません。

問題のクエリは以下の通りです(上記と同じものです):

SELECT
DISTINCT ON (p.user_id)
p.user_id, p.id post_id, p.created_at granted_at
FROM badge_posts p
WHERE p.topic_id = 81 AND -- 81 は対象のトピックです
  (:backfill OR p.id IN (:post_ids) )

そして、表示されるエラーメッセージは以下の通りです:

No Badges to be assigned.

Unique  (cost=25.87..25.88 rows=2 width=16)
  ->  Sort  (cost=25.87..25.87 rows=2 width=16)
        Sort Key: p.user_id
        ->  Hash Join  (cost=6.73..25.86 rows=2 width=16)
              Hash Cond: (t.category_id = c.id)
              ->  Nested Loop  (cost=4.32..23.44 rows=4 width=20)
                    ->  Index Scan using topics_pkey on topics t  (cost=0.14..8.16 rows=1 width=8)
                          Index Cond: (id = 81)
                          Filter: ((deleted_at IS NULL) AND visible)
                    ->  Bitmap Heap Scan on posts p  (cost=4.18..15.24 rows=4 width=20)
                          Recheck Cond: (topic_id = 81)
                          Filter: ((deleted_at IS NULL) AND (post_type = ANY ('{1,2,3}'::integer[])))
                          ->  Bitmap Index Scan on index_posts_on_topic_id_and_sort_order  (cost=0.00..4.17 rows=4 width=0)
                                Index Cond: (topic_id = 81)
              ->  Hash  (cost=2.21..2.21 rows=16 width=4)
                    ->  Seq Scan on categories c  (cost=0.00..2.21 rows=16 width=4)
                          Filter: (allow_badges AND (NOT read_restricted))

非公開の投稿を無視しないように、クエリをどのように修正すればよいでしょうか?

お時間をいただき、ありがとうございます。

以下の通り変更してみてください。

FROM badge_posts p

FROM posts p

に。

badge_posts テーブルには、非公開の投稿やプライベートカテゴリで作成された投稿は含まれていません。