본문으로 건너뛰기

3.1 Virtual memory와 page cache

Linux process가 보는 virtual address와 실제 physical memory는 page table로 연결됩니다. PostgreSQL의 shared buffers, backend private memory, executable mapping, filesystem page cache가 모두 같은 physical memory를 경쟁합니다.

free -h
grep -E 'MemAvailable|Cached|Dirty|Writeback|Slab' /proc/meminfo
cat /proc/PID/smaps_rollup

freeused만 보고 부족하다고 판단하지 않습니다. MemAvailable은 reclaim 가능한 cache를 고려한 추정치입니다. Page cache는 PostgreSQL data file read를 빠르게 만들 수 있으므로 사용 중인 memory입니다.

Process memory 구분

  • RSS: 현재 physical memory에 올라온 page
  • PSS: shared page를 process 수에 비례해 나눈 값
  • anonymous: heap, stack 같은 file에 직접 대응하지 않는 memory
  • file-backed: executable, library, mapped file

PostgreSQL 전체 memory를 추정할 때 shared_buffers + max_connections × work_mem처럼 단순 곱하면 과대 계산하기 쉽습니다. work_mem은 connection당 한 번이 아니라 sort와 hash node마다 쓰일 수 있지만 모든 query가 동시에 최대치를 쓰지도 않습니다. 실제 concurrency와 plan node를 측정합니다.

Dirty page

Dirty가 늘고 Writeback이 지속되면 kernel flusher와 storage latency를 확인합니다. Database의 checkpoint와 kernel writeback이 동시에 몰리면 write burst가 커질 수 있습니다.

vmstat 1
pidstat -r -p PID 1

Major fault는 storage에서 page를 가져와야 할 가능성이 있고, minor fault는 storage I/O 없이 mapping을 해결합니다. Fault 수는 workload 특성과 함께 비교합니다.