Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See and clear Postgres caches/buffers?

Sometimes I run a Postgres query and it takes 30 seconds. Then, I immediately run the same query and it takes 2 seconds. It appears that Postgres has some sort of caching. Can I somehow see what that cache is holding? Can I force all caches to be cleared for tuning purposes?

I'm basically looking for a Postgres version of the following SQL Server command:

DBCC FREEPROCCACHE DBCC DROPCLEANBUFFERS 

But I would also like to know how to see what is actually contained in that buffer.

like image 946
User1 Avatar asked Aug 01 '09 13:08

User1


People also ask

How do I flush the buffer cache in PostgreSQL?

In PostgreSQL, we do not have any predefined functionality to clear the cache from the memory. To clear the database level cache, we need to shut down the whole instance and to clear the operating system cache, we need to use the operating system utility commands.

Does PostgreSQL cache results?

It will execute it each time. It will cache the data it needs to do the execution (assuming that is small enough to stay in cache), but not the results.

What is Postgres buffer?

A buffer manager manages data transfers between shared memory and persistent storage and can have a significant impact on the performance of the DBMS. The PostgreSQL buffer manager works very efficiently. In this chapter, the PostgreSQL buffer manager is described.

What is meant by buffer cache?

In SQL Server, the buffer cache is the memory that allows you to query frequently accessed data quickly. When data is written to or read from a SQL Server database, the buffer manager copies it into the buffer cache (aka the buffer pool).


1 Answers

You can see what's in the PostgreSQL buffer cache using the pg_buffercache module. I've done a presentation called "Inside the PostgreSQL Buffer Cache" that explains what you're seeing, and I show some more complicated queries to help interpret that information that go along with that.

It's also possible to look at the operating system cache too on some systems, see [pg_osmem.py] for one somewhat rough example.

There's no way to clear the caches easily. On Linux you can stop the database server and use the drop_caches facility to clear the OS cache; be sure to heed the warning there to run sync first.

like image 85
Greg Smith Avatar answered Sep 21 '22 13:09

Greg Smith