title: Postgres 9.1: Measure time taken for a transaction (including commit)
body: <p>I have the following stored procedure, insertMessageOne:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION insertMessageOne(sender_id INT, receiver_id INT, queue_id INT, context INT, priority INT, text varchar(2000))
  RETURNS BOOLEAN 
  AS $$
  DECLARE
      start timestamp;
      stop timestamp;
  BEGIN
      start = clock_timestamp();
      INSERT INTO message(sender_id, receiver_id, queue_id, context, priority, text) VALUES($1,$2,$3,$4,$5,$6);
      stop = clock_timestamp();
      RAISE NOTICE 'Timestamp: (%) senderId: (%) insertMessageOne: (%)', stop, sender_id, stop - start;
      RETURN TRUE;
  EXCEPTION
      WHEN OTHERS THEN
          RAISE NOTICE 'EXCEPTION: INSERT SINGLE-Q MESSAGE FAILED';
          RETURN FALSE;
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>As seen above, I measure the execution time for this stored procedure using start and stop timestamps at the beginning and end of the stored procedure. However, as the actual transaction commit happens <em>after</em> the stored procedure ends, the time logged is off by 10-20 ms (See <a href="https://stackoverflow.com/questions/20307375/execution-time-of-a-prepared-statements-executequery-method-vs-corresponding">Execution time of a Prepared statement&#39;s executeQuery() method vs. corresponding Stored procedure execution time</a>) typically required for committing a transaction. How can I log the time that includes time taken for committing the transaction as well? pgBench is probably not good enough for me, because I want these numbers when an actual live application (from middleware) calls this stored procedure. </p>