This project involves modifying PostgreSQL’s buffer replacement policy to implement YACLOCK (Yet Another Clock), a custom algorithm that replaces the default clock-based strategy.
- Benchmarking (Part 1)
- Buffer Management in PostgreSQL
- YACLOCK Replacement Policy
- Implementation Guide
- Testing
- Shared vs Local Buffers
- Free List vs Buffer Pool
- Compilation & Execution
- References
Before modification, PostgreSQL was benchmarked using pgbench:
- Shared Buffers: 64MB
- TPS: ~8234.7
- Latency: ~1.214 ms
- Cache Hit Ratio: ~99.84%
Command used:
pgbench -i -s 4
pgbench -c 10 -j 10 -T 360- Shared Buffers: Cache pages from permanent tables/indexes.
- Local Buffers: Used for session-specific temporary relations.
- Free List: Set of unused buffer frames.
- Buffer Pool: Entire memory-resident structure containing all buffer frames.
YACLOCK uses a circular queue of buffer frames, each with a refBit, and a clock hand called next.
- Page already in buffer →
refBit = 1 - Page not in buffer, free list not empty → Insert at tail,
refBit = 0 - Page not in buffer, free list empty → Use clock-hand-based eviction:
- Skip pinned frames
- If
refBit == 1, set it to 0 and skip - If
refBit == 0, evict and move frame to tail
- Update
nextif it points to the removed frame - Remove frame from the queue
- Reset
nextto NULL if queue is empty
Modify only:
cs3223_assign1/freelist_yaclock.cKey Functions:
StrategyAccessBuffer(int buf_id, int event_num)StrategyInitialize()StrategyShmemSize()StrategyGetBuffer()StrategyFreeBuffer()
Use ShmemInitStruct() for shared memory. Do not use malloc.
Run test cases using:
$ ./test-yaclock.sh
$ pg_ctl stopEach test file (in test_bufmgr/testcases/) simulates operations on a 43-page relation movies.
Operation types:
read_pin_block(blkno)read_unpin_block(blkno)unpin_block(blkno)
| Feature | Shared Buffers | Local Buffers |
|---|---|---|
| Scope | Global | Per-process |
| Concurrency | Requires locks | No locking needed |
| Used For | Permanent tables/indexes | Temporary session-specific data |
| Memory Location | Shared memory | Backend-local memory |
- Buffer Pool: All buffer frames
- Free List: Subset of unused buffer frames
Flow:
- Try buffer pool → cache hit
- Else, try free list → use unused buffer
- Else, apply replacement policy (YACLOCK)
make freelist_yaclock.o
make yaclock
./test-yaclock.sh
pg_ctl stopTo restore default Clock policy:
make clock