Upload
vu-hung-nguyen
View
7.757
Download
2
Tags:
Embed Size (px)
DESCRIPTION
A brief introduction to PostgreSQL
Citation preview
PostgreSQL’s (short)
INTRODUCTIONNguyen Vu Hung
2010/05/05
Agenda
• Postgres history• Licenses• Supported OSes• Installation• Main features
– TBD: Compare PostgreSQL with MySQL.– Functions, Indexes, Trigger, MVCC, Cursor, View,
• Tools• Q&A
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
History
• University of California, Berkeley originated.
• Postgres: 1982. Oracle: 1977. MySQL: 1995.
• 1998: Prototyped.
• Annually Releases.
• 2010/05/05: 9.0 Beta
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.
GPL versus MIT-style License
• MIT-style license– More freedom.– Simple.
MIT: Massachusetts Institute of Technology
Supported OSes
• Linux: 32 bit, 64 bit
• Windows: 32 bit, 64 bit
• Mac OS X
• Solaris
• FreeBSD
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
Main Features
• A built-in language called PL/pgSQL resembles Oracle's procedural language PL/SQL.
• Scripting languages supported through plPHP, PL/Python, PL/Ruby,
• Compiled languages C, C++, Java (via PL/Java)
• Functions: aka stored procedures.
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
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);
?>
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?
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 ]
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 )
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.
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;
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;
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?
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';
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: $$
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