SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PDF
What is new in PostgreSQL 14?
Mydbops
 
PPT
Oracle12c Pluggable Database Hands On - TROUG 2014
Özgür Umut Vurgun
 
PDF
External Language Stored Procedures for MySQL
Antony T Curtis
 
PDF
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
mCloud
 
PDF
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
Mydbops
 
PDF
MySQL database replication
PoguttuezhiniVP
 
PDF
MySQL Audit using Percona audit plugin and ELK
I Goo Lee
 
PDF
Using Perl Stored Procedures for MariaDB
Antony T Curtis
 
PDF
Centralized + Unified Logging
Gabor Kozma
 
PDF
MySQL Replication Update -- Zendcon 2016
Dave Stokes
 
PDF
MySQL Replication Basics -Ohio Linux Fest 2016
Dave Stokes
 
PDF
MySQL as a Document Store
Dave Stokes
 
PDF
Installing postgres & postgis
John Ashmead
 
PPT
8b. Column Oriented Databases Lab
Fabio Fumarola
 
PDF
Percona Toolkit for Effective MySQL Administration
Mydbops
 
PPT
8a. How To Setup HBase with Docker
Fabio Fumarola
 
PDF
Introduction to Redis
Dvir Volk
 
PDF
Evolution of MongoDB Replicaset and Its Best Practices
Mydbops
 
PPTX
SQL Server vs Postgres
chandra sekhar pathivada , PMP,ITIL,MCTS
 
What is new in PostgreSQL 14?
Mydbops
 
Oracle12c Pluggable Database Hands On - TROUG 2014
Özgür Umut Vurgun
 
External Language Stored Procedures for MySQL
Antony T Curtis
 
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
mCloud
 
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
Mydbops
 
MySQL database replication
PoguttuezhiniVP
 
MySQL Audit using Percona audit plugin and ELK
I Goo Lee
 
Using Perl Stored Procedures for MariaDB
Antony T Curtis
 
Centralized + Unified Logging
Gabor Kozma
 
MySQL Replication Update -- Zendcon 2016
Dave Stokes
 
MySQL Replication Basics -Ohio Linux Fest 2016
Dave Stokes
 
MySQL as a Document Store
Dave Stokes
 
Installing postgres & postgis
John Ashmead
 
8b. Column Oriented Databases Lab
Fabio Fumarola
 
Percona Toolkit for Effective MySQL Administration
Mydbops
 
8a. How To Setup HBase with Docker
Fabio Fumarola
 
Introduction to Redis
Dvir Volk
 
Evolution of MongoDB Replicaset and Its Best Practices
Mydbops
 

Viewers also liked (20)

PDF
Get to know PostgreSQL!
Oddbjørn Steffensen
 
PDF
Introduction to PostgreSQL
Mark Wong
 
PDF
PostgreSQL Deep Internal
EXEM
 
PDF
Karabük Üniversitesi Programlama Günleri - PostgreSQL Sunumu
atifceylan
 
PDF
Mvcc Unmasked (Bruce Momjian)
Ontico
 
PDF
MySQL 5.7: Focus on InnoDB
Mario Beck
 
PPTX
The nightmare of locking, blocking and isolation levels!
Boris Hristov
 
PPTX
PostgreSQL - Object Relational Database
Mubashar Iqbal
 
PDF
The Power of MySQL Explain
MYXPLAIN
 
ODP
Mysql For Developers
Carol McDonald
 
PDF
Como migrar una base de datos de mysql a power designer
Alex Bernal
 
PDF
Mv unmasked.w.code.march.2013
EDB
 
ODP
Explain
Ligaya Turmelle
 
PDF
Inno db internals innodb file formats and source code structure
zhaolinjnu
 
ODP
Introduction to PostgreSQL
Jim Mlodgenski
 
PDF
PostgreSQL and RAM usage
Alexey Bashtanov
 
PDF
Converting from MySQL to PostgreSQL
John Ashmead
 
PPTX
PostgreSQL Hangout Parameter Tuning
Ashnikbiz
 
PPTX
PostgreSQL Hangout Replication Features v9.4
Ashnikbiz
 
PPTX
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Reactive.IO
 
Get to know PostgreSQL!
Oddbjørn Steffensen
 
Introduction to PostgreSQL
Mark Wong
 
PostgreSQL Deep Internal
EXEM
 
Karabük Üniversitesi Programlama Günleri - PostgreSQL Sunumu
atifceylan
 
Mvcc Unmasked (Bruce Momjian)
Ontico
 
MySQL 5.7: Focus on InnoDB
Mario Beck
 
The nightmare of locking, blocking and isolation levels!
Boris Hristov
 
PostgreSQL - Object Relational Database
Mubashar Iqbal
 
The Power of MySQL Explain
MYXPLAIN
 
Mysql For Developers
Carol McDonald
 
Como migrar una base de datos de mysql a power designer
Alex Bernal
 
Mv unmasked.w.code.march.2013
EDB
 
Inno db internals innodb file formats and source code structure
zhaolinjnu
 
Introduction to PostgreSQL
Jim Mlodgenski
 
PostgreSQL and RAM usage
Alexey Bashtanov
 
Converting from MySQL to PostgreSQL
John Ashmead
 
PostgreSQL Hangout Parameter Tuning
Ashnikbiz
 
PostgreSQL Hangout Replication Features v9.4
Ashnikbiz
 
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Reactive.IO
 
Ad

Similar to A brief introduction to PostgreSQL (20)

KEY
PostgreSQL
Reuven Lerner
 
PDF
Demystifying PostgreSQL
NOLOH LLC.
 
PDF
Demystifying PostgreSQL (Zendcon 2010)
NOLOH LLC.
 
PDF
PostgreSQL - Case Study
S.Shayan Daneshvar
 
PPTX
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
MhmdMk10
 
PDF
PostgreSQL 9.0 & The Future
Aaron Thul
 
PPTX
PostgreSQL Terminology
Showmax Engineering
 
ODP
PostgreSQL 8.4 TriLUG 2009-11-12
Andrew Dunstan
 
PPT
Object Relational Database Management System
Amar Myana
 
PPTX
PostgreSQL - It's kind've a nifty database
Barry Jones
 
PDF
PostgreSQL versus MySQL - What Are The Real Differences
All Things Open
 
PDF
An evening with Postgresql
Joshua Drake
 
PDF
Postgresql 9.3 overview
Aveic
 
PDF
Postgres for MySQL (and other database) people
Command Prompt., Inc
 
PDF
Oracle postgre sql-mirgration-top-10-mistakes
Jim Mlodgenski
 
PDF
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Jim Mlodgenski
 
PDF
0292-introduction-postgresql.pdf
Mustafa Keskin
 
PPTX
Migrating To PostgreSQL
Grant Fritchey
 
PPT
PO WER - Piotr Mariat - Sql
Zespół Szkół nr 26
 
PPT
vdocument.in_chapter26-postgresql.ppt
AbdurRahmanMurugesan1
 
PostgreSQL
Reuven Lerner
 
Demystifying PostgreSQL
NOLOH LLC.
 
Demystifying PostgreSQL (Zendcon 2010)
NOLOH LLC.
 
PostgreSQL - Case Study
S.Shayan Daneshvar
 
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
MhmdMk10
 
PostgreSQL 9.0 & The Future
Aaron Thul
 
PostgreSQL Terminology
Showmax Engineering
 
PostgreSQL 8.4 TriLUG 2009-11-12
Andrew Dunstan
 
Object Relational Database Management System
Amar Myana
 
PostgreSQL - It's kind've a nifty database
Barry Jones
 
PostgreSQL versus MySQL - What Are The Real Differences
All Things Open
 
An evening with Postgresql
Joshua Drake
 
Postgresql 9.3 overview
Aveic
 
Postgres for MySQL (and other database) people
Command Prompt., Inc
 
Oracle postgre sql-mirgration-top-10-mistakes
Jim Mlodgenski
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Jim Mlodgenski
 
0292-introduction-postgresql.pdf
Mustafa Keskin
 
Migrating To PostgreSQL
Grant Fritchey
 
PO WER - Piotr Mariat - Sql
Zespół Szkół nr 26
 
vdocument.in_chapter26-postgresql.ppt
AbdurRahmanMurugesan1
 
Ad

More from Vu Hung Nguyen (20)

PPTX
Co ban horenso - Tai lieu training noi bo
Vu Hung Nguyen
 
PDF
Funix techtalk: Tự học hiệu quả thời 4.0
Vu Hung Nguyen
 
PDF
Học cờ cùng con - Nguyễn Vỹ Kỳ Anh [U8]
Vu Hung Nguyen
 
PDF
Japanese for it bridge engineers
Vu Hung Nguyen
 
PPTX
Basic IT Project Management Terminologies
Vu Hung Nguyen
 
PDF
2018 Học cờ cùng con - Nguyễn Vũ Kỳ Anh [U7]
Vu Hung Nguyen
 
PDF
Làm việc hiệu quả với sếp Nhật (2017)
Vu Hung Nguyen
 
PDF
Problem Solving Skills (for IT Engineers)
Vu Hung Nguyen
 
PPTX
Using Shader in cocos2d-x
Vu Hung Nguyen
 
PPTX
Pham Anh Tu - TK Framework
Vu Hung Nguyen
 
PDF
My idol: Magnus Carlsen vs. Ky Anh 2G1 NGS Newton
Vu Hung Nguyen
 
PDF
Basic advanced scrum framework
Vu Hung Nguyen
 
PDF
FPT Univ. Talkshow IT khong chi la lap trinh
Vu Hung Nguyen
 
PDF
Basic & Advanced Scrum Framework
Vu Hung Nguyen
 
PDF
Agile Vietnam Conference 2016: Recap
Vu Hung Nguyen
 
PDF
IT Public Speaking Guidelines
Vu Hung Nguyen
 
PDF
Kanban: Cơ bản và Nâng cao
Vu Hung Nguyen
 
PDF
Học cờ vua cùng con Nguyễn Vũ Kỳ Anh (U6)
Vu Hung Nguyen
 
PPTX
Fuji Technology Workshop: Learning Skills
Vu Hung Nguyen
 
PDF
Anti patterns in it project management
Vu Hung Nguyen
 
Co ban horenso - Tai lieu training noi bo
Vu Hung Nguyen
 
Funix techtalk: Tự học hiệu quả thời 4.0
Vu Hung Nguyen
 
Học cờ cùng con - Nguyễn Vỹ Kỳ Anh [U8]
Vu Hung Nguyen
 
Japanese for it bridge engineers
Vu Hung Nguyen
 
Basic IT Project Management Terminologies
Vu Hung Nguyen
 
2018 Học cờ cùng con - Nguyễn Vũ Kỳ Anh [U7]
Vu Hung Nguyen
 
Làm việc hiệu quả với sếp Nhật (2017)
Vu Hung Nguyen
 
Problem Solving Skills (for IT Engineers)
Vu Hung Nguyen
 
Using Shader in cocos2d-x
Vu Hung Nguyen
 
Pham Anh Tu - TK Framework
Vu Hung Nguyen
 
My idol: Magnus Carlsen vs. Ky Anh 2G1 NGS Newton
Vu Hung Nguyen
 
Basic advanced scrum framework
Vu Hung Nguyen
 
FPT Univ. Talkshow IT khong chi la lap trinh
Vu Hung Nguyen
 
Basic & Advanced Scrum Framework
Vu Hung Nguyen
 
Agile Vietnam Conference 2016: Recap
Vu Hung Nguyen
 
IT Public Speaking Guidelines
Vu Hung Nguyen
 
Kanban: Cơ bản và Nâng cao
Vu Hung Nguyen
 
Học cờ vua cùng con Nguyễn Vũ Kỳ Anh (U6)
Vu Hung Nguyen
 
Fuji Technology Workshop: Learning Skills
Vu Hung Nguyen
 
Anti patterns in it project management
Vu Hung Nguyen
 

Recently uploaded (20)

PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 

A brief introduction to PostgreSQL

  • 1. PostgreSQL ’s (short) INTRODUCTION Nguyen Vu Hung [email_address] 2010/05/05
  • 2. Agenda Postgres history Licenses Supported OSes Installation Main features TBD: Compare PostgreSQL with MySQL. Functions, Indexes, Trigger, MVCC, Cursor, View, Tools Q&A
  • 3. 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
  • 4. History University of 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-style License 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 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 .
  • 10. 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
  • 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 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?
  • 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 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;
  • 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. 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';
  • 19. 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';
  • 20. 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: $$
  • 21.  
  • 22. 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