How to tune my Postgres server?


How to tune my Postgres server?



I have a 4 GB server with 80GB hard disk. The website is really slow when switching between pages. I checked top processes and postgres processes with 70 -100 % cpu usage keep popping up. they dont last for more than 4 seconds usually and then its a new one. In my postgres database table i have about 12 tables but one of them has about 90 million entries( rows). This is the table causing the slowdown. But how do i tune my postgres parameters like shared_buffer size and all for optimum performance?



It runs on Ubuntu 16 OS, its a django webapp.



my database name is roctim it has size 15 gigabites (as of now, but is increasing)


postgres=# select datname, pg_size_pretty(pg_database_size(oid)) as db_size from pg_database;

datname | db_size
-----------+---------
template1 | 6857 kB
template0 | 6857 kB
postgres | 6992 kB
roctim | 15 GB



the size of my biggest table is 15 GB called "Webapp_sensordata". It collects and stores livedata from machines like crushers, conveyors etc.



When I run select query from pg_stat_statements order by total_time desc;,
I get


select query from pg_stat_statements order by total_time desc;


SELECT "Webapp_sensordata"."id", "Webapp_sensordata"."timestamp",
"Webapp_sensordata"."value", "Webapp_sensordata"."machine_id",
"Webapp_sensordata"."type_id"
FROM "Webapp_sensordata"
WHERE "Webapp_sensordata"."machine_id" = ?
ORDER BY "Webapp_sensordata"."timestamp" DESC
LIMIT ?



and


SELECT "Webapp_sensordata"."id", "Webapp_sensordata"."timestamp",
"Webapp_sensordata"."value", "Webapp_sensordata"."machine_id",
"Webapp_sensordata"."type_id"
FROM "Webapp_sensordata"
WHERE ("Webapp_sensordata"."type_id" = ?
AND "Webapp_sensordata"."machine_id" = ?)
ORDER BY "Webapp_sensordata"."timestamp" DESC
LIMIT ?



as the top two queries, they both access the big table sensor data.



Also my shared_buffer and working_mem is set to the default right now (128mb and 4mb)



When I ran EXPLAIN ANALYZE the second query shown above, i got


QUERY PLAN
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
----------------------
Sort (cost=62127.86..62135.64 rows=3111 width=24) (actual
time=781.051..781.230 rows=2860 loops=1)
Sort Key: "timestamp" DESC
Sort Method: quicksort Memory: 320kB
-> Index Scan using "Webapp_sensordata_machine_id_e353fc5a" on
"Webapp_sensordata" (cost=0.57..61947.37 rows=3111 width=24) (actual
time=4.190..779.783 rows=2860 loops=1)
Index Cond: (machine_id = 3)
Filter: (type_id = 1)
Rows Removed by Filter: 31440
Planning time: 4.572 ms
Execution time: 781.449 ms
(9 rows)





Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please learn how to create a Minimal, Complete, and Verifiable Example.
– Bear Brown
Jun 28 at 13:41





I tried my best to explain with all the details i have. I'm not a database expert, if the answer needs more information im glad to post it as soon as i know what more is needed
– user6714507
Jun 28 at 13:42





Well, to help you people need to know more - which OS you have? Add size of your database - using for example select datname, pg_size_pretty(pg_database_size(oid)) as db_size from pg_database. Add size of your biggest table - using for example select ns.nspname, c.relname, pg_size_pretty(pg_total_relation_size(c.oid)) as table_size from pg_class c join pg_namespace ns on c.relnamespace=ns.oid where relkind='r' order by table_size desc.
– JosMac
Jun 28 at 14:08



select datname, pg_size_pretty(pg_database_size(oid)) as db_size from pg_database


select ns.nspname, c.relname, pg_size_pretty(pg_total_relation_size(c.oid)) as table_size from pg_class c join pg_namespace ns on c.relnamespace=ns.oid where relkind='r' order by table_size desc





You need to find the slow statements, run EXPLAIN (ANALYZE, BUFFERS) on them and add the result to the question.
– Laurenz Albe
Jun 28 at 14:37


EXPLAIN (ANALYZE, BUFFERS)





@JosMac I've added the results from your queries.
– user6714507
Jun 29 at 9:00




1 Answer
1



Your query would be faster if both conditions can be handled during the index scan by a multi-column index:


CREATE INDEX ON "Webapp_sensordata" (type_id, machine_id);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift