postgresql architectures in production

37
PostgreSQL Architectures in Production Dimitri Fontaine PostgreSQL Major Contributor FOSDEM 2021, FEB 6-7, ONLINE

Upload: others

Post on 25-May-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PostgreSQL Architectures in Production

PostgreSQL Architectures in Production

Dimitri Fontaine PostgreSQL Major Contributor

F O S D E M 2 0 2 1 , F E B 6 - 7 , O N L I N E

Page 2: PostgreSQL Architectures in Production
Page 3: PostgreSQL Architectures in Production

PostgreSQL for developers

Dimitri Fontaine PostgreSQL Major Contributor

A B O O K A B O U T P O S T G R E S Q L B Y D I M I T R I F O N T A I N E

Page 4: PostgreSQL Architectures in Production

Spec ia l Discount

Use Code for 30%

“FOSDEM2021”

Page 5: PostgreSQL Architectures in Production

PostgreSQL Architectures in Production

Dimitri Fontaine PostgreSQL Major Contributor

P O S T G R E S B U I L D 2 0 2 0 , D E C 8 - 9 , O N L I N E

Page 6: PostgreSQL Architectures in Production

Postgres Architectures in Production

Page 7: PostgreSQL Architectures in Production

PostgreSQLThe World's Most Advanced Open Source

Relational Database

Page 8: PostgreSQL Architectures in Production

ArchitecturesProduction Components and Dependencies

Page 9: PostgreSQL Architectures in Production

in ProductionThis is happening live!

Page 10: PostgreSQL Architectures in Production

Availability: Service & Data

Page 11: PostgreSQL Architectures in Production

High Availability

• Maintain service

• Failover

• Automated?

• Maintain data

• Disaster Recovery

• Streaming Replication

Page 12: PostgreSQL Architectures in Production

Classic Postgres ArchitectureUsing Streaming Replication with Two Standby Nodes to enable Service and Data Availability

Page 13: PostgreSQL Architectures in Production

Postgres Failover

Page 14: PostgreSQL Architectures in Production

Included in Postgres

• Streaming Replication • Sync or Async

• Per transaction setting

• ALTER ROLE vip

SET synchronous_commit

TO ‘remote_apply’; • ALTER ROLE … SET … • ALTER DATABASE … SET …

A

B C

Page 15: PostgreSQL Architectures in Production

Included in Postgres

• Online Streaming changes • Replication Slots • pg_basebackup • pg_rewind • Fast forward

• Cascading Replication • Online Standby Promotion

A

B C

Page 16: PostgreSQL Architectures in Production

Included in Postgres

• Disaster Recovery basics: PITR

• Setup a “standby” with a target

• Archiving provided separately A

B C

Page 17: PostgreSQL Architectures in Production

Included in Postgres

• Client Side HA • Multi-Host Connection String

• Client still has to reconnect when

connection is lost, of course A

B C

postgres://hostA:5502,hostB:5503,hostC:5501/dbname?target_session_attrs=read-write&sslmode=require

Page 18: PostgreSQL Architectures in Production

Not included in Postgres

• Archiving • Base Backups

• WAL Archiving

• Retention setup for Disaster Recovery

Page 19: PostgreSQL Architectures in Production

Not included in Postgres

• Online “Membership” changes

• Idea that roles are DYNAMIC • Ensure retention during failover

• For WALs

• And for base backups

• Automated node role changes

• Restart to reconnect to new primary

Page 20: PostgreSQL Architectures in Production

Not included in Postgres

• Configuration Management • postgresql.conf • pg_hba.conf • Some setting change require a restart

• Extension Management

• Postgres Upgrades • Minor upgrade (scheduled restarts)

• Major upgrades

Page 21: PostgreSQL Architectures in Production

Automated Failover

Page 22: PostgreSQL Architectures in Production

pg_auto_failover

Page 23: PostgreSQL Architectures in Production

Primary

Secondary

Application Monitor

SQL

SQL (fallback)

Streaming Replication

Health checks

Health checks

Single StandbyOnly provides failover, no HA

Page 24: PostgreSQL Architectures in Production

Single Standby

$ pg_autoctl create monitor —auth trust —self-signed-ssl —run

$ pg_autoctl create postgres \ —auth trust \ —self-signed-ssl. \ —monitor postgres://host/pg_auto_failover \ —run

$ pg_autoctl create postgres \ —auth trust \ —self-signed-ssl. \ —monitor postgres://host/pg_auto_failover \ —run

Page 25: PostgreSQL Architectures in Production

number_sync_standby = �

Node A

Primary

replication quorum = truecandidate priority = ��

Node B

Secondary

replication quorum = truecandidate priority = ��

Node C

Secondary

replication quorum = truecandidate priority = ��

Application Monitor

SQL

SQL (fallback)

Streaming Replication

Health checks

Two Standby NodesAllows to implement HA

Page 26: PostgreSQL Architectures in Production

Two Standby Nodes

$ pg_autoctl create postgres \ —auth trust \ —self-signed-ssl. \ —monitor postgres://host/pg_auto_failover \ —run

Page 27: PostgreSQL Architectures in Production

number_sync_standby = �

Node A

Primary

replication quorum = truecandidate priority = ��

Node B

Secondary

replication quorum = truecandidate priority = ��

Node C

Secondary

replication quorum = truecandidate priority = ��

Node D

Secondary

replication quorum = truecandidate priority = ��

Application Monitor

SQL

SQL (fallback)

Health checks

Streaming Replication

Three Standby Nodes

Page 28: PostgreSQL Architectures in Production

Three Standby Nodes

$ pg_autoctl create postgres \ —auth trust \ —self-signed-ssl. \ —monitor postgres://host/pg_auto_failover \ —run

Page 29: PostgreSQL Architectures in Production

number_sync_standby = �

Node A

Primary

replication quorum = truecandidate priority = ��

Node B

Secondary

replication quorum = truecandidate priority = ��

Node C

Secondary

replication quorum = falsecandidate priority = �

Node D

Secondary

replication quorum = truecandidate priority = ��

Application Monitor

SQL

SQL (fallback)

Health checks

Streaming Replication

Three Standby Nodes, one async

Page 30: PostgreSQL Architectures in Production

Three Standby Nodes, One Async

$ pg_autoctl set node candidate-priority 0 $ pg_autoctl set node replication-quorum false

$ pg_autoctl create postgres \ —auth trust \ —self-signed-ssl. \ —monitor postgres://host/pg_auto_failover \ —replication-quorum false \ —candidate-priority 0 \ —run

Page 31: PostgreSQL Architectures in Production

Included in pg_auto_failover

• Streaming Replication

• Replication slots

• Maintained on standby nodes

• Easy setup including SSL certificates

• Dynamic settings, online changes

$ pg_autoctl create postgres

Page 32: PostgreSQL Architectures in Production

Included in pg_auto_failover

• Online “Membership” changes

• Idea that roles are DYNAMIC • Ensure retention during failover

• For WALs

• And for base backups

• Automated HBA editing

$ pg_autoctl show uri $ pg_autoctl show state $ pg_autoctl get formation settings

Page 33: PostgreSQL Architectures in Production

Included in pg_auto_failover

• Online membership changes

• Online formation setting changes

• Maintenance operations

$ pg_autoctl set node candidate-priority 0 $ pg_autoctl set node replication-quorum false $ pg_autoctl set formation number-sync-standbys 2

$ pg_autoctl enable maintenance $ pg_autoctl disable maintenance

Page 34: PostgreSQL Architectures in Production

Included in pg_auto_failover

• Network split detection / protection

• Active monitoring and role changes

• Automated and manual Failover

• Including manual Switchover

• Including manual choice of target node

$ pg_autoctl perform failover $ pg_autoctl perform switchover $ pg_autoctl perform promotion —name node_d

Page 35: PostgreSQL Architectures in Production

Not included in pg_auto_failover yet

• Archiving and Disaster Recovery

• Configuration Management • postgresql.conf • pg_hba.conf • Some setting change require a restart

• Extension Management

• Postgres Upgrades • Minor upgrade (scheduled restarts)

• Major upgrades

Page 36: PostgreSQL Architectures in Production

PostgreSQL Architectures in Production

Dimitri Fontaine PostgreSQL Major Contributor

P O S T G R E S B U I L D 2 0 2 0 , D E C 8 - 9 , O N L I N E

Page 37: PostgreSQL Architectures in Production

Ask Me Two Questions!

Dimitri Fontaine Citus Data, Microsoft

@tapoueh

T H E A R T O F P O S T G R E S Q L