Bird
Raised Fist0
PostgreSQLquery~10 mins

Logical replication basics in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Logical replication basics
Setup Publisher
Create Publication
Setup Subscriber
Create Subscription
Data Changes on Publisher
Changes Sent as WAL Logs
Subscriber Applies Changes
Data Synced on Subscriber
Logical replication starts by setting up a publisher and subscriber. Changes on the publisher are sent as logs and applied on the subscriber to keep data in sync.
Execution Sample
PostgreSQL
CREATE PUBLICATION mypub FOR TABLE users;
CREATE SUBSCRIPTION mysub CONNECTION 'host=pubhost dbname=mydb user=replicator password=secret' PUBLICATION mypub;
This code creates a publication on the publisher for the 'users' table and a subscription on the subscriber to receive changes.
Execution Table
StepActionDetailsResult
1Create PublicationCREATE PUBLICATION mypub FOR TABLE users;Publication 'mypub' created on publisher
2Create SubscriptionCREATE SUBSCRIPTION mysub CONNECTION 'host=pubhost dbname=mydb user=replicator password=secret' PUBLICATION mypub;Subscription 'mysub' created on subscriber
3Insert Data on PublisherINSERT INTO users VALUES (1, 'Alice');Change logged in WAL
4Send WAL LogsLogical replication sends changes from publisher to subscriberChange sent to subscriber
5Apply Changes on SubscriberSubscriber applies insert to 'users' tableData inserted on subscriber
6Verify DataSELECT * FROM users;Row (1, 'Alice') present on subscriber
7ExitReplication running continuouslyProcess ongoing, no exit
💡 Logical replication runs continuously to keep data synced; no termination unless stopped
Variable Tracker
VariableStartAfter Step 3After Step 5Final
users table on publisherempty1 row inserted1 row inserted1 row inserted
users table on subscriberemptyempty1 row inserted1 row inserted
WAL logsemptyinsert loggedsent and appliedup to date
Key Moments - 3 Insights
Why does the subscriber not have data before creating the subscription?
Because data is only sent after the subscription is created and connected, as shown in execution_table step 4 and 5.
Does logical replication copy the entire database automatically?
No, it replicates only the tables included in the publication, as seen in step 1 where only 'users' table is published.
What happens if data changes on the publisher after subscription creation?
Changes are continuously sent and applied on the subscriber, keeping data in sync as shown in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the 'users' table on the subscriber after step 3?
AEmpty
B1 row inserted
CSubscription created
DData deleted
💡 Hint
Check variable_tracker column 'After Step 3' for 'users table on subscriber'
At which step does the subscriber apply the data change?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at execution_table row describing 'Apply Changes on Subscriber'
If the publication included two tables, how would the replication change?
AOnly one table would replicate
BBoth tables' changes would replicate
CNo tables would replicate
DSubscriber would reject subscription
💡 Hint
Refer to key_moments about publication scope and step 1 in execution_table
Concept Snapshot
Logical replication basics in PostgreSQL:
- Publisher creates a publication for specific tables
- Subscriber creates a subscription to that publication
- Data changes on publisher are sent as WAL logs
- Subscriber applies changes to keep data synced
- Replication runs continuously until stopped
Full Transcript
Logical replication in PostgreSQL involves setting up a publisher and subscriber. The publisher creates a publication specifying which tables to replicate. The subscriber creates a subscription connecting to the publisher and subscribing to that publication. When data changes occur on the publisher, these changes are sent as write-ahead logs (WAL) to the subscriber. The subscriber applies these changes to its tables, keeping data synchronized. This process runs continuously, ensuring ongoing replication of data changes. The example shows creating a publication for the 'users' table, creating a subscription, inserting data on the publisher, and the data appearing on the subscriber after replication.

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

  1. Step 1: Understand logical replication concept

    Logical replication copies only data changes from selected tables, not the entire database.
  2. 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.
  3. Final Answer:

    To copy data changes from specific tables between databases -> Option A
  4. 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

  1. Step 1: Identify command for publication creation

    The command to create a publication is CREATE PUBLICATION followed by publication name and tables.
  2. 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.
  3. Final Answer:

    CREATE PUBLICATION mypub FOR ALL TABLES; -> Option C
  4. Quick Check:

    Publication creation = CREATE PUBLICATION [OK]
Hint: Publication uses CREATE PUBLICATION, subscription uses CREATE SUBSCRIPTION [OK]
Common Mistakes:
  • Mixing publication and subscription commands
  • Using replication slot command for publication
  • Confusing database creation with replication setup
3. Given the following commands on the publisher:
CREATE PUBLICATION mypub FOR TABLE customers;
And on the subscriber:
CREATE SUBSCRIPTION mysub CONNECTION 'host=source dbname=mydb user=replicator password=secret' PUBLICATION mypub;
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

  1. Step 1: Understand publication and subscription setup

    The publication includes the customers table, and the subscription connects to it, enabling replication of changes.
  2. 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.
  3. Final Answer:

    The new row will be replicated to the subscriber's customers table -> Option A
  4. 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

  1. Step 1: Check publication includes tables

    If the publication does not include the desired tables, no changes will be sent to the subscriber.
  2. 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.
  3. Final Answer:

    The publication does not include the tables you want to replicate -> Option D
  4. 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

  1. Step 1: Understand selective table replication

    To replicate only the orders table, the publication must include only that table.
  2. 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.
  3. Final Answer:

    CREATE PUBLICATION mypub FOR TABLE orders; -> Option B
  4. Quick Check:

    Include only needed tables in publication [OK]
Hint: List only needed tables in publication to exclude others [OK]
Common Mistakes:
  • Using FOR ALL TABLES when exclusion is needed
  • Including unwanted tables in publication
  • Confusing publication and subscription roles