PostgreSQL ’s  (short)   INTRODUCTION Nguyen Vu Hung [email_address] 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
License Copyright (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 &quot;Software&quot;), 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 &quot;AS IS&quot;, 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 ?>   $dbconn4  =  pg_connect ( $conn_string ); $conn_string  =  &quot;host=sheep port=5432 dbname=test user=test password=bar&quot; ; $dbconn3  =  pg_connect ( &quot;host=sheep port=5432 dbname=test user=test password=foo&quot; ); $dbconn2  =  pg_connect ( &quot;host=localhost port=5432 dbname=test&quot; ); $dbconn  =  pg_connect ( &quot;dbname=test&quot; ); <?php
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,, Access, 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 other users until a transaction is committed. MySQL? InnoDB, Falcon,  ISAM .
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 Cursors CREATE 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 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? CREATE VIEW comedies AS  SELECT *  FROM films  WHERE kind = 'Comedy';
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_Conversion http://wiki.postgresql.org/wiki/Why_PostgreSQL_Instead_of_MySQL_2009 http://en.wikipedia.org/wiki/MySQL http://en.wikipedia.org/wiki/Oracle_Corporation

A brief introduction to PostgreSQL

  • 1.
    PostgreSQL ’s (short) INTRODUCTION Nguyen Vu Hung [email_address] 2010/05/05
  • 2.
    Agenda Postgres historyLicenses Supported OSes Installation Main features TBD: Compare PostgreSQL with MySQL. Functions, Indexes, Trigger, MVCC, Cursor, View, Tools Q&A
  • 3.
    Overview PostgreSQL, akaPostgres. 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
  • 4.
    History Universityof California, Berkeley originated. Postgres: 1982. Oracle: 1977. MySQL: 1995. 1998: Prototyped. Annually Releases. 2010/05/05: 9.0 Beta
  • 5.
    License Copyright (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 &quot;Software&quot;), 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 &quot;AS IS&quot;, 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.
  • 6.
    GPL versus MIT-styleLicense MIT-style license More freedom. Simple. MIT: Massachusetts Institute of Technology
  • 7.
    Supported OSes Linux:32 bit, 64 bit Windows: 32 bit, 64 bit Mac OS X Solaris FreeBSD
  • 8.
    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
  • 9.
    Main Features Abuilt-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 .
  • 10.
    PL/pgSQL CREATE ORREPLACE 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
  • 11.
    PHP and PostgresSQL?> $dbconn4  =  pg_connect ( $conn_string ); $conn_string  =  &quot;host=sheep port=5432 dbname=test user=test password=bar&quot; ; $dbconn3  =  pg_connect ( &quot;host=sheep port=5432 dbname=test user=test password=foo&quot; ); $dbconn2  =  pg_connect ( &quot;host=localhost port=5432 dbname=test&quot; ); $dbconn  =  pg_connect ( &quot;dbname=test&quot; ); <?php
  • 12.
    PHP PostgreSQL wrappersPEAR 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,, Access, SQLite,… Other languages?
  • 13.
    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 ]
  • 14.
    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 )
  • 15.
    MVCC Multi-Version Concurrency Control User access through a snapshot of database. Changes to be made without being visible to other users until a transaction is committed. MySQL? InnoDB, Falcon, ISAM .
  • 16.
    Cursors Used insteadof 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;
  • 17.
    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;
  • 18.
    View Defines aview of a query. Run every time the view is referenced in a query. Question: What is the difference between Cursor and View? CREATE VIEW comedies AS SELECT * FROM films WHERE kind = 'Comedy';
  • 19.
    Other Features Integrityconstraints 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';
  • 20.
    Tools Psql: Commandline front-end pgAdmin: GUI front-end phpPgadmin: Web based front-end MS ODBC MS Office + Postgres NaviCat: $$ DeZign: $$ EMS SQL Manager for PostgreSQL: $$
  • 21.
  • 22.
    References http://www.wikivs.com/wiki/MySQL_vs_PostgreSQL http://en.wikipedia.org/wiki/PostgreSQLhttp://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_Conversion http://wiki.postgresql.org/wiki/Why_PostgreSQL_Instead_of_MySQL_2009 http://en.wikipedia.org/wiki/MySQL http://en.wikipedia.org/wiki/Oracle_Corporation