40
2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群: 100910388 PostgreSQL专业3群: 150657323 PostgreSQL专业4群: 461170054 QQ 交流群 信公众号 官方网站 新浪微博 Postgre中国用户会 欢迎投稿 : PostgresChina www.postgres.cn [email protected] 文档翻译 309292849 FaceBook Twitter China PostgreSQL User Group China PostgreSQL User Group

Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

2017 Postgres大象会 PGConf.CN 2017

PostgreSQL专业2群: 100910388PostgreSQL专业3群: 150657323PostgreSQL专业4群: 461170054

QQ交流群 微信公众号 官方网站新浪微博

Postgre中国用户会

欢迎投稿:

PostgresChina www.postgres.cn

[email protected]

文档翻译群 :309292849

FaceBook

Twitter

China PostgreSQL User Group

China PostgreSQL User Group

Page 2: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Making Postgres Central in Your Data Center

BRUCE MOMJIAN

This talk explores why Postgres is uniquely capable of functioningas a central database in enterprises. Title concept from Josh BerkusCreative Commons Attribution License http://momjian.us/presentations

Last updated: June, 2017

1 / 39

Page 3: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Outline

1. Object-relational (extensibility)

2. NoSQL

3. Data analytics

4. Foreign data wrappers (database federation)

5. Central role

2 / 39

Page 4: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

1. Object-Relational (Extensibility)

Object-relational databases like Postgres support support classesand inheritance, but most importantly, they define databasefunctionality as objects that can be easily manipulated.http://en.wikipedia.org/wiki/Object-relational_database

3 / 39

Page 5: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

How Is this Accomplished?

starelid

staattnum

staop

pg_statistic

oprleft

oprright

oprresult

oprcom

oprnegate

oprlsortop

oprrsortop

oprcode

oprrest

oprjoin

pg_operator

typrelid

typelem

typinput

typoutput

typbasetype

pg_type

prolang

prorettype

pg_proc

pg_rewrite

ev_class

datlastsysoid

pg_database

tgfoid

tgrelid

pg_trigger

inhrelid

pg_inherits

inhparent

pg_language

pg_namespacepg_depend pg_shadow

pg_aggregate

aggfinalfn

aggtransfn

aggfnoid

aggtranstype

castsource

casttarget

pg_cast

castfunc

pg_description

pg_constraint

contypid

pg_conversion

conproc

amopopr

amopclaid

pg_attribute

indexrelid

attnum

amopclaid

atttypid

indrelid

pg_attrdef

pg_group

adrelid

pg_index

adnum

pg_am

pg_amop

amgettuple

reltoastidxid

aminsert

reltoastrelid amcostestimate

amproc

ambeginscan

pg_amproc

amrescan

relfilenode

amendscan

relam

ammarkpos

reltype amrestrpos

pg_class

ambuild

opcdeftype

ambulkdelete

pg_opclass

attrelid

http://www.postgresql.org/docs/current/static/catalogs.html

4 / 39

Page 6: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Example: ISBN Data Type

CREATE EXTENSION isn;

\dTList of data types

Schema | Name | Description--------+--------+--------------------------------------------------public | ean13 | International European Article Number (EAN13)public | isbn | International Standard Book Number (ISBN)public | isbn13 | International Standard Book Number 13 (ISBN13)public | ismn | International Standard Music Number (ISMN)public | ismn13 | International Standard Music Number 13 (ISMN13)public | issn | International Standard Serial Number (ISSN)public | issn13 | International Standard Serial Number 13 (ISSN13)public | upc | Universal Product Code (UPC)

http://www.postgresql.org/docs/current/static/isn.html

5 / 39

Page 7: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

ISBN Behaves Just Like Built-In Types

\dTS…pg_catalog | integer | -2 billion to 2 billion integer, 4-byte storage…public | isbn | International Standard Book Number (ISBN)

6 / 39

Page 8: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

The System Catalog Entry for INTEGER

SELECT * FROM pg_type WHERE typname = ’int4’;-[ RECORD 1 ]--+---------typname | int4typnamespace | 11typowner | 10typlen | 4typbyval | ttyptype | btypcategory | Ntypispreferred | ftypisdefined | ttypdelim | ,typrelid | 0typelem | 0typarray | 1007typinput | int4intypoutput | int4outtypreceive | int4recvtypsend | int4sendtypmodin | -typmodout | -typanalyze | -typalign | itypstorage | ptypnotnull | ftypbasetype | 0

7 / 39

Page 9: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

The System Catalog Entry for ISBN

SELECT * FROM pg_type WHERE typname = ’isbn’;-[ RECORD 1 ]--+---------------typname | isbntypnamespace | 2200typowner | 10typlen | 8typbyval | ttyptype | btypcategory | Utypispreferred | ftypisdefined | ttypdelim | ,typrelid | 0typelem | 0typarray | 16405typinput | isbn_intypoutput | public.isn_outtypreceive | -typsend | -typmodin | -typmodout | -typanalyze | -typalign | dtypstorage | ptypnotnull | ftypbasetype | 0

8 / 39

Page 10: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Not Just Data Types, Languages

CREATE EXTENSION plpythonu;

\dLList of languages

Name | Owner | Trusted | Description-----------+----------+---------+------------------------------------------plpgsql | postgres | t | PL/pgSQL procedural languageplpythonu | postgres | f | PL/PythonU untrusted procedural language

http://www.postgresql.org/docs/current/static/plpython.html

9 / 39

Page 11: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Available Languages

◮ PL/Java

◮ PL/Perl

◮ PL/pgSQL (like PL/SQL)

◮ PL/PHP

◮ PL/Python

◮ PL/R (like SPSS)

◮ PL/Ruby

◮ PL/Scheme

◮ PL/sh

◮ PL/Tcl

◮ SPI (C)

http://www.postgresql.org/docs/current/static/external-pl.html

10 / 39

Page 12: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Specialized Indexing Methods

◮ BRIN

◮ BTREE

◮ Hash

◮ GIN (generalized inverted index)

◮ GiST (generalized search tree)

◮ SP-GiST (space-partitioned GiST)

http://www.postgresql.org/docs/current/static/indexam.html

11 / 39

Page 13: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Index Types Are Defined in the System Catalogs Too

SELECT amname FROM pg_am ORDER BY 1;amname--------brinbtreehashgingistspgist

http://www.postgresql.org/docs/current/static/catalog-pg-am.html

12 / 39

Page 14: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Operators Have Similar Flexibility

Operators are function calls with left and right arguments of specified types:

\doSSchema | Name | Left arg type | Right arg type | Result type | Description

pg_catalog | + | integer | integer | integer | add

\dfSSchema | Name | Result data type | Argument data types | Type

…pg_catalog | int4pl | integer | integer, integer | normal

13 / 39

Page 15: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Other Extensibility

◮ Casts are defined in pg_cast, int4(float8)

◮ Aggregates are defined in pg_aggregate, sum(int4)

14 / 39

Page 16: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Externally Developed Plug-Ins

◮ PostGIS (Geographical Information System)

◮ PL/v8 (server-side JavaScript)

◮ experimentation, e.g. full text search was originallyexternally developed

15 / 39

Page 17: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Offshoots of Postgres

◮ AsterDB

◮ Greenplum

◮ Informix

◮ Netezza

◮ ParAccel

◮ Postgres XC

◮ Redshift (Amazon)

◮ Truviso

◮ Vertica

◮ Yahoo! Everest

https://wiki.postgresql.org/wiki/PostgreSQL_derived_databaseshttp://de.slideshare.net/pgconf/elephant-roads-a-tour-of-postgres-forks

16 / 39

Page 18: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Offshoots of Postgres

https://raw.github.com/daamien/artwork/master/inkscape/PostgreSQL_timeline/timeline_postgresql.png

17 / 39

Page 19: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Plug-In Is Not a Bad Word

Many databases treat extensions as special cases, with seriouslimitations. Postgres built-ins use the same API as extensions, so llextensions operate just like built-in functionality.

18 / 39

Page 20: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Extensions and Built-In FacilitiesBehave the Same

ExtensionsPL/R

ISN

PostGIS

Postgres System Tables

int4

btree

sum()

PL/pgSQL

19 / 39

Page 21: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

2. NoSQL

SQL

20 / 39

Page 22: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

NoSQL Types

There is no single NoSQL technology. They all take differentapproaches and have different features and drawbacks:

◮ Key-value stores, e.g. Redis

◮ Document databases, e.g. MongoDB (JSON)

◮ Columnar stores: Cassandra

◮ Graph databases: Neo4j

21 / 39

Page 23: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Why NoSQL Exists

Generally, NoSQL is optimized for:

◮ Fast simple queries

◮ Auto-sharding

◮ Flexible schemas

22 / 39

Page 24: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

NoSQL Sacrifices

◮ A powerful query language

◮ A sophisticated query optimizer

◮ Data normalization

◮ Joins

◮ Referential integrity

◮ Durability

23 / 39

Page 25: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Are These Drawbacks Worth the Cost?

◮ Difficult Reporting Data must be brought to the client foranalysis, e.g. no aggregates or data analysis functions.Schema-less data requires complex client-side knowledge forprocessing

◮ Complex Application Design Without powerful querylanguage and query optimizer, the client software isresponsible for efficiently accessing data and for dataconsistency

◮ Durability Administrators are responsible for data retention

24 / 39

Page 26: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

When Should NoSQL Be Used?

◮ Massive write scaling is required, more than a single servercan provide

◮ Only simple data access pattern is required

◮ Additional resource allocation for development is acceptable

◮ Strong data retention or transactional guarantees are notrequired

◮ Unstructured duplicate data that greatly benefits fromcolumn compression

25 / 39

Page 27: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

When Should Relational Storage Be Used?

◮ Easy administration

◮ Variable workloads and reporting

◮ Simplified application development

◮ Strong data retention

26 / 39

Page 28: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

The Best of Both Worlds: Postgres

Postgres has many NoSQL features without the drawbacks:

◮ Schema-less data types, with sophisticated indexing support

◮ Transactional schema changes with rapid additional andremoval of columns

◮ Durability by default, but controllable per-table orper-transaction

27 / 39

Page 29: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Schema-Less Data: JSON

CREATE TABLE customer (id SERIAL, data JSON);

INSERT INTO customer VALUES (DEFAULT, ’{"name" : "Bill", "age" : 21}’);

SELECT data->’name’ FROM customer WHERE data->>’age’ = ’21’;?column?----------"Bill"

28 / 39

Page 30: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Easy Relational Schema Changes

ALTER TABLE customer ADD COLUMN status CHAR(1);BEGIN WORK;ALTER TABLE customer ADD COLUMN debt_limit NUMERIC(10,2);ALTER TABLE customer ADD COLUMN creation_date TIMESTAMP WITH TIME ZONE;ALTER TABLE customer RENAME TO cust;COMMIT;

29 / 39

Page 31: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

3. Data Analytics

◮ Aggregates

◮ Optimizer

◮ Server-side languages, e.g. PL/R

◮ Window functions

◮ Bitmap heap scans

◮ Tablespaces

◮ Data partitioning

◮ Materialized views

◮ Common table expressions (CTE)

◮ BRIN indexes

◮ GROUPING SETS, ROLLUP, CUBE

◮ Parallelism

◮ Sharding (in progress)http://www.slideshare.net/PGExperts/really-big-elephants-postgresql-dw-15833438http://wiki.postgresql.org/images/3/38/PGDay2009-EN-Datawarehousing_with_PostgreSQL.pdf

30 / 39

Page 32: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Read-Only Slaves for Analytics

Network

Data WarehouseMaster Server

/pg_xlog/pg_xlog

31 / 39

Page 33: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

4. Foreign Data Wrappers (Database Federation)

Foreign data wrappers (SQL MED) allow queries to read and writedata to foreign data sources. Foreign database support includes:

◮ CouchDB

◮ Informix

◮ MongoDB

◮ MySQL

◮ Neo4j

◮ Oracle

◮ Postgres

◮ Redis

The transfer of joins, aggregates, and sorts to foreign servers is not yetimplemented.http://www.postgresql.org/docs/current/static/ddl-foreign-data.htmlhttp://wiki.postgresql.org/wiki/Foreign_data_wrappers

32 / 39

Page 34: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Foreign Data Wrappers to Interfaces

◮ JDBC

◮ LDAP

◮ ODBC

33 / 39

Page 35: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Foreign Data Wrappers toNon-Traditional Data Sources

◮ Files

◮ HTTP

◮ AWS S3

◮ Twitter

34 / 39

Page 36: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Foreign Data Wrapper Example

CREATE SERVER postgres_fdw_test

FOREIGN DATA WRAPPER postgres_fdw

OPTIONS (host ’localhost’, dbname ’fdw_test’);

CREATE USER MAPPING FOR PUBLIC

SERVER postgres_fdw_test

OPTIONS (password ’’);

CREATE FOREIGN TABLE other_world (greeting TEXT)

SERVER postgres_fdw_test

OPTIONS (table_name ’world’);

\detList of foreign tablesSchema | Table | Server--------+-------------+-------------------public | other_world | postgres_fdw_test(1 row)

Foreign Postgres server name in red; foreign table name in blue

35 / 39

Page 37: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Read and Read/Write Data Sources

Postgres

ora_tab

tw_tab

mon_tabMongoDB

Twitter

Oracle

36 / 39

Page 38: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

5. Postgres Centrality

Postgres can rightly take a central place in the data center withits:

◮ Object-relation flexibility and extensibility

◮ NoSQL-like workloads

◮ Powerful data analytics capabilities

◮ Access to foreign data sources

No other database has all of these key components.

37 / 39

Page 39: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Postgres’s Central Role

Extensions

NoSQL

Postgres

WarehouseData

Foreign DataWrappers

Window Functions

Data Paritioning

Bitmap Scans Sharding

Oracle

Twitter

MongoDB

Easy DDL

JSON

PL/R

ISN

PostGIS

38 / 39

Page 40: Making Postgres Central in Your Data Center · 2017 Postgres大象会 PGConf.CN 2017 PostgreSQL专业2群:100910388 PostgreSQL专业3群:150657323 PostgreSQL专业4群:461170054

Conclusion

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

39 / 39