Top Banner
The Future of Postgres Sharding BRUCE MOMJIAN This presentation will cover the advantages of sharding and future Postgres sharding implementation requirements. Creative Commons Attribution License http://momjian.us/presentations Last updated: October, 2015 1 / 22
22

The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Jan 06, 2017

Download

Engineering

Ontico
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: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

The Future of Postgres Sharding

BRUCE MOMJIAN

This presentation will cover the advantages of sharding andfuture Postgres sharding implementation requirements.Creative Commons Attribution License http://momjian.us/presentations

Last updated: October, 2015

1 / 22

Page 2: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Outline

1. Scaling

2. Vertical scaling options

3. Non-sharding horizontal scaling

4. Existing sharding options

5. Future sharding

The Future of Postgres Sharding 2 / 22

Page 3: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Scaling

Database scaling is the ability to increase database throughput byutilizing additional resources such as I/O, memory, CPU, oradditional computers.However, the high concurrency and write requirements ofdatabase servers make scaling a challenge. Sometimes scaling isonly possible with multiple sessions, while other options requiredata model adjustments or server configuration changes.Postgres Scaling Opportunitieshttp://momjian.us/main/presentations/overview.html#scaling

The Future of Postgres Sharding 3 / 22

Page 4: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Vertical Scaling

Vertical scaling can improve performance on a single server by:

◮ Increasing I/O with

◮ faster storage◮ tablespaces on storage devices◮ striping (RAID 0) across storage devices◮ Moving WAL to separate storage

◮ Adding memory to reduce read I/O requirements

◮ Adding more and faster CPUs

The Future of Postgres Sharding 4 / 22

Page 5: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Non-sharding Horizontal Scaling

Non-sharding horizontal scaling options include:

◮ Read scaling using Pgpool and streaming replication

◮ CPU/memory scaling with asynchronous multi-master

The entire data set is stored on each server.

The Future of Postgres Sharding 5 / 22

Page 6: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Why Use Sharding?

◮ Only sharding can reduce I/O, by splitting data across servers

◮ Sharding benefits are only possible with a shardableworkload

◮ The shard key should be one that evenly spreads the data

◮ Changing the sharding layout can cause downtime

◮ Additional hosts reduce reliability; additional standbyservers might be required

The Future of Postgres Sharding 6 / 22

Page 7: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Typical Sharding Criteria

◮ List

◮ Range

◮ Hash

The Future of Postgres Sharding 7 / 22

Page 8: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Existing Sharding Solutions

◮ Application-based sharding

◮ PL/Proxy

◮ Postgres-XC

◮ pg_shard

◮ Hadoop

The data set is sharded (striped) across servers.

The Future of Postgres Sharding 8 / 22

Page 9: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Sharding Using Foreign Data Wrappers (FDW)

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

SQL Queries

SQL Queries

PG FDW

Foreign Server Foreign Server Foreign Server

The Future of Postgres Sharding 9 / 22

Page 10: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

FDW Sort/Join/Aggregate Pushdown

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

SQL Queries

SQL Queries

PG FDW

with joins, sorts, aggregates

Foreign Server Foreign Server Foreign Server

The Future of Postgres Sharding 10 / 22

Page 11: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Advantages of FDW Sort/Join/Aggregate Pushdown

◮ Sort pushdown reduces CPU and memory overhead on thecoordinator

◮ Join pushdown reduces coordinator join overhead, andreduces the number of rows transferred

◮ Aggregate pushdown causes summarized values to be passedback from the shards

◮ WHERE clause restrictions are already pushed down

The Future of Postgres Sharding 11 / 22

Page 12: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Pushdown of Static Tables

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

SQL Queries

SQL Queries

with joins to static data

PG FDW

and static data restrictions

Foreign S. Foreign S. Foreign S.static static static

The Future of Postgres Sharding 12 / 22

Page 13: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Implementing Pushdown of Static Tables

Static table pushdown allows join pushdown where the queryrestriction is on the static table and not on the shared key. Statictables can be pushed to shards using:

◮ Triggers with Slony-like distribution

◮ WAL logical decoding

The optimizer must also know which tables are duplicated on theshards for proper join pushdown.

The Future of Postgres Sharding 13 / 22

Page 14: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Query Routing and DDL to Proper Shards

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

SQL Queries

SQL Queries

PG FDW

Foreign Server Foreign Server Foreign Server

The Future of Postgres Sharding 14 / 22

Page 15: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Implementing Query Routing andDDL to Proper Shards

◮ Could use the existing partitioning system

◮ inheritance◮ CHECK constraint◮ constraint exclusion◮ trigger for INSERT, UPDATE, and DELETE

The Future of Postgres Sharding 15 / 22

Page 16: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Parallel FDW Access

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

�����������������������������������������������������������������������������������������������������������������������������������������������������������������

SQL Queries

PG FDW

Parallel Queries

Foreign Server Foreign Server Foreign Server

The Future of Postgres Sharding 16 / 22

Page 17: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Advantages of Parallel FDW Access

◮ Can use libpq’s asynchronous API to issue multiple pendingqueries

◮ Ideal for queries that must run on every shard, e.g.

◮ restrictions on static tables◮ queries with no sharded-key reference◮ queries with multiple shared-key references

◮ Aggregates like AVG() must be computed on the controller byhaving shards return SUM() and COUNT()

The Future of Postgres Sharding 17 / 22

Page 18: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Global Snapshot Manager

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

SQL Queries

SQL Queries

ManagerGlobal Snapshot

PG FDW

Foreign Server Foreign Server Foreign Server

The Future of Postgres Sharding 18 / 22

Page 19: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Implementing a Global Snapshot Manager

◮ We already support sharing snapshots among clients withpg_export_snapshot()

◮ We already support exporting snapshots to other serverswith the GUC hot_standby_feedback

The Future of Postgres Sharding 19 / 22

Page 20: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Global Transaction Manager

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������

SQL Queries

SQL Queries

Foreign Server

ManagerGlobal Transaction

PG FDW

Foreign Server Foreign Server

The Future of Postgres Sharding 20 / 22

Page 21: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Implementing a Global Transaction Manager

◮ Can use prepared transactions (two-phase commit)

◮ Transaction manager can be internal or external

◮ Can use an industry-standard protocol like XA

The Future of Postgres Sharding 21 / 22

Page 22: The Future of Postgres Sharding / Bruce Momjian (PostgreSQL)

Conclusion

http://momjian.us/presentations https://www.flickr.com/photos/anotherpintplease/

The Future of Postgres Sharding 22 / 22