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

Similar documents
Sharding with postgres_fdw

Write a Foreign Data Wrapper in 15 minutes

SQL/MED and More. Management of External Data in PostgreSQL and Microsoft SQL Server. Seminar Database Systems

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

How to Set Up pgagent for Postgres Plus. A Postgres Evaluation Quick Tutorial From EnterpriseDB

Programming Database lectures for mathema

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

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

Postgres Plus Cloud Database!

Version of this tutorial: 1.06a (this tutorial will going to evolve with versions of NWNX4)

A Brief Introduction to MySQL

PostgreSQL Functions By Example

DBI-Link: 3.0. PgCon Ottawa 2008 Copyright David Fetter 2008 All Rights Reserved

Connect to MySQL or Microsoft SQL Server using R

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:

IceWarp to IceWarp Server Migration

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

Database migration using Wizard, Studio and Commander. Based on migration from Oracle to PostgreSQL (Greenplum)

Lab 2: PostgreSQL Tutorial II: Command Line

Database Extension 1.5 ez Publish Extension Manual

The Advantages of PostgreSQL

CS346: Database Programming.

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

Simba Apache Cassandra ODBC Driver

Postgres Plus Migration Guide

Various Load Testing Tools

Web Development using PHP (WD_PHP) Duration 1.5 months

RDS Migration Tool Customer FAQ Updated 7/23/2015

Database Administration with MySQL

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Product: DQ Order Manager Release Notes

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Cloud Services. Introduction...2 Overview...2. Security considerations Installation...3 Server Configuration...4

Using ODBC with MDaemon 6.5

TrueSight Operations Management Monitoring Studio

SyncThru Database Migration

Guide to the MySQL Workbench Migration Wizard: From Microsoft SQL Server to MySQL

Windows PowerShell Cookbook

Advanced Object Oriented Database access using PDO. Marcus Börger

NetIQ Identity Manager Setup Guide

Moving the TRITON Reporting Databases

Business Interaction Server. Configuration Guide Rev A

Using the vcenter Orchestrator Plug-In for Microsoft Active Directory

IGEL Universal Management. Installation Guide

Comparing MySQL and Postgres 9.0 Replication

User Management Resource Administrator. UMRA tables. User Guide

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

The SkySQL Administration Console

Using IRDB in a Dot Net Project

CounterPoint SQL and Magento ecommerce Interface

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Websense Support Webinar: Questions and Answers

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Release Notes For Versant/ODBC On Windows. Release

SQL Server Instance-Level Benchmarks with DVDStore

Data warehousing with PostgreSQL

Monitoring PostgreSQL database with Verax NMS

NetBrain Discovery Appliance Manual

NetBrain Enterprise Edition 6.0a NetBrain Server Backup and Failover Setup

Databases and SQL. Homework. Matthias Danner. June 11, Matthias Danner Databases and SQL June 11, / 16

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

Database migration. from Sybase ASE to PostgreSQL. Achim Eisele and Jens Wilke. 1&1 Internet AG

BlackBerry Enterprise Server Resource Kit

ODBC Client Driver Help Kepware, Inc.

IRF2000 IWL3000 SRC1000 Application Note - Apps with OSGi - Condition Monitoring with WWH push

MongoDB. An introduction and performance analysis. Seminar Thesis

Configuring Hadoop Distributed File Service as an Optimized File Archive Store

A Shared-nothing cluster system: Postgres-XC

SOA Software API Gateway Appliance 7.1.x Administration Guide

Getting started with PostgreSQL

New Features... 1 Installation... 3 Upgrade Changes... 3 Fixed Limitations... 4 Known Limitations... 5 Informatica Global Customer Support...

defacto installation guide

How, What, and Where of Data Warehouses for MySQL

LICENSE4J LICENSE ACTIVATION AND VALIDATION PROXY SERVER USER GUIDE

User Migration Tool. Note. Staging Guide for Cisco Unified ICM/Contact Center Enterprise & Hosted Release 9.0(1) 1

Active Directory LDAP Quota and Admin account authentication and management

Integrating VoltDB with Hadoop

A Generic Model for Querying Multiple Databases in a Distributed Environment Using JDBC and an Uniform Interface

Architecting the Future of Big Data

PostgreSQL as an integrated data analysis platform

Bruce Momjian June, Postgres Plus Technical Overview

Accessing External Databases from Mobile Applications

"SQL Database Professional " module PRINTED MANUAL

MIGRATING TO AVALANCHE 5.0 WITH MS SQL SERVER

FileMaker 11. ODBC and JDBC Guide

WebSpy Vantage Ultimate 2.2 Web Module Administrators Guide

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

The manual contains complete instructions on 'converting' your data to version 4.21.

AWS Schema Conversion Tool. User Guide Version 1.0

Dell KACE K1000 System Management Appliance Version 5.4. Service Desk Administrator Guide

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

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

Installation Guide. Release 3.1

CHAPTER 5 INTELLIGENT TECHNIQUES TO PREVENT SQL INJECTION ATTACKS

halfile Server Move Contents Overview Moving SQL Server databases to a new server

Aradial Installation Guide

Transcription:

PostgreSQL at the centre of your dataverse! PGBR 2011! Presented by Dave Page! 3 rd November 2011! EnterpriseDB, Postgres Plus and Dynatune are trademarks of EnterpriseDB Corporation. Other names may be trademarks of their respective owners. 2010. All rights reserved. 1

Who is Dave Page?! u Lead developer of pgadmin u postgresql.org webmaster and sysadmin u Senior Software Architect at EnterpriseDB, responsible for: PostgreSQL and component Installers! Postgres Enterprise Manager! Postgres Plus Standard Server! PostgreSQL Solution Pack! u Member of PostgreSQL Core Team! PostgreSQL Europe Board of Directors! PostgreSQL Community Association of Canada Board of Directors! 2

Centre of my dataverse?! u Embarrassing marketing moment Image credit: NASA/JPL-Caltech/ESA/Harvard-Smithsonian CfA 3

Why?! u Application integration u Cross database/application reporting u Data migration u Data sharding u Database federation 4

About SQL/MED! 5

SQL/MED! u SQL standard for Management of External Data u Defined in ISO/IEC 9075-9:2003 u Specifies how external data sources are accessed from an SQL database 6

Before SQL/MED! u Import data into [temporary] tables: COPY! psql scripts! Custom loader programs! u Stored functions/procedures: PL/Proxy primarily intended for advanced partitioning! DBI:Link Perl project that uses views, functions and rules to make remote tables appear local! dblink Contrib module allowing access to remote PostgreSQL servers! 7

Disadvantages! u Custom loader/management code may be required u May need need to run batch tasks for incremental imports u Data may not be presented as relations, but functions u Custom stored functions may be required u Different data sources may have different interfaces 8

With SQL/MED! u Data is presented to the user like any other table or relation u Standardised set of object types and configuration commands used to setup and configure a remote data source u Integrated credential management ensures usernames and passwords can be managed securely, for each role u Deep integration with PostgreSQL s planner for improved query planning and optimisation 9

Current Limitations! u Data is read only u Planner limitations: No defined API for qual (WHERE clause) push down! No join push down! u API deficiency: no simple way to pass complex data from the planner callback to the executor callbacks 10

Using SQL/MED! 11

SQL Objects Foreign Data Wrapper! u Also known as an FDW u Defines the type or remote data source u Consists of: Handler function! Validator function (optional)! u Refers to both the SQL object, and less formally, the binary code that implements the interface to the remote data source 12

Relational DBMS FDWs! u MySQL Written by Dave Page! https://github.com/dpage/mysql_fdw! u ODBC Written by Zheng Yang (GSoC project)! https://github.com/zhengyang/odbc_fdw! u Oracle Written by Laurenz Albe! http://pgfoundry.org/projects/oracle-fdw! 13

NoSQL FDWs! u CouchDB Written by Zheng Yang (GSoC project)! https://github.com/zhengyang/couchdb_fdw! u Redis Written by Dave Page! Includes experimental qual pushdown for key values! https://github.com/dpage/redis_fdw! 14

File FDWs! u CSV Included as an extension with PostgreSQL 9.1! http://www.postgresql.org/docs/9.1/static/file-fdw.html! u Text Array Written by Andrew Dunstan! Presents [ragged] CSV files as text[] data! https://github.com/adunstan/file_text_array_fdw!! 15

Other FDWs! u LDAP Written by Dickson S. Guedes! https://github.com/guedes/ldap_fdw! u Twitter Written by Hitoshi Harada! https://github.com/umitanuki/twitter_fdw!! 16

Creating an FDW! u Create the functions: CREATE FUNCTION mysql_fdw_handler() RETURNS fdw_handler AS '$libdir/mysql_fdw' LANGUAGE C STRICT; CREATE FUNCTION mysql_fdw_validator(text[], oid) RETURNS void AS '$libdir/mysql_fdw' LANGUAGE C STRICT; 17

Creating an FDW! u Create the FDW object: CREATE FOREIGN DATA WRAPPER mysql_fdw HANDLER mysql_fdw_handler VALIDATOR mysql_fdw_validator; 18

Creating an FDW! u Or use PostgreSQL 9.1 s EXTENSIONs mechanism: CREATE EXTENSION mysql_fdw; 19

SQL Objects Foreign Server! u Defines a specific server or source of data, for example: A PostgreSQL database! A MySQL server! A Twitter account! A delimited file! u Each server uses one FDW. One FDW supports multiple servers. 20

Creating a Foreign Server! u Create the foreign server object: CREATE SERVER mysql_svr FOREIGN DATA WRAPPER mysql_fdw OPTIONS (address '127.0.0.1', port '3306'); u mysql_fdw supports the following server options: address The hostname or IP address of the MySQL server (default: 127.0.0.1)! port The port number that the MySQL server is listening on (default: 3306)! 21

SQL Objects Foreign Table! u Defines a table representing data on a foreign server, e.g: A PostgreSQL table or view! A delimited file! An SQL query against a MySQL database! u Each table uses one foreign server. Each server supports multiple tables u The Foreign Table object may be used in PostgreSQL as a read-only table, e.g: SELECT * FROM foreign f JOIN local l ON (f.id = l.id) ORDER BY f.name 22

Creating a Foreign Table! u Create the foreign table object: CREATE FOREIGN TABLE tbl (c1 text, c2 text) SERVER mysql_svr; u mysql_fdw supports the following table options: database The name of the MySQL database (optional)! query An SQL query to return the desired data! table The name of a table (quoted and qualified if needed) containing the desired data! Note: Either table or query must be specified, but not both. 23

SQL Objects User Mapping! u Defines security information used to connect to a foreign server u Other options may be specified, if the FDW supports it u Each user mapping applies to one server. Each server supports multiple user mappings u User mappings may be defined for PUBLIC or individual roles 24

Creating a User Mapping! u Create the user mapping object: CREATE USER MAPPING FOR dpage SERVER mysql_svr OPTIONS (username 'dpage', password 'Foo'); u mysql_fdw supports the following user mapping options: username the username to use to connect to the MySQL server! password the password corresponding to the username specified! 25

Writing FDWs! 26

Requirements SQL Functions! u Handler function Must be written in C! Provides pointers to callback functions in the FDW! u Validator function Must be written in C! Optional! Validates options for:! Foreign Data Wrapper! Foreign Servers! Foreign Tables! User Mappings! 27

Handler Function (pseudo code)! /* * Foreign-data wrapper handler function: return a struct with pointers * to my callback routines. */ Datum mysql_fdw_handler(pg_function_args) { FdwRoutine *fdwroutine = makenode(fdwroutine); fdwroutine->planforeignscan = mysqlplanforeignscan; fdwroutine->explainforeignscan = mysqlexplainforeignscan; fdwroutine->beginforeignscan = mysqlbeginforeignscan; fdwroutine->iterateforeignscan = mysqliterateforeignscan; fdwroutine->rescanforeignscan = mysqlrescanforeignscan; fdwroutine->endforeignscan = mysqlendforeignscan; } PG_RETURN_POINTER(fdwroutine); 28

Requirements SQL Functions! u Handler function Must be written in C! Provides pointers to callback functions in the FDW! u Validator function Must be written in C! Optional! Validates options for:! Foreign Data Wrapper! Foreign Servers! Foreign Tables! User Mappings! 29

Validator Function (pseudo code)! /* * This tends to be a long and boring function, so here s some pseudo code * instead. See https://github.com/dpage/mysql_fdw/blob/master/mysql_fdw.c * for a working example. */ Datum mysql_fdw_validator(pg_function_args) { List *options_list = untransformreloptions(pg_getarg_datum(0)); Oid catalog = PG_GETARG_OID(1); /* Object type table, user mapping etc. */ foreach(option, options_list) { if(!mysqlisvalidoption(option, catalog) ereport(error, (errcode(errcode_fdw_invalid_option_name), errmsg("invalid option \"%s\"", option->name))); } } /* If the option is valid, we may also want to validate the value */ 30

Requirements API Functions! u PlanForeignScan Plans the foreign scan on the remote server! May or may not actually do anything remotely! Returns cost estimates to the planner! u ExplainForeignScan Optionally adds additional data to EXPLAIN output! u BeginForeignScan Performs initialisation required for the foreign scan! 31

PlanForeignScan (pseudo code)! static FdwPlan * mysqlplanforeignscan(oid foreigntableid, PlannerInfo *root, RelOptInfo *baserel) { /* Connect to the remote server */ MYSQL *conn = mysql_connect(server, port, username, password); /* Get statistics for the remote scan */ rows = mysql_query( SELECT count(*) FROM table ); /* Set the number of rows in the relation */ baserel->rows = rows; /* Calculate a startup cost for the scan */ fdwplan->startup_cost = 10; if (!IsLocal(server)) fdwplan->startup_cost += 15; /* Finally, calculate the total cost */ fdwplan->total_cost = rows + fdwplan->startup_cost; } return fdwplan; 32

Requirements API Functions! u PlanForeignScan Plans the foreign scan on the remote server! May or may not actually do anything remotely! Returns cost estimates to the planner! u ExplainForeignScan Optionally adds additional data to EXPLAIN output! u BeginForeignScan Performs initialisation required for the foreign scan! 33

ExplainForeignScan (pseudo code)! static void mysqlexplainforeignscan(foreignscanstate *node, ExplainState *es) { /* Give some possibly useful info about startup costs, if needed */ } if (es->costs) { if (IsLocal(server)) } ExplainPropertyLong("Local server startup cost", 10, es); else ExplainPropertyLong("Remote server startup cost", 25, es); 34

Requirements API Functions! u PlanForeignScan Plans the foreign scan on the remote server! May or may not actually do anything remotely! Returns cost estimates to the planner! u ExplainForeignScan Optionally adds additional data to EXPLAIN output! u BeginForeignScan Performs initialisation required for the foreign scan! 35

BeginForeignScan (pseudo code)! static void mysqlbeginforeignscan(foreignscanstate *node, int eflags) { /* Connect to the remote server */ MYSQL *conn = mysql_connect(server, port, username, password); /* Build the remote SQL query */ query = sprintf(query, SELECT * FROM %s, table); /* Stash away the state info for use by other API functions */ festate = (MySQLFdwExecutionState *) palloc(sizeof(mysqlfdwexecutionstate)); node->fdw_state = (void *) festate; festate->conn = conn; festate->query = query; } /* This will store the remote query result */ festate->result = NULL; 36

Requirements API Functions! u IterateForeignScan Begin executing the foreign scan on first invocation! Returns one tuple per call! u ReScanForeignScan Reset the scan to start again from the beginning! u EndForeignScan Complete the foreign scan! Release resources! 37

IterateForeignScan (pseudo code)! static TupleTableSlot * mysqliterateforeignscan(foreignscanstate *node) { MySQLFdwExecutionState *festate = (MySQLFdwExecutionState *) node->fdw_state; TupleTableSlot *slot = node->ss.ss_scantupleslot; /* Execute the query, if required */ if (!festate->result) festate->result = mysql_query(festate->conn, festate->query); /* Get the next row from the remote server */ row = mysql_fetch_row(festate->result); /* If there s a row, convert to a tuple and store it in the slot */ if (row) { ConvertMySqlRowToTuple(row, tuple); ExecStoreTuple(tuple, slot); } } return slot; 38

Requirements API Functions! u IterateForeignScan Begin executing the foreign scan on first invocation! Returns one tuple per call! u ReScanForeignScan Reset the scan to start again from the beginning! u EndForeignScan Complete the foreign scan! Release resources! 39

ReScanForeignScan (pseudo code)! static void mysqlrescanforeignscan(foreignscanstate *node) { MySQLFdwExecutionState *festate = (MySQLFdwExecutionState *) node->fdw_state; } /* Reset the scan so it can start over */ mysql_free_result(festate->result); festate->result = NULL; 40

Requirements API Functions! u IterateForeignScan Begin executing the foreign scan on first invocation! Returns one tuple per call! u ReScanForeignScan Reset the scan to start again from the beginning! u EndForeignScan Complete the foreign scan! Release resources! 41

EndForeignScan (pseudo code)! static void mysqlrescanforeignscan(foreignscanstate *node) { MySQLFdwExecutionState *festate = (MySQLFdwExecutionState *) node->fdw_state; /* Cleanup the query string */ pfree(festate->query); festate->query = NULL; /* Cleanup the scan result */ mysql_free_result(festate->result); festate->result = NULL; /* Cleanup the remote connection */ mysql_close(festate->conn); festate->conn = NULL; } /* Cleanup the FDW state */ pfree(festate); festate = NULL; 42

Using FDWs! 43

Create the Objects! raptor:pgsql91 dpage$ bin/psql fdw psql (9.1.0) Type "help" for help. fdw=# CREATE EXTENSION mysql_fdw; CREATE EXTENSION 44

Create the Objects! raptor:pgsql91 dpage$ bin/psql fdw psql (9.1.0) Type "help" for help. fdw=# CREATE EXTENSION mysql_fdw; CREATE EXTENSION fdw=# CREATE SERVER mysql_svr fdw-# FOREIGN DATA WRAPPER mysql_fdw fdw-# OPTIONS (address '127.0.0.1', port '3306'); CREATE SERVER 45

Create the Objects! raptor:pgsql91 dpage$ bin/psql fdw psql (9.1.0) Type "help" for help. fdw=# CREATE EXTENSION mysql_fdw; CREATE EXTENSION fdw=# CREATE SERVER mysql_svr fdw-# FOREIGN DATA WRAPPER mysql_fdw fdw-# OPTIONS (address '127.0.0.1', port '3306'); CREATE SERVER fdw=# CREATE FOREIGN TABLE employees ( fdw(# fdw(# fdw(# id integer, name text, address text) fdw-# SERVER mysql_svr fdw-# OPTIONS (table 'hr.employees'); CREATE FOREIGN TABLE 46

Create the Objects! fdw=# CREATE EXTENSION mysql_fdw; CREATE EXTENSION fdw=# CREATE SERVER mysql_svr fdw-# FOREIGN DATA WRAPPER mysql_fdw fdw-# OPTIONS (address '127.0.0.1', port '3306'); CREATE SERVER fdw=# CREATE FOREIGN TABLE employees ( fdw(# id integer, fdw(# name text, fdw(# address text) fdw-# SERVER mysql_svr fdw-# OPTIONS (table 'hr.employees'); CREATE FOREIGN TABLE fdw=# CREATE FOREIGN TABLE overtime_2010 ( fdw(# id integer, fdw(# employee_id integer, fdw(# hours integer) fdw-# SERVER mysql_svr fdw-# OPTIONS (query 'SELECT id, employee_id, hours FROM hr.overtime WHERE year = 2010;'); CREATE FOREIGN TABLE 47

Run a Query! fdw=# CREATE FOREIGN TABLE employees ( fdw(# id integer, fdw(# name text, fdw(# address text) fdw-# SERVER mysql_svr fdw-# OPTIONS (table 'hr.employees'); CREATE FOREIGN TABLE fdw=# CREATE FOREIGN TABLE overtime_2010 ( fdw(# id integer, fdw(# employee_id integer, fdw(# hours integer) fdw-# SERVER mysql_svr fdw-# OPTIONS (query 'SELECT id, employee_id, hours FROM hr.overtime WHERE year = 2010;'); CREATE FOREIGN TABLE fdw=# SELECT * FROM employees; id name address ----+-------------+----------------------------- 1 Dave Page 123 High Street, Oxford 2 John Smith 54 Church Lane, Glasgow 3 Fred Bloggs 3b Grouse Court, Birmingham (3 rows) 48

Explain a Query! fdw=# CREATE FOREIGN TABLE overtime_2010 ( fdw(# id integer, fdw(# employee_id integer, fdw(# hours integer) fdw-# SERVER mysql_svr fdw-# OPTIONS (query 'SELECT id, employee_id, hours FROM hr.overtime WHERE year = 2010;'); CREATE FOREIGN TABLE fdw=# SELECT * FROM employees; id name address ----+-------------+----------------------------- 1 Dave Page 123 High Street, Oxford 2 John Smith 54 Church Lane, Glasgow 3 Fred Bloggs 3b Grouse Court, Birmingham (3 rows) fdw=# EXPLAIN SELECT * FROM employees; QUERY PLAN ---------------------------------------------------------------- Foreign Scan on employees (cost=10.00..13.00 rows=3 width=68) Local server startup cost: 10 MySQL query: SELECT * FROM hr.employees (3 rows) 49

Run another Query! fdw=# SELECT * FROM employees; id name address ----+-------------+----------------------------- 1 Dave Page 123 High Street, Oxford 2 John Smith 54 Church Lane, Glasgow 3 Fred Bloggs 3b Grouse Court, Birmingham (3 rows) fdw=# EXPLAIN SELECT * FROM employees; QUERY PLAN ---------------------------------------------------------------- Foreign Scan on employees (cost=10.00..13.00 rows=3 width=68) Local server startup cost: 10 MySQL query: SELECT * FROM hr.employees (3 rows) fdw=# SELECT e.id, e.name, hours FROM employees e LEFT OUTER JOIN overtime_2010 o ON (e.id = o.employee_id); id name hours ----+-------------+------- 1 Dave Page 23 2 John Smith 3 Fred Bloggs 14 (3 rows) 50

Explain another Query! id name hours ----+-------------+------- 1 Dave Page 23 2 John Smith 3 Fred Bloggs 14 (3 rows) fdw=# EXPLAIN SELECT e.id, e.name, hours FROM employees e LEFT OUTER JOIN overtime_2010 o ON (e.id = o.employee_id); QUERY PLAN ----------------------------------------------------------------------------------- ----------- Nested Loop Left Join (cost=20.00..25.09 rows=3 width=40) Join Filter: (e.id = o.employee_id) -> Foreign Scan on employees e (cost=10.00..13.00 rows=3 width=36) Local server startup cost: 10 MySQL query: SELECT * FROM hr.employees -> Materialize (cost=10.00..12.01 rows=2 width=8) -> Foreign Scan on overtime_2010 o (cost=10.00..12.00 rows=2 width=8) Local server startup cost: 10 MySQL query: SELECT id, employee_id, hours FROM hr.overtime WHERE year = 2010; (9 rows) 51

Questions? Email: dpage@pgadmin.org Twitter: @pgsnake! 52

Thank you!! 53