In any given week, some 50% of the questions on #postgresql IRC and 75% on pgsql-performance are requests for help with a slow query. However, it is rare for the requester to include complete information about their slow query, frustrating both them and those who try to help.
Please post performance related questions to the pgsql-performance mailing list or to the IRC channel.
Note: if asking for help on IRC, post relevant information onto a paste site like https://paste.depesz.com/ and not directly on IRC.
See also: Guide to reporting problems
You will save yourself a lot of time if you try the following things before you post your question:
Please plan to spend ~30 minutes collecting needed info and preparing your question. Thousands of people will see it, but if you don't make it easy for people to help you, they may not respond. If it's well-written and provides needed information, most requests receive helpful responses. Some even lead to bugfixes or other patches. But you're unlikely to receive much unpaid help from the community without a significant effort on your part to provide as much information as may be relevant.
Please provide the exact server version you are using (SELECT version(); is an easy way to get it). The behavior of the planner changes in every release, so this is important.
What OS / version ? At least for linux, you can get the distribution by running:
tail /etc/*release
Post the definitions of all tables and indexes referenced in the query. If the query touches views or custom functions, we'll need those definitions as well. Run psql command "\d table" with the tables/views/indices referenced in the problem query.
In addition to the table definition, please also post the approximate number of rows in the table(s):
Does the table have anything unusual about it?
EXPLAIN (ANALYZE, BUFFERS, SETTINGS) tells us how the query actually was executed, not just how it was planned. This is essential information in finding out how the planner was wrong, if anywhere. Example: EXPLAIN (ANALYZE, BUFFERS) select * from tablename; If you can't run an EXPLAIN (ANALYZE, BUFFERS) because the query never completes, then say so.
EXPLAIN (ANALYZE, BUFFERS, SETTINGS) result into explain.depesz.com and post links to the resulting pages. We love this, because it makes the plans much easier to read and examine.SET track_io_timing = on;Was this query always slow, or has it gotten slower over time? If the plan/execution of the query used to be different, do you have copies of those query plans? Has anything changed in your database other than the accumulation of data?
Please post the essential information about your hardware platform. If any elements of your hardware are unusual, please include detailed information on those. See the Guide to reporting problems for what sort of hardware information is useful.
Are you running autovacuum? If so, with what settings? If not, are you doing manual VACUUM and/or ANALYZE? How often? SELECT * FROM pg_stat_user_tables WHERE relname='table_name';
For data writing queries: have you moved the WAL to a different disk? Changed the settings?
What database configuration settings have you changed? What are their values? (These are things like "shared_buffers", "work_mem", "enable_seq_scan", "effective_io_concurrency", "effective_cache_size", etc). See Server Configuration for a useful query that will show all of your non-default database settings, in an easier to read format than posting pieces of your postgresql.conf file.
Useful to check statistics leading to bad join plan. SELECT (SELECT sum(x) FROM unnest(most_common_freqs) x) frac_MCV, tablename, attname, inherited, null_frac, n_distinct, array_length(most_common_vals,1) n_mcv, array_length(histogram_bounds,1) n_hist, correlation FROM pg_stats WHERE attname='...' AND tablename='...' ORDER BY 1 DESC;
Some suggested values to start with: