Get function timings in pgAdminIII

时间:2015-09-01 21:37:31

标签: postgresql pgadmin

I'd like to compare two functions in pgAdmin's SQL editor. Here's the script. However, when I run it, seems the start_time and end_time have the same value no matter how many iterations. (But, the Query returned successfully with no result in nnn ms. message does go higher with every increase in the loop size.) Why?

DO
$$
DECLARE
  start_time timestamp;
  end_time timestamp;
  diff interval;
BEGIN
 SELECT now() INTO start_time;
 FOR i IN 1..1000 LOOP
   --PERFORM uuid_generate_v1mc();
   PERFORM id_generator();
 END LOOP;
 SELECT now() INTO end_time;
 SELECT end_time - start_time INTO diff;
 RAISE NOTICE '%', start_time;
 RAISE NOTICE '%', end_time; 
 RAISE NOTICE '%', diff; 
END
$$

1 个答案:

答案 0 :(得分:2)

From the manual

Since these functions [CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE] return the start time of the current transaction, their values do not change during the transaction.

And then further down:

transaction_timestamp() is equivalent to CURRENT_TIMESTAMP, but is named to clearly reflect what it returns. statement_timestamp() returns the start time of the current statement (more specifically, the time of receipt of the latest command message from the client). statement_timestamp() and transaction_timestamp() return the same value during the first command of a transaction, but might differ during subsequent commands. clock_timestamp() returns the actual current time, and therefore its value changes even within a single SQL command

So you probably want to use clock_timestamp()