PostgreSQL Performance & Query Optimization · บทเรียน

กลยุทธ์การจำลองข้อมูลแบบสตรีมและเชิงตรรกะ

ทำความเข้าใจและกำหนดค่าวิธีการจำลองข้อมูลรูปแบบต่าง ๆ เช่น การจำลองแบบสตรีมและเชิงตรรกะ เพื่อให้ระบบพร้อมใช้งานสูงและรองรับการอ่านที่เพิ่มขึ้น

บทเรียน 2 จาก 412 ขั้นตอน

กลยุทธ์การจำลองข้อมูลแบบสตรีมและเชิงตรรกะ เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Data Replication?

Imagine your database goes down! Replication is key to preventing data loss and ensuring your application stays online. It creates copies of your database.

It's vital for:

  • High Availability (HA): If the main server fails, a copy can take over.
  • Read Scaling: Distribute read queries across multiple servers, reducing load on the primary.

The Primary-Replica Setup

The most common replication setup is a Primary-Replica (or Master-Slave) model. One database server acts as the primary, handling all writes.

  • Primary: Processes all write operations (INSERT, UPDATE, DELETE).
  • Replica(s): Receive copies of data changes from the primary and can serve read-only queries.

This ensures data consistency and offloads read traffic.

Streaming Replication: The Basics

Streaming Replication is PostgreSQL's built-in, physical replication method. It works by continuously shipping the Write-Ahead Log (WAL) records from the primary to one or more replicas.

WAL records are low-level descriptions of every change made to the database. Replicas apply these changes to stay in sync.

WAL Shipping in Action

Here's how streaming replication works:

  • The primary server writes all database changes to its WAL files.
  • A dedicated process (WAL sender) on the primary sends these WAL records to replica servers.
  • A process (WAL receiver) on each replica receives and applies these records.

This keeps the replica's data identical to the primary's, block by block.

Setting Up Streaming Replication (Primary)

To enable streaming replication on the primary, you need to configure postgresql.conf:

  • wal_level = replica (or higher)
  • max_wal_senders = 10 (number of replicas/connections)
  • listen_addresses = '*' (allow connections from replicas)

You'll also need to set up pg_hba.conf to allow replica connections.

ALTER SYSTEM SET wal_level = 'replica';
ALTER SYSTEM SET max_wal_senders = 10;
ALTER SYSTEM SET listen_addresses = '*';

-- After these, restart PostgreSQL

Initializing a Replica with pg_basebackup

Before a replica can stream WAL, it needs a full copy of the primary's data. pg_basebackup is the standard tool for this.

It creates a consistent snapshot of the primary's data directory, which then becomes the replica's initial data.

pg_basebackup -h primary_host -p 5432 -U replication_user -D /var/lib/postgresql/data_replica -F p -Xs stream -P

Configuring the Replica

After pg_basebackup, the replica needs a few files:

  • A standby.signal file in its data directory (for PostgreSQL 12+).
  • A postgresql.conf with hot_standby = on (if you want to run queries).
  • A primary_conninfo setting in postgresql.conf to tell it where to connect to the primary.

This allows it to start up and connect as a replica.

ALTER SYSTEM SET hot_standby = 'on';
ALTER SYSTEM SET primary_conninfo = 'host=primary_host port=5432 user=replication_user password=your_password';

-- Create an empty standby.signal file:
touch /var/lib/postgresql/data_replica/standby.signal

-- Then start the replica PostgreSQL instance

Logical Replication: A Different Approach

Logical Replication is a newer, more flexible replication method introduced in PostgreSQL 10. Unlike streaming replication, it replicates data based on its logical representation (row changes, not physical blocks).

This allows for selective replication of tables or even databases, and cross-version/cross-platform replication.

Publications and Subscriptions

Logical replication uses a publish-subscribe model:

  • A Publication is created on the primary, defining which tables or changes to publish.
  • A Subscription is created on the replica, defining which publications to subscribe to.

The primary then sends logical decoding output (changes) to the subscribers, which apply them.

Setting Up Logical Replication (Example)

Here's a simplified example of setting up logical replication:

On Primary:

CREATE PUBLICATION my_pub FOR TABLE my_table;

On Replica:

CREATE SUBSCRIPTION my_sub CONNECTION 'host=primary_host dbname=mydb user=repl_user password=xyz' PUBLICATION my_pub;

Remember to configure wal_level = logical and max_replication_slots on the primary.

ALTER SYSTEM SET wal_level = 'logical';
ALTER SYSTEM SET max_replication_slots = 5;

-- On Primary:
CREATE PUBLICATION my_pub FOR TABLE my_table;

-- On Replica:
CREATE SUBSCRIPTION my_sub CONNECTION 'host=primary_host dbname=mydb user=repl_user password=xyz' PUBLICATION my_pub;

Replication Strategy Check

You need to set up replication for a PostgreSQL database. You want an exact, block-for-block copy of your entire database on a standby server for high availability. Which replication method is best suited for this goal?

Recap: Streaming vs. Logical

We've explored two key PostgreSQL replication strategies:

  • Streaming Replication: A physical, continuous copy of the entire database using WAL files. Great for high availability and read scaling with identical replicas.
  • Logical Replication: A flexible, publish-subscribe model replicating logical changes (rows). Ideal for selective replication, cross-version upgrades, or integrating with other systems.

Choosing the right strategy depends on your specific needs for consistency, flexibility, and performance.

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

เรียนรู้ SQL ด้วย AI tutor — ฟรี

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

คอร์ส
22
บทเรียน
88

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

บทเรียน “กลยุทธ์การจำลองข้อมูลแบบสตรีมและเชิงตรรกะ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การจำลองข้อมูลแบบสตรีมและเชิงตรรกะ”

ทำความเข้าใจและกำหนดค่าวิธีการจำลองข้อมูลรูปแบบต่าง ๆ เช่น การจำลองแบบสตรีมและเชิงตรรกะ เพื่อให้ระบบพร้อมใช้งานสูงและรองรับการอ่านที่เพิ่มขึ้น คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “กลยุทธ์การจำลองข้อมูลแบบสตรีมและเชิงตรรกะ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การจัดกลุ่มการเชื่อมต่อด้วย PgBouncer
  2. กลยุทธ์การจำลองข้อมูลแบบสตรีมและเชิงตรรกะ
  3. การแบ่งส่วนข้อมูลและ PostgreSQL แบบกระจาย
  4. การขยายการอ่านด้วย Hot Standby และการกระจายโหลด
← กลับไปที่ PostgreSQL Performance & Query Optimization