0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · 课时

监控复制健康状况

学习有效监控复制配置的状态和健康状况,主动识别并解决问题。

监控复制健康状况 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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!

常见问题解答

「监控复制健康状况」课时是免费的吗?

是的 — 「监控复制健康状况」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

「监控复制健康状况」这节课中我会学到什么?

学习有效监控复制配置的状态和健康状况,主动识别并解决问题。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「监控复制健康状况」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?

能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 自动故障转移工具(Patroni)
  2. 监控复制健康状况
  3. 灾难恢复策略
  4. 使用 PgBouncer 和 HAProxy 路由连接
← 返回 Advanced PostgreSQL: Indexing, Partitioning, Replication