简体   繁体   中英

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. How to make that /copy will not overwrite my file(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. See here for more details:

statement_timestamp() and transaction_timestamp() return the same value during the first command of a transaction,

To fix you issue, use timeofday() or clock_timestamp() instead of NOW() :

timeofday() is a historical PostgreSQL function. Like clock_timestamp(), it returns the actual current time, but as a formatted text string rather than a timestamp with time zone value.

To use it whit EXTRACT(), cast the result to TIMESTAMP with 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:

#!/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.

Considerations

  • The function accept a argument but than does not use it. Eventually remove the parameter.
  • Loading the function every time you want to run it is a bad design. Better you separate the function definition from running it in two different files. The function should loaded only once, by database setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM