Advanced PostgreSQL: Indexing, Partitioning, Replication · บทเรียน

การจัดการการสมัครรับข้อมูล

ตั้งค่าและจัดการการสมัครรับข้อมูลบนเซิร์ฟเวอร์สำรอง เพื่อรับและนำการเปลี่ยนแปลงจากพับลิเคชันไปใช้

บทเรียน 3 จาก 411 ขั้นตอน

การจัดการการสมัครรับข้อมูล เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What is a Logical Subscription?

In PostgreSQL's logical replication, a subscription is the receiving end. It's configured on a subscriber server to connect to a publisher server and pull data changes from a specific publication.

Think of it as subscribing to a newspaper. The publisher creates the newspaper (publication), and you (the subscriber) sign up to receive it.

The Subscriber Server Role

The server where you create a subscription becomes the subscriber. This server will connect to the publisher, receive the changes (inserts, updates, deletes, truncates), and apply them to its local tables.

It's crucial that the tables on the subscriber have the same structure as the published tables on the master for replication to work correctly.

Creating a New Subscription

To start receiving data, you use the CREATE SUBSCRIPTION command. You need to specify the connection details to the publisher and the name of the publication you want to subscribe to.

This command is run on the subscriber database.

CREATE SUBSCRIPTION my_app_sub
CONNECTION 'dbname=source_db host=192.168.0.10 user=repl_user password=securepass'
PUBLICATION app_data_pub;

Essential Subscription Options

When creating a subscription, several options control its behavior:

  • COPY_DATA: If true (default), existing data from the published tables is copied.
  • ENABLED: If true (default), replication starts immediately.
  • CREATE_SLOT: If true (default), a replication slot is created on the publisher.
  • SLOT_NAME: You can specify an existing slot name instead of creating a new one.

Full Subscription Example

Let's create a subscription named sales_replica. It connects to a publisher at 10.0.0.5, user repl_admin, for the publication sales_publication.

We explicitly ask to copy existing data and enable it right away.

-- Execute this on the subscriber database
CREATE SUBSCRIPTION sales_replica
CONNECTION 'dbname=sales_db host=10.0.0.5 user=repl_admin password=mysecret'
PUBLICATION sales_publication
WITH (copy_data = true, create_slot = true, enabled = true);

Monitoring Subscription Status

After creating a subscription, you'll want to ensure it's running correctly. PostgreSQL provides the pg_stat_subscription view for this.

It shows details like connection info, enabled status, and the current state of replication.

-- On the subscriber server
SELECT
  subname,
  subenabled,
  substate,
  subslotname,
  subconninfo
FROM pg_stat_subscription;

Enabling and Disabling Subscriptions

You might need to temporarily pause replication, for example, during maintenance or schema changes. You can enable or disable a subscription using ALTER SUBSCRIPTION.

  • DISABLE: Stops the replication process.
  • ENABLE: Resumes the replication process.
-- Temporarily stop replication for 'sales_replica'
ALTER SUBSCRIPTION sales_replica DISABLE;

-- Resume replication
ALTER SUBSCRIPTION sales_replica ENABLE;

Refreshing Publication List

If the publisher adds new tables to a publication, the subscriber won't automatically start replicating them. You need to tell the subscriber to refresh its understanding of the publication.

The REFRESH PUBLICATION command updates the subscription's table list.

-- Update 'sales_replica' to include any newly added tables
ALTER SUBSCRIPTION sales_replica REFRESH PUBLICATION;

Dropping a Subscription

When a subscription is no longer needed, you can drop it. This command is executed on the subscriber server. By default, it also attempts to drop the associated replication slot on the publisher.

Ensure the subscription is disabled before dropping it to prevent errors.

-- First, disable the subscription if it's enabled
ALTER SUBSCRIPTION sales_replica DISABLE;

-- Then, drop the subscription
DROP SUBSCRIPTION sales_replica;

Subscription Management Check

You have a subscription named user_data_sub that is actively replicating data. You need to perform some schema changes on the subscriber and want to temporarily stop the data flow without removing the subscription entirely.

Recap: Managing Subscriptions

In this lesson, you learned how to manage subscriptions in PostgreSQL logical replication. You can create new subscriptions, monitor their status, enable/disable them for maintenance, refresh their publication list, and finally drop them when no longer needed.

Mastering these commands is key to maintaining a robust and flexible logical replication setup.

เริ่มต้นได้ฟรี

เรียนรู้ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
11
บทเรียน
44

คำถามที่พบบ่อย

บทเรียน “การจัดการการสมัครรับข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการการสมัครรับข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการการสมัครรับข้อมูล”

ตั้งค่าและจัดการการสมัครรับข้อมูลบนเซิร์ฟเวอร์สำรอง เพื่อรับและนำการเปลี่ยนแปลงจากพับลิเคชันไปใช้ คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการการสมัครรับข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม

ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานการจำลองข้อมูลเชิงตรรกะ
  2. การกำหนดค่าพับลิเคชัน
  3. การจัดการการสมัครรับข้อมูล
  4. การแก้ไขข้อขัดแย้งในการจำลองแบบเชิงตรรกะ
← กลับไปที่ Advanced PostgreSQL: Indexing, Partitioning, Replication