在 pgAdminIII 中获取函数计时

Get function timings in pgAdminIII

我想比较 pgAdmin 的 SQL 编辑器中的两个函数。这是脚本。但是,当我 运行 它时,无论迭代多少次, start_timeend_time 似乎都具有相同的值。 (但是,Query returned successfully with no result in nnn ms. 消息确实随着循环大小的每次增加而增加。)为什么?

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
$$

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.

然后再往下:

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

所以你可能想使用 clock_timestamp()