Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Logical Replication Basics in PostgreSQL
📖 Scenario: You are setting up a simple logical replication system between two PostgreSQL databases to keep data synchronized for a small business. This will help the business maintain a live copy of important data on a secondary server for reporting and backup.
🎯 Goal: Build a basic logical replication setup by creating a publication on the primary database and a subscription on the secondary database to replicate a specific table.
📋 What You'll Learn
Create a table named products with columns id (integer primary key) and name (text).
Create a publication named product_pub that publishes the products table.
Create a subscription named product_sub on the subscriber database that connects to the publisher and subscribes to product_pub.
Ensure the subscription replicates data from the products table.
💡 Why This Matters
🌍 Real World
Logical replication is used to keep data synchronized between databases for reporting, backup, or scaling read workloads.
💼 Career
Database administrators and backend engineers often set up and maintain logical replication to ensure data availability and consistency across systems.
Progress0 / 4 steps
1
Create the products table on the primary database
Write a SQL statement to create a table called products with two columns: id as an integer primary key and name as text.
PostgreSQL
Hint
Use CREATE TABLE with the column definitions exactly as specified.
2
Create a publication named product_pub for the products table
Write a SQL statement to create a publication called product_pub that publishes the products table.
PostgreSQL
Hint
Use CREATE PUBLICATION with the exact publication name and specify the table.
3
Create a subscription named product_sub on the subscriber database
Write a SQL statement to create a subscription called product_sub that connects to the primary database at host=primary_host port=5432 dbname=primary_db user=replicator password=secret and subscribes to the publication product_pub.
PostgreSQL
Hint
Use CREATE SUBSCRIPTION with the exact subscription name, connection string, and publication name.
4
Verify the subscription replicates the products table
Add a SQL query to check the status of the subscription product_sub in the pg_stat_subscription view to confirm it is active and replicating.
PostgreSQL
Hint
Use a SELECT query on pg_stat_subscription filtering by the subscription name.
Practice
(1/5)
1. What is the main purpose of logical replication in PostgreSQL?
easy
A. To copy data changes from specific tables between databases
B. To create a full backup of the database
C. To optimize query performance
D. To encrypt data during transfer
Solution
Step 1: Understand logical replication concept
Logical replication copies only data changes from selected tables, not the entire database.
Step 2: Compare options with concept
Encrypting data during transfer, creating a full backup of the database, and optimizing query performance are unrelated to logical replication.
Final Answer:
To copy data changes from specific tables between databases -> Option A
Quick Check:
Logical replication = Copy data changes [OK]
Hint: Logical replication copies changes, not full backups [OK]
Common Mistakes:
Confusing logical replication with physical backup
Thinking it copies entire database
Assuming it improves query speed
2. Which SQL command is used to create a publication for logical replication?
easy
A. CREATE REPLICATION SLOT myslot LOGICAL;
B. CREATE SUBSCRIPTION mysub CONNECTION 'conninfo' PUBLICATION mypub;
C. CREATE PUBLICATION mypub FOR ALL TABLES;
D. CREATE DATABASE mydb WITH REPLICATION;
Solution
Step 1: Identify command for publication creation
The command to create a publication is CREATE PUBLICATION followed by publication name and tables.
Step 2: Analyze options
Only CREATE PUBLICATION mypub FOR ALL TABLES; creates a publication. CREATE SUBSCRIPTION mysub CONNECTION 'conninfo' PUBLICATION mypub; creates a subscription, CREATE REPLICATION SLOT myslot LOGICAL; creates a replication slot, and CREATE DATABASE mydb WITH REPLICATION; creates a database.
Final Answer:
CREATE PUBLICATION mypub FOR ALL TABLES; -> Option C
What will happen when a new row is inserted into the customers table on the publisher?
medium
A. The new row will be replicated to the subscriber's customers table
B. The new row will not be replicated because subscription is missing
C. The entire customers table will be copied again
D. An error will occur because publications cannot replicate inserts
Solution
Step 1: Understand publication and subscription setup
The publication includes the customers table, and the subscription connects to it, enabling replication of changes.
Step 2: Analyze effect of insert on replication
Inserts on the published table are sent to the subscriber, so the new row will appear there.
Final Answer:
The new row will be replicated to the subscriber's customers table -> Option A
Quick Check:
Insert on published table = replicated row [OK]
Hint: Inserts on published tables replicate if subscription exists [OK]
Common Mistakes:
Thinking subscription is missing when it is created
Assuming full table copy on each insert
Believing inserts are not replicated
4. You created a subscription but notice no data is replicating. Which of the following is a likely cause?
medium
A. Logical replication does not support inserts
B. The subscriber database is offline
C. You forgot to create a replication slot on the subscriber
D. The publication does not include the tables you want to replicate
Solution
Step 1: Check publication includes tables
If the publication does not include the desired tables, no changes will be sent to the subscriber.
Step 2: Evaluate other options
The subscriber database is offline is unlikely because subscription requires the subscriber to be online. Logical replication does not support inserts is false; inserts are supported. You forgot to create a replication slot on the subscriber is incorrect because replication slots are created on the publisher, not subscriber.
Final Answer:
The publication does not include the tables you want to replicate -> Option D
Quick Check:
Missing tables in publication = no replication [OK]
Hint: Ensure publication includes tables to replicate [OK]
Common Mistakes:
Confusing replication slot location
Assuming inserts are unsupported
Ignoring publication table list
5. You want to replicate only changes from the orders table but exclude the order_logs table, which is large and not needed on the subscriber. How should you set up the publication?
hard
A. CREATE PUBLICATION mypub FOR TABLE orders, order_logs;
B. CREATE PUBLICATION mypub FOR TABLE orders;
C. CREATE PUBLICATION mypub FOR ALL TABLES;
D. CREATE PUBLICATION mypub FOR TABLE order_logs;
Solution
Step 1: Understand selective table replication
To replicate only the orders table, the publication must include only that table.
Step 2: Analyze options for correct table inclusion
CREATE PUBLICATION mypub FOR ALL TABLES; replicates all tables, including order_logs which is unwanted. CREATE PUBLICATION mypub FOR TABLE orders, order_logs; includes both tables. CREATE PUBLICATION mypub FOR TABLE order_logs; includes only order_logs, which is unwanted.
Final Answer:
CREATE PUBLICATION mypub FOR TABLE orders; -> Option B
Quick Check:
Include only needed tables in publication [OK]
Hint: List only needed tables in publication to exclude others [OK]