A brief introduction to PostgreSQL


Published on

A brief introduction to PostgreSQL

Published in: Technology
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

A brief introduction to PostgreSQL

  1. 1. PostgreSQL ’s (short) INTRODUCTION Nguyen Vu Hung [email_address] 2010/05/05
  2. 2. Agenda <ul><li>Postgres history </li></ul><ul><li>Licenses </li></ul><ul><li>Supported OSes </li></ul><ul><li>Installation </li></ul><ul><li>Main features </li></ul><ul><ul><li>TBD: Compare PostgreSQL with MySQL. </li></ul></ul><ul><ul><li>Functions, Indexes, Trigger, MVCC, Cursor, View, </li></ul></ul><ul><li>Tools </li></ul><ul><li>Q&A </li></ul>
  3. 3. Overview <ul><li>PostgreSQL, aka Postgres. </li></ul><ul><li>Object-relational database management system (ORDBMS). </li></ul><ul><li>MIT-style license </li></ul><ul><ul><li>Free and open source. </li></ul></ul><ul><ul><ul><li>Run the program, for any purpose (freedom 0) </li></ul></ul></ul><ul><ul><ul><li>Study how the program works, and adapt it to your needs (freedom 1) </li></ul></ul></ul><ul><ul><ul><li>Redistribute copies so you can help your neighbor (freedom 2) </li></ul></ul></ul><ul><ul><ul><li>Improve the program, and release your improvements to the public, so that the whole community benefits (freedom 3) </li></ul></ul></ul><ul><li>Rich features </li></ul><ul><ul><li>Has most the features Oracle does. </li></ul></ul><ul><li>*Quite* easy to use </li></ul><ul><ul><li>Not so popular as MySQL </li></ul></ul><ul><li>Stable </li></ul><ul><li>Fast </li></ul>
  4. 4. History <ul><li>University of California, Berkeley originated. </li></ul><ul><li>Postgres: 1982. Oracle: 1977. MySQL: 1995. </li></ul><ul><li>1998: Prototyped. </li></ul><ul><li>Annually Releases. </li></ul><ul><li>2010/05/05: 9.0 Beta </li></ul>
  5. 5. License <ul><li>Copyright (c) <year> <copyright holders> </li></ul><ul><li>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. </li></ul>
  6. 6. GPL versus MIT-style License <ul><li>MIT-style license </li></ul><ul><ul><li>More freedom. </li></ul></ul><ul><ul><li>Simple. </li></ul></ul>MIT: Massachusetts Institute of Technology
  7. 7. Supported OSes <ul><li>Linux: 32 bit, 64 bit </li></ul><ul><li>Windows: 32 bit, 64 bit </li></ul><ul><li>Mac OS X </li></ul><ul><li>Solaris </li></ul><ul><li>FreeBSD </li></ul>
  8. 8. Installation <ul><li>CentOS, Fedora, RHEL </li></ul><ul><ul><li>yum install postgresql postgresql-server </li></ul></ul><ul><ul><li># chkconfig postgresql on </li></ul></ul><ul><ul><li># service postgresql start </li></ul></ul><ul><li>Windows: </li></ul><ul><ul><li>Download and run: </li></ul></ul><ul><ul><ul><li>postgresql-8.4.0-1-windows.exe </li></ul></ul></ul>
  9. 9. Main Features <ul><li>A built-in language called PL/pgSQL resembles Oracle's procedural language PL/SQL . </li></ul><ul><li>Scripting languages supported through plPHP , PL/Python , PL/Ruby , </li></ul><ul><li>Compiled languages C , C++ , Java (via PL/Java ) </li></ul><ul><li>Functions: aka stored procedures . </li></ul>
  10. 10. PL/pgSQL <ul><li>CREATE OR REPLACE FUNCTION generate_string(integer) </li></ul><ul><li>RETURNS SETOF varchar AS $$ </li></ul><ul><li>BEGIN </li></ul><ul><li>FOR _i IN 1 .. $1 LOOP </li></ul><ul><li>RETURN NEXT '<item>'||_i||'</item>'; </li></ul><ul><li>END LOOP; </li></ul><ul><li>RETURN; </li></ul><ul><li>END; $$ LANGUAGE plpgsql; </li></ul><ul><li>SELECT array_to_string( ARRAY(SELECT * FROM generate_string(1000)), ''); </li></ul>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. 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. 12. PHP PostgreSQL wrappers <ul><li>PEAR MDB2 </li></ul><ul><ul><li>Provides a common API for all supported RDBMS. </li></ul></ul><ul><ul><li>Supports MySQL, Postgres, Oracle, MSSQL, SQLite,… </li></ul></ul><ul><li>ADODB </li></ul><ul><ul><li>A Database abstraction library for PHP. </li></ul></ul><ul><ul><li>Supports MySQL, PostgreSQL, Oracle, MS SQL,, Access, SQLite,… </li></ul></ul><ul><li>Other languages? </li></ul>
  13. 13. Indexes <ul><li>User-defined methods. </li></ul><ul><li>Built-in support for B+-tree , hash , GiST and GiN </li></ul><ul><ul><li>GIN index lookups are about three times faster than GiST </li></ul></ul><ul><ul><li>GIN indexes take about three times longer to build than GiST </li></ul></ul><ul><ul><li>GIN indexes are about ten times slower to update than GiST </li></ul></ul><ul><ul><li>GIN indexes are two-to-three times larger than GiST </li></ul></ul>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. 14. Triggers <ul><li>On DML (Data Manipulation Language) </li></ul><ul><ul><li>SELECT, INSERT, UPDATE, DELETE </li></ul></ul><ul><li>No triggers on VIEW. </li></ul><ul><li>Similar to PL/SQL </li></ul><ul><ul><li>TBD: Compare to MySQL. </li></ul></ul>CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] } ON table [ FOR [ EACH ] { ROW | STATEMENT } ] EXECUTE PROCEDURE funcname ( arguments )
  15. 15. MVCC <ul><li>Multi-Version Concurrency Control </li></ul><ul><ul><li>User access through a snapshot of database. </li></ul></ul><ul><ul><li>Changes to be made without being visible to other users until a transaction is committed. </li></ul></ul><ul><ul><li>MySQL? </li></ul></ul><ul><ul><ul><li>InnoDB, Falcon, ISAM . </li></ul></ul></ul>
  16. 16. Cursors <ul><li>Used instead of FOR. </li></ul><ul><li>Avoid memory overrun. </li></ul><ul><li>Large data set. </li></ul>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. 17. 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;
  18. 18. View <ul><li>Defines a view of a query. </li></ul><ul><li>Run every time the view is referenced in a query. </li></ul><ul><li>Question: What is the difference between Cursor and View? </li></ul>CREATE VIEW comedies AS SELECT * FROM films WHERE kind = 'Comedy';
  19. 19. Other Features <ul><li>Integrity constraints check (foreign keys) </li></ul><ul><li>Inner, outer, cross join </li></ul><ul><li>Sub SELECT (nested SELECT) </li></ul><ul><li>Transactions </li></ul><ul><li>SQL:2008 supports </li></ul><ul><li>SSL encryption </li></ul><ul><li>Binary/textual large object storage </li></ul><ul><ul><li>Different search algorithm for different type/density of data. </li></ul></ul><ul><li>Online backup (Oracle: RMAN) </li></ul><ul><li>Point-in-time recovery: Restore to any time in the past. </li></ul><ul><li>Regular expression. </li></ul><ul><ul><li>SELECT record FROM myrecords WHERE record ~* '^a'; </li></ul></ul>
  20. 20. Tools <ul><li>Psql: Command line front-end </li></ul><ul><li>pgAdmin: GUI front-end </li></ul><ul><li>phpPgadmin: Web based front-end </li></ul><ul><li>MS ODBC </li></ul><ul><ul><li>MS Office + Postgres </li></ul></ul><ul><li>NaviCat: $$ </li></ul><ul><li>DeZign: $$ </li></ul><ul><li>EMS SQL Manager for PostgreSQL: $$ </li></ul>
  21. 22. References <ul><li>http://www.wikivs.com/wiki/MySQL_vs_PostgreSQL </li></ul><ul><li>http://en.wikipedia.org/wiki/PostgreSQL </li></ul><ul><li>http://www.postgresql.org/ </li></ul><ul><li>http://www-css.fnal.gov/dsg/external/freeware/pgsql-vs-mysql.html </li></ul><ul><li>http://www.postgresql.org/docs/ </li></ul><ul><li>http://wiki.postgresql.org/wiki/Oracle_to_Postgres_Conversion </li></ul><ul><li>http://wiki.postgresql.org/wiki/Why_PostgreSQL_Instead_of_MySQL_2009 </li></ul><ul><li>http://en.wikipedia.org/wiki/MySQL </li></ul><ul><li>http://en.wikipedia.org/wiki/Oracle_Corporation </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.