Questions tagged [postgresql-performance]

About PostgreSQL query optimization. Include sufficient information in questions. For complex, advanced questions or if you're looking to tune without modifying queries, ask on https://dba.stackexchange.com/ instead.

In PostgreSQL there are typically many different queries to achieve the same output from a given input. If you already have a working query but need a faster solution then this tag may be appropriate. Tag with as well.

Questions about overall Postgres server performance tuning, or the performance of queries you cannot change are probably better directed to dba.stackexchange.com. See the Stack Overflow Help about suitable questions.

Read the Slow Query Questions page on the PostgreSQL Wiki before posting, including the "Things to try before you post" section. Using EXPLAIN ANALYZE is particularly important.

When posting questions, include:

  • Your Postgres version, at least the major version like "15" or "9.6". (Since Postgres 10, the major version is just the leading number.) Find out with: SELECT version();

  • The full query text, in readable format and as brief as possible. But don't remove anything that might be relevant. Describe the expected result, include an example.

  • Definition of involved objects. Best as valid CREATE TABLE and CREATE INDEX scripts. The output from \d+ tablename (for tables) in psql is second best.

  • Cardinalities (rough number of rows) in involved tables. And rough number of distinct values in relevant columns.

  • Query plan(s) obtained with EXPLAIN (BUFFERS, ANALYZE) (from v12 on, include SETTINGS in the parentheses). If possible, paste on explain.depesz.com and include link(s).

  • If possible, link to a short demo on dbfiddle.uk or similar.

  • Only if relevant, a brief mention of your hardware and system, like:
    "CentOS 6.1, Xeon E5-2450 with 64GB RAM, 4-disk RAID 10 of Intel X-25E SSDs on Dell PERC H810 controller with flash write-back cache"

541 questions
161
votes
10 answers

Running PostgreSQL in memory only

I want to run a small PostgreSQL database which runs in memory only, for each unit test I write. For instance: @Before void setUp() { String port = runPostgresOnRandomPort(); connectTo("postgres://localhost:"+port+"/in_memory_db"); //…
Chi-Lan
  • 3,575
  • 3
  • 22
  • 24
95
votes
8 answers

Best way to delete millions of rows by ID

I need to delete about 2 million rows from my PG database. I have a list of IDs that I need to delete. However, any way I try to do this is taking days. I tried putting them in a table and doing it in batches of 100. 4 days later, this is still…
Anthony Greco
  • 2,885
  • 4
  • 27
  • 39
85
votes
3 answers

Optimize GROUP BY query to retrieve latest row per user

I have the following log table for user messages (simplified form) in Postgres 9.2: CREATE TABLE log ( log_date DATE, user_id INTEGER, payload INTEGER ); It contains up to one record per user and per day. There will be approximately…
53
votes
1 answer

Finding similar strings with PostgreSQL quickly

I need to create a ranking of similar strings in a table. I have the following table create table names ( name character varying(255) ); Currently, I'm using pg_trgm module which offers the similarity function, but I have an efficiency problem. I…
cdarwin
  • 4,141
  • 9
  • 42
  • 66
50
votes
4 answers

Postgres query optimization (forcing an index scan)

Below is my query. I am trying to get it to use an index scan, but it will only seq scan. By the way the metric_data table has 130 million rows. The metrics table has about 2000 rows. metric_data table columns: metric_id integer , t timestamp , d…
48
votes
2 answers

How to understand an EXPLAIN ANALYZE

I am not very familiar with looking at EXPLAIN ANALYZE results, I have a huge problem with my queries being too slow. I have tried to read up on how to interpret results from an explain queries, but I still don't know what I should be looking for,…
44
votes
6 answers

How do I speed up counting rows in a PostgreSQL table?

We need to count the number of rows in a PostgreSQL table. In our case, no conditions need to be met, and it would be perfectly acceptable to get a row estimate if that significantly improved query speed. Basically, we want select count(id) from…
Juan Carlos Coto
  • 11,900
  • 22
  • 62
  • 102
39
votes
2 answers

Postgres not using index when index scan is much better option

I have a simple query to join two tables that's being really slow. I found out that the query plan does a seq scan on the large table email_activities (~10m rows) while I think using indexes doing nested loops will actually be faster. I rewrote the…
Ryan Her
  • 1,067
  • 1
  • 9
  • 14
34
votes
3 answers

Any downsides of using data type "text" for storing strings?

According to the PostgreSQL Documentation, they support 3 data-types for character data: character varying(n), varchar(n) variable-length with limit character(n), char(n) fixed-length, blank padded text …
hemantvsn
  • 1,316
  • 3
  • 12
  • 24
33
votes
6 answers

Keep PostgreSQL from sometimes choosing a bad query plan

I have a strange problem with PostgreSQL performance for a query, using PostgreSQL 8.4.9. This query is selecting a set of points within a 3D volume, using a LEFT OUTER JOIN to add a related ID column where that related ID exists. Small changes in…
33
votes
4 answers

Improving query speed: simple SELECT in big postgres table

I'm having trouble regarding speed in a SELECT query on a Postgres database. I have a table with two integer columns as key: (int1,int2) This table has around 70 million rows. I need to make two kind of simple SELECT queries in this…
alexdemartos
  • 553
  • 1
  • 4
  • 8
25
votes
2 answers

Does the order of columns in a Postgres table impact performance?

In Postgres does the order of columns in a CREATE TABLE statement impact performance? Consider the following two cases: CREATE TABLE foo ( a TEXT, B VARCHAR(512), pkey INTEGER PRIMARY KEY, bar_fk INTEGER REFERENCES bar(pkey), …
ams
  • 60,316
  • 68
  • 200
  • 288
25
votes
4 answers

PostgreSQL query runs faster with index scan, but engine chooses hash join

The query: SELECT "replays_game".* FROM "replays_game" INNER JOIN "replays_playeringame" ON "replays_game"."id" = "replays_playeringame"."game_id" WHERE "replays_playeringame"."player_id" = 50027 If I set SET enable_seqscan = off, then it does the…
22
votes
4 answers

Postgres: Optimizing querying by datetime

I have a table that has a datetime field "updated_at". A lot of my queries will be querying on this field using range queries such as rows that have updated_at > a certain date. I already added an index to updated_at, but most of my queries are…
Henley
  • 21,258
  • 32
  • 119
  • 207
18
votes
2 answers

Configuration parameter work_mem in PostgreSQL on Linux

I have to optimize queries by tuning basic PostgreSQL server configuration parameters. In documentation I've came across the work_mem parameter. Then I checked how changing this parameter would influence performance of my query (using sort). I…
Grzes
  • 971
  • 1
  • 13
  • 28
1
2 3
36 37