An attempt to find orphan temp tables

Original query:

WITH active_backends AS (
SELECT backend_id
  FROM pg_stat_get_backend_idset() AS a(backend_id)
)
SELECT c.relname, n.nspname
  FROM pg_class AS c
  JOIN pg_namespace AS n
    ON c.relnamespace = n.oid
 WHERE pg_is_other_temp_schema(c.relnamespace)
   AND NOT EXISTS (
       SELECT 1
         FROM active_backends
        WHERE active_backends.backend_id =
              CASE WHEN left(n.nspname, 8) = 'pg_temp_'
                   THEN CAST(right(n.nspname, -8) AS integer)
                   WHEN left(n.nspname, 14) = 'pg_toast_temp_'
                   THEN CAST(right(n.nspname, -14) AS integer)
              END
       );

Prettified:

WITH active_backends AS (
  SELECT
    "backend_id"
  FROM
    pg_stat_get_backend_idset() a("backend_id")
)
SELECT
  "c"."relname",
  "n"."nspname"
FROM
  "pg_class" c
  JOIN "pg_namespace" n ON ("c"."relnamespace" = "n"."oid")
WHERE
  pg_is_other_temp_schema("c"."relnamespace") AND
  NOT EXISTS(SELECT 1 FROM "active_backends" WHERE "active_backends"."backend_id" = CASE WHEN left("n"."nspname", 8) = 'pg_temp_' THEN right("n"."nspname", -8)::int WHEN left("n"."nspname", 14) = 'pg_toast_temp_' THEN right("n"."nspname", -14)::int END)