title: Using parameter as column name in Postgres function
body: <p>I have a Postgres table bearing the following form</p>
  
  <pre><code>CREATE TABLE "public"."days" 
  (
   "id" integer NOT NULL,
   "day" character varying(9) NOT NULL,
   "visits" bigint[] NOT NULL,
   "passes" bigint[] NOT NULL
  );
  </code></pre>
  
  <p>I would like to write a function that allows me to return the <code>visits</code> or the <code>passees</code> column as its result for a specified <code>id</code>.  My first attempt goes as follows</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION day_entries(INT,TEXT) RETURNS BIGINT[] LANGUAGE sql AS
  'SELECT $2 FROM days WHERE id = $1;'
  </code></pre>
  
  <p>which fails with an error along the lines of </p>
  
  <blockquote>
    <p>return type mismatch in function declared to return bigint[]
    DETAIL: Actual return type is text.</p>
  </blockquote>
  
  <p>If I put in <code>visits</code> in place of the <code>$2</code> things work just as expected.  It would make little sense to define several functions to match different columns from the <code>days</code> table.  Is there a way to pass the actual column name as a parameter while still keeping Postgres happy? </p>