How does PostgreSQL cache statements and data?

Generally, only the contents of table and index files will be cached in the shared buffer space.

Query plans are cached in some circumstances. The best way to ensure this is to PREPARE the query once, then EXECUTE it each time.

The results of a query are not automatically cached. If you rerun the same query — even if it’s letter-for-letter identical, and no updates have been performed on the DB — it will still execute the whole plan. It will, of course, make use of any table/index data that’s already in the shared buffers cache; so it will not necessarily have to read all the data from disk again.

Update on plan caching

Plan caching is generally done per session. This means only the connection that makes the plan can use the cached version. Other connections have to make and use their own cached versions. This isn’t really a performance issue because the saving you get from reusing a plan is almost always miniscule compared to the cost of connecting anyway. (Unless your queries are really complicated.)

It does cache if you use PREPARE: http://www.postgresql.org/docs/current/static/sql-prepare.html

It does cache when the query is in a PL/plSQL function: http://www.postgresql.org/docs/current/static/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING

It does not cache ad-hoc queries entered in psql.

Hopefully someone else can elaborate on any other cases of query plan caching.

Leave a Comment