Skip to main content

Overview

Knowledge Stack uses PostgreSQL’s pg_prewarm extension to load frequently accessed tables and indexes into memory when the database starts. This eliminates cold-start latency and ensures fast query performance from the moment your instance is ready. The most performance-critical operations — chunk retrieval, vector search, and folder/document listing — all benefit from prewarming.

What Gets Prewarmed

The following data is loaded into PostgreSQL’s shared buffers, ordered by access frequency:

Running the Prewarm

To manually trigger cache prewarming, run the following SQL:
This returns a table showing each object and the number of 8KB pages loaded into memory:

Automatic Prewarming on Startup

To prewarm automatically when your database starts, add the following to your database initialization script:

Docker Compose Integration

If you run PostgreSQL via Docker Compose, you can add an init script: docker-compose.yml:
init-db.sh:

Performance Considerations

Memory Impact

  • Each page loaded is 8KB
  • Prewarming uses PostgreSQL’s shared_buffers setting
  • If shared_buffers is too small, prewarming may evict other cached data
  • Recommendation: Set shared_buffers to at least 256MB. A common guideline is 25% of total system RAM.

When to Prewarm

  • After a cold start: The most important time to prewarm — restores fast query performance immediately
  • After database migrations: When new data structures or indexes are created
  • On a schedule: Periodically (e.g., nightly) if your workload frequently evicts cached data

Monitoring Cache Usage

To see how much space your tables and indexes occupy:

Troubleshooting

”function knowledgestack.prewarm_cache() does not exist”

The prewarm function is created by a database migration. Ensure all migrations have been applied to your database.

”extension pg_prewarm does not exist”

The pg_prewarm extension should be created automatically by the migration. You can verify it exists:

“permission denied for function pg_prewarm”

Your database user needs superuser privileges or the pg_read_all_data role to execute pg_prewarm.

Further Reading