title: Dynamic calculation formulas in Postgres
body: <p>I'd like to know if there is a possibility to create something like named calculations in Postgres? Say, I have a table:</p>
  
  <pre><code>create table foo (
    bar smallint,
    baz smallint
  )
  </code></pre>
  
  <p>I can run <code>select (bar / baz) * 100 from foo;</code> to get my result, but I'd like to have a formula (pseudocode): <code>avg_foo ::= (bar/baz)*100</code> and then do <code>select avg_foo from foo;</code> to get the same result. Ideally, I'd like to have a separate table with calculations:</p>
  
  <pre><code>create table calculations (
    name varchar,
    formula varchar
  )
  </code></pre>
  
  <p>So that I could create calculations dynamically and use them in selects, like this:</p>
  
  <pre><code>insert into calculations (name, formula) values
    ('sum_bar_baz', 'bar+baz'),
    ('mult_bar_baz', 'bar*baz');
  select sum_bar_baz, mult_bar_baz from foo;
  </code></pre>
  
  <p>How do I do this with Postgres?</p>