PostgreSQL Performance: keep seldomly used small database in memory while server busy with big database

StackOverflow https://stackoverflow.com/questions/17704871

  •  03-06-2022
  •  | 
  •  

Question

I have a server with 64GB RAM and PostgreSQL 9.2. On it is one small database "A" with only 4GB which is only queried once an hour or so and one big database "B" with about 60GB which gets queried 40-50x per second!

As expected, Linux and PostgreSQL fill the RAM with the bigger database's data as it is more often accessed.

My problem now is that the queries to the small database "A" are critical and have to run in <500ms. The logfile shows a couple of queries per day that took >3s though. If I execute them by hand they, too, take only 10ms so my indexes are fine.

So I guess that those long runners happen when PostgreSQL has to load chunks of the small databases indexes from disk.

I already have some kind of "cache warmer" script that repeats "SELECT * FROM x ORDER BY y" queries to the small database every second but it wastes a lot of CPU power and only improves the situation a little bit.

Any more ideas how to tell PostgreSQL that I really want that small database "sticky" in memory?

Was it helpful?

Solution

PostgreSQL doesn't offer a way to pin tables in memory, though the community would certainly welcome people willing to work on well thought out, tested and benchmarked proposals for allowing this from people who're willing to back those proposals with real code.

The best option you have with PostgreSQL at this time is to run a separate PostgreSQL instance for the response-time-critical database. Give this DB a big enough shared_buffers that the whole DB will reside in shared_buffers.

Do NOT create a tablespace on a ramdisk or other non-durable storage and put the data that needs to be infrequently but rapidly accessed there. All tablespaces must be accessible or the whole system will stop; if you lose a tablespace, you effectively lose the whole database cluster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top