PostgreSQL - Object Relational Database

35

Transcript of PostgreSQL - Object Relational Database

Page 1: PostgreSQL - Object Relational Database
Page 2: PostgreSQL - Object Relational Database

Presenter: Mubashar Iqbal

Senior Software Engineer

Object Relational Database System

Page 3: PostgreSQL - Object Relational Database

Judging Criteria ??

Page 4: PostgreSQL - Object Relational Database

Fast

Page 5: PostgreSQL - Object Relational Database

Flexible

Page 6: PostgreSQL - Object Relational Database

Powerful

Page 7: PostgreSQL - Object Relational Database

Scalable

Page 8: PostgreSQL - Object Relational Database

Easy Deployment

Page 9: PostgreSQL - Object Relational Database

What’s in your mind ?

Page 10: PostgreSQL - Object Relational Database
Page 11: PostgreSQL - Object Relational Database

Introduction

The world's most advanced open source object-relational database system. The open

source Oracle. PostgreSQL has a large distributed developer and user community.

Community-owned with many companies involved.

Supported operating systems

● Linux

● Unix

● Mac OS X

● Solaris

● Windows

Native programming interfaces for:

C/C++, Java, .Net, Perl, Python, Ruby, PHP

Page 12: PostgreSQL - Object Relational Database

Development Priorities

● Designed by/for Database Administrators

● Data integrity

● Security

● Reliability

● Standards

● DB Features

● Performance

● Ease-of-use

● Programmer Features

Page 13: PostgreSQL - Object Relational Database

Most Common Uses

● ERP

● Data Warehouse

● Geographic

● OEM applications

● Network tools

● CRM

Page 14: PostgreSQL - Object Relational Database

Prominent users

● Yahoo! for web user behavioral analysis, storing two petabytes and claimed to be

the largest data warehouse using a heavily modified version of PostgreSQL

● Sony Online multiplayer online games.

● Reddit social news website.

● Skype VoIP application, central business databases.

● Sun xVM, Sun's virtualization and datacenter automation suite.

● MusicBrainz, open online music encyclopedia.

● MyYearbook social networking site.

● Instagram, a popular mobile photo sharing service

● Disqus, an online discussion and commenting service

Page 15: PostgreSQL - Object Relational Database

Features

● PostgreSQL often described as an open-source version of Oracle.

● BSD/MIT type license

● Reliability is PostgreSQL's top priority.

● Well-engineered, capable of supporting high-transaction and mission-critical

applications.

● Comprehensive documentation and manuals available for free online.

● Commercial support is available from independent vendors.

● PostgreSQL is fully ACID compliant.

● PostgreSQL is considered the solemn, full-featured, workhorse for transactional

enterprise applications, with strong ACID compliance.

Page 16: PostgreSQL - Object Relational Database

Features (contd..)

● PostgreSQL supports one storage engine.

● SSL encryption

● Online backup

● Point-in-time recovery: Restore to any time in the past.

● Regular expression

Page 17: PostgreSQL - Object Relational Database

Tools

● Psql: Command line front-end

● pgAdmin: GUI front-end

● phpPgadmin: Web based front-end

● MS ODBC

● MS Office + Postgres

● NaviCat: $$

● DeZign: $$

● EMS SQL Manager for PostgreSQL: $$

Page 18: PostgreSQL - Object Relational Database

Data Types

● Numeric Types

● Character Types

● Hierarchical Types

● Binary Data Types

● Geometric Types

● Network Address Types

● Text Search Types

● UUID Type

● XML Type

● JSON Type

● Arrays

● Composite Types

Page 19: PostgreSQL - Object Relational Database

Indexes

B-tree: B-trees can handle equality and range queries on data that can be sorted into

some ordering (<, <=,=,>=,>)

Hash: Hash indexes can only handle simple equality comparisons

GIN: GIN indexes are inverted indexes which can handle values that contain more than

one key, arrays for example, GIN operator classes for one-dimensional arrays

(<@,@>,=,&&)

GiST: Generalized Search Tree, it is a tree-structured access method and also known as

two-dimensional geometric data types (<@,@>,=,&&,>>,<<,&<,>&,~=)

Page 20: PostgreSQL - Object Relational Database

Functions

A stored procedure and user-defined function is a set of SQL and procedural statements

(declarations, assignments, loops, flow-of-control) that stored on the database server and

can be invoked using the SQL interface.

CREATE FUNCTION function_name(p1 type, p2 type)

RETURNS type AS

BEGIN

-- logic

END;

LANGUAGE language_name;

Page 21: PostgreSQL - Object Relational Database

Triggers

On DML (Data Manipulation Language) SELECT, INSERT, UPDATE, DELETE

CREATE TRIGGER

name { BEFORE | AFTER } { event [ OR ... ] }

ON

table [ FOR [ EACH ] { ROW | STATEMENT } ]

EXECUTE PROCEDURE

funcname ( arguments )

Page 22: PostgreSQL - Object Relational Database

Cursors

● Used instead of FOR.

● Avoid memory overrun.

● Large data set.

DECLARE curs1 refcursor;

curs2 CURSOR FOR SELECT * FROM tenk1;

OPEN curs1 FOR SELECT * FROM foo WHERE key = mykey;

FETCH curs2 INTO foo, bar, baz;

CLOSE curs1;

Page 23: PostgreSQL - Object Relational Database

View

View consists of a stored query accessible as a virtual table in a relational database or a

set of documents in a document-oriented database composed of the result set of a query.

Views are a great way to simplify your data model.

CREATE VIEW table_information AS

SELECT * FROM table WHERE id = 123;

Now you can simply query your new table directly:

SELECT * FROM table_information;

Page 24: PostgreSQL - Object Relational Database

User-defined objects

New types of almost all objects inside the database can be created, including:

● Casts

● Conversions

● Data types

● Domains

● Functions, including aggregate functions and window functions

● Indexes including custom indexes for custom types

● Operators (existing ones can be overloaded)

● Procedural languages

Page 25: PostgreSQL - Object Relational Database

Replication Methods

1. Master/Slave

● Asynchronous

● Synchronous

2. Multi-Master

● Asynchronous

● Synchronous

3. Proxy

4. Standby system

Page 26: PostgreSQL - Object Relational Database

Master/Slave Replication

Asynchronous Synchronous

High availability High availibility

Read performance Better read performance

Offline peers Worse write performance

asyncM S M S

sync

Page 27: PostgreSQL - Object Relational Database

Multi-Master Replication

Asynchronous Synchronous

Read performance High availiability

Faster access across WANs Read performance

Manage offline peers Difficult to get good write performance

M M M Masync sync

Page 28: PostgreSQL - Object Relational Database

Scaling behaviour

Page 29: PostgreSQL - Object Relational Database

Comparison of scaling behaviour

Page 30: PostgreSQL - Object Relational Database

Hierarchical Database

Data is organized into a tree like structure.

Representing information using parent/child relationships.

Each parent can have many children, but each child has only one parent also known as a 1-

to-many relationship.

Different ways store data like this are

• Enumeration path (ltree)

• Adjacency List

• Nested Sets

Page 31: PostgreSQL - Object Relational Database

LTree – Label Tree

● Ltree is a PostgreSQL module.

● It is implements a data type ltree for representing labels of data stored in a

hierarchical tree-like structure.

● Labels must be less than 256 bytes long. ltree stores a label path.

● A label path is a sequence of zero or more labels separated by dots.

● ltree supports several types of indexes that can speed up the indicated operators.

● Ltree performance is much better when you need to do ad-hoc queries over the tree

● Faster than recursive function that constantly needs to recalculate the branching.

● Some other databases have similar types. SQL Server 2008 has a datatype called

HierarchyID which serves the same purpose as ltree but with different syntax.

Page 32: PostgreSQL - Object Relational Database

ExampleTechnique Adjacency Ltree

Query WITH RECURSIVE d AS (

SELECT id

FROM sponsorship WHERE id = 799

UNION ALL

SELECT s.id

FROM d JOIN sponsorship s ON

s.parent_fk = d.id

)

SELECT * FROM d ORDER BY id

LIMIT 100;

WITH p AS (

SELECT path FROM

sponsorship

WHERE id=799

)

SELECT s.id

FROM sponsorship s, p

WHERE s.path <@ p.path

ORDER BY s.id LIMIT 100;

Total Runtime 1946.48 ms 28.00 ms

Page 33: PostgreSQL - Object Relational Database

More Details

1. Value Expression

http://www.postgresql.org/docs/9.2/static/sql-expressions.html

2. String Functions and Operators

http://www.postgresql.org/docs/9.2/static/functions-string.html

3. Mathematical Functions and Operators

http://www.postgresql.org/docs/9.2/static/functions-math.html

4. MySQL vs PostgreSQL

http://get.enterprisedb.com/whitepapers/Postgres_Plus_8.4_vs_MySQL_5.5.pdf

5. Scaling Behaviour

http://tweakers.net/reviews/657/1/database-test-dual-intel-xeon-5160-introduction.html

Page 34: PostgreSQL - Object Relational Database

References

http://www.postgresql.org/

http://tweakers.net/reviews/657/5/database-test-dual-intel-xeon-5160-

comparison-of-scaling-behaviour.html

http://www.slideshare.net/petereisentraut/replication-solutions-for-postgresql

http://gbif.blogspot.com/2012/06/taxonomic-trees-in-postgresql.html

http://www.slideshare.net/vuhung16plus/postgre-sqlintroduction20100506

Page 35: PostgreSQL - Object Relational Database