title: Execute Function First in Postgres
body: <p>I need to call a FUNCTION in postgreSQL before a SELECT is done on a table. My first thought was to use a TRIGGER but it appears you cannot trigger on select.</p>
  
  <p>Thus, to work around this I created a VIEW that runs a select on the table and the function all at once. I.e.:</p>
  
  <pre><code>CREATE VIEW people_view AS
      SELECT get_department(), name, title, department
      FROM people_table
  </code></pre>
  
  <p>So, in a nutshell... the get_department() function would update the department column from external data (this is all using foreign data tables and wrappers).</p>
  
  <p>The problem is, the function executes after name, title, department are selected and not before. So if I run it once it doesn't work. If I run it twice it does (because it updated after the first run).</p>
  
  <p>Sorry if this doesn't make much sense. I don't typically do DB work. What I'd like to do is get "get_department()" to execute first in the SELECT. I tried to put the function call in a sub-query, but it still did not execute first. The only thought I have left is perhaps an explicit join to force the order? I have no idea :-(.</p>
  
  <p>Basically, I just want to execute a function before a SELECT transparently for the person running the query... and I guess you can't do that with triggers. If there's a better workaround, I'd be more than happy to give it a go.</p>
  
  <p>Thanks,
  Isekal</p>