-- H6: replace v_feed's double LEFT JOIN + COUNT(DISTINCT) (which materializes a
-- likes×comments Cartesian per upload before de-duping) with correlated scalar
-- subqueries. Each count now uses its own index (idx_like_upload /
-- idx_comment_upload) and there is no GROUP BY. The output columns are
-- unchanged, so every consumer keeps working.
CREATE OR REPLACE VIEW v_feed AS
SELECT
    u.id,
    u.event_id,
    u.user_id,
    usr.display_name             AS uploader_name,
    usr.is_banned,
    usr.uploads_hidden,
    u.preview_path,
    u.thumbnail_path,
    u.mime_type,
    u.caption,
    u.created_at,
    (SELECT COUNT(*) FROM "like" l
       WHERE l.upload_id = u.id)                              AS like_count,
    (SELECT COUNT(*) FROM comment c
       WHERE c.upload_id = u.id AND c.deleted_at IS NULL)     AS comment_count
FROM upload u
JOIN "user" usr ON u.user_id = usr.id
WHERE u.deleted_at IS NULL
  AND usr.uploads_hidden = FALSE;
