One thing to note, if this is a query you would like to run on a replica, temp tables are a non-starter.
I really wish that wasn't the case. I have quite a few analytical queries I had to optimize with temp tables and indexes, and I really wish I could run on my hot standby. I in most cases I can't refactor them to use a CTE for performance reasons. Anyways, my 2ยข. - Adam