Today I Learned

hashrocket A Hashrocket project

Sleeping In PostgreSQL

Generally you want your SQL statements to run against your database as quickly as possible. For those times when you are doing some sort of debugging or just want your queries to look very computationally expensive, PostgreSQL offers the pg_sleep function.

To sleep for 5 seconds, try the following:

> select now(); select pg_sleep(5); select now();
 2016-01-08 16:30:21.251081-06
(1 row)

Time: 0.274 ms

(1 row)

Time: 5001.459 ms
 2016-01-08 16:30:26.252953-06
(1 row)

Time: 0.260 ms

As you'll notice, the pg_sleep statement took about 5 seconds.


See More #sql TILs
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.