Optimizing a user retention query
It's a bit hard to tell how your tables are linked together, but I think the following should be more efficient:

WITH weeks AS (
  SELECT make_interval(weeks =&gt; n) as week 
  from generate_series(0, 3) AS n
)
SELECT week, 
       count(x.user_email) as "Returning Users"
FROM weeks
  LEFT JOIN (
     SELECT act.user_email, age(act.timestamp, acc.timestamp) as diff
     FROM accounts acc
       JOIN action act on act.user_email = acc.user_email
  ) x ON x.diff &gt;=  week and diff &lt; week + '1 week'::interval
FROM weeks
GROUP BY week;