0Pricing
PostgreSQL Performance & Query Optimization · درس

ضبط الذاكرة (shared_buffers وwork_mem)

حسّنوا استخدام الذاكرة عبر تهيئة `shared_buffers` و`work_mem` والإعدادات الأخرى المرتبطة بالذاكرة بشكل صحيح.

ضبط الذاكرة (shared_buffers وwork_mem) درس مجاني في PostgreSQL Performance & Query Optimization على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في PostgreSQL Performance & Query Optimization، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة PostgreSQL Performance & Query Optimization 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Memory Matters for PostgreSQL

Optimizing how PostgreSQL uses memory is crucial for blazing-fast performance. Think of memory as your database's short-term workspace.

Proper memory configuration means less waiting for data from slow disks and quicker query processing.

Shared Buffers Explained

shared_buffers is one of the most important memory settings. It defines the amount of RAM PostgreSQL uses for caching data pages.

  • What it does: Stores frequently accessed data blocks from tables and indexes.
  • Why it matters: Reduces disk I/O, as PostgreSQL can serve data directly from RAM.

Configuring Shared Buffers

A common starting point for shared_buffers is 25% of your total system RAM. For dedicated database servers, you might go up to 40%.

  • Too low? Frequent disk reads.
  • Too high? Less RAM for OS/other processes, potentially causing swapping.

Remember to restart PostgreSQL after changing this setting in postgresql.conf.

work_mem for Sorts & Hashes

work_mem is memory allocated for individual query operations like sorting (ORDER BY, GROUP BY) and hash tables (Hash Joins).

Unlike shared_buffers, this memory is private to each operation and temporary. Multiple operations in a single query, or multiple concurrent queries, can each use their own work_mem.

Fine-tuning work_mem

If a query operation exceeds its allocated work_mem, it 'spills to disk,' using temporary files. This is much slower!

You can set work_mem globally, or even per-session or per-query for specific needs. A good starting point is 4-16MB, but monitor for spills using EXPLAIN ANALYZE.

Maintenance Memory (maintenance_work_mem)

maintenance_work_mem is dedicated memory for maintenance tasks, specifically:

  • VACUUM
  • CREATE INDEX
  • ALTER TABLE (adding columns, etc.)

These operations can be very memory-intensive. Setting this higher than work_mem is common, e.g., 64-512MB, to speed up maintenance.

effective_cache_size: The Planner's Guide

effective_cache_size is not memory allocated by PostgreSQL. Instead, it tells the query planner how much memory it expects to be available for caching data.

This includes shared_buffers AND the OS file system cache. A higher value encourages the planner to use index scans over sequential scans, assuming data is likely in memory.

Modifying Configuration

Most of these settings are found in your postgresql.conf file. You can also change them at runtime using ALTER SYSTEM or ALTER DATABASE/ALTER ROLE for finer control.

Remember to reload the configuration (or restart PostgreSQL for shared_buffers) after making changes to postgresql.conf.

Collective Impact on Performance

When tuned correctly, these memory parameters work together to:

  • Reduce Disk I/O: Data served from RAM is much faster.
  • Speed up Queries: Sorts, joins, and aggregations complete quicker.
  • Optimize Maintenance: VACUUM and index creation run faster.
  • Improve Planner Decisions: Better query plans lead to better execution.

Quick Check: Memory Roles

Which PostgreSQL memory parameter is primarily used by individual query operations like sorting and hashing, and can lead to "spilling to disk" if set too low?

Recap: Memory Tuning Essentials

We've explored key PostgreSQL memory parameters:

  • shared_buffers: Main data cache, ~25% RAM.
  • work_mem: Per-operation memory for sorts/hashes.
  • maintenance_work_mem: For VACUUM, CREATE INDEX.
  • effective_cache_size: A hint for the query planner.

Tuning these carefully can significantly boost your database's performance!

الأسئلة الشائعة

هل درس «ضبط الذاكرة (shared_buffers وwork_mem)» مجاني؟

نعم — نص درس «ضبط الذاكرة (shared_buffers وwork_mem)» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة PostgreSQL Performance & Query Optimization، انتقل إلى CoddyKit PRO. تتضمن دورة PostgreSQL Performance & Query Optimization 4 دروس في المجموع.

ماذا ستتعلم في «ضبط الذاكرة (shared_buffers وwork_mem)»؟

حسّنوا استخدام الذاكرة عبر تهيئة `shared_buffers` و`work_mem` والإعدادات الأخرى المرتبطة بالذاكرة بشكل صحيح. تتمرن على PostgreSQL Performance & Query Optimization مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ PostgreSQL Performance & Query Optimization؟

لا تُشترط خبرة سابقة. PostgreSQL Performance & Query Optimization على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «ضبط الذاكرة (shared_buffers وwork_mem)»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس PostgreSQL Performance & Query Optimization هذا؟

نعم. كل درس في PostgreSQL Performance & Query Optimization يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. المعاملات الرئيسية في postgresql.conf
  2. ضبط الذاكرة (shared_buffers وwork_mem)
  3. ضبط إدخال وإخراج القرص ونقاط التحقق
  4. ضبط ثوابت تكلفة مخطّط الاستعلامات
← العودة إلى PostgreSQL Performance & Query Optimization