SlideShare a Scribd company logo
1 of 19
Download to read offline
Not just UNIQUE




    Jeff Davis
   Truviso, Inc.




                   1
Why is UNIQUE so unique?
●   Only constraint where two tuples can
     conflict with eachother
       –   That is, the existence of one tuple
            precludes others from existing
●   Effectively a predicate lock on a very
     simple predicate
●   Special code to enforce unique constraint
     in the BTree code – doesn't work GiST,
     etc.

                                                 2
Motivation for More General
        Constraints
●   PERIOD data type can represent a period
     of time:
       –   http://pgfoundry.org/projects/temporal
●   Definite beginning and end time, e.g., the
     period of time during which a professor
     is teaching in a classroom
●   But two professors can't teach in the
     same classroom at the same time
●   So periods of time cannot overlap
                                                    3
Digression: Temporal Data
●   Managing temporal data effectively is
     important and a very common problem.
●   Hopefully you saw Scott Bailey's talk
     earlier – if not, wait for the video.
●   Enforcing non-overlapping constraints is
     one of the few pieces that requires good
     support in the core PostgreSQL engine.



                                                4
Non-Overlapping Constraint
●   Very commonly known as a “schedule
     conflict”
●   How do you specify a non-overlapping
     constraint in PostgreSQL currently?
●   Any ideas?




                                           5
Non-Overlapping Constraint
●   Very commonly known as a “schedule
     conflict”
●   How do you specify a non-overlapping
     constraint in PostgreSQL currently?
●   Any ideas?
●   Most people don't enforce in DBMS
●   Maybe with a trigger that takes a full table
     lock?
       –   Horrible performance
                                                   6
Non-Overlapping Constraint
●   If the constraint is not enforced by the
       database...
●   ...then it will be enforced when two
       professors each believe they have
       reserved the same room
●   A duel?
●   Probably a less desirable constraint
     enforcement mechanism than a friendly
     error from the DBMS
                                               7
Operator Exclusion
           Constraints
●   New feature
●   Working on getting it accepted for
     PostgreSQL 8.5
●   Offers more general constraint
     enforcement mechanism




                                         8
Example

CREATE TABLE reservation
(
  room      TEXT,
  professor TEXT,
  during    PERIOD,
  EXCLUSION USING gist
    (room   CHECK WITH =,
     during CHECK WITH &&)
);
                             9
Example
CREATE TABLE reservation
(
  room      TEXT,
  professor TEXT,
  during    PERIOD,
  EXCLUSION USING gist
    (room   CHECK WITH =,
     during CHECK WITH &&)
);

Can be arbitrary expression of fields in table.
                                                  10
Example
CREATE TABLE reservation
(
  room      TEXT,
  professor TEXT,
  during    PERIOD,
  EXCLUSION USING gist
    (room   CHECK WITH =,
     during CHECK WITH &&)
);

Exclusion operator. In this case, “overlaps”.
                                                11
Example
CREATE TABLE reservation
(
  room      TEXT,
  professor TEXT,
  during    PERIOD,
  EXCLUSION USING gist
    (room   CHECK WITH =,
     during CHECK WITH &&)
);

Type of index to build and use for
enforcement.                         12
Operator Detects Conflicts
●   The operator is used to search for
     conflicts
●   Should return TRUE when two values
     conflict
●   Should return TRUE when two values
     conflict
●   So the “overlaps” operator (“&&”) would
     be used to enforce the constraint that
     no two tuples contain overlapping
     values
                                              13
Back to UNIQUE
●   If you specify all operators as “=”, the
       semantics are identical to UNIQUE
●   Performance slightly worse, because one
     additional index search is required
●   But can be used with GiST




                                               14
UNUNIQUE
●   If you specify all operators as “<>”, then
       constraint is the opposite of unique: all
       values must be the same!
●   However, won't work for types that don't
     have a search strategy for “<>”.
●   Not obviously useful, but it's there.




                                                   15
Multi-Column Constraints
●   ... EXCLUSION USING gist
           (a CHECK WITH =,
            b CHECK WITH &&) ...
●   Tuple1 conflicts with Tuple2 if and only if:
       –   Tuple1.a =  Tuple2.a AND
       –   Tuple1.b && Tuple2.b
●   Otherwise, both tuples can appear in the
     table.

                                                   16
Extra Capabilities
●   Support for predicates (WHERE)
       –   Constraint on a subset of the table
●   Support for arbitrary expressions
       –   ... EXCLUSION ((t::circle) 
            CHECK WITH &&) ...
●   Can use other tablespaces and index
     parameters, similar to UNIQUE.
●   Deferrable (work in progress)
●   GIN support (work in progress)
                                                 17
Future Work
●   Multiple constraints can use the same
     index
●   UNIQUE(a, b) and UNIQUE(a, c) can
     both use an index on (a, b, c)
●   Depending on selectivity of “a”, may
     perform much better than two separate
     indexes



                                             18
Conclusion
●   Constraints are always enforced
●   Sometimes by the DBMS (cheap),
     sometimes by real life (expensive)
●   The very simple, very common “schedule
     conflict” constraint is almost impossible
     to enforce with most DBMSs
●   Let's make it easy, scalable, and flexible.
●   “Operator Exclusion Constraints” in 8.5

                                                  19

More Related Content

Viewers also liked

Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...Command Prompt., Inc
 
configuring a warm standby, the easy way
configuring a warm standby, the easy wayconfiguring a warm standby, the easy way
configuring a warm standby, the easy wayCommand Prompt., Inc
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorCommand Prompt., Inc
 
Implementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenImplementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenCommand Prompt., Inc
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentationCommand Prompt., Inc
 
Elephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksElephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksCommand Prompt., Inc
 
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableHowdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableCommand Prompt., Inc
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsCommand Prompt., Inc
 
Postgres in Production - Best Practices 2014
Postgres in Production - Best Practices 2014Postgres in Production - Best Practices 2014
Postgres in Production - Best Practices 2014EDB
 
PostgreSQL - El camino de la disponibilidad
PostgreSQL - El camino de la disponibilidadPostgreSQL - El camino de la disponibilidad
PostgreSQL - El camino de la disponibilidadLenin Hernandez
 

Viewers also liked (17)

Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
 
configuring a warm standby, the easy way
configuring a warm standby, the easy wayconfiguring a warm standby, the easy way
configuring a warm standby, the easy way
 
Pg migrator
Pg migratorPg migrator
Pg migrator
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL Replicator
 
Go replicator
Go replicatorGo replicator
Go replicator
 
Backup and-recovery2
Backup and-recovery2Backup and-recovery2
Backup and-recovery2
 
Implementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenImplementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with Tungsten
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentation
 
A Practical Multi-Tenant Cluster
A Practical Multi-Tenant ClusterA Practical Multi-Tenant Cluster
A Practical Multi-Tenant Cluster
 
Temporal Data
Temporal DataTemporal Data
Temporal Data
 
Elephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksElephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forks
 
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableHowdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
 
Postgres in Production - Best Practices 2014
Postgres in Production - Best Practices 2014Postgres in Production - Best Practices 2014
Postgres in Production - Best Practices 2014
 
PostgreSQL - El camino de la disponibilidad
PostgreSQL - El camino de la disponibilidadPostgreSQL - El camino de la disponibilidad
PostgreSQL - El camino de la disponibilidad
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 

Similar to Not Just UNIQUE: Generalized Index Constraints

Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Ramith Jayasinghe
 
(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_iiNico Ludwig
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java ConcurrencyBen Evans
 
Parallel builds in Eclipse IDE workspace
Parallel builds in Eclipse IDE workspaceParallel builds in Eclipse IDE workspace
Parallel builds in Eclipse IDE workspaceMickael Istria
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaEddy Reyes
 
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018Mickael Istria
 
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)Ontico
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
Performance optimization techniques for Java code
Performance optimization techniques for Java codePerformance optimization techniques for Java code
Performance optimization techniques for Java codeAttila Balazs
 
Benchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbersBenchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbersJustin Dorfman
 
Benchmarks, performance, scalability, and capacity what s behind the numbers...
Benchmarks, performance, scalability, and capacity  what s behind the numbers...Benchmarks, performance, scalability, and capacity  what s behind the numbers...
Benchmarks, performance, scalability, and capacity what s behind the numbers...james tong
 
Mongo nyc nyt + mongodb
Mongo nyc nyt + mongodbMongo nyc nyt + mongodb
Mongo nyc nyt + mongodbDeep Kapadia
 
Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)Marsan Ma
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with EpsilonSina Madani
 
Building scalable application with sql server
Building scalable application with sql serverBuilding scalable application with sql server
Building scalable application with sql serverChris Adkin
 

Similar to Not Just UNIQUE: Generalized Index Constraints (20)

Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java Concurrency
 
Parallel builds in Eclipse IDE workspace
Parallel builds in Eclipse IDE workspaceParallel builds in Eclipse IDE workspace
Parallel builds in Eclipse IDE workspace
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and Gomega
 
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018
 
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
 
Spock pres
Spock presSpock pres
Spock pres
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Performance optimization techniques for Java code
Performance optimization techniques for Java codePerformance optimization techniques for Java code
Performance optimization techniques for Java code
 
Benchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbersBenchmarks, performance, scalability, and capacity what's behind the numbers
Benchmarks, performance, scalability, and capacity what's behind the numbers
 
Benchmarks, performance, scalability, and capacity what s behind the numbers...
Benchmarks, performance, scalability, and capacity  what s behind the numbers...Benchmarks, performance, scalability, and capacity  what s behind the numbers...
Benchmarks, performance, scalability, and capacity what s behind the numbers...
 
Mongo nyc nyt + mongodb
Mongo nyc nyt + mongodbMongo nyc nyt + mongodb
Mongo nyc nyt + mongodb
 
Rails DB migrate SAFE.pdf
Rails DB migrate SAFE.pdfRails DB migrate SAFE.pdf
Rails DB migrate SAFE.pdf
 
Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
 
rsyslog meets docker
rsyslog meets dockerrsyslog meets docker
rsyslog meets docker
 
Instant DBMS Homework Help
Instant DBMS Homework HelpInstant DBMS Homework Help
Instant DBMS Homework Help
 
Building scalable application with sql server
Building scalable application with sql serverBuilding scalable application with sql server
Building scalable application with sql server
 

More from Command Prompt., Inc

Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Command Prompt., Inc
 
Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Command Prompt., Inc
 
Integrating PostGIS in Web Applications
Integrating PostGIS in Web ApplicationsIntegrating PostGIS in Web Applications
Integrating PostGIS in Web ApplicationsCommand Prompt., Inc
 
Postgres for MySQL (and other database) people
Postgres for MySQL (and other database) peoplePostgres for MySQL (and other database) people
Postgres for MySQL (and other database) peopleCommand Prompt., Inc
 
Building Grails applications with PostgreSQL
Building Grails applications with PostgreSQLBuilding Grails applications with PostgreSQL
Building Grails applications with PostgreSQLCommand Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLCommand Prompt., Inc
 

More from Command Prompt., Inc (10)

Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2
 
Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1
 
Integrating PostGIS in Web Applications
Integrating PostGIS in Web ApplicationsIntegrating PostGIS in Web Applications
Integrating PostGIS in Web Applications
 
Postgres for MySQL (and other database) people
Postgres for MySQL (and other database) peoplePostgres for MySQL (and other database) people
Postgres for MySQL (and other database) people
 
Building Grails applications with PostgreSQL
Building Grails applications with PostgreSQLBuilding Grails applications with PostgreSQL
Building Grails applications with PostgreSQL
 
Pg amqp
Pg amqpPg amqp
Pg amqp
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
Database Hardware Benchmarking
Database Hardware BenchmarkingDatabase Hardware Benchmarking
Database Hardware Benchmarking
 
Vertically Challenged
Vertically ChallengedVertically Challenged
Vertically Challenged
 
Simpycity and Exceptable
Simpycity and ExceptableSimpycity and Exceptable
Simpycity and Exceptable
 

Not Just UNIQUE: Generalized Index Constraints