Sharding with postgres_fdw



Similar documents
How To Use Postgresql With Foreign Data In A Foreign Server In A Multi Threaded Database (Postgres)

A Shared-nothing cluster system: Postgres-XC

PostgreSQL Past, Present, and Future EDB All rights reserved.

Enabling development teams to move fast. PostgreSQL at Zalando

F1: A Distributed SQL Database That Scales. Presentation by: Alex Degtiar (adegtiar@cmu.edu) /21/2013

Agenda. ! Strengths of PostgreSQL. ! Strengths of Hadoop. ! Hadoop Community. ! Use Cases

Write a Foreign Data Wrapper in 15 minutes

Can the Elephants Handle the NoSQL Onslaught?

Why Zalando trusts in PostgreSQL

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

Practical Cassandra. Vitalii

Programming Database lectures for mathema

Databases for text storage

Linas Virbalas Continuent, Inc.

The elephant called PostgreSQL

Comparing SQL and NOSQL databases

Intro to Databases. ACM Webmonkeys 2011

Postgres Plus xdb Replication Server with Multi-Master User s Guide

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

How, What, and Where of Data Warehouses for MySQL

The Evolution of. Keith Alsheimer, CMO, EDB EnterpriseDB Corporation. All rights reserved. 1

Integrating VoltDB with Hadoop

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

Replicating to everything

MongoDB in the NoSQL and SQL world. Horst Rechner Berlin,

An Approach to Implement Map Reduce with NoSQL Databases

IBM Campaign Version-independent Integration with IBM Engage Version 1 Release 3 April 8, Integration Guide IBM

Database Scalability {Patterns} / Robert Treat

Database Administration with MySQL

MySQL és Hadoop mint Big Data platform (SQL + NoSQL = MySQL Cluster?!)

AWS Schema Conversion Tool. User Guide Version 1.0

YouTube Vitess. Cloud-Native MySQL. Oracle OpenWorld Conference October 26, Anthony Yeh, Software Engineer, YouTube.

MySQL Storage Engines

Integration of Apache Hive and HBase

COSC 6397 Big Data Analytics. 2 nd homework assignment Pig and Hive. Edgar Gabriel Spring 2015

MongoDB Developer and Administrator Certification Course Agenda

Database Management System Choices. Introduction To Database Systems CSE 373 Spring 2013

Introduction to Polyglot Persistence. Antonios Giannopoulos Database Administrator at ObjectRocket by Rackspace

Real-time Data Replication

IBM Campaign and IBM Silverpop Engage Version 1 Release 2 August 31, Integration Guide IBM

PostgreSQL vs. MySQL vs. Commercial Databases: It's All About What You Need

news from Tom Bacon about Monday's lecture

Oracle BI EE Implementation on Netezza. Prepared by SureShot Strategies, Inc.

DBMS / Business Intelligence, SQL Server

Database Extension 1.5 ez Publish Extension Manual

Structured Data Storage

A Brief Introduction to MySQL

Why NoSQL? Your database options in the new non- relational world IBM Cloudant 1

White Paper. Optimizing the Performance Of MySQL Cluster

Monitoring PostgreSQL database with Verax NMS

CitusDB Architecture for Real-Time Big Data

PostgreSQL Audit Extension User Guide Version 1.0beta. Open Source PostgreSQL Audit Logging

Integrating Big Data into the Computing Curricula

Lecture 10: HBase! Claudia Hauff (Web Information Systems)!

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

Lofan Abrams Data Services for Big Data Session # 2987

Document Oriented Database

EDG Project: Database Management Services

B.1 Database Design and Definition

X4-2 Exadata announced (well actually around Jan 1) OEM/Grid control 12c R4 just released

plproxy, pgbouncer, pgbalancer Asko Oja

Performance Testing of Big Data Applications

Comparison of the Frontier Distributed Database Caching System with NoSQL Databases

Kafka & Redis for Big Data Solutions

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

1Z0-117 Oracle Database 11g Release 2: SQL Tuning. Oracle

TERADATA QUERY GRID. Teradata User Group September 2014

Portable Scale-Out Benchmarks for MySQL. MySQL User Conference 2008 Robert Hodges CTO Continuent, Inc.

Weaving Stored Procedures into Java at Zalando

Data warehousing with PostgreSQL

Introduction to NoSQL Databases and MapReduce. Tore Risch Information Technology Uppsala University

PostgreSQL Features, Futures and Funding. Simon Riggs

Big Data With Hadoop

Hacettepe University Department Of Computer Engineering BBM 471 Database Management Systems Experiment

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Using MySQL for Big Data Advantage Integrate for Insight Sastry Vedantam

RDS Migration Tool Customer FAQ Updated 7/23/2015

16.1 MAPREDUCE. For personal use only, not for distribution. 333

MySQL. Leveraging. Features for Availability & Scalability ABSTRACT: By Srinivasa Krishna Mamillapalli

High-Volume Data Warehousing in Centerprise. Product Datasheet

Lecture Data Warehouse Systems

Spider Storage Engine: The sharding plugin of MySQL/MariaDB Introducing and newest topics. Spiral-Arm / Team-Lab Kentoku SHIBA

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Integrate Master Data with Big Data using Oracle Table Access for Hadoop

Benchmarking Couchbase Server for Interactive Applications. By Alexey Diomin and Kirill Grigorchuk

Using RDBMS, NoSQL or Hadoop?

Programming Hadoop 5-day, instructor-led BD-106. MapReduce Overview. Hadoop Overview

MyOra 3.5. User Guide. SQL Tool for Oracle. Kris Murthy

OLTP Meets Bigdata, Challenges, Options, and Future Saibabu Devabhaktuni

3 Case Studies of NoSQL and Java Apps in the Real World

Oracle Database 10g: Introduction to SQL

NOSQL INTRODUCTION WITH MONGODB AND RUBY GEOFF

Distributed Database Access in the LHC Computing Grid with CORAL

Real Time Fraud Detection With Sequence Mining on Big Data Platform. Pranab Ghosh Big Data Consultant IEEE CNSV meeting, May Santa Clara, CA

NoSQL replacement for SQLite (for Beatstream) Antti-Jussi Kovalainen Seminar OHJ-1860: NoSQL databases

Partitioning under the hood in MySQL 5.5

Transcription:

Sharding with postgres_fdw Postgres Open 2013 Chicago Stephen Frost sfrost@snowman.net Resonate, Inc. Digital Media PostgreSQL Hadoop techjobs@resonateinsights.com http://www.resonateinsights.com

Stephen Frost PostgreSQL Major Contributor, Committer Implemented Roles in 8.3 Column-Level Privileges in 8.4 Contributions to PL/pgSQL, PostGIS Resonate, Inc. Principal Database Engineer Online Digital Media Company We're Hiring! - techjobs@resonateinsights.com

planet.postgresql.org Do you read...

What is an FDW? First, SQL/MED SQL/ Management of External Data Standard to allow integration with external data Foreign data can be nearly anything: SQL Databases, CSV Files, Text Files, NoSQL Databases, Cacheing systems, etc.. Defines the notion of a 'FOREIGN TABLE' Foreign tables are "views" to external data No data is stored in the DB

What is an FDW? (part 2) FDWs are the back-end piece to support SQL/MED PostgreSQL provides a generic FDW API An FDW is a PG EXTENSION implementing the API PG Extensions already exist for: RDMS's: Oracle, MySQL, ODBC, JDBC NoSQL's: CouchDB, Mongo, Redis Files: CSV, Text, even JSON "Other": Twitter, HTTP Our focus will be on (ab)using postgres_fdw

Basics of FDW connections Connecting to another actual RDBMS is complicated CREATE FOREIGN SERVER CREATE USER MAPPING CREATE FOREIGN TABLE 'SERVER' provides a name and options to connect 'USER' maps the local user to the remote user 'TABLE' defines: A local TABLE object, with columns, etc A remote TABLE (through a FOREIGN SERVER) Connecting with a file FDW is simpler (no user map)

Using postgres_fdw CREATE EXTENSION postgres_fdw; CREATE FOREIGN SERVER shard01 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'shard01', dbname 'mydb',...) All libpq options accepted except user/pw User/PW is specified in user mappings Cost options (fdw_startup_cost, fdw_tuple_cost)

Createing User Mappings CREATE USER MAPPING FOR myrole SERVER shard01 OPTIONS (user 'myrole', password 'abc123') Only takes user and password options User mappings are tied to servers User must exist on client and server Must use a password for non-superuser maps

Creating Foreign Tables CREATE FOREIGN TABLE mytable_shard01 ( a int OPTIONS (column_name 'b'), b int OPTIONS (column_name 'a'),... SERVER shard01 OPTIONS (table_name 'mytable'); Can pick remote schema, remote table, and remote column These don't have to match the local system Very important for sharding

Remote Query Execution Each backend manages its own remote connections When a foreign table is queried: PG opens a connection to the remote server Starts a transaction on the remote server A cursor is created for the query WHERE clauses are pushed to remote server Data is pulled through the remote cursor when rows are requested during query execution

More on Query Execution The remote transaction ends when the local transaction ends Rolls back or commits based on local transaction Rows inserted are not visible on remote until the local transaction completes Be careful of 'idle in transaction' connections.. Connections are kept after the foreign query Re-used for later requests to the same server No explicit limit on number of connections Each connection uses up memory, of course.

Query costing with FDWs Approach to costing can be changed Options can be set at server or table level fdw_startup_cost and fdw_tuple_cost use_remote_estimate - false (default) Looks up statistics for the table locally Statistics updated with ANALYZE use_remote_estimate - true Queries the remote server to determine cost info Uses EXPLAIN on remote side ANALYZE your tables!

Sharding What is sharding? Horizontal partitioning across servers Break up large tables based on a key/range Replicate small / common data across nodes Why sharding? Allows (more) parallelization of work Scales beyond a single server Challenges Data consistency Difficult to query aganist

Dealing with 32 shards Why 32? Pre-sharding Only 8 physical servers Four clusters per node Too many to manage manually Script everything Building the clusters User/role creation Table creation, etc, etc.. Use a CM System (Puppet, Chef, etc.)

Sharding suggestions Still partition on shards Smaller tables, smaller indexes Use inheiritance and CHECK constraints Foreign tables can use parent tables Break up sequence spaces Define a range for each shard Put constraints to ensure correct sequence used Consider one global sequence approach

FDW Challenges Not parallelized! Queries against foreign tables are done serially Transactions commit with the head node What is pushed down and what isn't? Conditionals Only built-in data types, operators, functions Joins aren't (yet...) Not able to call remote functions directly Foreign Tables are one-to-one Inserts go to all columns (can't have defaults..)

Parallelizing Need an independent "job starting" process cron pgagent Daemon w/ LISTEN/NOTIFY Use triggers on remote tables to NOTIFY View / Manage jobs through the head node Custom background worker...?

Working through FDWs Use lots of views Script building them UNION ALL is your friend Add constants/conditionals to view's query Use DO-INSTEAD rules for updates Put them on foreign system too for joins, etc Get friendly with triggers Use them to run remote procedures Remember that everything is serial! Bottlenecks, network latency can be a factor

View Example CREATE FOREIGN TABLE workflow.jobs_shard1 ( workflow_name text, name text, state text ) SERVER shard1 OPTIONS (schema_name 'workflow', table_name 'jobs');... CREATE FOREIGN TABLE workflow.workflow_shard1 ( name text, state text ) SERVER shard1 OPTIONS (schema_name 'workflow', table_name 'workflow');... CREATE VIEW workflow.workflow AS SELECT 'shard1'::text AS shard, * FROM workflow_shard.workflow_shard1 UNION ALL SELECT 'shard2'::text AS shard, * FROM workflow_shard.workflow_shard2 UNION ALL SELECT 'shard3'::text AS shard, * FROM workflow_shard.workflow_shard3... CREATE VIEW workflow.jobs AS SELECT 'shard1'::text AS shard, * FROM workflow_shard.jobs_shard1 UNION ALL SELECT 'shard2'::text AS shard, * FROM workflow_shard.jobs_shard2 UNION ALL SELECT 'shard3'::text AS shard, * FROM workflow_shard.jobs_shard3...

What's PG do? Let's try a join.. EXPLAIN SELECT * FROM workflow JOIN jobs ON (workflow.shard = jobs.shard and workflow.name = workflow_name); QUERY PLAN --------------------------------------------------------------------------------------------------- Merge Join (cost=13235.29..41555.08 rows=1878610 width=224) Merge Cond: ((('shard1'::text) = ('shard1'::text)) AND (workflow_shard1.name = jobs_shard1.workflow_name)) -> Sort (cost=6367.75..6410.79 rows=17216 width=128) Sort Key: ('shard1'::text), jobs_shard1.workflow_name -> Append (cost=100.00..4036.48 rows=17216 width=128) -> Foreign Scan on jobs_shard1 (cost=100.00..126.14 rows=538 width=128) -> Foreign Scan on jobs_shard2 (cost=100.00..126.14 rows=538 width=128) -> Foreign Scan on jobs_shard3 (cost=100.00..126.14 rows=538 width=128) -> Foreign Scan on jobs_shard4 (cost=100.00..126.14 rows=538 width=128)... -> Materialize (cost=6867.54..6976.66 rows=21824 width=96) -> Sort (cost=6867.54..6922.10 rows=21824 width=96) Sort Key: ('shard1'::text), workflow_shard1.name -> Append (cost=100.00..4174.72 rows=21824 width=96) -> Foreign Scan on workflow_shard1 (cost=100.00..130.46 rows=682 width=96) -> Foreign Scan on workflow_shard2 (cost=100.00..130.46 rows=682 width=96) -> Foreign Scan on workflow_shard3 (cost=100.00..130.46 rows=682 width=96)... (73 rows)

Playing with views Looking at one shard.. EXPLAIN SELECT * FROM workflow JOIN jobs ON (workflow.shard = jobs.shard and workflow.name = workflow_name) WHERE workflow.shard = 'shard1'; QUERY PLAN ----------------------------------------------------------------------------------------- Hash Join (cost=232.86..329.41 rows=1835 width=224) Hash Cond: (workflow_shard1.name = jobs_shard1.workflow_name) -> Append (cost=100.00..130.46 rows=682 width=96) -> Foreign Scan on workflow_shard1 (cost=100.00..130.46 rows=682 width=96) -> Hash (cost=126.14..126.14 rows=538 width=128) -> Append (cost=100.00..126.14 rows=538 width=128) -> Foreign Scan on jobs_shard1 (cost=100.00..126.14 rows=538 width=128) (7 rows) Much better, but means you have to remember... Still works through prepared queries

Verbose Shows the query to be sent EXPLAIN (verbose) SELECT * FROM workflow JOIN jobs ON (workflow.shard = jobs.shard and workflow.name = workflow_name) WHERE workflow.shard = 'shard1'; QUERY PLAN ------------------------------------------------------------------------------------------------------------ Hash Join (cost=232.86..329.41 rows=1835 width=224) Output: ('shard1'::text), workflow_shard1.name, workflow_shard1.state, ('shard1'::text), jobs_shard1.workflow_name, jobs_shard1.name, jobs_shard1.state Hash Cond: (workflow_shard1.name = jobs_shard1.workflow_name) -> Append (cost=100.00..130.46 rows=682 width=96) -> Foreign Scan on workflow_shard.workflow_shard1 (cost=100.00..130.46 rows=682 width=96) Output: 'shard1'::text, workflow_shard1.name, workflow_shard1.state Remote SQL: SELECT name, state FROM workflow.workflow -> Hash (cost=126.14..126.14 rows=538 width=128) Output: ('shard1'::text), jobs_shard1.workflow_name, jobs_shard1.name, jobs_shard1.state -> Append (cost=100.00..126.14 rows=538 width=128) -> Foreign Scan on workflow_shard.jobs_shard1 (cost=100.00..126.14 rows=538 width=128) Output: 'shard1'::text, jobs_shard1.workflow_name, jobs_shard1.name, jobs_shard1.state Remote SQL: SELECT workflow_name, name, state FROM workflow.jobs (13 rows)

Firing a remote procedure Have to set it up as an INSERT trigger Arguments and result end up being columns On the shards: CREATE TABLE fire_func ( id bigint, a int, b int, result int ); CREATE FUNCTION add_two () RETURNS trigger AS $_$ begin new.result = new.a + new.b; return new; end; $_$ LANGUAGE plpgsql; CREATE TRIGGER add_two_trig BEFORE INSERT ON fire_func FOR EACH ROW EXECUTE PROCEDURE add_two(); On the head node: CREATE FOREIGN TABLE fire_func_shard1 ( id bigint, a int, b int, result int ) SERVER shard1 OPTIONS (schema_name 'workflow', table_name 'fire_func'); CREATE FOREIGN TABLE fire_func_shard2 ( id bigint, a int, b int, result int ) SERVER shard2 OPTIONS (schema_name 'workflow', table_name 'fire_func');... =# insert into fire_func_shard2 (id, a, b) values (100, 1, 2) returning id, a, b, result; id a b result -----+---+---+-------- 100 1 2 3 (1 row)

Managing foreign tables Scripts, ideally generalized Generating foreign tables Building views Use a schema migration system Roll-your-own External options (Sqitch, etc) Use "foreign schemas"

Improvements for FDWs Parallelize work Make Append() send all FDW queries at once Use a round-robin approach to pulling data Buffer results Better user management Credential proxying Automatic user maps Trusted inter-server connections

More idle thoughts Make UNION ALL views updatable Inheiritance for foreign tables Auto-discover foreign table definition Join push-down Scripting the server/user map/table creation Building views over the foregn tables How views are implemented / run by PG Build system to trigger actions off of a table update Managing workflows, external processes REAL PARTITIONING

Thank you! Stephen Frost sfrost@snowman.net @net_snow