Without Schema2 tool calls
I'll help you diagnose this cache hit ratio drop. Let me first check what PostgreSQL-related files or data we have in your current directory.
A drop from 99% to 85% cache hit ratio is significant and worth investigating. This could be either issue, but there are specific ways to diagnose which. Here's how to investigate:
## Quick Diagnostic Queries
Run these queries to understand what's happening:
**1. Check for sequential scans on large tables:**
```sql
SELECT
schemaname,
tablename,
seq_scan,
seq_tup_read,
idx_scan,
seq_tup_read / NULLIF(seq_scan, 0) as avg_seq_tuples,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) as size
FROM pg_stat_user_tables
WHERE seq_scan > 0
ORDER BY seq_tup_read DESC
LIMIT 20;
```
**2. Check cache hit ratio by table:**
```sql
SELECT
schemaname,
tablename,
heap_blks_hit,
heap_blks_read,
CASE WHEN (heap_blks_hit + heap_blks_read) > 0
THEN round(100.0 * heap_blks_hit / (heap_blks_hit + heap_blks_read), 2)
ELSE NULL
END as cache_hit_ratio
FROM pg_statio_user_tables
WHERE (heap_blks_hit + heap_blks_read) > 0
ORDER BY (heap_blks_hit + heap_blks_read) DESC
LIMIT 20;
```
**3. Identify recent query patterns:**
```sql
SELECT
query,
calls,
total_exec_time,
mean_exec_time,
shared_blks_hit,
shared_blks_read,
CASE WHEN (shared_blks_hit + shared_blks_read) > 0
THEN round(100.0 * shared_blks_hit / (shared_blks_hit + shared_blks_read), 2)
ELSE NULL
END as query_cache_hit_ratio
FROM pg_stat_statements
ORDER BY (shared_blks_hit + shared_blks_read) DESC
LIMIT 20;
```
*(Requires `pg_stat_statements` extension)*
## What to Look For
**Signs it's full table scans:**
- High `seq_scan` counts on large tables
- Queries with low cache hit ratios in `pg_stat_statements`
- Missing indexes on frequently queried columns
- New queries or data growth making indexes ineffective
**Signs you need more shared_buffers:**
- All queries show decreased cache hit ratios uniformly
- Working set size has grown beyond current `shared_buffers`
- No obvious problematic queries
- Database size increased significantly
## Recommendations
1. **If it's full table scans**: Adding indexes or optimizing queries will be more effective than increasing memory
2. **If it's memory**: Only increase `shared_buffers` if your working set actually needs it. Rule of thumb: 25% of RAM for dedicated DB servers, 10-15% for shared servers
3. **Check for data growth**: Has your database size increased recently? Compare with your current `shared_buffers` setting
Would you like me to help you analyze specific query results or create a monitoring script to track this over time?
I'll help you diagnose this PostgreSQL cache hit ratio drop. Let me first check what files you have available that might give us context.
A drop from 99% to 85% cache hit ratio is significant and needs investigation. Both scenarios you mentioned are possible, but they have different indicators. Let me help you diagnose this:
## Key Diagnostic Queries
**1. Check current cache hit ratio by database:**
```sql
SELECT
datname,
blks_hit::float / (blks_hit + blks_read)::float * 100 AS cache_hit_ratio,
blks_hit, blks_read
FROM pg_stat_database
WHERE blks_read > 0
ORDER BY cache_hit_ratio;
```
**2. Identify tables with low cache hit ratios:**
```sql
SELECT
schemaname, relname,
heap_blks_hit::float / NULLIF(heap_blks_hit + heap_blks_read, 0)::float * 100 AS cache_hit_ratio,
seq_scan, seq_tup_read, idx_scan,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||relname)) AS size
FROM pg_statio_user_tables
WHERE heap_blks_read > 0
ORDER BY heap_blks_hit::float / NULLIF(heap_blks_hit + heap_blks_read, 0)::float
LIMIT 20;
```
**3. Find queries doing sequential scans on large tables:**
```sql
SELECT
schemaname, relname, seq_scan, seq_tup_read,
seq_tup_read / NULLIF(seq_scan, 0) AS avg_seq_tup_read,
idx_scan,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||relname)) AS table_size
FROM pg_stat_user_tables
WHERE seq_scan > 0
ORDER BY seq_tup_read DESC
LIMIT 20;
```
## Distinguishing the Two Scenarios
**Full table scans (more likely):**
- You'll see high `seq_scan` and `seq_tup_read` values
- Specific tables show up with poor cache ratios
- The problem is localized to certain tables/queries
- **Fix:** Add missing indexes, optimize queries
**Insufficient shared_buffers:**
- Cache hit ratio drops uniformly across all tables
- Working set size > shared_buffers
- You've recently added significant data volume
- **Fix:** Increase shared_buffers (but carefully)
## Quick Checks
**Compare your working set to shared_buffers:**
```sql
-- Current shared_buffers setting
SHOW shared_buffers;
-- Active data being accessed
SELECT pg_size_pretty(sum(pg_total_relation_size(schemaname||'.'||relname)))
FROM pg_stat_user_tables
WHERE last_seq_scan > NOW() - INTERVAL '7 days'
OR last_idx_scan > NOW() - INTERVAL '7 days';
```
## Most Likely Cause
A sudden drop like this (99% → 85% over a week) is **more likely to be full table scans** from:
- New queries missing indexes
- Changed query patterns
- Reports/analytics queries added recently
- Data growth making existing indexes less effective
Increasing shared_buffers usually gives gradual improvements, not sudden drops when undersized.
## Next Steps
1. Run the sequential scan query above to identify culprits
2. Check `pg_stat_statements` for slow queries (if enabled)
3. Look at `EXPLAIN ANALYZE` for your most frequent queries
4. Consider: Did anything change in your application code this week?
Would you like me to create a diagnostic script to run these checks and analyze the results?