Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in...

51
Horizontal scaling with PL/Proxy Jan Urba´ nski [email protected] New Relic PGConf.Russia 2015, Moscow, February 7 Jan Urba´ nski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 1 / 48

Transcript of Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in...

Page 1: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Horizontal scaling with PL/Proxy

Jan [email protected]

New Relic

PGConf.Russia 2015, Moscow, February 7

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 1 / 48

Page 2: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Problem definition

Outline

1 Scaling PostgreSQL horizontallyProblem definitionGetting ready to scale

2 The PL/Proxy language

3 PgBouncer

4 Usage scenarios

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 2 / 48

Page 3: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Problem definition

PostgreSQL in the VPS world

I maximum capacity of available machines is limited

I however, the number of available machines is limitless

I need to be able to add resources without disrupting current operations

I hosts will fail: not if but when

I typical for VPS scenarios, but enforces good engineering practiceseven if you manage your own metal

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 3 / 48

Page 4: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Problem definition

Challenges

I normalisation goes out the windowI idea: independent parts of the application get independent database

hostsI not friendly for developers, who need to manage the complexity inside

the appI oftentimes, not effective: a single module’s data outgrows the biggest

available node

I plan for using multiple machines from the beginning

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 4 / 48

Page 5: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Getting ready to scale

Outline

1 Scaling PostgreSQL horizontallyProblem definitionGetting ready to scale

2 The PL/Proxy language

3 PgBouncer

4 Usage scenarios

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 5 / 48

Page 6: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Getting ready to scale

Stored procedure API layer

I route application data access through stored procedures

BAD

insert into orders (select * from parts join ... where

tmpl = $1 and user id = $2 ...)

WORSE

Order.new(Parts.find(:tmpl id =>

tmpl id).includes(...).where(:user id => user id)).save!

BETTER

select create order(tmpl id, user id)

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 6 / 48

Page 7: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Getting ready to scale

Stored procedure API layer

I route application data access through stored procedures

BAD

insert into orders (select * from parts join ... where

tmpl = $1 and user id = $2 ...)

WORSE

Order.new(Parts.find(:tmpl id =>

tmpl id).includes(...).where(:user id => user id)).save!

BETTER

select create order(tmpl id, user id)

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 6 / 48

Page 8: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Getting ready to scale

Stored procedure API layer

I route application data access through stored procedures

BAD

insert into orders (select * from parts join ... where

tmpl = $1 and user id = $2 ...)

WORSE

Order.new(Parts.find(:tmpl id =>

tmpl id).includes(...).where(:user id => user id)).save!

BETTER

select create order(tmpl id, user id)

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 6 / 48

Page 9: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Getting ready to scale

Stored procedure API layer

I route application data access through stored procedures

BAD

insert into orders (select * from parts join ... where

tmpl = $1 and user id = $2 ...)

WORSE

Order.new(Parts.find(:tmpl id =>

tmpl id).includes(...).where(:user id => user id)).save!

BETTER

select create order(tmpl id, user id)

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 6 / 48

Page 10: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Scaling PostgreSQL horizontally Getting ready to scale

Stored procedure API layer cont.

I database people regain control over database access

I much bigger freedom to do schema changes

I defines a clean interface between developers and DBAsI it’s not an all or nothing proposition!

I define a procedural API to the hottest part of the databaseI keep accessing the rest through evil ORMs or whatever else

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 7 / 48

Page 11: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language How PL/Proxy works

Outline

1 Scaling PostgreSQL horizontally

2 The PL/Proxy languageHow PL/Proxy worksLanguage syntax

3 PgBouncer

4 Usage scenarios

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 8 / 48

Page 12: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language How PL/Proxy works

Proxy functions

I a language for writing remote procedure calls

I very simple syntax, just a few constructs

I only handles connection and distribution, the rest is built on top ofexisting mechanisms

I could mostly be reimplemented in any unsafe procedural language(PL/PerlU, PL/PythonU) or with dblink

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 9 / 48

Page 13: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language How PL/Proxy works

Function execution

I user calls a PL/Proxy function

I the system determines the target host

I a persistent connection to that host is opened

I code is run on the remote side

I result is sent back to the original PL/Proxy function caller

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 10 / 48

Page 14: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language How PL/Proxy works

Simple proxy function example

Execute function on remote host

create function create_order(tmpl_id int, account_id int)

returns orders

language plproxy

as $func$

connect ’host=10.0.10.1 dbname=orders’;

$func$;

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 11 / 48

Page 15: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language How PL/Proxy works

Determining code to run

I by default, an identically named procedure is called on the remote side

I arguments are passed to the remote procedure

I the result type is validated against the proxy function’s result type

I this makes it completely transparent to the caller

I you can seamlessly (and gradually) substitute your regular storedprocedures with PL/Proxy functions

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 12 / 48

Page 16: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

Outline

1 Scaling PostgreSQL horizontally

2 The PL/Proxy languageHow PL/Proxy worksLanguage syntax

3 PgBouncer

4 Usage scenarios

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 13 / 48

Page 17: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

CONNECT

I connect specifies a libpq connection stringI several ways of specifying the string

I a literal stringI one of the arguments of the procedureI a function invocation

I useful for static partitioning or local testing

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 14 / 48

Page 18: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

Simple proxy function example

Execute function on remote host

create function create_order(tmpl_id int, account_id int)

returns orders

language plproxy

as $func$

connect ’host=10.0.10.1 dbname=orders’;

$func$;

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 15 / 48

Page 19: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

CLUSTER and RUN ON

I hardcoding connection strings won’t work if you have your datapartitioned

I for partitioned setups, cluster and run on are the solution

I cluster allows specifying the set of hosts where the function mightrun

I run on takes a partitioning key, calculates the partition number andruns the function

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 16 / 48

Page 20: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

RUN ON cont

I run on any and run on all exist as wellI with run an all the query is run in parallel on all partitionsI results are combined and returned to the caller

I the partitioning key can also be specified using a function invocation

I built-in function hashtext creating stable hashes of text values

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 17 / 48

Page 21: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

CLUSTER and RUN ON example

Partitioning

create function create_order(tmpl_id int, account_id int)

returns orders

language plproxy

as $func$

cluster ’appdata’;

run on account_id;

$func$;

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 18 / 48

Page 22: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

Partitioning internals

I a cluster is a list of connection stringsI PL/Proxy requires the number of partitions to be a power of 2

I annoying, but not that muchI you can use the same connection strings for several partitionsI changing the number of partitions is a pain, plan ahead and start with

32 partitions

I the partitioning key needs to be an integer (int4 or int8)

I the target partition is determined with a simple mod

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 19 / 48

Page 23: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

Defining clusters

I a legacy procedure-based approachI procedures in other languages to return partition lists and configI need to manage several of them, additional warts regarding cachingI much easier to use the foreign server interface

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 20 / 48

Page 24: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

Defining clusters cont

I PL/Proxy now provides a foreign data wrapper

I use create server to define clusters

I use a number of options called p0, p1, p2, ... with values beingconnection strings

I user mappings can supply additional libpq parameters

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 21 / 48

Page 25: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

Foreign data wrapper configuration

Defining a cluster

create server appdata foreign data wrapper plproxy options (

p0 ’dbname=appdata1 host=10.0.10.1’,

p1 ’dbname=appdata2 host=10.0.10.2’

);

create user mapping for webserver server appdata options (

password ’tiger’

);

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 22 / 48

Page 26: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

SPLIT

I split is a way to write queries that need to access more than onepartition

I the PL/Proxy procedure should receive equal-length arrays ofarguments

I an array of the same length should be passed to run on

I for each run on element, the specified partition gets a call with anarray of corresponding arguments

I once all queries are complete, result are stitched together and returned

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 23 / 48

Page 27: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

SPLIT example

Accessing multiple partitions

create function latest_orders(tmpl_ids int[],

account_ids int[])

returns setof orders

language plproxy

as $func$

cluster ’appdata’;

split all; -- shorthand for "split tmpl_ids, account_ids;"

run on account_ids;

$func$;

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 24 / 48

Page 28: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

The PL/Proxy language Language syntax

Limitations

I no transactional guarantees!I changing the partitioning key is a huge hassle

I but then again, in which partitioning technology it isn’t?

I eventually, a connection will be open from every backend to everypartition

I to avoid keeping lots of backends running, use PgBouncer

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 25 / 48

Page 29: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

PgBouncer Using PgBouncer with PL/Proxy

Outline

1 Scaling PostgreSQL horizontally

2 The PL/Proxy language

3 PgBouncerUsing PgBouncer with PL/Proxy

4 Usage scenarios

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 26 / 48

Page 30: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

PgBouncer Using PgBouncer with PL/Proxy

What is PgBouncer?

I a connection pooler for PostgreSQL, implementing the Postgresprotocol

I sibling project to PL/ProxyI in fact, they used to be bundled together, now they’re both standalone

projects

I very useful even if you’re not using PL/ProxyI helps with web apps that don’t support persistent connectionsI has a bunch tricks that make operating a Postgres cluster simpler

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 27 / 48

Page 31: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

PgBouncer Using PgBouncer with PL/Proxy

How does PgBouncer work?

I configure a list of database that the pooler will handle

I PgBouncer listens for Postgres protocol connections and parses thestartup packet

I it then proxies queries to the appropriate database, possibly reusingpreviously opened connections

I no forking, no backend startup overhead, can handle hundreds ofconnections per second

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 28 / 48

Page 32: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

PgBouncer Using PgBouncer with PL/Proxy

PgBouncer operating modes

I reusing connections breaks some featuresI transactionsI session parameter changes, prepared plansI the list goes on...

I the pooler can use one of several modesI session mode, connections reused only if client disconnectsI transaction mode, connections reused when client commitsI statement mode, like transaction mode, but transactions are disabled

I statement mode is meant to be used with PL/Proxy

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 29 / 48

Page 33: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

PgBouncer Using PgBouncer with PL/Proxy

PgBouncer tricks

I set timeout on idle in transaction connections

I runtime config changesI pausing access to a given database

I starts queueing new queries to the databaseI waits while all active queries are finishedI disconnects from the databaseI allows restarting the database without clients noticing

I online restartI start a new pooler process, transfer active TCP connectionsI allows restarting the pooler without clients noticing

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 30 / 48

Page 34: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Outline

1 Scaling PostgreSQL horizontally

2 The PL/Proxy language

3 PgBouncer

4 Usage scenariosAKA stories from the trenches

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 31 / 48

Page 35: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Setting up the cluster

I use a dedicated database as a “shell” with all the PL/Proxy functions

I run PgBouncer in statement mode on each partition hostI run PgBouncer on the shell host, too

I if shell is 100% PL/Proxy, it can use statement modeI typically, the shell contains app data that didn’t need to be partitionedI in that case, use session or transaction mode

I partitions only get connections from the shell Postgres

I the shell only gets connections from PgBouncer

I be ruthless with iptables and pg hba.conf

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 32 / 48

Page 36: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Setting up the cluster - diagram

Application

PL/Proxy PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

PgBouncerstatement mode

Partitiondatabase

Partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 33 / 48

Page 37: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Online reconfiguration

I changing cluster configuration is just an alter server

I changes applied immediately and atomicallyI it’s even transactional!

I server settings can include things like TCP keepalives

I PL/Proxy triggers run as the table owner, be sure to add a usermapping for them

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 34 / 48

Page 38: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Upgrading hardware on partition host

Zero downtime database hardware upgrade:

1 set up streaming replication to the new host

2 pause access to the old host via PgBouncer

3 promote the replica

4 change PgBouncer config on old host to point to new host

5 unpause PgBouncer on old host

6 alter PL/Proxy settings on shell to point to new host

7 once old host has no connections, decommission it

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 35 / 48

Page 39: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Upgrading hardware - diagram

replication

PgBouncerstatement mode

New partitiondatabase(replica)

Application

PL/Proxy PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

PgBouncerstatement mode

Partitiondatabase

Old partitiondatabase(primary)

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 36 / 48

Page 40: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Upgrading hardware - diagram

replication

PgBouncerstatement mode

New partitiondatabase(replica)

Application

PL/Proxy PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

PgBouncerstatement mode

Partitiondatabase

Old partitiondatabase(primary)

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 37 / 48

Page 41: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Upgrading hardware - diagram

PgBouncerstatement mode

New partitiondatabase(primary)

Application

PL/Proxy PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

PgBouncerstatement mode

Partitiondatabase

Old partitiondatabase(primary)

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 38 / 48

Page 42: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Upgrading hardware - diagram

PgBouncerstatement mode

Partitiondatabase

Application

PL/Proxy PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

PgBouncerstatement mode

Partitiondatabase

Old partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 39 / 48

Page 43: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Upgrading hardware - diagram

PgBouncerstatement mode

Partitiondatabase

Application

PL/Proxy PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

PgBouncerstatement mode

Partitiondatabase

Old partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 40 / 48

Page 44: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Upgrading hardware - diagram

PgBouncerstatement mode

Partitiondatabase

Application

PL/Proxy PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

Partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 41 / 48

Page 45: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Adding a new partition

Splitting data from partition A to B:

1 create a migrated table to list already migrated IDs

2 write custom partitioning function

1 calculate target partition2 return it if it’s not A3 looks it up in migrated, return B if found4 return A

3 alter PL/Proxy functions to use the new function

4 kick off migration process, update migrated as you go

5 once all data is migrated, alter the foreign server config and restoreoriginal PL/Proxy partitioning function definition

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 42 / 48

Page 46: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Adding a partition - diagram

migrated

account_id int

Application

PL/ProxyPL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

Partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 43 / 48

Page 47: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Adding a partition - diagram

PL/Proxy

PgBouncerstatement mode

migrated

account_id int

Application

PL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

New partitiondatabase

Partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 44 / 48

Page 48: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Adding a partition - diagram

already migrated?

PL/ProxyPL/Proxy

PgBouncerstatement mode

migrated

account_id int

Application

PL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

New partitiondatabase

Partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 45 / 48

Page 49: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Adding a partition - diagram

migration process

already migrated?

PL/ProxyPL/Proxy

PgBouncerstatement mode

migrated

account_id int

Application

PL/Proxy

PgBouncerS/T mode

Shelldatabase

PgBouncerstatement mode

New partitiondatabase

Partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 46 / 48

Page 50: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios AKA stories from the trenches

Adding a partition - diagram

PgBouncerstatement mode

PL/Proxy

Partitiondatabase

PL/Proxy

PgBouncerstatement mode

Application

PL/Proxy

PgBouncerS/T mode

Shelldatabase

Partitiondatabase

PgBouncerstatement mode

Partitiondatabase

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 47 / 48

Page 51: Horizontal scaling with PL/Proxy · Scaling PostgreSQL horizontally Problem de nition PostgreSQL in the VPS world I maximum capacityof available machines is limited I however, thenumberof

Usage scenarios Questions

Questions?

Jan Urbanski (New Relic) Horizontal scaling with PL/Proxy PGConf.Russia 2015 48 / 48