Skip to content
Database

数据库复制

An introduction to database replication and change data capture.

复制可以让数据与另一个位置保持同步。像 Supabase Pipelines 这样的逻辑复制产品使用变更数据捕获(CDC)来读取数据库的更改,并将其应用到目标位置。

🌐 Replication keeps data synchronized with another location. Logical replication products such as Supabase Pipelines use change data capture (CDC) to read database changes and apply them to a destination.

复制方法 #

🌐 Replication methods

Supabase 支持三种复制方式。你可以根据自己是否需要另一个 Supabase Postgres 数据库、一个到目标系统的托管复制管道,或者对自己的逻辑复制设置有完全控制来选择。

🌐 Supabase supports three replication methods. Choose based on whether you need another Supabase Postgres database, a managed replication pipeline to a destination system, or full control over your own logical replication setup.

阅读副本 #

🌐 Read replicas

只读副本是额外的 Supabase Postgres 数据库,与主数据库保持同步。当你想要只读查询能力、在其他地区降低延迟,或者在仍然使用 Supabase Postgres 的情况下,将分析读取与应用写入隔离开时,可以使用它们。

🌐 Read replicas are additional Supabase Postgres databases kept in sync with your primary database. Use them when you want read-only query capacity, lower latency in another region, or to isolate analytical reads from application writes while staying inside Supabase Postgres.

查看 设置只读副本。

🌐 See Set up read replicas.

管道 #

🌐 Pipelines

Supabase Pipelines 是一个托管的 CDC 产品,用于将数据从 Supabase Postgres 移动到支持的目标系统。它使用 Postgres 逻辑复制,并结合开源的 Supabase ETL 引擎。目标是存储你的复制数据的地方;管道会先复制为初始同步选择的表的现有行,然后使用持续复制(CDC)将后续数据库更改发送到该目标。

🌐 Supabase Pipelines is a managed CDC product for moving data from Supabase Postgres to supported destination systems. It uses Postgres logical replication with the open-source Supabase ETL engine. A destination is where your replicated data is stored; a pipeline copies existing rows for the tables selected for initial sync, then uses ongoing replication (CDC) to send subsequent database changes to that destination.

查看 设置管道。

🌐 See Set up Pipelines.

手动复制 #

🌐 Manual replication

手动复制使用与管道相同的底层 Postgres 逻辑复制功能,但你需要自己配置和操作各个部分。当你想要连接像 Airbyte、Estuary、Fivetran、Materialize、Stitch、AWS DMS 或其他支持 Postgres 逻辑复制的系统时,可以使用这种方式。

🌐 Manual replication uses the same underlying Postgres logical replication features as Pipelines, but you configure and operate the pieces yourself. Use this path when you want to connect tools such as Airbyte, Estuary, Fivetran, Materialize, Stitch, AWS DMS, or another system that supports Postgres logical replication.

查看 设置手动复制。

🌐 See Set up manual replication.

支持的目的地 #

🌐 Supported destinations

目标状态
BigQuery公开测试版
ClickHouse公开测试版
DuckLake公开测试版
Snowflake公开测试版

用例 #

🌐 Use cases

你可能会用数据库复制来做:

🌐 You might use database replication for:

  • 分析与数据仓库:在单独的平台上对复制的数据运行分析查询。初次同步和持续复制仍然会使用源数据库的资源。
  • 数据整合:让你的数据在技术栈的不同系统和服务之间保持同步。
  • 操作报告:保留一份所选应用数据的副本,以便你可以在另一个系统中查询。

🌐 Related features

有关实时功能以及将数据同步到浏览器和移动应用,请参见 Realtime。

🌐 For realtime features and syncing data to browsers and mobile apps, see Realtime.

实时功能也使用 Postgres 变化,但它是用于将数据库更新广播给客户端,而不是在另一个系统中维护数据库的副本。

🌐 Realtime also uses Postgres changes, but it is intended for broadcasting database updates to clients rather than maintaining a copy of your database in another system.

概念和术语 #

🌐 Concepts and terms

预写日志 (WAL) #

🌐 Write-Ahead Log (WAL)

Postgres 会在将更改写入数据文件之前,先把数据库的变化记录到先写日志(WAL)中。WAL 会存储在叫做段的文件里。检查点会把修改过的数据页写入磁盘,这样在发生崩溃时就能从最近的位置开始恢复。一旦不再需要用于恢复、归档或复制,旧的 WAL 段就可以回收或删除。

🌐 Postgres records database changes in the Write-Ahead Log (WAL) before writing them to data files. WAL is stored in files called segments. A checkpoint writes modified data pages to disk so crash recovery can start from a recent position. Older WAL segments can be recycled or removed once they are no longer needed for recovery, archiving, or replication.

逻辑复制和WAL #

🌐 Logical replication and WAL

逻辑复制是一种复制方法,Postgres 使用 WAL 文件将更改传输到另一个 Postgres 数据库,或者传输到支持读取 WAL 文件的系统。

🌐 Logical replication is a method of replication where Postgres uses WAL files to transmit changes to another Postgres database, or to a system that supports reading WAL files.

LSN#

LSN 是日志序列号,用来标识 WAL 中的位置。它通常用来确定订阅者的复制进度,以及计算复制槽的延迟。

🌐 LSN is a Log Sequence Number that identifies a position in the WAL. It is often used to determine the progress of replication in subscribers and calculate the lag of a replication slot.

逻辑复制架构 #

🌐 Logical replication architecture

逻辑复制使用这些组件:

🌐 Logical replication uses these components:

  • 发布:定义要发布的源表和变更类型,可选择列列表和行过滤器。
  • 复制槽:跟踪消费者的进度并保留它仍然需要的 WAL。槽使用输出插件来解码更改;它不绑定到单个发布。
  • 消费者:读取解码后的更改并将其应用到目标。另一个 Postgres 数据库可以使用订阅来管理此连接及其发布。Pipelines 直接连接到复制流,而无需创建 Postgres 订阅。

逻辑复制输出格式 #

🌐 Logical replication output format

逻辑复制通常有两种输出形式,pgoutput 和 wal2json。输出方式就是 Postgres 向任何活动的复制槽发送更改的方式。

🌐 Logical replication is typically output in two forms, pgoutput and wal2json. The output method is how Postgres sends changes to any active replication slot.

逻辑复制配置 #

🌐 Logical replication configuration

逻辑复制槽会保留 WAL,直到它的消费者确认不再需要它。如果 Postgres 在消费者赶上之前移除了必要的 WAL,那么这个槽可能会变得无法使用,消费者可能需要一个新的初始副本。

🌐 Logical replication slots retain WAL until their consumers confirm that they no longer need it. If Postgres removes required WAL before a consumer catches up, the slot can become unusable and the consumer may need a new initial copy.

Postgres 设置控制复制容量和 WAL 保留时间。更高的保留限制可以降低丢失所需 WAL 的风险,但可能会占用更多的数据库存储空间。把这些设置当作高级配置,在修改之前先检查可用磁盘空间。在 Supabase 上,可以通过 Supabase CLI 配置支持的设置。

🌐 Postgres settings control replication capacity and WAL retention. Higher retention limits reduce the risk of losing required WAL, but can consume more database storage. Treat these settings as advanced configuration and check available disk before changing them. On Supabase, configure the supported settings with the Supabase CLI.

设置描述Supabase 配置
max_replication_slots最大复制槽数量。管道需要一个主槽,并且每个活动的表同步工作线程可以临时使用一个额外的槽。仅限命令行
wal_keep_size为备用服务器保留的最少旧 WAL 数量。此设置与每个槽的保留上限分开。仅命令行
max_slot_wal_keep_size复制槽在检查点时间可以保留的最大 WAL。-1 允许槽保留无限的 WAL。当其消费者落后太多时,有限的限制可能会使槽失效。仅限 CLI
checkpoint_timeout自动 WAL 检查点之间的最长时间。槽 WAL 限制在检查点时执行。仅限 CLI