title: Reuse computed select value
body: <p>I'm trying to use <a href="http://postgis.net/docs/ST_SnapToGrid.html" rel="nofollow">ST_SnapToGrid</a> and then <code>GROUP BY</code> the grid cells (x, y). Here is what I did first:</p>
  
  <pre><code>SELECT
    COUNT(*)                      AS n,
    ST_X(ST_SnapToGrid(geom, 50)) AS x,
    ST_Y(ST_SnapToGrid(geom, 50)) AS y
  FROM points
  GROUP BY x, y
  </code></pre>
  
  <p>I don't want to recompute <code>ST_SnapToGrid</code> for both <code>x</code> and <code>y</code>. So I changed it to use a sub-query:</p>
  
  <pre><code>SELECT
    COUNT(*)   AS n,
    ST_X(geom) AS x,
    ST_Y(geom) AS y
  FROM (
    SELECT 
        ST_SnapToGrid(geom, 50) AS geom
    FROM points
  ) AS tmp
  GROUP BY x, y
  </code></pre>
  
  <p>But when I run <code>EXPLAIN</code>, both of these queries have the exact same execution plan:</p>
  
  <pre><code>GroupAggregate  (...)
    -&gt;  Sort  (...)
          Sort Key: (st_x(st_snaptogrid(points.geom, 0::double precision))), (st_y(st_snaptogrid(points.geom, 0::double precision)))
          -&gt;  Seq Scan on points  (...)
  </code></pre>
  
  <p><strong>Question</strong>: Will PostgreSQL reuse the result value of <code>ST_SnapToGrid()</code>?<br>
  If not, is there a way to make it do this?</p>