Original query:

SELECT COALESCE(json_agg(data.*), '[]'::JSON) as data FROM (
    SELECT
        appointments.id,
        appointments.bookable_id,
        appointments.bookable_type,
        appointments.title,
        appointments.start_at AT TIME ZONE 'UTC' as start,
        appointments.end_at AT TIME ZONE 'UTC' as end,
        appointments.agenda_id,
        appointments.ical_rrule as rrule,
        appointments.ical_exdate as exdate,
        appointments.ical_rdate as rdate,
        appointments.ical_rdate as rdate,
        appointments.free_of_availabilities,
        appointments.comment,
        appointments.user_comment,
        appointments.ics_feed_id,
        appointments.all_day,
        appointments.prefix_status_icon,
        appointments.created_at,

        COALESCE(
            NULLIF(ARRAY_AGG(distinct appointments_customers_rows), '{NULL}'),
            '{}'
        ) AS appointment_customers
    FROM appointments

    LEFT JOIN LATERAL (
        SELECT
            appointments_customers.id,
            appointments_customers.booked_online,
            appointments_customers.send_sms,
            appointments_customers.send_email,
            appointments_customers.enable_all_notifications,
            appointments_customers.no_show,
            appointments_customers.price,
            appointments_customers.quantity,
            appointments_customers.customer_id,
            appointments_customers.confirmed,
            appointments_customers.comment,
            appointments_customers.currency,

            COALESCE(
                NULLIF(ARRAY_AGG(DISTINCT bookables_attributes_values_rows), '{NULL}'),
                '{}'
            ) AS bookables_attributes_values,
            (ARRAY_AGG(DISTINCT customers))[1] AS customer,

            COALESCE((ARRAY_AGG(invoice.appointments_customers_invoice_id))[1], null) as invoice_id
        FROM appointments_customers

        LEFT JOIN LATERAL (
            SELECT
                appointments_customers_invoice_id
            FROM
                appointments_customers_has_invoices
            WHERE
                appointments_customer_id = appointments_customers.id
            LIMIT 1
        ) as invoice ON TRUE

        LEFT JOIN LATERAL (
            SELECT
                bookables_attributes_values.id,
                bookables_attributes_values.bookables_attribute_id,
                COALESCE(bookables_attributes_values.text_value, bookables_attributes_values.number_value::TEXT) as value
            FROM
                bookables_attributes_values
            WHERE
                bookables_attributes_values.appointments_customer_id = appointments_customers.id
        ) bookables_attributes_values_rows ON true

        LEFT JOIN LATERAL (
                SELECT
                    customers.id,
                    customers.email,
                    customers.comment,
                    customers.status,
                    customers.created_at,
                    customers.updated_at,
                    customers.enabled,
                    customers.locale,
                    customers.born_on,
                    customers.send_email,
                    customers.send_sms,
                    customers.external_reference,
                    customers.questionnaire_reply_sent_on,
                    customers.avatar_file_name,
                    customers.avatar_content_type,
                    customers.avatar_file_size,
                    customers.avatar_updated_at,
                    customers.avatar_type,
                    customers.enable_all_notifications,
                    customers.partner_newsletter,
                    customers.avs_no,
                    customers.cada_no,
                    customers.insured_no,
                    customers.insured_company,

                    details.id as details_id,
                    details.firstname,
                    details.lastname,
                    details.phone,
                    details.mobile,
                    details.region,
                    details.zip,
                    details.address,
                    details.fax,
                    details.canton_id,
                    details.website,
                    details.country_id,
                    countries.name_en as country_name_en,
                    countries.name_fr as country_name_fr,
                    countries.name_de as country_name_de,
                    details.gender,
                    details.latitude,
                    details.longitude,

                    COALESCE(
                        NULLIF(ARRAY_AGG(DISTINCT custom_attributes_rows), '{NULL}'),
                        '{}'
                     ) AS custom_attributes
                FROM
                    customers
                INNER JOIN details ON details.customer_id = customers.id
                LEFT JOIN
                    countries ON countries.id = details.country_id

                LEFT JOIN LATERAL (
                    SELECT
                        extra_customer_values.id,
                        COALESCE(extra_customer_values.text_value, extra_customer_values.number_value::TEXT) as value,
                        extra_customer_values.extra_customer_attribute_id,
                        extra_customer_values.customer_id
                    FROM
                        extra_customer_values
                    WHERE
                        extra_customer_values.customer_id = customers.id
                    GROUP BY
                        extra_customer_values.id
                ) as custom_attributes_rows ON TRUE

                WHERE
                    (customers.id = appointments_customers.customer_id)
                GROUP BY
                    customers.id, details.id, countries.id
        ) customers ON true
        WHERE
            (appointments.id = appointments_customers.appointment_id)
            AND appointments_customers.enabled
        GROUP BY appointments_customers.id
    ) appointments_customers_rows ON true
    WHERE
        appointments.agenda_id IN (
            SELECT id FROM agendas WHERE id IN (
                SELECT agenda_id FROM agendas_pro_users WHERE pro_user_id = 585
            ) AND enabled
        )
        AND appointments.enabled
        AND (
            (
                (appointments.ical_rrule = '' OR appointments.ical_rrule IS NULL) AND
                appointments.end_at >= '2019-01-01' AND
                appointments.start_at <= '2019-1-14'
            )
            OR
            (
                (appointments.ical_rrule != '' AND appointments.ical_rrule IS NOT NULL) AND
                appointments.start_at <= '2019-1-14' AND
                (appointments.ical_end_date IS NULL OR appointments.ical_end_date >= '2019-01-01')
            )
        )
    GROUP BY appointments.id
) data;