0
0
PostgreSQLquery~10 mins

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

Choose your learning style9 modes available
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.