Directly, no, no. But you can make an indirect and fairly close assessment by checking the time right before and immediately after the request that interests you.
$sql = "Your Query"; $bm = "SELECT extract(epoch FROM clock_timestamp())"; $query = "{$bm}; {$sql}; {$bm};";
The clock_timestamp () function gives you the actual server time when the instruction starts. Since SELECT does not contain tables, we can expect it to be almost instantaneous. I assume that any Pg driver offers support for multiple requests; it is important that these 3 requests (real and 2 additions) are combined, otherwise you will also measure the data transfer time ...
For PHP, I have a function to handle this. As a result, it looks as follows:
<?php function pgquery($sql, $conn) { // Prepend and append benchmarking queries $bm = "SELECT extract(epoch FROM clock_timestamp())"; $query = "{$bm}; {$sql}; {$bm};"; // Execute the query, and time it (data transport included) $ini = microtime(true); pg_send_query($conn, $query); while ($resource = pg_get_result($conn)) { $resources[] = $resource; } $end = microtime(true); // "Extract" the benchmarking results $q_ini = pg_fetch_row(array_shift($resources)); $q_end = pg_fetch_row(array_pop($resources)); // Compute times $time = round($end - $ini, 4);
I just left the basics there. $ conn contains a link to the Pg connection, and $ resources contains an array of pg resources returned (in case you sent several requests in your sql file).
$ time has a total time since the request remains for the Pg server until the result reaches the result. $ q-time contains only the actual request time (or a very good approximation).
Add error handling and other processing to your liking, I have a lot, but this is not relevant to your question.
user832146
source share