Top Banner
The PostgreSQL Replication Protocol Tools and opportunities Postgres Open 2011 Chicago, IL Magnus Hagander [email protected] PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
35

New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Oct 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

The PostgreSQL Replication Protocol

Tools and opportunities

Postgres Open 2011Chicago, IL

Magnus [email protected]

PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING

Page 2: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

PostgreSQL Replication● Added in PostgreSQL 9.0● Based on streaming WAL (Transaction

Log)● Starts from base backup● Uses standard recovery code● Layered on top of regular protocol

Page 3: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Parts of the puzzle● Connection processing and startup● The PostgreSQL protocol● The replication specific protocol● pg_basebackup

Page 4: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Normal client connection

1. TCP connection established (5432)

Page 5: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Normal client connection

1. TCP connection established (5432)

2. fork()

Page 6: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Normal client connection

1. TCP connection established (5432)

2. fork()

3. SSL negotiation

Page 7: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Normal client connection

1. TCP connection established (5432)

2. fork()

3. SSL negotiation

4. Get database/username/options

Page 8: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Normal client connection

1. TCP connection established (5432)

2. fork()

3. SSL negotiation

4. Get database/username/options

5. Perform authentication

Page 9: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Normal client connection

1. TCP connection established (5432)

2. fork()

3. SSL negotiation

4. Get database/username/options

5. Perform authentication

6. Select database

Page 10: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Normal client connection

1. TCP connection established (5432)

2. fork()

3. SSL negotiation

4. Get database/username/options

5. Perform authentication

6. Select database

7. Enter query processing loop

Page 11: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Replication client

1. TCP connection established (5432)

2. fork()

3. SSL negotiation

4. Get database/username/options (fixed)

5. Perform authentication

6. Select database

7. Enter query processing loop

Page 12: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Replication client

1. TCP connection established (5432)

2. fork()

3. SSL negotiation

4. Get database/username/options

5. Perform authentication

6. Start walsender

Page 13: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

What's the walsender?!● Special purpose PostgreSQL backend● Not connected with a database● Only accepts simple queries● Returns mix of resultsets and streams● 9.0: only basic log streaming

● Client connects, requests WAL streaming starting at position <x>

Page 14: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

The PostgreSQL protocol● Very simple● Always TCP● Message-based, bi-directional● Optionally SSL encrypted

● Entire stream wrapped

Page 15: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

A message

MessageType (byte)

Message Length(32-bit) Message...

Page 16: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Standard query exchange

Z <size> <Transaction Status>ReadyForQuery

Q 13 SELECT 1,2,3 SimpleQuery

T <size> <col1>,<col2>,<col3>RowDescription

D <size> 1,2,3DataRow

C <size> SELECTCommandTag

Page 17: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Streaming replication

Q 22 START_REPLICATION 0/0 SimpleQuery

W <size> 0,0CopyOutResponse

d <size> <xlog data>CopyData

d <size> <xlog data>CopyData

Z <size> <Transaction Status>ReadyForQuery

Page 18: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Advances in 9.1● Synchronous replication

● (not going to cover that)● Hot Standby Feedback Loop

● (not going to cover that)● Walsender “micro language”

Page 19: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Walsender micro-language● Full grammar in walsender mode● Few commands, few options● Still very picky about formats● Not designed for manual consumption● Foundation for future improvements

Page 20: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Walsender in 9.1● IDENTIFY_SYSTEM● START_REPLICATION <position>● BASE_BACKUP

[LABEL 'label'][PROGRESS][FAST][WAL][NOWAIT]

Page 21: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Base backups● Single-command base backups● No need for separate

pg_start_backup()/pg_stop_backup()● Can still control backup label● Can still control fast/slow checkpoint

● Not a silver bullet● Old method is still there!

Page 22: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Base backups● Still not for manual consumption● Use bin/pg_basebackup● Integration in third party modules and

applications

Page 23: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Streaming base backups● Tar format stream

● Easy to stream● No global archive header● Alignment-at-512-bytes cheap

● One tar stream per tablespace● Sequential transmission

Page 24: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Streaming base backups

Z <size> <Transaction Status>ReadyForQuery

Q 24 BASE_BACKUP LABEL 'foo' SimpleQuery

T <size> spcoid, spclocation, sizeRowDescription

D <size> <tablespace information>DataRow

C <size> SELECTCommandTag

Page 25: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Streaming base backups

H <size> 0,0CopyOutResponse

d <size> <tar data>CopyData

c <size>CopyDone

H <size> 0,0CopyOutResponse

d <size> <tar data>CopyData

…..

Page 26: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Using pg_basebackup

● pg_basebackup-D <directory>-F<p|t>-c <fast|spread>-l <label>-z

● Plus all “standard” libpq client options

Page 27: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Progress reporting● Add -P to the commandline● Expensive!

● Scans all tablespaces twice● Inexact – but gives a good hint

Page 28: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Base backups and WAL● Restore from base backup requires WAL

archiving● Complex to set up and monitor

● Append WAL to command, or use -x● walsender includes required WAL files at

end of tar file● Use wal_keep_segments!

Page 29: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Future improvements

Page 30: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Streaming WAL archive● Log archiving still uses archive_command● 16Mb-blocks, or archive_timeout● Replication protocol already does this● pg_receivexlog

Page 31: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Prevent WAL cycling● WAL cycled normally during backups● In -x mode, might still be needed● If cycled too soon, backup fails

Page 32: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

WAL streaming during backup

● Combine streaming wal archive with pg_basebackup

● During backup, log is streamed in parallel● Less WAL to keep on master

Page 33: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Relocatable tablespaces● Currently, only $PGDATA can be moved● In theory...● Support moving other tablespaces● Both for streaming and regular base

backups!

Page 34: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Incremental backups● “rsync” style?● Using LSN?● Decrease size of log archive without more

full backups

Page 35: New The PostgreSQL Replication Protocol - Hagander PostgreSQL Replication... · 2020. 9. 10. · PostgreSQL Replication Added in PostgreSQL 9.0 Based on streaming WAL (Transaction

Thank you!

Questions?

Twitter: @magnushaganderhttp://blog.hagander.net/[email protected]