Original query:

SELECT "main"."ID" "ID", "main"."subject" "subject", "main"."contenttype" "contenttype", "reads"."read" "read", "main"."sender" "sender", "main"."sender_name" "sender_name", "main"."to" "to", "main"."to_name" "to_name", "main"."to_count" "to_count", "main"."date" "date", (COALESCE("replied"."replied", 0)) "replied", "follow"."ID" "follow"
FROM (SELECT "main"."ID" "ID", "main"."subject" "subject", "main"."contenttype" "contenttype", "main"."sender" "sender", "main"."sender_name" "sender_name", "main"."to" "to", "main"."to_name" "to_name", "main"."to_count" "to_count", "main"."date" "date"
FROM "messages" "main"
WHERE (("main"."mailbox" = (0::smallint) AND (("main"."ownergroup" IN (1, 2, 11, 12) OR "main"."owneruser" = 1005 OR ("main"."owneruser" IS NULL AND "main"."ownergroup" IS NULL)))))
ORDER BY "main"."date" DESC, "main"."ID" DESC
LIMIT 100) "main"
LEFT JOIN LATERAL (SELECT "ID" FROM "follows" WHERE ("creator" = 1005 AND "entity" = 'messages' AND "index" = "main"."ID")) "follow" ON TRUE
LEFT JOIN LATERAL (SELECT (1) "read"
FROM "messagereads" "mes"
WHERE ("mes"."creator" = 1005 AND "mes"."message" = "main"."ID")) "reads" ON TRUE
LEFT JOIN (SELECT (1) "replied", "reference" "ID"
FROM "messages" "mes"
WHERE ("mailbox" <> (1::smallint) AND "reference" IS NOT NULL)
GROUP BY "reference") "replied" ON "replied"."ID" = "main"."ID"
ORDER BY "main"."date" DESC, "main"."ID" DESC

Prettified:

SELECT
  "main"."ID" AS ID,
  "main"."subject" AS subject,
  "main"."contenttype" AS contenttype,
  "reads"."read" AS read,
  "main"."sender" AS sender,
  "main"."sender_name" AS sender_name,
  "main"."to" AS to,
  "main"."to_name" AS to_name,
  "main"."to_count" AS to_count,
  "main"."date" AS date,
  COALESCE("replied"."replied", 0) AS replied,
  "follow"."ID" AS follow
FROM
  (
    SELECT
      "main"."ID" AS ID,
      "main"."subject" AS subject,
      "main"."contenttype" AS contenttype,
      "main"."sender" AS sender,
      "main"."sender_name" AS sender_name,
      "main"."to" AS to,
      "main"."to_name" AS to_name,
      "main"."to_count" AS to_count,
      "main"."date" AS date
    FROM
      "messages" main
    WHERE
      "main"."mailbox" = 0::smallint AND
      (
        "main"."ownergroup" IN (1, 2, 11, 12) OR
        "main"."owneruser" = 1005 OR
        (
          "main"."owneruser" IS NULL AND
          "main"."ownergroup" IS NULL
        )
      )
    ORDER BY
      "main"."date" DESC,
      "main"."ID" DESC
    LIMIT
      100
  ) main
  LEFT JOIN (
    SELECT
      "ID"
    FROM
      "follows"
    WHERE
      "creator" = 1005 AND
      "entity" = 'messages' AND
      "index" = "main"."ID"
  ) follow ON true
  LEFT JOIN (
    SELECT
      1 AS read
    FROM
      "messagereads" mes
    WHERE
      "mes"."creator" = 1005 AND
      "mes"."message" = "main"."ID"
  ) reads ON true
  LEFT JOIN (
    SELECT
      1 AS replied,
      "reference" AS ID
    FROM
      "messages" mes
    WHERE
      "mailbox" <> 1::smallint AND
      "reference" IS NOT NULL
    GROUP BY
      "reference"
  ) replied ON ("replied"."ID" = "main"."ID")
ORDER BY
  "main"."date" DESC,
  "main"."ID" DESC