SlideShare a Scribd company logo
1 of 46
Download to read offline
Logical Replication
Internals
Noriyoshi Shinoda
December 6, 2017
PGConf.ASIA 2017
Speaker
Noriyoshi Shinoda
– Company
– Hewlett-Packard Japan, Ltd
– Now
– System design, tuning, consulting on RDBMS such as PostgreSQL, Oracle Database,
Microsoft SQL Server, Vertica, Sybase ASE etc
– Oracle ACE
– Writing 15 books for Oracle Database
– Investigation and verification of open source products
– URL
– Slideshare
– https://www.slideshare.net/noriyoshishinoda/
– Oracle ACE
– http://www.oracle.com/technetwork/jp/database/articles/vivadeveloper/index-1838335-ja.html
2
Agenda
– What is Logical Replication?
– Let’s try!
– Architecture
– Restrictions
– Trouble shooting
3
What is Logical Replication?
4
What is Logical Replication?
What is Logical Replication?
5
– Is
– PostgreSQL 10 new features
– Replicate per table
– Replicate per transaction
– Updatable replication destination tables
– Guarantee that the results of the SQL statement will be identical (= Logical)
– Adopt Publish / Subscribe model
– ≒ Slony-I
– Is Not
– Re-execute SQL
– Physical page format match
What is Logical Replication?
Replication condition
6
– Conditions for replicable tables
– Identical schema name
– Identical table name
– Identical column name
– Identical data type
– Different data types are available if implicit type conversion is possible
Let’s try it!
7
Let’s try it!
Publisher instance
8
# TYPE DATABASE USER ADDRESS METHOD
host pubdb repusr1 192.168.1.100/32 md5
- Modify pg_hba.conf file
pubdb=# CREATE ROLE repusr1 PASSWORD 'Passw0rd' LOGIN REPLICATION ;
CREATE ROLE
- Replicate data1 table of pubdb database to subdb database
- Create role with REPLICATION attribute / LOGIN attribute
- Role connected from Subscriber instance
pubdb=# GRANT CREATE ON DATABASE pubdb TO pubusr1 ;
GRANT
- Grant database CREATE privilege to table owner (pubusr1)
- 'DATABASE = replication' item not required
Let’s try it!
9
- Modify postgresql.conf file
wal_level = logical
Publisher instance
Let’s try it!
10
– Create table for replication
pubdb=> CREATE TABLE data1 (c1 INT PRIMARY KEY, c2 VARCHAR(5)) ;
CREATE TABLE
pubdb=> CREATE PUBLICATION pub1 FOR TABLE data1 ;
CREATE PUBLICATION
– Create the PUBLICATION object
– Grant SELECT permission to the table for connected user
pubdb=> GRANT SELECT ON data1 TO repusr1 ;
GRANT
Publisher database
Let’s try it!
Subscriber database
11
– Create table for replication
subdb=> CREATE TABLE data1 (c1 INT PRIMARY KEY, c2 VARCHAR(5)) ;
CREATE TABLE
subdb=# CREATE SUBSCRIPTION sub1 CONNECTION
'host=pubhost1 dbname=pubdb user=repusr1 password=Passw0rd'
PUBLICATION pub1 ;
CREATE SUBSCRIPTION
– Create the SUBSCRIPTION object (require SUPERUSER)
Let’s try it!
Confirmation
12
– Publisher instance
pubdb=# SELECT application_name, state, sync_state FROM
pg_stat_replication ;
application_name | state | sync_state
------------------+-----------+------------
sub1 | streaming | async
(1 row)
subdb=> SELECT subname, received_lsn FROM pg_stat_subscription ;
subname | received_lsn
---------+--------------
sub1 | 0/1C8FF738
– Subscriber instance
Architecture
13
Subscriber InstancePublisher Instance
Architecture
Components
14
Publisher Database Subscriber Database
PUBLICATION SUBSCRIPTION
Tables
logical
replication
worker
Tables
Tables
Tables
WAL decode message
connect
wal
sender
pgoutput plugin
logical
replication
launcher
Logical Replication Slot
Architecture
Processes
15
– wal sender process {user} {client ip} {state}
– On PUBLICATION instance
– Send decoded WAL messages
– Launch for each connection from SUBSCRIPTION
– bgworker: logical replication launcher
– On PUBLICATION / SUBSCRIPTION instance
– Start the logical replication worker process
– bgworker: logical replication worker for subscription {oid}
– On SUBSCRIPTION instance
– Connect to wal sender process
– Receive decoded WAL message and update the table
– Launch for each SUBSCRIPTION
Architecture
Behavior when PUBLICATION is created
16
– Store information in the pg_publication catalog
– PUBLICATION name
– Which DML to replicate (INSERT / UPDATE / DELETE)
– Whether to cover all tables (FOR ALL TABLES clause)
– Store information in the pg_publication_rel catalog
– Table OID and PUBLICATION OID
– If FOR ALL TABLES clause is specified, nothing is stored in this catalog
– Table name can be referenced from pg_publication_tables catalog
Architecture
Behavior when SUBSCRIPTION is created
17
– Store information in pg_subscription catalog
– Connected instance information
– Replication Slot name
– Connected PUBLICATION name
– Synchronous / Asynchronous replication attribute
– Connect to PUBLICATION instance
– Check REPLICATION attribute of connected user
– Create Logical Replication Slot (default name is SUBSCRIPTION name)
– Do not check whether PUBLICATION exists
– Register replication target tables in pg_subscription_rel catalog
– Synchronize initial snapshot
– Executed asynchronously
– Existing data on SUBSCRIPTION insntance will not be deleted
Architecture
Synchronize initial snapshot
18
Subscriber InstancePublisher Instance
Backend process
wal sender process
wal sender process
2. Create Replication Slot
3. Create Temporary Replication Slot
{sub name}_{sub oid}_sync_{rel oid}
4. COPY TO STDOUT statement
logical replication worker
logical replication worker
Backend process
1.CREATE SUBSCRIPTION
Decoded Initial Data
Decoded Transaction Data
Architecture
Replication Slot
19
– Logical Replication Slot
– One-to-one with SUBSCRIPTION
– Manage sent WAL
– Provide replication plugin
– Automatic execution of the following SQL statement when creating SUBSCRIPTION
pg_create_logical_replication_slot ( name, 'pgoutput' )
– Replication Slot name
– SUBSCRIPTION name by default
– Can change CREATE SUBSCRIPTION WITH (slot_name=name) statement
Architecture
pgoutput plugin and message
20
– pgoutput plugin
– Default plugin for Logical Replication
– Used by wal sender process
– Create Logical Replication messages from WAL
– Convert character encoding
– Convert text message from binary data
– Does not support text output by pg_logical_slot_get_changes function?
– Message
– All text data
– Protocol
– https://www.postgresql.org/docs/10/static/protocol-logicalrep-message-formats.html
– When executed UPDATE / DELET statement
– Send data specifying the update target column and updated data
Architecture
Replica Identity
21
Table attribute PUBLICATION SUBSCRIPTION
PRIMARY KEY Yes Yes
REPLICA IDENTITY FULL Yes No
REPLICA IDENTITY USING INDEX
+ UNIQUE index + NOT NULL
Yes Yes
– UPDATE / DELETE for a table without a primary key or REPLICA IDENTITY
setting is an SQL execution error
pubdb=> UPDATE data1 SET c2='update' WHERE c1=100 ;
ERROR: cannot update table "data1" because it does not have replica
identity and publishes updates
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER
TABLE.
– The condition under which the UPDATE statement / DELETE statement is
replicated
Architecture
Behavior during DML execution (asynchronous replication)
22
① COMMIT ③ SIGUSR1backend
②WAL
④ COMMIT
⑥ To Subscription
pgoutput
wal
sender
Architecture
Behavior during DML execution (synchronous replication)
23
① COMMIT ③ SIGUSR1
backend
②WAL
⑥ SIGUSR1⑦ COMMIT
⑤ To Subscription
pgoutput
wal
sender
Architecture
Storage
24
– Logical log snapshot
– Created when wal sender process receives SIGUSR1 signal from wal writer process
– Read the WAL file and create snapshot
– File path
${PGDATA}/pg_logical/snapshots/{LSN upper}-{LSN lower}.snap
${PGDATA}/pg_replslot/{SLOT_NAME}/xid-{XID}-lsn-{LSN upper}-{LSN
lower}.snap
– Logical log snapshot when SUBSCRIPTION stop
– Created if the SUBSCRIPTION instance is stopped and the wal sender process is
restarted
– It is deleted when the transfer is completed
– File path
Restrictions
25
Restrictions
SQL statement that can not be replicated
26
– TRUNCATE statement
– ALTER TABLE statement
– CREATE TABLE statement
– When executing the CREATE REPLICATION FOR ALL TABLES statement
Restrictions
Objects that can not be replicated
27
– Only the table can be added to PUBLICATION
– pg_class.relkind = 'r'
– Objects that can not be replicated
– MATERIALIZED VIEW
– INDEX
– SEQUENCE
– FOREIGN TABLE
– UNLOGGED TABLE
– INHERIT TABLE = OK (ONLY clause)
– Partition parent table
– Large Object
Restrictions
SERIAL column / GENERATED AS IDENTITY column
28
– Internally use SEQUENCE for SERIAL and GENERATED AS IDENTITY
columns
– The sequence value is transferred to SUBSCRIPTION but no sequence
operation is executed
Restrictions
SERIAL column / GENERATED AS IDENTITY column
29
PUBLICATION
CREATE TABLE t(a SERIAL, b CHAR)
INSERT INTO t(b) VALUES ('A')
SUBSCRIPTION
CREATE TABLE t(a SERIAL, b CHAR)
INSERT INTO t(b) VALUES ('B')
(1, 'A') (1, 'A')
(2, 'B')
INSERT INTO t(b) VALUES ('C')
(2, 'B')
(1, 'C')
Restrictions
Trigger
30
– Partial execution
– Only ROW TRIGGER execute
– UPDATE OF trigger is not executed
– STATEMENT TRIGGER is executed only at initial data transfer
– SUBSCRIPTION database
– Require ALTER TABLE ENABLE ALWAYS or REPLICA TRIGGER statement
– Bug on10.0 : BEFORE ROW DELETE trigger not issued
– https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=360fd1a7b2fe779cc9e696b813
b12f6a8e83b558
– Fixed on10.1
Restrictions
Parameter log_statement
31
– Even if log_statement = 'all' set, the SQL statement by replication does not be
logged
Restrictions
Bidirectional replication
32
– Bidirectional replication can not be performed
– It can be configured, however WAL circulates
– Mutual replication between databases is possible if tables are different
Database#1 Database#2
SUBSCRIPTION#1
table_a table_a
PUBLICATION#1
SUBSCRIPTION#2 PUBLICATION#2
WAL circulates
Restrictions
Replication within instance
33
subdb=# CREATE SUBSCRIPTION sub1 CONNECTION 'dbname=pubdb' WITH
(CONNECT=off) ;
– Be careful to create the replication within instance
– The CREATE SUBSCRIPTION statement hangs when trying to simply configure it
– It is necessary to create SUBSCRIPTION and Replication Slot indivisually
pubdb=# SELECT
pg_create_logical_replication_slot ('sub1', 'pgoutput') ;
– Create SUBSCRIPTION
– Create Logical Replication Slot manually
Restrictions
Combined with Streaming Replication
34
– Mixable with Streaming Replication environment
– Logical replication from slave instance is not possible
– Logical Replication Slot can not be created on standby instance
– Logical Decoding can not be executed on the standby instance
Trouble shooting
35
Trouble shooting
Log of resource shortage
36
– max_replication_slots shortage (PUBLICATION)
ERROR: could not create replication slot "sub1": ERROR: all
replication slots are in use
– max_wal_senders shortage (PUBLICATION)
FATAL: number of requested standby connections exceeds
max_wal_senders (currently 10)
– max_logical_replication_workers shortage (SUBSCRIPTION)
WARNING: out of logical replication worker slots
HINT: You might need to increase max_logical_replication_workers.
– max_worker_processes shortage (SUBSCRIPTION)
No log output
Trouble shooting
Other log
37
– Lack of permission at the time of the initial data copy (PUBLICATION)
ERROR: could not start initial contents copy for table
"public.data1": ERROR: permission denied for relation data1
– Execution of DROP SUBSCRIPTION statement (normal)
FATAL: terminating logical replication worker due to administrator
command
LOG: worker process: logical replication worker for subscription
16408 (PID 77332) exited with exit code 1
Trouble shooting
Conflict pattern and behavior
38
Conflict pattern Replication Log output
Primary Key / Unique Key Violation Stop Yes
CHECK constraint violation Stop Yes
No UPDATE data Continue No
No DETETE data Continue No
No table Stop Yes
No partial columns Stop Yes
Data convert error Stop Yes
Lock table Wait No
Lock updated rows Wait No
– Conflict with the behavior that occurs in the SUBSCRIPTION database
Trouble shooting
Conflict pattern and behavior
39
– Impact on applications when conflict occurs
– Even if conflicts occur, SQL to the table does not be blocked
– Conflict detection
– Detect from logfile
– pg_stat_replication.flush_lag / write_lag
– pg_replication_slots.confirmed_flush_lsn not equal pg_current_wal_lsn()
– Behavior at conflict occurrence in SUBSCRIPTION instance
– When constraint violation is detected, the logical replication worker process stop
– Restart after 5 seconds and restart WAL apply
– Repeat the above until constraint violation is resolved
Trouble shooting
Log at error occurrence
40
– Log of primary key violation on SUBSCRIPTION side
ERROR: duplicate key value violates unique constraint "pk_data1"
DETAIL: Key (c1)=(500) already exists.
LOG: worker process: logical replication worker for subscription
16414 (PID 9644) exited with exit code 1
LOG: terminating walsender process due to replication timeout
LOG: starting logical decoding for slot "sub1"
DETAIL: streaming transactions committing after 0/5600ED48, reading
WAL from 0/5600ED10
– Log on wal sender timeout on PUBLICATION side
Trouble shooting
Log at error occurrence
41
– SUBSCRIPTION side log
ERROR: invalid memory alloc request size 258291203
CONTEXT: slot "sub1", output plugin "pgoutput", in the change
callback, associated LSN 0/2B2543E8LOG: could not send data to
client: Broken pipe FATAL: connection to client lost
ERROR: could not receive data from WAL stream:
ERROR: invalid memory alloc request size 258291203
CONTEXT: slot "sub1", output plugin "pgoutput", in the change
callback, associated LSN 0/2B2543E8
– Memory allocation error during WAL decoding
– Occurred when transferring bytea type
– Text transformation according to the parameter bytea_output
– Ensure the memory of the "record size x 2 + 1" bytes by default
– PUBLICATION side log
Trouble shooting
Conflict Resolution
42
– The conflict is not automatically resolved
– There are two ways to solve (on SUBSCRIPTION side)
– Delete the record where the conflict occurred (resolve the constraint violation)
– Skipping WAL where conflict has occurred (resolution of constraint violation /
elimination of memory shortage)
Trouble shooting
Conflict Resolution
43
– Skip WAL where conflict occurred
– Specify the LSN that starts applying WAL
postgres=# SELECT pg_current_wal_lsn() ;
pg_current_wal_lsn
--------------------
0/7200B4F0
(1 row)
– Confirm current LSN on PUBLICATION side
Trouble shooting
Conflict Resolution
44
subdb=# SELECT * FROM pg_replication_origin_status;
local_id | external_id | remote_lsn | local_lsn
----------+-------------+------------+-----------
1 | pg_16425 | 0/7200B068 | 0/DA0078E8
(1 row)
– Confirm external_id on SUBSCRIPTION side
– pg_{pg_subscription.oid} is output to the external_id column
subdb=# SELECT oid, subname FROM pg_subscription ;
oid | subname
-------+---------
16425| sub1
Trouble shooting
Conflict Resolution
45
subdb=# SELECT pg_replication_origin_advance('pg_16399',
'0/82708760') ;
ERROR: replication origin with OID 1 is already active for PID 5566
– Sometimes it gets an error
– Specify the start LSN on SUBSCRIPTION side
subdb=# SELECT
pg_replication_origin_advance ('pg_16425', '0/7200B4F0') ;
pg_replication_origin_advance
-------------------------------
(1 row)
Thank you
noriyoshi.shinoda@hpe.com
46

More Related Content

What's hot

PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
elliando dias
 
MySQL Performance schema missing_manual_flossuk
MySQL Performance schema missing_manual_flossukMySQL Performance schema missing_manual_flossuk
MySQL Performance schema missing_manual_flossuk
Valeriy Kravchuk
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
Denish Patel
 

What's hot (20)

MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
 
EDB Postgres DBA Best Practices
EDB Postgres DBA Best PracticesEDB Postgres DBA Best Practices
EDB Postgres DBA Best Practices
 
MySQL Performance schema missing_manual_flossuk
MySQL Performance schema missing_manual_flossukMySQL Performance schema missing_manual_flossuk
MySQL Performance schema missing_manual_flossuk
 
Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
 
MySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer GuideMySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer Guide
 
Solving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsSolving PostgreSQL wicked problems
Solving PostgreSQL wicked problems
 
Replication in PostgreSQL tutorial given in Postgres Conference 2019
Replication in PostgreSQL tutorial given in Postgres Conference 2019Replication in PostgreSQL tutorial given in Postgres Conference 2019
Replication in PostgreSQL tutorial given in Postgres Conference 2019
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
MySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group ReplicationMySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group Replication
 
Percona Live 2022 - MySQL Architectures
Percona Live 2022 - MySQL ArchitecturesPercona Live 2022 - MySQL Architectures
Percona Live 2022 - MySQL Architectures
 
Bloat and Fragmentation in PostgreSQL
Bloat and Fragmentation in PostgreSQLBloat and Fragmentation in PostgreSQL
Bloat and Fragmentation in PostgreSQL
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
 
PostgreSQL: Advanced indexing
PostgreSQL: Advanced indexingPostgreSQL: Advanced indexing
PostgreSQL: Advanced indexing
 
Parallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDBParallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDB
 
Logical replication with pglogical
Logical replication with pglogicalLogical replication with pglogical
Logical replication with pglogical
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
Introduction to Vacuum Freezing and XID
Introduction to Vacuum Freezing and XIDIntroduction to Vacuum Freezing and XID
Introduction to Vacuum Freezing and XID
 
Top-10-Features-In-MySQL-8.0 - Vinoth Kanna RS - Mydbops Team
Top-10-Features-In-MySQL-8.0 - Vinoth Kanna RS - Mydbops TeamTop-10-Features-In-MySQL-8.0 - Vinoth Kanna RS - Mydbops Team
Top-10-Features-In-MySQL-8.0 - Vinoth Kanna RS - Mydbops Team
 

Similar to PGConf.ASIA 2017 Logical Replication Internals (English)

Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespace
Marco Tusa
 
Introduction4 SQLite
Introduction4 SQLiteIntroduction4 SQLite
Introduction4 SQLite
Stanley Huang
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OSIDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
Cuneyt Goksu
 
Oracle10g New Features I
Oracle10g New Features IOracle10g New Features I
Oracle10g New Features I
Denish Patel
 

Similar to PGConf.ASIA 2017 Logical Replication Internals (English) (20)

Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespace
 
Ensuring Data Protection Using Oracle Flashback Features - Presentation
Ensuring Data Protection Using Oracle Flashback Features - PresentationEnsuring Data Protection Using Oracle Flashback Features - Presentation
Ensuring Data Protection Using Oracle Flashback Features - Presentation
 
Best New Features of Oracle Database 12c
Best New Features of Oracle Database 12cBest New Features of Oracle Database 12c
Best New Features of Oracle Database 12c
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Let's scale-out PostgreSQL using Citus (English)
Let's scale-out PostgreSQL using Citus (English)Let's scale-out PostgreSQL using Citus (English)
Let's scale-out PostgreSQL using Citus (English)
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Introduction4 SQLite
Introduction4 SQLiteIntroduction4 SQLite
Introduction4 SQLite
 
Optimizing your Database Import!
Optimizing your Database Import! Optimizing your Database Import!
Optimizing your Database Import!
 
SCALE 15x Minimizing PostgreSQL Major Version Upgrade Downtime
SCALE 15x Minimizing PostgreSQL Major Version Upgrade DowntimeSCALE 15x Minimizing PostgreSQL Major Version Upgrade Downtime
SCALE 15x Minimizing PostgreSQL Major Version Upgrade Downtime
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactions
 
从 Oracle 合并到 my sql npr 实例分析
从 Oracle 合并到 my sql   npr 实例分析从 Oracle 合并到 my sql   npr 实例分析
从 Oracle 合并到 my sql npr 实例分析
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
 
Fast federated SQL with Apache Calcite
Fast federated SQL with Apache CalciteFast federated SQL with Apache Calcite
Fast federated SQL with Apache Calcite
 
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OSIDUG NA 2014 / 11 tips for DB2 11 for z/OS
IDUG NA 2014 / 11 tips for DB2 11 for z/OS
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
 
Flink's SQL Engine: Let's Open the Engine Room!
Flink's SQL Engine: Let's Open the Engine Room!Flink's SQL Engine: Let's Open the Engine Room!
Flink's SQL Engine: Let's Open the Engine Room!
 
Oracle10g New Features I
Oracle10g New Features IOracle10g New Features I
Oracle10g New Features I
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Watch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML UtilitiesWatch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML Utilities
 

More from Noriyoshi Shinoda

More from Noriyoshi Shinoda (20)

PostgreSQL Unconference #5 ICU Collation
PostgreSQL Unconference #5 ICU CollationPostgreSQL Unconference #5 ICU Collation
PostgreSQL Unconference #5 ICU Collation
 
Babelfish Compatibility
Babelfish CompatibilityBabelfish Compatibility
Babelfish Compatibility
 
PostgreSQL Unconference #29 Unicode IVS
PostgreSQL Unconference #29 Unicode IVSPostgreSQL Unconference #29 Unicode IVS
PostgreSQL Unconference #29 Unicode IVS
 
PostgreSQL Conference Japan 2021 B2 Citus 10
PostgreSQL Conference Japan 2021 B2 Citus 10PostgreSQL Conference Japan 2021 B2 Citus 10
PostgreSQL Conference Japan 2021 B2 Citus 10
 
Citus 10 verification result (Japanese)
Citus 10 verification result (Japanese)Citus 10 verification result (Japanese)
Citus 10 verification result (Japanese)
 
PostgreSQL Unconference #26 No Error on PostgreSQL
PostgreSQL Unconference #26 No Error on PostgreSQLPostgreSQL Unconference #26 No Error on PostgreSQL
PostgreSQL Unconference #26 No Error on PostgreSQL
 
PostgreSQL 14 Beta1 New Features with Examples (Japanese)
PostgreSQL 14 Beta1 New Features with Examples (Japanese)PostgreSQL 14 Beta1 New Features with Examples (Japanese)
PostgreSQL 14 Beta1 New Features with Examples (Japanese)
 
PostgreSQL 14 Beta 1 New Features with Examples (English)
PostgreSQL 14 Beta 1 New Features with Examples (English)PostgreSQL 14 Beta 1 New Features with Examples (English)
PostgreSQL 14 Beta 1 New Features with Examples (English)
 
Add PLEASE clause to Oracle Database
Add PLEASE clause to Oracle DatabaseAdd PLEASE clause to Oracle Database
Add PLEASE clause to Oracle Database
 
PostgreSQL 12 New Features with Examples (English) GA
PostgreSQL 12 New Features with Examples (English) GAPostgreSQL 12 New Features with Examples (English) GA
PostgreSQL 12 New Features with Examples (English) GA
 
db tech showcase 2019 D10 Oracle Database New Features
db tech showcase 2019 D10 Oracle Database New Featuresdb tech showcase 2019 D10 Oracle Database New Features
db tech showcase 2019 D10 Oracle Database New Features
 
EDB Postgres Vision 2019
EDB Postgres Vision 2019 EDB Postgres Vision 2019
EDB Postgres Vision 2019
 
PostgreSQL 12 Beta 1 New Features with Examples (Japanese)
PostgreSQL 12 Beta 1 New Features  with Examples (Japanese)PostgreSQL 12 Beta 1 New Features  with Examples (Japanese)
PostgreSQL 12 Beta 1 New Features with Examples (Japanese)
 
PostgreSQL 12 Beta 1 New Features with Examples (English)
PostgreSQL 12 Beta 1 New Features with Examples (English)PostgreSQL 12 Beta 1 New Features with Examples (English)
PostgreSQL 12 Beta 1 New Features with Examples (English)
 
Let's scale-out PostgreSQL using Citus (Japanese)
Let's scale-out PostgreSQL using Citus (Japanese)Let's scale-out PostgreSQL using Citus (Japanese)
Let's scale-out PostgreSQL using Citus (Japanese)
 
PostgreSQL 11 New Features With Examples (Japanese)
PostgreSQL 11 New Features With Examples (Japanese)PostgreSQL 11 New Features With Examples (Japanese)
PostgreSQL 11 New Features With Examples (Japanese)
 
PostgreSQL 11 New Features With Examples (English)
PostgreSQL 11 New Features With Examples (English)PostgreSQL 11 New Features With Examples (English)
PostgreSQL 11 New Features With Examples (English)
 
Citus 7.5 Beta 検証結果
Citus 7.5 Beta 検証結果Citus 7.5 Beta 検証結果
Citus 7.5 Beta 検証結果
 
PostgreSQL 11 New Features Japanese version (Beta 1)
PostgreSQL 11 New Features Japanese version (Beta 1)PostgreSQL 11 New Features Japanese version (Beta 1)
PostgreSQL 11 New Features Japanese version (Beta 1)
 
PostgreSQL 11 New Features English version (Beta 1)
PostgreSQL 11 New Features English version (Beta 1)PostgreSQL 11 New Features English version (Beta 1)
PostgreSQL 11 New Features English version (Beta 1)
 

Recently uploaded

怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
vexqp
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
cnajjemba
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
vexqp
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1
ranjankumarbehera14
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
vexqp
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
vexqp
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
wsppdmt
 
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Abortion pills in Riyadh +966572737505 get cytotec
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
gajnagarg
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
wsppdmt
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
gajnagarg
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
ptikerjasaptiker
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 

Recently uploaded (20)

怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
 
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 

PGConf.ASIA 2017 Logical Replication Internals (English)

  • 2. Speaker Noriyoshi Shinoda – Company – Hewlett-Packard Japan, Ltd – Now – System design, tuning, consulting on RDBMS such as PostgreSQL, Oracle Database, Microsoft SQL Server, Vertica, Sybase ASE etc – Oracle ACE – Writing 15 books for Oracle Database – Investigation and verification of open source products – URL – Slideshare – https://www.slideshare.net/noriyoshishinoda/ – Oracle ACE – http://www.oracle.com/technetwork/jp/database/articles/vivadeveloper/index-1838335-ja.html 2
  • 3. Agenda – What is Logical Replication? – Let’s try! – Architecture – Restrictions – Trouble shooting 3
  • 4. What is Logical Replication? 4
  • 5. What is Logical Replication? What is Logical Replication? 5 – Is – PostgreSQL 10 new features – Replicate per table – Replicate per transaction – Updatable replication destination tables – Guarantee that the results of the SQL statement will be identical (= Logical) – Adopt Publish / Subscribe model – ≒ Slony-I – Is Not – Re-execute SQL – Physical page format match
  • 6. What is Logical Replication? Replication condition 6 – Conditions for replicable tables – Identical schema name – Identical table name – Identical column name – Identical data type – Different data types are available if implicit type conversion is possible
  • 8. Let’s try it! Publisher instance 8 # TYPE DATABASE USER ADDRESS METHOD host pubdb repusr1 192.168.1.100/32 md5 - Modify pg_hba.conf file pubdb=# CREATE ROLE repusr1 PASSWORD 'Passw0rd' LOGIN REPLICATION ; CREATE ROLE - Replicate data1 table of pubdb database to subdb database - Create role with REPLICATION attribute / LOGIN attribute - Role connected from Subscriber instance pubdb=# GRANT CREATE ON DATABASE pubdb TO pubusr1 ; GRANT - Grant database CREATE privilege to table owner (pubusr1) - 'DATABASE = replication' item not required
  • 9. Let’s try it! 9 - Modify postgresql.conf file wal_level = logical Publisher instance
  • 10. Let’s try it! 10 – Create table for replication pubdb=> CREATE TABLE data1 (c1 INT PRIMARY KEY, c2 VARCHAR(5)) ; CREATE TABLE pubdb=> CREATE PUBLICATION pub1 FOR TABLE data1 ; CREATE PUBLICATION – Create the PUBLICATION object – Grant SELECT permission to the table for connected user pubdb=> GRANT SELECT ON data1 TO repusr1 ; GRANT Publisher database
  • 11. Let’s try it! Subscriber database 11 – Create table for replication subdb=> CREATE TABLE data1 (c1 INT PRIMARY KEY, c2 VARCHAR(5)) ; CREATE TABLE subdb=# CREATE SUBSCRIPTION sub1 CONNECTION 'host=pubhost1 dbname=pubdb user=repusr1 password=Passw0rd' PUBLICATION pub1 ; CREATE SUBSCRIPTION – Create the SUBSCRIPTION object (require SUPERUSER)
  • 12. Let’s try it! Confirmation 12 – Publisher instance pubdb=# SELECT application_name, state, sync_state FROM pg_stat_replication ; application_name | state | sync_state ------------------+-----------+------------ sub1 | streaming | async (1 row) subdb=> SELECT subname, received_lsn FROM pg_stat_subscription ; subname | received_lsn ---------+-------------- sub1 | 0/1C8FF738 – Subscriber instance
  • 14. Subscriber InstancePublisher Instance Architecture Components 14 Publisher Database Subscriber Database PUBLICATION SUBSCRIPTION Tables logical replication worker Tables Tables Tables WAL decode message connect wal sender pgoutput plugin logical replication launcher Logical Replication Slot
  • 15. Architecture Processes 15 – wal sender process {user} {client ip} {state} – On PUBLICATION instance – Send decoded WAL messages – Launch for each connection from SUBSCRIPTION – bgworker: logical replication launcher – On PUBLICATION / SUBSCRIPTION instance – Start the logical replication worker process – bgworker: logical replication worker for subscription {oid} – On SUBSCRIPTION instance – Connect to wal sender process – Receive decoded WAL message and update the table – Launch for each SUBSCRIPTION
  • 16. Architecture Behavior when PUBLICATION is created 16 – Store information in the pg_publication catalog – PUBLICATION name – Which DML to replicate (INSERT / UPDATE / DELETE) – Whether to cover all tables (FOR ALL TABLES clause) – Store information in the pg_publication_rel catalog – Table OID and PUBLICATION OID – If FOR ALL TABLES clause is specified, nothing is stored in this catalog – Table name can be referenced from pg_publication_tables catalog
  • 17. Architecture Behavior when SUBSCRIPTION is created 17 – Store information in pg_subscription catalog – Connected instance information – Replication Slot name – Connected PUBLICATION name – Synchronous / Asynchronous replication attribute – Connect to PUBLICATION instance – Check REPLICATION attribute of connected user – Create Logical Replication Slot (default name is SUBSCRIPTION name) – Do not check whether PUBLICATION exists – Register replication target tables in pg_subscription_rel catalog – Synchronize initial snapshot – Executed asynchronously – Existing data on SUBSCRIPTION insntance will not be deleted
  • 18. Architecture Synchronize initial snapshot 18 Subscriber InstancePublisher Instance Backend process wal sender process wal sender process 2. Create Replication Slot 3. Create Temporary Replication Slot {sub name}_{sub oid}_sync_{rel oid} 4. COPY TO STDOUT statement logical replication worker logical replication worker Backend process 1.CREATE SUBSCRIPTION Decoded Initial Data Decoded Transaction Data
  • 19. Architecture Replication Slot 19 – Logical Replication Slot – One-to-one with SUBSCRIPTION – Manage sent WAL – Provide replication plugin – Automatic execution of the following SQL statement when creating SUBSCRIPTION pg_create_logical_replication_slot ( name, 'pgoutput' ) – Replication Slot name – SUBSCRIPTION name by default – Can change CREATE SUBSCRIPTION WITH (slot_name=name) statement
  • 20. Architecture pgoutput plugin and message 20 – pgoutput plugin – Default plugin for Logical Replication – Used by wal sender process – Create Logical Replication messages from WAL – Convert character encoding – Convert text message from binary data – Does not support text output by pg_logical_slot_get_changes function? – Message – All text data – Protocol – https://www.postgresql.org/docs/10/static/protocol-logicalrep-message-formats.html – When executed UPDATE / DELET statement – Send data specifying the update target column and updated data
  • 21. Architecture Replica Identity 21 Table attribute PUBLICATION SUBSCRIPTION PRIMARY KEY Yes Yes REPLICA IDENTITY FULL Yes No REPLICA IDENTITY USING INDEX + UNIQUE index + NOT NULL Yes Yes – UPDATE / DELETE for a table without a primary key or REPLICA IDENTITY setting is an SQL execution error pubdb=> UPDATE data1 SET c2='update' WHERE c1=100 ; ERROR: cannot update table "data1" because it does not have replica identity and publishes updates HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE. – The condition under which the UPDATE statement / DELETE statement is replicated
  • 22. Architecture Behavior during DML execution (asynchronous replication) 22 ① COMMIT ③ SIGUSR1backend ②WAL ④ COMMIT ⑥ To Subscription pgoutput wal sender
  • 23. Architecture Behavior during DML execution (synchronous replication) 23 ① COMMIT ③ SIGUSR1 backend ②WAL ⑥ SIGUSR1⑦ COMMIT ⑤ To Subscription pgoutput wal sender
  • 24. Architecture Storage 24 – Logical log snapshot – Created when wal sender process receives SIGUSR1 signal from wal writer process – Read the WAL file and create snapshot – File path ${PGDATA}/pg_logical/snapshots/{LSN upper}-{LSN lower}.snap ${PGDATA}/pg_replslot/{SLOT_NAME}/xid-{XID}-lsn-{LSN upper}-{LSN lower}.snap – Logical log snapshot when SUBSCRIPTION stop – Created if the SUBSCRIPTION instance is stopped and the wal sender process is restarted – It is deleted when the transfer is completed – File path
  • 26. Restrictions SQL statement that can not be replicated 26 – TRUNCATE statement – ALTER TABLE statement – CREATE TABLE statement – When executing the CREATE REPLICATION FOR ALL TABLES statement
  • 27. Restrictions Objects that can not be replicated 27 – Only the table can be added to PUBLICATION – pg_class.relkind = 'r' – Objects that can not be replicated – MATERIALIZED VIEW – INDEX – SEQUENCE – FOREIGN TABLE – UNLOGGED TABLE – INHERIT TABLE = OK (ONLY clause) – Partition parent table – Large Object
  • 28. Restrictions SERIAL column / GENERATED AS IDENTITY column 28 – Internally use SEQUENCE for SERIAL and GENERATED AS IDENTITY columns – The sequence value is transferred to SUBSCRIPTION but no sequence operation is executed
  • 29. Restrictions SERIAL column / GENERATED AS IDENTITY column 29 PUBLICATION CREATE TABLE t(a SERIAL, b CHAR) INSERT INTO t(b) VALUES ('A') SUBSCRIPTION CREATE TABLE t(a SERIAL, b CHAR) INSERT INTO t(b) VALUES ('B') (1, 'A') (1, 'A') (2, 'B') INSERT INTO t(b) VALUES ('C') (2, 'B') (1, 'C')
  • 30. Restrictions Trigger 30 – Partial execution – Only ROW TRIGGER execute – UPDATE OF trigger is not executed – STATEMENT TRIGGER is executed only at initial data transfer – SUBSCRIPTION database – Require ALTER TABLE ENABLE ALWAYS or REPLICA TRIGGER statement – Bug on10.0 : BEFORE ROW DELETE trigger not issued – https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=360fd1a7b2fe779cc9e696b813 b12f6a8e83b558 – Fixed on10.1
  • 31. Restrictions Parameter log_statement 31 – Even if log_statement = 'all' set, the SQL statement by replication does not be logged
  • 32. Restrictions Bidirectional replication 32 – Bidirectional replication can not be performed – It can be configured, however WAL circulates – Mutual replication between databases is possible if tables are different Database#1 Database#2 SUBSCRIPTION#1 table_a table_a PUBLICATION#1 SUBSCRIPTION#2 PUBLICATION#2 WAL circulates
  • 33. Restrictions Replication within instance 33 subdb=# CREATE SUBSCRIPTION sub1 CONNECTION 'dbname=pubdb' WITH (CONNECT=off) ; – Be careful to create the replication within instance – The CREATE SUBSCRIPTION statement hangs when trying to simply configure it – It is necessary to create SUBSCRIPTION and Replication Slot indivisually pubdb=# SELECT pg_create_logical_replication_slot ('sub1', 'pgoutput') ; – Create SUBSCRIPTION – Create Logical Replication Slot manually
  • 34. Restrictions Combined with Streaming Replication 34 – Mixable with Streaming Replication environment – Logical replication from slave instance is not possible – Logical Replication Slot can not be created on standby instance – Logical Decoding can not be executed on the standby instance
  • 36. Trouble shooting Log of resource shortage 36 – max_replication_slots shortage (PUBLICATION) ERROR: could not create replication slot "sub1": ERROR: all replication slots are in use – max_wal_senders shortage (PUBLICATION) FATAL: number of requested standby connections exceeds max_wal_senders (currently 10) – max_logical_replication_workers shortage (SUBSCRIPTION) WARNING: out of logical replication worker slots HINT: You might need to increase max_logical_replication_workers. – max_worker_processes shortage (SUBSCRIPTION) No log output
  • 37. Trouble shooting Other log 37 – Lack of permission at the time of the initial data copy (PUBLICATION) ERROR: could not start initial contents copy for table "public.data1": ERROR: permission denied for relation data1 – Execution of DROP SUBSCRIPTION statement (normal) FATAL: terminating logical replication worker due to administrator command LOG: worker process: logical replication worker for subscription 16408 (PID 77332) exited with exit code 1
  • 38. Trouble shooting Conflict pattern and behavior 38 Conflict pattern Replication Log output Primary Key / Unique Key Violation Stop Yes CHECK constraint violation Stop Yes No UPDATE data Continue No No DETETE data Continue No No table Stop Yes No partial columns Stop Yes Data convert error Stop Yes Lock table Wait No Lock updated rows Wait No – Conflict with the behavior that occurs in the SUBSCRIPTION database
  • 39. Trouble shooting Conflict pattern and behavior 39 – Impact on applications when conflict occurs – Even if conflicts occur, SQL to the table does not be blocked – Conflict detection – Detect from logfile – pg_stat_replication.flush_lag / write_lag – pg_replication_slots.confirmed_flush_lsn not equal pg_current_wal_lsn() – Behavior at conflict occurrence in SUBSCRIPTION instance – When constraint violation is detected, the logical replication worker process stop – Restart after 5 seconds and restart WAL apply – Repeat the above until constraint violation is resolved
  • 40. Trouble shooting Log at error occurrence 40 – Log of primary key violation on SUBSCRIPTION side ERROR: duplicate key value violates unique constraint "pk_data1" DETAIL: Key (c1)=(500) already exists. LOG: worker process: logical replication worker for subscription 16414 (PID 9644) exited with exit code 1 LOG: terminating walsender process due to replication timeout LOG: starting logical decoding for slot "sub1" DETAIL: streaming transactions committing after 0/5600ED48, reading WAL from 0/5600ED10 – Log on wal sender timeout on PUBLICATION side
  • 41. Trouble shooting Log at error occurrence 41 – SUBSCRIPTION side log ERROR: invalid memory alloc request size 258291203 CONTEXT: slot "sub1", output plugin "pgoutput", in the change callback, associated LSN 0/2B2543E8LOG: could not send data to client: Broken pipe FATAL: connection to client lost ERROR: could not receive data from WAL stream: ERROR: invalid memory alloc request size 258291203 CONTEXT: slot "sub1", output plugin "pgoutput", in the change callback, associated LSN 0/2B2543E8 – Memory allocation error during WAL decoding – Occurred when transferring bytea type – Text transformation according to the parameter bytea_output – Ensure the memory of the "record size x 2 + 1" bytes by default – PUBLICATION side log
  • 42. Trouble shooting Conflict Resolution 42 – The conflict is not automatically resolved – There are two ways to solve (on SUBSCRIPTION side) – Delete the record where the conflict occurred (resolve the constraint violation) – Skipping WAL where conflict has occurred (resolution of constraint violation / elimination of memory shortage)
  • 43. Trouble shooting Conflict Resolution 43 – Skip WAL where conflict occurred – Specify the LSN that starts applying WAL postgres=# SELECT pg_current_wal_lsn() ; pg_current_wal_lsn -------------------- 0/7200B4F0 (1 row) – Confirm current LSN on PUBLICATION side
  • 44. Trouble shooting Conflict Resolution 44 subdb=# SELECT * FROM pg_replication_origin_status; local_id | external_id | remote_lsn | local_lsn ----------+-------------+------------+----------- 1 | pg_16425 | 0/7200B068 | 0/DA0078E8 (1 row) – Confirm external_id on SUBSCRIPTION side – pg_{pg_subscription.oid} is output to the external_id column subdb=# SELECT oid, subname FROM pg_subscription ; oid | subname -------+--------- 16425| sub1
  • 45. Trouble shooting Conflict Resolution 45 subdb=# SELECT pg_replication_origin_advance('pg_16399', '0/82708760') ; ERROR: replication origin with OID 1 is already active for PID 5566 – Sometimes it gets an error – Specify the start LSN on SUBSCRIPTION side subdb=# SELECT pg_replication_origin_advance ('pg_16425', '0/7200B4F0') ; pg_replication_origin_advance ------------------------------- (1 row)