WITH headrecords AS ( SELECT instance.jsonb, lower(f_unaccent(instance.jsonb ->> 'title')) AS title FROM supertenant_mod_inventory_storage.instance instance JOIN supertenant_mod_inventory_storage.holdings_record holdings_record ON lower(f_unaccent(instance.jsonb ->> 'id'::text)) = lower(f_unaccent(holdings_record.jsonb ->> 'instanceId'::text)) WHERE ( (((lower(f_unaccent(instance.jsonb ->> 'title')) ~ lower(f_unaccent('(^|[[:punct:]]|[[:space:]]|(?=[[:punct:]]|[[:space:]])).*($|[[:punct:]]|[[:space:]]|(?<=[[:punct:]]|[[:space:]]))'))) OR ( lower(f_unaccent(instance.jsonb ->> 'contributors')) ~ lower(f_unaccent('(^|[[:punct:]]|[[:space:]]|(?=[[:punct:]]|[[:space:]]))"name":([[:punct:]]|[[:space:]])+".*"($|[[:punct:]]|[[:space:]]|(?<=[[:punct:]]|[[:space:]]))')) ) ) OR ( lower(f_unaccent(instance.jsonb ->> 'identifiers')) ~ lower(f_unaccent('(^|[[:punct:]]|[[:space:]]|(?=[[:punct:]]|[[:space:]]))"value":([[:punct:]]|[[:space:]])+".*"($|[[:punct:]]|[[:space:]]|(?<=[[:punct:]]|[[:space:]]))')) ) ) AND ( lower(f_unaccent(holdings_record.jsonb ->> 'permanentLocationId')) LIKE lower(f_unaccent('53cf956f-c1df-410b-8bea-27f712cca7c0')) ) ) AND lower(f_unaccent(instance.jsonb ->> 'title')) < ( SELECT lower(f_unaccent(instance.jsonb ->> 'title')) FROM supertenant_mod_inventory_storage.instance instance JOIN supertenant_mod_inventory_storage.holdings_record holdings_record ON lower(f_unaccent(instance.jsonb ->> 'id'::text)) = lower(f_unaccent(holdings_record.jsonb ->> 'instanceId'::text)) ORDER BY lower(f_unaccent(instance.jsonb ->> 'title')) OFFSET 10000 LIMIT 1 ) ORDER BY lower(f_unaccent(instance.jsonb ->> 'title')) LIMIT 30 OFFSET 0 ) , allrecords AS ( SELECT instance.jsonb, lower(f_unaccent(instance.jsonb ->> 'title')) AS title FROM supertenant_mod_inventory_storage.instance instance JOIN supertenant_mod_inventory_storage.holdings_record holdings_record ON lower(f_unaccent(instance.jsonb ->> 'id'::text)) = lower(f_unaccent(holdings_record.jsonb ->> 'instanceId'::text)) WHERE ( (((lower(f_unaccent(instance.jsonb ->> 'title')) ~ lower(f_unaccent('(^|[[:punct:]]|[[:space:]]|(?=[[:punct:]]|[[:space:]])).*($|[[:punct:]]|[[:space:]]|(?<=[[:punct:]]|[[:space:]]))'))) OR ( lower(f_unaccent(instance.jsonb ->> 'contributors')) ~ lower(f_unaccent('(^|[[:punct:]]|[[:space:]]|(?=[[:punct:]]|[[:space:]]))"name":([[:punct:]]|[[:space:]])+".*"($|[[:punct:]]|[[:space:]]|(?<=[[:punct:]]|[[:space:]]))')) ) ) OR ( lower(f_unaccent(instance.jsonb ->> 'identifiers')) ~ lower(f_unaccent('(^|[[:punct:]]|[[:space:]]|(?=[[:punct:]]|[[:space:]]))"value":([[:punct:]]|[[:space:]])+".*"($|[[:punct:]]|[[:space:]]|(?<=[[:punct:]]|[[:space:]]))')) ) ) AND ( lower(f_unaccent(holdings_record.jsonb ->> 'permanentLocationId')) LIKE lower(f_unaccent('53cf956f-c1df-410b-8bea-27f712cca7c0')) ) ) AND ( SELECT COUNT(*) FROM headrecords ) < 30 ) SELECT headrecords.jsonb, title, 0 AS count FROM headrecords WHERE ( SELECT COUNT(*) FROM headrecords ) >= 30 UNION ( SELECT allrecords.jsonb, title, ( SELECT COUNT(*) FROM allrecords ) AS count FROM allrecords ORDER BY title LIMIT 30 OFFSET 0 ) ORDER BY title;