PostgreSQL: How to Clear Cache of the Database Sessions

This is one of the important discussion for PostgreSQL Database Administrator.
Yes, it is true that PostgreSQL has a caching system by setting “shared_buffers”.

But I didn’t find any perfect solution to clear the cache of PostgreSQL Server.
If you really want to clear cache, you should restart the PostgreSQL Server.

In this post, I provide you a short note on PostgreSQL Discard functionality to discard temp, query plan for running sessions.

Using DISCARD you can release internal resources of a Database Sessions.

DISCARD TEMP: Using this, you can drop all temporary tables which are created in the current session.

DISCARD PLANS: Using this, you can release all internal cached query plans.

DISCARD ALL: Using this, you can reset a session to its original state. You cannot execute this command inside the transaction block.

Once you have executed DISCARD ALL, internally following sequence executes.

Anvesh Patel

Leave a Reply

8 Comments on "PostgreSQL: How to Clear Cache of the Database Sessions"

Notify of
Sort by:   newest | oldest | most voted

Hi there, I enjoy reading through your article post.
I like to write a little comment to support you.


Thank you for sharing excellent informations. Your website is very cool. I’m impressed by the details that you’ve on this site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found simply the information I already searched all over the place and simply could not come across. What a perfect web-site.


I appreciate you sharing this blog.Really thank you! Much obliged.


wow, awesome blog.Really thank you! Will read on…


Just wow !, thank you very much.
You are fantastic man, Please Keep sharing…


Thank you sir, for sharing this….


let me check, i have doubt…


thank you..