22
PostgreSQL’s (short) INTRODUCTION Nguyen Vu Hung [email protected] 2010/05/05

A brief introduction to PostgreSQL

Embed Size (px)

DESCRIPTION

A brief introduction to PostgreSQL

Citation preview

Page 1: A brief introduction to PostgreSQL

PostgreSQL’s (short)

INTRODUCTIONNguyen Vu Hung

[email protected]

2010/05/05

Page 2: A brief introduction to PostgreSQL

Agenda

• Postgres history• Licenses• Supported OSes• Installation• Main features

– TBD: Compare PostgreSQL with MySQL.– Functions, Indexes, Trigger, MVCC, Cursor, View,

• Tools• Q&A

Page 3: A brief introduction to PostgreSQL

Overview

• PostgreSQL, aka Postgres.• Object-relational database management system (ORDBMS).• MIT-style license

– Free and open source.• Run the program, for any purpose (freedom 0) • Study how the program works, and adapt it to your needs (freedom 1) • Redistribute copies so you can help your neighbor (freedom 2) • Improve the program, and release your improvements to the public, so that

the whole community benefits (freedom 3) • Rich features

– Has most the features Oracle does.• *Quite* easy to use

– Not so popular as MySQL• Stable• Fast

Page 4: A brief introduction to PostgreSQL

History

• University of California, Berkeley originated.

• Postgres: 1982. Oracle: 1977. MySQL: 1995.

• 1998: Prototyped.

• Annually Releases.

• 2010/05/05: 9.0 Beta

Page 5: A brief introduction to PostgreSQL

LicenseCopyright (c) <year> <copyright holders> Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Page 6: A brief introduction to PostgreSQL

GPL versus MIT-style License

• MIT-style license– More freedom.– Simple.

MIT: Massachusetts Institute of Technology

Page 7: A brief introduction to PostgreSQL

Supported OSes

• Linux: 32 bit, 64 bit

• Windows: 32 bit, 64 bit

• Mac OS X

• Solaris

• FreeBSD

Page 8: A brief introduction to PostgreSQL

Installation

• CentOS, Fedora, RHEL– yum install postgresql postgresql-server – # chkconfig postgresql on– # service postgresql start

• Windows:– Download and run:

• postgresql-8.4.0-1-windows.exe

Page 10: A brief introduction to PostgreSQL

PL/pgSQL

CREATE OR REPLACE FUNCTION generate_string(integer)

RETURNS SETOF varchar AS $$

BEGIN

FOR _i IN 1 .. $1 LOOP

RETURN NEXT '<item>'||_i||'</item>';

END LOOP;

RETURN;

END; $$ LANGUAGE plpgsql;

SELECT array_to_string( ARRAY(SELECT *

FROM generate_string(1000)), '');

FUNCTION GetEmplNm (p_emplid IN VARCHAR2)

RETURN VARCHAR2 IS BEGIN IF NVL(g_emplid1,'X') <> p_emplid THEN BEGIN SELECT name INTO g_name FROM ps_personal_data WHERE emplid = p_emplid; EXCEPTION WHEN OTHERS THEN g_name := NULL; END; g_emplid1 := p_emplid; END IF; RETURN g_name; END GetEmplNm;

PL/SQL

Page 11: A brief introduction to PostgreSQL

PHP and PostgresSQL<?php

$dbconn = pg_connect("dbname=test");

$dbconn2 = pg_connect("host=localhost port=5432 dbname=test");

$dbconn3 =    pg_connect("host=sheep port=5432 dbname=test user=test password=foo");

$conn_string = "host=sheep port=5432 dbname=test user=test password=bar";

$dbconn4 = pg_connect($conn_string);

?>

Page 12: A brief introduction to PostgreSQL

PHP PostgreSQL wrappers• PEAR MDB2

– Provides a common API for all supported RDBMS. – Supports MySQL, Postgres, Oracle, MSSQL, SQLite,

• ADODB– A Database abstraction library for PHP. – Supports MySQL, PostgreSQL, Oracle, MS SQL,, Acc

ess, SQLite,…

• Other languages?

Page 13: A brief introduction to PostgreSQL

Indexes• User-defined methods.• Built-in support for B+-tree, hash, GiST and GiN

– GIN index lookups are about three times faster than GiST

– GIN indexes take about three times longer to build than GiST

– GIN indexes are about ten times slower to update than GiST

– GIN indexes are two-to-three times larger than GiST

GiST: Generalized Search Tree B+ tree: BplusTree

CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] name ON table [ USING method ] ( { column | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ) [ WITH ( storage_parameter = value [, ... ] ) ][ TABLESPACE tablespace ][ WHERE predicate ]

Page 14: A brief introduction to PostgreSQL

Triggers

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

• No triggers on VIEW.

• Similar to PL/SQL– TBD: Compare to MySQL.

CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] } ON table [ FOR [ EACH ] { ROW | STATEMENT } ] EXECUTE PROCEDURE funcname ( arguments )

Page 15: A brief introduction to PostgreSQL

MVCC

• Multi-Version Concurrency Control – User access through a snapshot of database.– Changes to be made without being visible to o

ther users until a transaction is committed.– MySQL?

• InnoDB, Falcon, ISAMISAM.

Page 16: A brief introduction to PostgreSQL

Cursors• Used instead of FOR.• Avoid memory overrun.• Large data set.

DECLARE curs1 refcursor; curs2 CURSOR FOR SELECT * FROM tenk1; curs3 CURSOR (key integer) IS SELECT * FROM tenk1 WHERE unique1 = key;

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

FETCH curs2 INTO foo, bar, baz;

CLOSE curs1;

Page 17: A brief introduction to PostgreSQL

Functions and CursorsCREATE FUNCTION myfunc(refcursor, refcursor) RETURNS SETOF refcursor AS $$ BEGIN

OPEN $1 FOR SELECT * FROM table_1; RETURN NEXT $1; OPEN $2 FOR SELECT * FROM table_2; RETURN NEXT $2;

END; $$ LANGUAGE plpgsql;

-- need to be in a transaction to use cursors. BEGIN;

SELECT * FROM myfunc('a', 'b');

FETCH ALL FROM a; FETCH ALL FROM b;COMMIT;

Page 18: A brief introduction to PostgreSQL

View

CREATE VIEW comedies AS SELECT * FROM films WHERE kind = 'Comedy';

• Defines a view of a query.

• Run every time the view is referenced in a query.

• Question: What is the difference between Cursor and View?

Page 19: A brief introduction to PostgreSQL

Other Features

• Integrity constraints check (foreign keys)• Inner, outer, cross join• Sub SELECT (nested SELECT)• Transactions• SQL:2008 supports• SSL encryption• Binary/textual large object storage

– Different search algorithm for different type/density of data.• Online backup (Oracle: RMAN)• Point-in-time recovery: Restore to any time in the past.• Regular expression.

– SELECT record FROM myrecords WHERE record ~* '^a';

Page 20: A brief introduction to PostgreSQL

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 21: A brief introduction to PostgreSQL
Page 22: A brief introduction to PostgreSQL

References

• http://www.wikivs.com/wiki/MySQL_vs_PostgreSQL• http://en.wikipedia.org/wiki/PostgreSQL• http://www.postgresql.org/• http://www-css.fnal.gov/dsg/external/freeware/pgsql-vs-

mysql.html• http://www.postgresql.org/docs/• http://wiki.postgresql.org/wiki/Oracle_to_Postgres_Conve

rsion• http://wiki.postgresql.org/wiki/Why_PostgreSQL_Instead

_of_MySQL_2009• http://en.wikipedia.org/wiki/MySQL• http://en.wikipedia.org/wiki/Oracle_Corporation