SlideShare a Scribd company logo
What‘s new in PostgreSQL 9

      Dan-Claudiu DRAGOŞ
PostgreSQL 9




                             About PostgreSQL


• This presentation was inspired by the sessions I‘ve attended at
  Fosdem 2010

• PostgreSQL means:
    –   Standards followed closely
    –   A true relational database
    –   Complex, advanced features that puts it far ahead of its competition
    –   The best tool to use for advanced database-based applications!


• This comes at a cost: it can‘t compete with simpler database
  systems on poor hardware and writing „simple things“ tends to get
  harder


                                                                               2
PostgreSQL 9




                   Common Table Expressions

• Available since version 8.4
• They are about defining an in-place data structure by the result of a
  query or a query combination
• This data structure can then be queried further
• Recursive join over this data structure is also possible


• Some of this functionality was available before by using:
        • Views (named data structures stored into the database backend)
        • Subselects


• The join recursion is a brand new feature



                                                                       3
PostgreSQL 9




                Common Table Expressions

• Basic usage

      WITH [RECURSIVE] name AS
      (
        SELECT field1 FROM table WHERE field2=“abc”
      )
      SELECT * FROM name ORDER BY field;




                                                      4
PostgreSQL 9




                   Common Table Expressions

• Join recursion

   – Assume we have the following corporate structure:

       -- department structure represented here is as follows:
       --
       -- ROOT-+->A-+->B-+->C
       --      |         |
       --      |         +->D-+->F
       --      +->E-+->G


   – We want all the departments under A (or under any department, actually)




                                                                       5
PostgreSQL 9




                   Common Table Expressions

• Join recursion
       CREATE TABLE department (
           id INTEGER PRIMARY KEY,
           parent_department INTEGER REFERENCES department,
           name TEXT
       );
       INSERT INTO department (id, parent_department, "name")
       VALUES
            (0, NULL, 'ROOT'),
            (1, 0, 'A'),
            (2, 1, 'B'),
            (3, 2, 'C'),
            (4, 2, 'D'),
            (5, 0, 'E'),
            (6, 4, 'F'),
            (7, 5, 'G');


                                                                6
PostgreSQL 9




                 Common Table Expressions

• Join recursion
      WITH RECURSIVE subdepartment AS
      (
          -- non-recursive term
          SELECT * FROM department WHERE name = 'A'

         UNION ALL

         -- recursive term
         SELECT d.*
         FROM
              department AS d
         JOIN
              subdepartment AS sd
              ON (d.parent_department = sd.id)
      )
      SELECT *
      FROM subdepartment
      ORDER BY name;

                                                      7
PostgreSQL 9




                    Common Table Expressions

• Join recursion

   – The non-recursive term is optional
   – There are some restrictions when it comes to defining the
     recursive term:
           –   No subquery using the recursive term (no double recursion)
           –   No aggregate functions such as COUNT(*)
           –   No HAVING, GROUP BY
           –   No ORDER BY, no LIMIT
           –   No FULL JOIN
           –   The recursive term must be on the left hand side:
                  » JOIN reference ON (recursiveterm.field = reference.field)


• The database engine performs a brute-force, all-terms comparison
  so performance can sometimes be an issue


                                                                                8
PostgreSQL 9




          Hot Standby and Streaming Replication

• They are both about database synchronization
• Hot Standby is a „push“
• Streaming Replication is a „pull“

• The issue at hand is duplicating the contents of a Master Database
  into one or more Slaves

• Hot Standby means that every query ran on Master is also ran on
  the Slave; this also works for table locks!

• Streaming replication means that every database transaction is
  requested by the Slave and then applied on it


                                                           9
PostgreSQL 9




                   Deferrable Unique Constraints

• This query sequence always fails:

# CREATE   TABLE test (a INT PRIMARY KEY);
# INSERT   INTO test values (1), (2);
# UPDATE   test SET a = a + 1;
 ERROR:    duplicate key value violates unique constraint "test_pkey"
 DETAIL:    Key (a)=(2) already exists.


• What 9.0 version provides is that the uniqueness constraint may be
  checked after running the query, not as the query runs:

# CREATE TABLE test (a INT PRIMARY KEY DEFERRABLE);




                                                                  10
PostgreSQL 9




                      Exclusion Constraints

• Before 9.0, exclusion would be checked by using operator “=“
• Version 9.0 allows for a more complex approach

# CREATE TABLE reservation
 (
   room      TEXT,
   speaker   TEXT,
   during    PERIOD);

 ALTER TABLE reservation ADD CONSTRAINT test_exclude EXCLUDE USING gist
   (room WITH =,during WITH &&);


• The constraint is defined on 2 fields; the point is not having the
  same room reserved at the same time
• The example uses 2 modules from „contrib“ (temporal and gist)

                                                                11
PostgreSQL 9




                      Exclusion Constraints

• Example using the previous table definition:

# INSERT INTO reservation (speaker ,room, during) VALUES ( ‘Dan', ‘Flex
   Room', period('2010-12-10 12:00:00', '2010-12-10 13:00:00'));
 INSERT 0 1
# INSERT INTO reservation (speaker, room, during) VALUES ( ‘Catalin',
   ‘Presentation', period('2010-12-10 15:00:00', '2010-12-10 16:00:00'));
 INSERT 0 1
# INSERT INTO reservation (speaker ,room, during) VALUES ( ‘Andrei', ‘Flex
   Room', period('2010-12-10 12:30:00', '2010-12-10 13:00:00'));
 ERROR: conflicting key value violates exclusion constraint "test_exclude"
 DETAIL: Key (room, during)=(Flex Room, [2010-12-10 12:30:00+02, 2010-12-
   10 13:00:00+02)) conflicts with existing key (room, during)=(Flex Room,
   [2010-12-10 12:00:00+02, 2010-12-10 13:00:00+02)).


• This feature is available in PostgreSQL only


                                                               12
Smaller enhancements

• GRANT / REVOKE on Schemas
   – PostgreSQL has Databases -> Schemas -> Tables -> Fields
   – Until 9.0 you could grant (revoke) rights on Databases and Tables only
   – This functionality could be achieved before 9.0 by writing a script


• Faster VACUUM FULL
   – This is the more or less equivalent to the OPTIMIZE query in MySQL
   – This command used to be pretty slow on large databases
   – In 9.0 it works by performing a copy of the database


• Better 64bit support
   – 64bit binaries were already available on Linux
   – Windows native 64bit binary is the new thing in version 9.0



                                                                        13
PL/PgSQL enhancements

• Unnamed functions
   – In 9.0 you can run code snippets in place
   – Before 9.0 you had to define a stored procedure and then call it

    DO language plpgsql $$
    DECLARE
    vr record;

    BEGIN

    FOR vr IN SELECT tablename FROM pg_tables WHERE tableowner = ‘dan' AND schemaname
      NOT IN ('pg_catalog','information_schema')
    LOOP
      EXECUTE 'GRANT SELECT ON ' || vr.tablename || ' TO tester';
    END LOOP;
    END
    $$;




                                                                        14
PL/PgSQL enhancements

• Named parameter calls
   – Before 9.0 you had to provide all the function parameters and care for their order,
     as provided in the function definition
   – Version 8.4 added “default parameters”, allowing skipping certain parameters
     (they would be replaced with their default values)

   – Example function:

    CREATE FUNCTION test (a INT, b TEXT) RETURNS TEXT AS $$
    DECLARE
      value TEXT;
    BEGIN
      value := 'a is ' || a::text || ' and b is ' || b;
      RETURN value;
    END;
    $$ LANGUAGE plpgsql;




                                                                          15
PL/PgSQL enhancements

• Named parameter calls
   – Before 9.0 you would have called it this way:

    SELECT test(1,'foo');
    test
    -------------------------
    a is 1 and b is foo
    (1 row)



   – Version 9.0 allows this type of function call:

    SELECT test( b:='foo', a:=1);
    test
    -------------------------
    a is 1 and b is foo
    (1 row)




                                                      16
That’s it




• Questions?




                           17

More Related Content

PPTX
What's new in PostgreSQL 11 ?
PDF
[PGDay.Seoul 2020] PostgreSQL 13 New Features
PDF
Performance improvements in PostgreSQL 9.5 and beyond
PDF
PostgreSQL performance improvements in 9.5 and 9.6
PDF
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PPTX
PostgreSQL Database Slides
PPTX
PDF
Юра Гуляев. Oracle tables
What's new in PostgreSQL 11 ?
[PGDay.Seoul 2020] PostgreSQL 13 New Features
Performance improvements in PostgreSQL 9.5 and beyond
PostgreSQL performance improvements in 9.5 and 9.6
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PostgreSQL Database Slides
Юра Гуляев. Oracle tables

What's hot (20)

PPTX
Oracle Database 12.1.0.2 New Features
PPTX
PostgreSQL- An Introduction
PDF
PostgreSQL Performance Tuning
PPT
My sql with querys
PPTX
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
PDF
Mysql Explain Explained
TXT
Oracle ORA Errors
PDF
8.4 Upcoming Features
PPTX
Database administration commands
PDF
Introduction into MySQL Query Tuning for Dev[Op]s
PDF
9.1 Grand Tour
PDF
9.1 Mystery Tour
PPTX
PDF
Test Dml With Nologging
 
PDF
Data Processing Inside PostgreSQL
 
PPT
SQLMAP Tool Usage - A Heads Up
ODP
Postgre sql unleashed
PDF
PGConf.ASIA 2017 Logical Replication Internals (English)
Oracle Database 12.1.0.2 New Features
PostgreSQL- An Introduction
PostgreSQL Performance Tuning
My sql with querys
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Mysql Explain Explained
Oracle ORA Errors
8.4 Upcoming Features
Database administration commands
Introduction into MySQL Query Tuning for Dev[Op]s
9.1 Grand Tour
9.1 Mystery Tour
Test Dml With Nologging
 
Data Processing Inside PostgreSQL
 
SQLMAP Tool Usage - A Heads Up
Postgre sql unleashed
PGConf.ASIA 2017 Logical Replication Internals (English)
Ad

Viewers also liked (13)

PPTX
Carmelo De Grazia Suárez: Bancamiga promoviendo el consumo eficiente
PDF
Elements_Jul07_CGU_DMGray
PDF
SicuroIMS Iraq Threat Assessment 3 - 16 August 2016
PPTX
12 carmelo de grazia suárez bancamiga está donde tú estás
PPTX
11 carmelo de grazia suarez servicios bancamiga
PDF
Ryno CV- December2015.
PDF
Livro A Marca Chamada Você é indicado na sessão livros do jornal Empresas&amp...
PPT
Cómo insertar imágenes en Blogger.com
PPTX
spring bed harga 2 jutaan
PPTX
Measuring mainstreaming progress, success and impact guidance
PPTX
Друзям меншим допормагаємо, птахи
PDF
Buku Garis Panduan Penulisan Disertasi/Tesis 2015
PPTX
La AstronomíA
Carmelo De Grazia Suárez: Bancamiga promoviendo el consumo eficiente
Elements_Jul07_CGU_DMGray
SicuroIMS Iraq Threat Assessment 3 - 16 August 2016
12 carmelo de grazia suárez bancamiga está donde tú estás
11 carmelo de grazia suarez servicios bancamiga
Ryno CV- December2015.
Livro A Marca Chamada Você é indicado na sessão livros do jornal Empresas&amp...
Cómo insertar imágenes en Blogger.com
spring bed harga 2 jutaan
Measuring mainstreaming progress, success and impact guidance
Друзям меншим допормагаємо, птахи
Buku Garis Panduan Penulisan Disertasi/Tesis 2015
La AstronomíA
Ad

Similar to Techday2010 Postgresql9 (20)

PDF
10 Reasons to Start Your Analytics Project with PostgreSQL
PDF
PostgreSQL 9.6 새 기능 소개
PDF
What's New In PostgreSQL 9.4
PDF
Get to know PostgreSQL!
PDF
PostgreSQL and PL/Java
ODP
PostgreSQL 8.4 TriLUG 2009-11-12
PDF
Postgres Vienna DB Meetup 2014
PDF
Countdown to PostgreSQL v9.5 - Foriegn Tables can be part of Inheritance Tree
KEY
PostgreSQL
PDF
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
PPTX
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
PPTX
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
PPTX
SQL Server Select Topics
PDF
Building node.js applications with Database Jones
DOCX
DataBase Management System Lab File
PPTX
Drupal II: The SQL
PPTX
Oracle Database 12c - New Features for Developers and DBAs
PPTX
Oracle Database 12c - New Features for Developers and DBAs
PPT
chap 9 dbms.ppt
10 Reasons to Start Your Analytics Project with PostgreSQL
PostgreSQL 9.6 새 기능 소개
What's New In PostgreSQL 9.4
Get to know PostgreSQL!
PostgreSQL and PL/Java
PostgreSQL 8.4 TriLUG 2009-11-12
Postgres Vienna DB Meetup 2014
Countdown to PostgreSQL v9.5 - Foriegn Tables can be part of Inheritance Tree
PostgreSQL
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
SQL Server Select Topics
Building node.js applications with Database Jones
DataBase Management System Lab File
Drupal II: The SQL
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
chap 9 dbms.ppt

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
A Presentation on Artificial Intelligence
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
CIFDAQ's Market Insight: SEC Turns Pro Crypto
The Rise and Fall of 3GPP – Time for a Sabbatical?
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
A Presentation on Artificial Intelligence
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Techday2010 Postgresql9

  • 1. What‘s new in PostgreSQL 9 Dan-Claudiu DRAGOŞ
  • 2. PostgreSQL 9 About PostgreSQL • This presentation was inspired by the sessions I‘ve attended at Fosdem 2010 • PostgreSQL means: – Standards followed closely – A true relational database – Complex, advanced features that puts it far ahead of its competition – The best tool to use for advanced database-based applications! • This comes at a cost: it can‘t compete with simpler database systems on poor hardware and writing „simple things“ tends to get harder 2
  • 3. PostgreSQL 9 Common Table Expressions • Available since version 8.4 • They are about defining an in-place data structure by the result of a query or a query combination • This data structure can then be queried further • Recursive join over this data structure is also possible • Some of this functionality was available before by using: • Views (named data structures stored into the database backend) • Subselects • The join recursion is a brand new feature 3
  • 4. PostgreSQL 9 Common Table Expressions • Basic usage WITH [RECURSIVE] name AS ( SELECT field1 FROM table WHERE field2=“abc” ) SELECT * FROM name ORDER BY field; 4
  • 5. PostgreSQL 9 Common Table Expressions • Join recursion – Assume we have the following corporate structure: -- department structure represented here is as follows: -- -- ROOT-+->A-+->B-+->C -- | | -- | +->D-+->F -- +->E-+->G – We want all the departments under A (or under any department, actually) 5
  • 6. PostgreSQL 9 Common Table Expressions • Join recursion CREATE TABLE department ( id INTEGER PRIMARY KEY, parent_department INTEGER REFERENCES department, name TEXT ); INSERT INTO department (id, parent_department, "name") VALUES (0, NULL, 'ROOT'), (1, 0, 'A'), (2, 1, 'B'), (3, 2, 'C'), (4, 2, 'D'), (5, 0, 'E'), (6, 4, 'F'), (7, 5, 'G'); 6
  • 7. PostgreSQL 9 Common Table Expressions • Join recursion WITH RECURSIVE subdepartment AS ( -- non-recursive term SELECT * FROM department WHERE name = 'A' UNION ALL -- recursive term SELECT d.* FROM department AS d JOIN subdepartment AS sd ON (d.parent_department = sd.id) ) SELECT * FROM subdepartment ORDER BY name; 7
  • 8. PostgreSQL 9 Common Table Expressions • Join recursion – The non-recursive term is optional – There are some restrictions when it comes to defining the recursive term: – No subquery using the recursive term (no double recursion) – No aggregate functions such as COUNT(*) – No HAVING, GROUP BY – No ORDER BY, no LIMIT – No FULL JOIN – The recursive term must be on the left hand side: » JOIN reference ON (recursiveterm.field = reference.field) • The database engine performs a brute-force, all-terms comparison so performance can sometimes be an issue 8
  • 9. PostgreSQL 9 Hot Standby and Streaming Replication • They are both about database synchronization • Hot Standby is a „push“ • Streaming Replication is a „pull“ • The issue at hand is duplicating the contents of a Master Database into one or more Slaves • Hot Standby means that every query ran on Master is also ran on the Slave; this also works for table locks! • Streaming replication means that every database transaction is requested by the Slave and then applied on it 9
  • 10. PostgreSQL 9 Deferrable Unique Constraints • This query sequence always fails: # CREATE TABLE test (a INT PRIMARY KEY); # INSERT INTO test values (1), (2); # UPDATE test SET a = a + 1; ERROR: duplicate key value violates unique constraint "test_pkey" DETAIL: Key (a)=(2) already exists. • What 9.0 version provides is that the uniqueness constraint may be checked after running the query, not as the query runs: # CREATE TABLE test (a INT PRIMARY KEY DEFERRABLE); 10
  • 11. PostgreSQL 9 Exclusion Constraints • Before 9.0, exclusion would be checked by using operator “=“ • Version 9.0 allows for a more complex approach # CREATE TABLE reservation ( room TEXT, speaker TEXT, during PERIOD); ALTER TABLE reservation ADD CONSTRAINT test_exclude EXCLUDE USING gist (room WITH =,during WITH &&); • The constraint is defined on 2 fields; the point is not having the same room reserved at the same time • The example uses 2 modules from „contrib“ (temporal and gist) 11
  • 12. PostgreSQL 9 Exclusion Constraints • Example using the previous table definition: # INSERT INTO reservation (speaker ,room, during) VALUES ( ‘Dan', ‘Flex Room', period('2010-12-10 12:00:00', '2010-12-10 13:00:00')); INSERT 0 1 # INSERT INTO reservation (speaker, room, during) VALUES ( ‘Catalin', ‘Presentation', period('2010-12-10 15:00:00', '2010-12-10 16:00:00')); INSERT 0 1 # INSERT INTO reservation (speaker ,room, during) VALUES ( ‘Andrei', ‘Flex Room', period('2010-12-10 12:30:00', '2010-12-10 13:00:00')); ERROR: conflicting key value violates exclusion constraint "test_exclude" DETAIL: Key (room, during)=(Flex Room, [2010-12-10 12:30:00+02, 2010-12- 10 13:00:00+02)) conflicts with existing key (room, during)=(Flex Room, [2010-12-10 12:00:00+02, 2010-12-10 13:00:00+02)). • This feature is available in PostgreSQL only 12
  • 13. Smaller enhancements • GRANT / REVOKE on Schemas – PostgreSQL has Databases -> Schemas -> Tables -> Fields – Until 9.0 you could grant (revoke) rights on Databases and Tables only – This functionality could be achieved before 9.0 by writing a script • Faster VACUUM FULL – This is the more or less equivalent to the OPTIMIZE query in MySQL – This command used to be pretty slow on large databases – In 9.0 it works by performing a copy of the database • Better 64bit support – 64bit binaries were already available on Linux – Windows native 64bit binary is the new thing in version 9.0 13
  • 14. PL/PgSQL enhancements • Unnamed functions – In 9.0 you can run code snippets in place – Before 9.0 you had to define a stored procedure and then call it DO language plpgsql $$ DECLARE vr record; BEGIN FOR vr IN SELECT tablename FROM pg_tables WHERE tableowner = ‘dan' AND schemaname NOT IN ('pg_catalog','information_schema') LOOP EXECUTE 'GRANT SELECT ON ' || vr.tablename || ' TO tester'; END LOOP; END $$; 14
  • 15. PL/PgSQL enhancements • Named parameter calls – Before 9.0 you had to provide all the function parameters and care for their order, as provided in the function definition – Version 8.4 added “default parameters”, allowing skipping certain parameters (they would be replaced with their default values) – Example function: CREATE FUNCTION test (a INT, b TEXT) RETURNS TEXT AS $$ DECLARE value TEXT; BEGIN value := 'a is ' || a::text || ' and b is ' || b; RETURN value; END; $$ LANGUAGE plpgsql; 15
  • 16. PL/PgSQL enhancements • Named parameter calls – Before 9.0 you would have called it this way: SELECT test(1,'foo'); test ------------------------- a is 1 and b is foo (1 row) – Version 9.0 allows this type of function call: SELECT test( b:='foo', a:=1); test ------------------------- a is 1 and b is foo (1 row) 16