0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · درس

مراقبة سلامة النسخ المتماثل

تعلّم مراقبة حالة إعداد النسخ المتماثل وسلامته بفاعلية، لتحديد المشكلات وحلها استباقيًا.

مراقبة سلامة النسخ المتماثل درس مجاني في Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Advanced PostgreSQL: Indexing, Partitioning, Replication، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.

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

Why Monitor Replication?

PostgreSQL replication is key for high availability and read scalability. But how do you know if it's working correctly?

Monitoring replication health is crucial to ensure your standby servers are up-to-date and ready to take over if the primary fails. Without it, you might discover issues only when it's too late!

Key Replication Metrics

When monitoring replication, you'll want to keep an eye on a few key metrics:

  • Replication Status: Is the standby connected and actively receiving data?
  • Replication Lag: How far behind is the standby compared to the primary?
  • WAL Activity: Are Write-Ahead Log (WAL) files being transferred and applied correctly?

The `pg_stat_replication` View

PostgreSQL provides a powerful system view called pg_stat_replication. This view offers detailed information about each active standby server connected to your primary.

It's your go-to source for real-time replication status and progress.

`pg_stat_replication` Live

Let's see what information pg_stat_replication provides. Run this query on your primary server:

SELECT
  client_addr,
  state,
  sync_state,
  sync_priority
FROM pg_stat_replication;

Understanding `state` & `sync_state`

The state and sync_state columns are vital:

  • state: Shows the standby's current activity (e.g., streaming, catching up, backup). streaming means it's actively receiving WAL records.
  • sync_state: Indicates if the standby is async (asynchronous) or sync (synchronous). Synchronous standbys guarantee data durability across multiple servers.

Understanding Replication Lag

Replication lag is the delay between when a transaction commits on the primary and when it's applied on the standby.

High lag means your standby isn't up-to-date. In a failover scenario, this could lead to data loss. We want lag to be as low as possible, ideally zero.

Calculating Replication Lag

You can calculate replication lag in bytes using pg_wal_lsn_diff(). This function measures the difference between two Log Sequence Numbers (LSNs).

A common way to see lag is by comparing the primary's current LSN to the standby's replay_lsn from pg_stat_replication.

SELECT
  client_addr,
  pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS lag_bytes
FROM pg_stat_replication;

Standby Recovery Status

What about checking the standby server itself? You can use the pg_is_in_recovery() function to confirm if a server is currently acting as a standby and applying WAL records.

It returns true if the server is in recovery mode (i.e., a standby) and false if it's a primary.

SELECT pg_is_in_recovery();

WAL Progress & LSNs

Beyond simple lag, you can track the progress of WAL application more granularly. The pg_wal_lsn_diff() function is also useful for comparing any two LSNs.

For example, you could compare write_lsn, flush_lsn, and replay_lsn from pg_stat_replication to pinpoint where delays are occurring (writing to disk, flushing, or replaying).

Quick Check: Replication Status

You run SELECT client_addr, state, sync_state FROM pg_stat_replication; on your primary and see the following output for one standby:

client_addr | state    | sync_state
------------+----------+-----------
192.168.1.5 | streaming | async

Which of the following statements are true about this standby?

Recap & Next Steps

You've learned how to monitor PostgreSQL replication health! We explored:

  • Why monitoring lag and status is critical.
  • Using the pg_stat_replication view on the primary.
  • Interpreting state and sync_state.
  • Calculating replication lag with pg_wal_lsn_diff().
  • Checking standby recovery status with pg_is_in_recovery().

Regularly checking these metrics helps ensure your high availability setup is robust and reliable!

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

هل درس «مراقبة سلامة النسخ المتماثل» مجاني؟

نعم — نص درس «مراقبة سلامة النسخ المتماثل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Advanced PostgreSQL: Indexing, Partitioning, Replication، انتقل إلى CoddyKit PRO. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.

ماذا ستتعلم في «مراقبة سلامة النسخ المتماثل»؟

تعلّم مراقبة حالة إعداد النسخ المتماثل وسلامته بفاعلية، لتحديد المشكلات وحلها استباقيًا. تتمرن على Advanced PostgreSQL: Indexing, Partitioning, Replication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Advanced PostgreSQL: Indexing, Partitioning, Replication؟

لا تُشترط خبرة سابقة. Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «مراقبة سلامة النسخ المتماثل»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Advanced PostgreSQL: Indexing, Partitioning, Replication هذا؟

نعم. كل درس في Advanced PostgreSQL: Indexing, Partitioning, Replication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. أدوات التحويل التلقائي عند التعطل (Patroni)
  2. مراقبة سلامة النسخ المتماثل
  3. استراتيجيات التعافي من الكوارث
  4. توجيه الاتصالات باستخدام PgBouncer وHAProxy
← العودة إلى Advanced PostgreSQL: Indexing, Partitioning, Replication