40
Major Features: Postgres 9.5 BRUCE MOMJIAN POSTGRESQL is an open-source, full-featured relational database. This presentation gives an overview of the Postgres 9.5 release. Creative Commons Attribution License http://momjian.us/presentations Last updated: November, 2015 1 / 40

Overview of Postgres 9.5

Embed Size (px)

Citation preview

Page 1: Overview of Postgres 9.5

Major Features: Postgres 9.5

BRUCE MOMJIAN

POSTGRESQL is an open-source, full-featured relational database.This presentation gives an overview of the Postgres 9.5 release.Creative Commons Attribution License http://momjian.us/presentations

Last updated: November, 2015

1 / 40

Page 2: Overview of Postgres 9.5

PostgreSQL the database…

◮ Open Source Object Relational DBMS since 1996

◮ Distributed under the PostgreSQL License

◮ Similar technical heritage as Oracle, SQL Server & DB2

◮ However, a strong adherence to standards (ANSI-SQL 2008)

◮ Highly extensible and adaptable design

◮ Languages, indexing, data types, etc.◮ E.g. PostGIS, JSONB, SQL/MED

◮ Extensive use throughout the world for applications andorganizations of all types

◮ Bundled into Red Hat Enterprise Linux, Ubuntu, CentOSand Amazon Linux

Major Features: Postgres 9.5 2 / 40

Page 3: Overview of Postgres 9.5

PostgreSQL the community…

◮ Independent community led by a Core Team of six

◮ Large, active and vibrant community

◮ www.postgresql.org

◮ Downloads, Mailing lists, Documentation

◮ Sponsors sampler:

◮ Google, Red Hat, VMWare, Skype, Salesforce, HP andEnterpriseDB

◮ http://www.postgresql.org/community/

Major Features: Postgres 9.5 3 / 40

Page 4: Overview of Postgres 9.5

EnterpriseDB the company…◮ Worldwide leader of Postgres based products and services

founded in 2004

◮ Customers include 63 of the Fortune 500 and 137 of theForbes Global 2000

◮ 2,700 customers range across many industries includingfinancial, healthcare, government, retail, telcommunications,and insurance

◮ Enterprise offerings:

◮ PostgreSQL Support, Services and Training◮ Postgres Plus Advanced Server with Oracle Compatibility◮ Tools for Monitoring, Replication, HA, Backup & Recovery

Community

◮ Citizenship

◮ Contributor of key features: Materialized Views, JSON, &more

◮ Nine community members on staff

Major Features: Postgres 9.5 4 / 40

Page 5: Overview of Postgres 9.5

EnterpriseDB the company…

Major Features: Postgres 9.5 5 / 40

Page 6: Overview of Postgres 9.5

EnterpriseDB Is a Leader

The Gartner report, Magic Quadrant for Operational Database Management Systems, byDonald Feinberg, Merv Adrian, Nick Heudecker, Adam Ronthal, and Terilyn Palanca waspublished October 12, 2015.

Major Features: Postgres 9.5 6 / 40

Page 7: Overview of Postgres 9.5

9.5 Feature Outline

1. INSERT … ON CONFLICT, also known as “UPSERT”

2. Block-Range Indexes (BRIN) which enable compact indexing ofvery large tables

3. Analytic operations GROUPING SETS, CUBE, and ROLLUP

4. Row-Level Security (RLS)

5. In-memory sorting and hashing performance improvements

6. Multi-core and large memory scalability improvements

7. Automated management of the number of WAL files

8. Additional JSONB data manipulation functions and operators

9. Enhancements to Foreign Data Wrappers

10. Allow Indexed PostGIS LIMIT distance calculationswithout CTEs

To be released in 2015, full item list athttp://www.postgresql.org/docs/devel/static/release-9-5.html

Major Features: Postgres 9.5 7 / 40

Page 8: Overview of Postgres 9.5

1. INSERT … ON CONFLICT

◮ Turns a conflicting INSERT into an UPDATE

◮ Works for VALUES and SELECT as a row source

◮ Handles concurrent operations without errors

◮ Is row-oriented, unlike MERGE, which is batch-oriented

◮ Does not have the problems associated with theUPSERT/MERGE implementations of other vendors(http://www.pgcon.org/2014/schedule/attachments/327_upsert_weird.pdf)

Major Features: Postgres 9.5 8 / 40

Page 9: Overview of Postgres 9.5

INSERT … ON CONFLICT Example

CREATE TABLE ins_update_test (x INTEGER PRIMARY KEY);

INSERT INTO ins_update_test VALUES (1);

INSERT INTO ins_update_test VALUES (1);

ERROR: duplicate key value violates unique constraint"ins_update_test_pkey"DETAIL: Key (x)=(1) already exists.

Major Features: Postgres 9.5 9 / 40

Page 10: Overview of Postgres 9.5

INSERT … ON CONFLICT Example

INSERT INTO ins_update_test VALUES (1)ON CONFLICT DO NOTHING;

INSERT 0 0

INSERT INTO ins_update_test VALUES (1)

ON CONFLICT (x) DO UPDATE SET x = 2;

INSERT 0 1

SELECT * FROM ins_update_test;x---2

Major Features: Postgres 9.5 10 / 40

Page 11: Overview of Postgres 9.5

INSERT … ON CONFLICT … EXCLUDED Example

CREATE TABLE customer (cust_id INTEGER PRIMARY KEY, name TEXT);

INSERT INTO customer VALUES (100, ’Big customer’);

INSERT INTO customer VALUES (100, ’Non-paying customer’);ERROR: duplicate key value violates unique constraint"customer_pkey"

DETAIL: Key (cust_id)=(100) already exists.

INSERT INTO customer VALUES (100, ’Non-paying customer’)

ON CONFLICT (cust_id) DO UPDATE SET name = EXCLUDED.name;

SELECT * FROM customer;cust_id | name---------+---------------------

100 | Non-paying customer

Major Features: Postgres 9.5 11 / 40

Page 12: Overview of Postgres 9.5

INSERT … ON CONFLICT with SELECT

CREATE TABLE merge (x INTEGER PRIMARY KEY);

INSERT INTO merge VALUES (1), (3), (5);

INSERT INTO merge SELECT * FROM generate_series(1, 5);ERROR: duplicate key value violates unique constraint"merge_pkey"DETAIL: Key (x)=(1) already exists

Major Features: Postgres 9.5 12 / 40

Page 13: Overview of Postgres 9.5

INSERT … ON CONFLICT with SELECT

INSERT INTO merge SELECT * FROM generate_series(1, 5)

ON CONFLICT DO NOTHING;

SELECT * FROM merge;x---13524

Major Features: Postgres 9.5 13 / 40

Page 14: Overview of Postgres 9.5

INSERT … ON CONFLICT … UPDATE with SELECT

CREATE TABLE merge2 (x INTEGER PRIMARY KEY, status TEXT);

INSERT INTO merge2 VALUES (1, ’old’), (3, ’old’), (5, ’old’);

INSERT INTO merge2 SELECT *, ’new’ FROM generate_series(2, 5)

ON CONFLICT (x) DO

UPDATE SET status = ’conflict’;

SELECT * FROM merge2;x | status---+----------1 | old2 | new3 | conflict4 | new5 | conflict

Major Features: Postgres 9.5 14 / 40

Page 15: Overview of Postgres 9.5

2. Block-Range Indexes (BRIN)

◮ Tiny indexes designed for large tables

◮ Minimum/maximum values stored for a range of blocks(default 1MB, 128 8k pages)

◮ Allows skipping large sections of the table that cannotcontain matching values

◮ Ideally for naturally-ordered tables, e.g. insert-only tables arechronologically ordered

◮ Index is 0.003% the size of the heap

◮ Indexes are inexpensive to update

◮ Index every column at little cost

◮ Slower lookups than btree

Major Features: Postgres 9.5 15 / 40

Page 16: Overview of Postgres 9.5

Block-Range Indexes (BRIN) ExampleCREATE TABLE brin_example ASSELECT generate_series(1,100000000) AS id;

CREATE INDEX btree_index ON brin_example(id);CREATE INDEX brin_index ON brin_example USING brin(id);

SELECT relname, pg_size_pretty(pg_relation_size(oid))FROM pg_classWHERE relname LIKE ’brin_%’ OR relname = ’btree_index’ORDER BY relname;

relname | pg_size_pretty--------------+----------------brin_example | 3457 MBbtree_index | 2142 MBbrin_index | 104 kB

http://michael.otacoo.com/postgresql-2/postgres-9-5-feature-highliMajor Features: Postgres 9.5 16 / 40

Page 17: Overview of Postgres 9.5

3. Analytic Operations GROUPING SETS,CUBE, and ROLLUP

◮ Allows specification of multiple GROUP BY combinations in asingle query

◮ Avoids the need for UNION ALL and recomputation

◮ Empty fields are left NULL

Major Features: Postgres 9.5 17 / 40

Page 18: Overview of Postgres 9.5

Employee Table

SELECT * FROM employee ORDER BY name;name | office | department-------+--------+------------Jill | PHL | MarketingLilly | SFO | SalesMark | PHL | MarketingNancy | PHL | SalesSam | SFO | SalesTim | PHL | Shipping

Major Features: Postgres 9.5 18 / 40

Page 19: Overview of Postgres 9.5

GROUP BY Example

SELECT office, COUNT(*)FROM employeeGROUP BY office;office | count--------+-------SFO | 2PHL | 4

SELECT department, COUNT(*)FROM employeeGROUP BY department;department | count------------+-------Marketing | 2Shipping | 1Sales | 3

Major Features: Postgres 9.5 19 / 40

Page 20: Overview of Postgres 9.5

GROUP BY with UNION ALL

SELECT office, NULL as department, COUNT(*)FROM employeeGROUP BY officeUNION ALLSELECT NULL as office, department, COUNT(*)FROM employeeGROUP BY departmentORDER BY 1;office | department | count--------+------------+-------PHL | | 4SFO | | 2

| Marketing | 2| Shipping | 1| Sales | 3

Major Features: Postgres 9.5 20 / 40

Page 21: Overview of Postgres 9.5

GROUPING SETS Example

SELECT office, department, COUNT(*)FROM employeeGROUP BY GROUPING SETS (office, department)ORDER BY office, department;office | department | count--------+------------+-------PHL | | 4SFO | | 2

| Marketing | 2| Sales | 3| Shipping | 1

Major Features: Postgres 9.5 21 / 40

Page 22: Overview of Postgres 9.5

ROLLUP Example

SELECT office, department, COUNT(*)FROM employeeGROUP BY ROLLUP (office, department)ORDER BY office, department;office | department | count--------+------------+-------PHL | Marketing | 2PHL | Sales | 1PHL | Shipping | 1PHL | | 4SFO | Sales | 2SFO | | 2

| | 6

Major Features: Postgres 9.5 22 / 40

Page 23: Overview of Postgres 9.5

CUBE Example

SELECT office, department, COUNT(*)FROM employeeGROUP BY CUBE (office, department)ORDER BY office, department;office | department | count--------+------------+-------PHL | Marketing | 2PHL | Sales | 1PHL | Shipping | 1PHL | | 4SFO | Sales | 2SFO | | 2

| Marketing | 2| Sales | 3| Shipping | 1| | 6

Major Features: Postgres 9.5 23 / 40

Page 24: Overview of Postgres 9.5

GROUPING SETS Equivalent of CUBE

SELECT office, department, COUNT(*)FROM employeeGROUP BY GROUPING SETS

((office, department), office, department, ())ORDER BY office, department;office | department | count--------+------------+-------PHL | Marketing | 2PHL | Sales | 1PHL | Shipping | 1PHL | | 4SFO | Sales | 2SFO | | 2

| Marketing | 2| Sales | 3| Shipping | 1| | 6

Major Features: Postgres 9.5 24 / 40

Page 25: Overview of Postgres 9.5

4. Row-Level Security (RLS)

◮ Allows SELECT, INSERT, UPDATE, OR DELETE permissioncontrol over existing rows with USING expression

◮ Also INSERT or UPDATE control over added and modifiedrows with CHECK expression

◮ Expressions can contain checks for the current user,subqueries, time comparisons, and function calls

◮ Enabled with GUC row_security, CREATE POLICY, and ALTER

TABLE … ENABLE ROW LEVEL SECURITY

Major Features: Postgres 9.5 25 / 40

Page 26: Overview of Postgres 9.5

Row-Level Security ExampleTable Setup

SHOW row_security;row_security--------------on

CREATE TABLE orders (id INTEGER, product TEXT,entered_by TEXT);

ALTER TABLE orders ENABLE ROW LEVEL SECURITY;

CREATE POLICY orders_control ON orders FOR ALL TO PUBLICUSING (entered_by = CURRENT_USER);

GRANT ALL ON TABLE orders TO PUBLIC;

Major Features: Postgres 9.5 26 / 40

Page 27: Overview of Postgres 9.5

Row-Level Security ExampleUser Setup

CREATE USER emp1;

CREATE USER emp2;

SET SESSION AUTHORIZATION emp1;

INSERT INTO orders VALUES (101, ’fuse’, CURRENT_USER);

SET SESSION AUTHORIZATION emp2;

INSERT INTO orders VALUES (102, ’bolt’, CURRENT_USER);

Major Features: Postgres 9.5 27 / 40

Page 28: Overview of Postgres 9.5

Row-Level Security ExampleTesting

SET SESSION AUTHORIZATION postgres;

SELECT * FROM orders;id | product | entered_by-----+---------+------------101 | fuse | emp1102 | bolt | emp2

Major Features: Postgres 9.5 28 / 40

Page 29: Overview of Postgres 9.5

Row-Level Security ExampleTesting

SET SESSION AUTHORIZATION emp1;

SELECT * FROM orders;id | product | entered_by-----+---------+------------101 | fuse | emp1

SET SESSION AUTHORIZATION emp2;

SELECT * FROM orders;id | product | entered_by-----+---------+------------102 | bolt | emp2

Major Features: Postgres 9.5 29 / 40

Page 30: Overview of Postgres 9.5

5. In-Memory Sorting andHashing Performance Improvements

◮ Allow VARCHAR(), TEXT and NUMERIC() to use theabbreviated sorting optimization

◮ Use memcmp()as quick string equality checks before collationcomparisons

◮ Decrease the average number of hash entries per bucket from10 to 1

◮ Pre-allocate the maximum number of hash buckets in caseswhere we are likely to use multiple work_mem-sized batches

◮ Allow CREATE INDEX, REINDEX, and CLUSTER to use inlinedsorting

◮ Allow use of 128-bit accumulators for aggregatecomputations

Major Features: Postgres 9.5 30 / 40

Page 31: Overview of Postgres 9.5

6. Multi-Core and Large MemoryScalability Improvements

◮ Improve concurrency of shared buffer replacement

◮ Reduce the number of page locks and pins during index scans

◮ Make backend local tracking of buffer pins memory efficient

◮ Improve lock scalability on multi-socket systems

◮ Increase the number of shared buffer mapping hash tableentries from 16 to 128

◮ Allow searching for a free shared buffer to use minimallocking

◮ Force buffer descriptors to be CPU-cache aligned (128 bytes)

◮ Reduce btree page pinning

Major Features: Postgres 9.5 31 / 40

Page 32: Overview of Postgres 9.5

7. Automated Management of the Number of WAL

Files

◮ New GUC variables min_wal_size and max_wal_size controlthe minimum and maximum size of the pg_xlog directory

◮ Previously checkpoint_segments controlled only themaximum directory size (previously WAL files were notremoved)

◮ Size specified in bytes, not segment files

◮ Allows use of additional WAL files only when needed

Major Features: Postgres 9.5 32 / 40

Page 33: Overview of Postgres 9.5

Management of WAL Files

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

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

���������

���������

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

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

���������

���������

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

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

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

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

���������

���������

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

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

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

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

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

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

���������

���������

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

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

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

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

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

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

111 1 1Begin 1

Rotate 2 22

2 2 2End 1

2 2 21 1

PostgreSQL Shared Buffer Cache Write−Ahead Log

Major Features: Postgres 9.5 33 / 40

Page 34: Overview of Postgres 9.5

8. Additional JSONB Data Manipulation Functionsand Operators

◮ Add jsonb_set(), which allows replacement of or additionto JSONB documents

◮ Allow removal of JSONB documents using the subtractionoperator

◮ Allow merging of JSONB documents using the concatenation(|| operator)

◮ Add function to remove null values from documents

Major Features: Postgres 9.5 34 / 40

Page 35: Overview of Postgres 9.5

9. Enhancements to Foreign Data Wrappers

◮ Add IMPORT FOREIGN SCHEMA to create a local tablematching the schema of a foreign table

◮ Allow foreign tables to be part of inheritance trees

◮ Allow CHECK constraints on foreign tables

◮ Add infrastructure for foreign table join pushdown

Major Features: Postgres 9.5 35 / 40

Page 36: Overview of Postgres 9.5

10. Allow Indexed PostGIS LIMIT

Distance Calculations without CTEs

◮ Nearest neighbor searches allow index lookups to return theclosest matches, e.g. return the 10 nearest points to a givenpoint

◮ Only the bounding boxes of two-dimensional objects areindexed, e.g. polygon, circle, line

◮ Previously LIMIT could not combine bounding box indexlookups with accurate calculations

◮ Now LIMIT bounding box index filtering can recheck usingaccurate distance calculations

◮ Workaround was to use a CTE with a 10x limit, then an outerquery to do accurate distance calculations

Major Features: Postgres 9.5 36 / 40

Page 37: Overview of Postgres 9.5

Pre-9.5 LIMIT Distance Example

WITH index_query AS (SELECT st_distance(geom,

’SRID=3005;POINT(1011102 450541)’) AS distance,parcel_id, address

FROM parcelsORDER BY geom <-> ’SRID=3005;POINT(1011102 450541)’LIMIT 100

)SELECT *FROM index_queryORDER BY distanceLIMIT 10;

http://boundlessgeo.com/2011/09/indexed-nearest-neighbour-search-in-postgis/

http://shisaa.jp/postset/postgis-postgresqls-spatial-partner-part-3.html

Major Features: Postgres 9.5 37 / 40

Page 38: Overview of Postgres 9.5

9.5 LIMIT Distance Example

SELECT st_distance(geom,’SRID=3005;POINT(1011102 450541)’) AS distance,

parcel_id, addressFROM parcelsORDER BY geom <-> ’SRID=3005;POINT(1011102 450541)’LIMIT 10

http://www.postgresonline.com/journal/archives/350-PostGIS-2.2-leveraging-power-of-PostgreSQL-9.5.html

http://postgis.net/docs/manual-dev/geometry_distance_knn.html

Major Features: Postgres 9.5 38 / 40

Page 39: Overview of Postgres 9.5

Additional Resources…

◮ Postgres Downloads:

◮ www.enterprisedb.com/downloads

◮ Product and Services information:

[email protected]

Major Features: Postgres 9.5 39 / 40

Page 40: Overview of Postgres 9.5

Conclusion

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

Major Features: Postgres 9.5 40 / 40