Database Storage နဲ့ Crash Recovery
Stock ပြင်တဲ့အခါ memory နဲ့ storage မှာ ဘာဖြစ်လဲ၊ PostgreSQL ရုတ်တရက် ရပ်သွားရင် ဘယ်လို ပြန်တက်လာလဲ လေ့လာပါ။
Code စမ်းလို့ရတဲ့ သင်ခန်းစာ ဖွင့်ရန်
ဒီစာမျက်နှာမှာ
Data Pages နဲ့ Memory Buffers
PostgreSQL က table နဲ့ index data ကို page တွေအဖြစ် စီထားတယ်။ Query တစ်ခုက page လိုလာရင် shared buffers ထဲမှာ ရှိပြီးသားကို သုံးနိုင်တယ်၊ မရှိရင် လိုတဲ့ page ကို ဖတ်ယူတယ်။ Memory ထဲမှာ ပြောင်းထားပြီး storage ဆီ မရေးရသေးတဲ့ page ကို dirty page လို့ ခေါ်တယ်။
-- Run twice and compare shared buffer hits and reads.
EXPLAIN (ANALYZE, BUFFERS)
SELECT title
FROM courses
WHERE id = 1;
Write-Ahead Log နဲ့ Checkpoints
Transaction က commit ဖြစ်ချိန်မှာ ပြင်ထားတဲ့ data page က memory ထဲမှာပဲ ရှိနေနိုင်ပါတယ်။ မီးပြတ်သွားရင်လည်း ဒီပြောင်းလဲမှုကို ပြန်ယူနိုင်ဖို့ log လိုအပ်ပါတယ်။ ပြောင်းထားတဲ့ data pages တွေကို storage မှာ အတည်တကျ မရေးခင် PostgreSQL က ပြောင်းလဲမှုတွေကို Write-Ahead Log (WAL) မှာ အရင်မှတ်တယ်။ ပုံမှန် durability setting တွေနဲ့ဆို commit အောင်မြင်ကြောင်း မပြောခင် လိုတဲ့ WAL records ကို storage ဆီ ရေးတယ်။ Data pages ကို နောက်မှ ရေးလို့ရတယ်။
-- PostgreSQL: compare these statistics before and after a write.
SELECT wal_records, wal_bytes
FROM pg_stat_wal;
SELECT num_timed, num_requested
FROM pg_stat_checkpointer;
Checkpoint- Recovery စနိုင်မယ့် အမှတ်တစ်ခု ထားဖို့ dirty pages တွေကို storage ဆီ ရေးတယ်။ Recovery လုပ်ရင် checkpoint နဲ့ ဆက်စပ်တဲ့ redo point ကနေ WAL ပြန်ဖတ်ပြီး ပြောင်းလဲမှုတွေ ပြန်သုံးနိုင်တယ်။
Crash recovery- Stock ပြင်တာ commit အောင်မြင်ပြီး data page မရေးရသေးခင် server ရပ်သွားရင် ပြန်ဖွင့်တဲ့အခါ WAL မှာ ရေးထားတဲ့ ပြောင်းလဲမှုကို ပြန်သုံးနိုင်တယ်။
Backup ကနေ Restore လုပ်ရမယ့် အချိန်
Data files နဲ့ လိုအပ်တဲ့ WAL ရှိနေသေးရင် PostgreSQL က crash ဖြစ်ပြီးနောက် data ကို ပြန်လည်တည်ငြိမ်အောင် လုပ်နိုင်ပါတယ်။ Data မှားဖျက်မိတာ၊ storage ပျက်တာဆိုရင် စမ်းသပ်ထားတဲ့ backup နဲ့ restore လုပ်နည်းကို သုံးပါ။ သတ်မှတ်ချိန်တစ်ခုဆီ ပြန်သွားဖို့ကိုတော့ သင့်တော်တဲ့ base backup နဲ့ အဲဒီအချိန်ထိ လိုတဲ့ archived WAL တွေ လိုတယ်။
# Restore a custom-format backup into a separate test database.
createdb learning_restore
pg_restore --exit-on-error -d learning_restore learning_db.dump
# Verify the restored course data.
psql -d learning_restore -c "SELECT COUNT(*) FROM courses;"