title: Cumulative sum over a table
body: <p>What is the best way to perform a cumulative sum over a table in Postgres, in a way that can bring the best performance and flexibility in case more fields / columns are added to the table.</p>
  
  <p><strong>Table</strong></p>
  
  <pre>
      a   b   d
  1   59  15  181
  2       16  268
  3           219
  4           102
  </pre>
  
  <p><strong>Cumulative</strong></p>
  
  <pre>
      a   b   d
  1   59  15  181
  2       31  449
  3           668
  4           770
  </pre>