patternsqlMajor
PostgreSQL: Force data into memory
Viewed 0 times
postgresqlforceintomemorydata
Problem
Is there a systematic way to force PostgreSQL to load a specific table into memory, or at least read it from disk so that it will be cached by the system?
Solution
Postgres 9.4 finally added an extension to preload data from relations into the OS or database buffer cache (at your choice):
This allows reaching full operating performance more quickly.
Run once in your database (detailed instructions here):
Then it's simple to preload any given relation. Basic example:
Finds the first table named
Or:
system, if this is supported, or throws an error otherwise.
reads the requested range of blocks; unlike
synchronous and supported on all platforms and builds, but may be
slower.
buffer cache.
The default is
Read the manual for more details.
Depesz blogged about it, too.
pg_prewarmThis allows reaching full operating performance more quickly.
Run once in your database (detailed instructions here):
CREATE EXTENSION pg_prewarm;Then it's simple to preload any given relation. Basic example:
SELECT pg_prewarm('my_tbl');Finds the first table named
my_tbl in the search path and loads it to the Postgres buffer cache.Or:
SELECT pg_prewarm('my_schema.my_tbl', 'prefetch');prefetch issues asynchronous prefetch requests to the operatingsystem, if this is supported, or throws an error otherwise.
readreads the requested range of blocks; unlike
prefetch, this issynchronous and supported on all platforms and builds, but may be
slower.
buffer reads the requested range of blocks into the databasebuffer cache.
The default is
buffer, which has the greatest impact (higher cost, best effect).Read the manual for more details.
Depesz blogged about it, too.
Code Snippets
CREATE EXTENSION pg_prewarm;SELECT pg_prewarm('my_tbl');SELECT pg_prewarm('my_schema.my_tbl', 'prefetch');Context
StackExchange Database Administrators Q#2041, answer score: 47
Revisions (0)
No revisions yet.