Search code examples
rubypostgresqltimingpg

How to get timing from the PgSQL DB


I'm attempting to get the timing of a query using the pg gem. I saw the documentation, but I did not find anything. I want to know if there is something like query.time.

I added \timing to ~/.psqlrc, so the command is active by default. If I write the query in the Postgres console the time is active.

This is the code:

conn = PGconn.open(:dbname => 'my_db') 
query=conn.exec('SELECT * from some_table')
puts query.num_tuples -> this work
puts query.time  -> undefined method

I need to measure the time of Postgres itself, I cannot use the Time class from Ruby.


Solution

  • Try this as it should return the time back.

    puts conn.exec("EXPLAIN ANALYZE SELECT * FROM some_table").values