title: How do I create a query template in Postgres?
body: <p>I have a query:</p>
  
  <pre><code>SELECT * FROM users WHERE firstname = 'joe'
  </code></pre>
  
  <p>I'd like to make a function to use this as a template.  I found some docs on functions, and they seem extremely obtuse for something so simple.  (<a href="https://www.postgresql.org/docs/9.1/sql-createfunction.html" rel="nofollow noreferrer">https://www.postgresql.org/docs/9.1/sql-createfunction.html</a>)</p>
  
  <p>What I'm looking for is something that should like this:</p>
  
  <pre><code>CREATE FUNCTION getUserByFirstName(text) AS TEMPLATE (
    SELECT * FROM users WHERE firstname = $1
  )
  </code></pre>
  
  <p><strong>How do I make query templates in postgres?</strong></p>
  
  <hr>
  
  <p>P.S. I know this must be answered all over the place, but I tried searching ~12 different mixtures of the words "template" "query" "function" and "postgres" to no avail.</p>
  
  <p><code>WITH</code> is similar, but not globally defined or reusable (<a href="https://www.postgresql.org/docs/9.2/queries-with.html" rel="nofollow noreferrer">https://www.postgresql.org/docs/9.2/queries-with.html</a>)</p>