[英]time of executing queries in postgreSQL
I'm trying to write a script which show me the time of executing some queries I have 2 problems with my script: 1. Why the result of function is 0? 我正在尝试编写一个脚本,该脚本向我显示执行某些查询的时间,我的脚本有2个问题:1.为什么函数的结果为0? 2. How to make that /copy will not overwrite my file(test.txt)?
2.如何使/ copy不会覆盖我的文件(test.txt)?
Script: 脚本:
#!/bin/bash
psql WYPOZYCZALNIA postgres<< EOF
CREATE OR REPLACE FUNCTION funkcja(i integer) returns double precision as'
DECLARE
czas_start double precision;
czas_stop double precision;
BEGIN
SELECT extract(epoch from now()) into czas_start;
insert into UZYTKOWNICY VALUES(16,''PIOTiR'',''510784543'');
SELECT extract(epoch from now()) into czas_stop;
RETURN czas_stop-czas_start;
end;
'language 'plpgsql';
\copy (select * from funkcja(50)) To 'test.txt'
\q
EOF
The function NOW()
is a alias for and transaction_timestamp(), and returns always the start time of the current transaction , therefore returns always the same value inside a stored procedure. 函数
NOW()
是和transaction_timestamp()的别名,并且始终返回当前事务的开始时间 ,因此在存储过程中始终返回相同的值。 See here for more details: 请参阅此处以获取更多详细信息:
statement_timestamp() and transaction_timestamp() return the same value during the first command of a transaction,
statement_timestamp()和transaction_timestamp()在事务的第一个命令期间返回相同的值,
To fix you issue, use timeofday()
or clock_timestamp()
instead of NOW()
: 要解决您的问题,请使用
timeofday()
或clock_timestamp()
而不是NOW()
:
timeofday() is a historical PostgreSQL function.
timeofday()是PostgreSQL的历史功能。 Like clock_timestamp(), it returns the actual current time, but as a formatted text string rather than a timestamp with time zone value.
像clock_timestamp()一样,它返回实际的当前时间,但以格式化的文本字符串的形式返回,而不是带有时区值的时间戳。
To use it whit EXTRACT(), cast the result to TIMESTAMP with timeofday()::TIMESTAMP
: 要在EXTRACT()中使用它,请使用
timeofday()::TIMESTAMP
将结果转换为timeofday()::TIMESTAMP
:
SELECT extract(epoch from timeofday()::TIMESTAMP) into czas_start;
insert into UZYTKOWNICY VALUES(16,''PIOTiR'',''510784543'');
SELECT extract(epoch from timeofday()::TIMESTAMP) into czas_stop;
How to append the result instead of overwrite the output file 如何附加结果而不是覆盖输出文件
The \\copy
command does not support append instead of overwrite, so better you don't use \\copy
but run the function in psql and redirect the output to the file: \\copy
命令不支持附加而不是覆盖,因此最好不要使用\\copy
而是在psql中运行该函数并将输出重定向到文件:
#!/bin/bash
psql WYPOZYCZALNIA postgres<< EOF
CREATE OR REPLACE FUNCTION funkcja(i integer) returns double precision as \$BODY$
DECLARE
czas_start double precision;
czas_stop double precision;
BEGIN
SELECT extract(epoch from timeofday()::TIMESTAMP) into czas_start;
INSERT INTO UZYTKOWNICY VALUES(16,'PIOTiR','510784543');
SELECT extract(epoch from timeofday()::TIMESTAMP) into czas_stop;
RETURN czas_stop-czas_start;
end;
\$BODY$ language 'plpgsql';
EOF
psql WYPOZYCZALNIA postgres -t -A -c "SELECT funkcja(50)" >> 'test.txt'
The psql option -A
means do not align the output and -t
means show only tuple in output. psql选项
-A
表示不对齐输出, -t
表示仅在输出中显示元组。
Considerations 注意事项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.