Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a query that takes long time to run in PostgreSQL

Tags:

sql

postgresql

To test my application I need a query to run for a long time (at least a couple of minutes). Any ideas on how to create this quickly?

The application needs to query the catalog to see a list of running queries.

My application uses postgresql. I am fine with creating additional dummy tables if required.

like image 877
Kalyan Vedala Avatar asked Feb 11 '16 10:02

Kalyan Vedala


People also ask

How make PostgreSQL query run faster?

Some of the tricks we used to speed up SELECT-s in PostgreSQL: LEFT JOIN with redundant conditions, VALUES, extended statistics, primary key type conversion, CLUSTER, pg_hint_plan + bonus. Photo by Richard Jacobs on Unsplash.

How do I fix slow query in PostgreSQL?

So How Do You Fix Slow Queries in PostgreSQL? To speed up this particular PostgreSQL slow query, we need to know whether we really need all rows. If not, we should only get N of them by adding a LIMIT clause. If they are, we should use a cursor.

Why Postgres query is slow?

PostgreSQL attempts to do a lot of its work in memory, and spread out writing to disk to minimize bottlenecks, but on an overloaded system with heavy writing, it's easily possible to see heavy reads and writes cause the whole system to slow as it catches up on the demands.

How do I find slow queries in PostgreSQL?

Typically discovered through slow response or extended increases in database CPU, the pg_stat_activity view can help to find out what query is causing issues. The pg_stat_activity view contains details of all currently running queries, including user, connection, and timing details.


1 Answers

this will run for 5 minutes:

select pg_sleep(5 * 60);

the parameter for pg_sleep() is the duration in seconds.

You can also sleep until a specific timestamp using pg_sleep_until()

More details in the manual:
http://www.postgresql.org/docs/9.5/static/functions-datetime.html#FUNCTIONS-DATETIME-DELAY

like image 78
a_horse_with_no_name Avatar answered Nov 08 '22 10:11

a_horse_with_no_name