Original query:

WITH "filtered_taggings" AS (      SELECT "superion"."tags".name, tg.taggable_id, tg.taggable_type FROM "superion"."tags"



                        +
       INNER JOIN "superion"."taggings" tg



                        +
         ON "superion"."tags".id = tg.tag_id



                        +
         AND "superion"."tags".tag_type = 0



                        +
         AND "superion"."tags".deleted_at IS NULL



                        +
         AND tg.deleted_at IS NULL



                        +
       WHERE to_tsvector('simple', COALESCE("superion"."tags".name, '')) @@ to_tsquery('simple', '''planning'':* & ''and'':* & ''eng'':*')



                        +
         AND "superion"."tags".sub_account_id = 126



                        +
 ) SELECT  1 AS one FROM "superion"."library_items" LEFT JOIN "superion"."taggings" ntg_884_1907_712_1295_889_817_772_883_1287_766_775_890_2093_773



                        +
               ON ntg_884_1907_712_1295_889_817_772_883_1287_766_775_890_2093_773.taggable_id = "superion"."library_items".id



                        +
               AND ntg_884_1907_712_1295_889_817_772_883_1287_766_775_890_2093_773.taggable_type = 'LibraryItem'



                        +
               AND ntg_884_1907_712_1295_889_817_772_883_1287_766_775_890_2093_773.tag_id IN (884, 1907, 712, 1295, 889, 817, 772, 883, 1287, 766, 775, 890, 2093, 773, 2644)



                        +
               AND ntg_884_1907_712_1295_889_817_772_883_1287_766_775_890_2093_773.deleted_at IS NULL LEFT OUTER JOIN (SELECT "superion"."library_items".id AS join_id , ts_rank(to_tsvector('simple', COALESCE("superion"."library_items".tit
le, '')) || to_tsvector('simple', COALESCE("superion"."library_items".description, '')), to_tsquery('simple', '''planning'':* & ''and'':* & ''eng'':*')) AS rank FROM "superion"."library_items" WHERE "library_items"."deleted_at" IS NULL AN
D (to_tsvector('simple', COALESCE("superion"."library_items".title, '')) || to_tsvector('simple', COALESCE("superion"."library_items".description, '')) @@ to_tsquery('simple', '''planning'':* & ''and'':* & ''eng'':*'))) ts

                        +
           ON "superion"."library_items".id = ts.join_id LEFT OUTER JOIN "superion"."library_item_sources" ON "library_item_sources"."library_item_id" = "library_items"."id" AND "library_item_sources"."deleted_at" IS NULL LEFT OUTER JOIN
"superion"."taggings" ON "taggings"."taggable_id" = "library_items"."id" AND "taggings"."taggable_type" = 'LibraryItem' AND "taggings"."deleted_at" IS NULL LEFT OUTER JOIN "superion"."tags" ON "tags"."id" = "taggings"."tag_id" LEFT OUTER
JOIN "superion"."tag_relevances" ON "tag_relevances"."tag_id" = "tags"."id" AND "tag_relevances"."deleted_at" IS NULL WHERE "library_items"."deleted_at" IS NULL AND "library_items"."domain_id" IN (126, 662, 4986, 4985, 4983, 4984, 4982, 4
978, 4979, 4977, 4974, 4972, 4970, 4968, 4969, 4967, 4961, 4963, 4962, 4960, 4959, 4980) AND "library_items"."deleted_at" IS NULL AND "library_items"."published_at" IS NOT NULL AND (COALESCE(library_items.data->>'archived_at', 'null') = '
null') AND ((          (+
             library_item_sources.enrollable_type = 'Group'



                        +
             AND library_item_sources.enrollable_id IN (1769,154)



                        +




                        +
           )



                        +
  OR           (



                        +
             library_item_sources.enrollable_type = 'User'



                        +
             AND library_item_sources.enrollable_id IN (8389)



                        +




                        +
           )



                        +
 ) OR (          (



                        +
             "superion"."tag_relevances".enrollable_type = 'Group'



                        +
             AND "superion"."tag_relevances".enrollable_id IN (1769,154)



                        +




                        +
           )



                        +
  OR           (



                        +
             "superion"."tag_relevances".enrollable_type = 'User'



                        +
             AND "superion"."tag_relevances".enrollable_id IN (8389)



                        +




                        +
           )



                        +
 )) AND (ntg_884_1907_712_1295_889_817_772_883_1287_766_775_890_2093_773.id IS NULL) AND "library_items"."deleted_at" IS NULL AND (ts.join_id IS NOT NULL        OR EXISTS(SELECT 1 FROM filtered_taggings



                        +
                   WHERE taggable_id = "superion"."library_items".id



                        +
                   AND taggable_type = 'LibraryItem')



                        +
 ) GROUP BY "superion"."library_items".id, "superion"."library_items".id  , ts.rank LIMIT 1 OFFSET 20