SlideShare a Scribd company logo
1 of 27
Download to read offline
Not just UNIQUE:
Exclusion Constraints



       Jeff Davis
      Truviso, Inc.




                        1
Schedule Conflict
●   One person can't be in two places at the 
     same time
●   Two people can't use an exclusive 
     resource at the same time
●   Business constraint
●   Just one instance of an interesting class 
      of problems


                                                 2
PERIOD Type
●   Need a concept of a period of time, 
     otherwise the system doesn't know what 
     a “schedule conflict” is
●   PERIOD data type:
       –   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

                                                    3
Constraints
●   Foreign Key
       –   Requires another tuple to exist
●   CHECK Constraint
       –   Only looks inside one tuple, can't compare 
            two tuples
       –   You can subvert the system by using a 
            UDF
               ●   Race conditions


                                                     4
Why is UNIQUE so unique?
●   Only constraint where two tuples can 
     conflict with eachother
       –   If x = 5 in one tuple, there can be no other 
              tuple where x = 5
●   Effectively a predicate lock on a very 
     simple predicate
       –   Transactions wait on other transactions 
             trying to insert the same value
●   Special code path inside Btree to enforce 
     uniqueness (doesn't work for GiST, etc.) 5
                         
Almost...
●   UNIQUE is almost what we need, but not 
     quite.
●   Need something that can understand 
     “overlaps”




                                              6
What's the Point?
●   Very common business constraint known 
     as a “schedule conflict”
●   Claims:
       –   Existing solutions in SQL are bad...




                                                  7
What's the Point?
●   Very common business constraint known 
     as a “schedule conflict”
●   Claims:
       –   Existing solutions in SQL are bad...
       –   ...except Exclusion Constraints in 
              PostgreSQL 9.0.




                                                  8
Non­Overlapping Constraint
●   Let's look at existing solutions for 
     schedule conflicts
●   How do you specify a non­overlapping 
     constraint in an SQL system?
       –   Any SQL system?
●   Ideas?



                                            9
Idea 1: Serialize
●   Only one writer
       –   Exclusive lock
●   Before updating any reservation, search 
     all existing reservations for conflicts
●   Horrible performance, availability 
     problems



                                               10
Idea 2: Quantize
●   Break into time slices, e.g. 1 hour.
●   Use UNIQUE constraint on beginning
●   Imposes unnecessary business constraint
       –   Nobody can reserve 1:30pm – 2:30pm
●   Code is not reusable for other businesses
       –   Hotels reserve by day
●   Not useful when quantum is too small
       –   Security, scientific apps, audit logs, etc.
                                                         11
Idea 3: Procedural Code
●   Triggers
●   Perhaps use dummy rows that exist only 
     for row­level locks
●   Perhaps application code
●   Probably will not perform well
●   Very business­specific, not reusable
●   Error prone
●   Good luck...
                                              12
Idea 4: Delayed Check
●   Record timestamp when reservation was 
     recorded
●   Make extra process check for conflicts 
     and notify victims asynchronously
●   Unhappy customers
●   Adds uncertainty after “commit”
●   Cascading problem

                                              13
Back to the Example
●   If the constraint is not enforced by the 
       DBMS...
●   ...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
                                                14
Exclusion Constraints
●   New feature in 9.0
●   Not constraint_exclusion!
●   Offers constraint enforcement mechanism 
     more general than UNIQUE
●   Declarative
●   Scalable
       –   Performance comparable to UNIQUE

                                              15
Example

CREATE TABLE reservation
(
  room      TEXT,
  professor TEXT,
  during    PERIOD,
  EXCLUDE   USING gist
    (room   WITH =,
     during WITH &&)
);
                           16
Example
CREATE TABLE reservation
(
  room      TEXT,
  professor TEXT,
  during    PERIOD,
  EXCLUDE   USING gist
    (room   WITH =,
     during WITH &&)
);

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

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

Type of index to build and use for 
enforcement.                          19
That was Easy
●   3 lines
●   Scalable
●   Flexible
●   Declarative
●   Robust
●   Safe during concurrent INSERTs and 
     UPDATEs

                                          20
Operator Detects Conflicts
●   The operator is used to search for 
     conflicts
●   Should return TRUE when two values 
     conflict
●   For example, the “overlaps” operator 
     (“&&”) would be used to enforce the 
     constraint that no two tuples contain 
     overlapping values

                                              21
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
●   Can be used with GiST or Hash indexes




                                                22
UNUNIQUE
●   If you specify the operator as “<>”, then 
       constraint is the opposite of UNIQUE: all 
       values must be the same!
●   Use case: At the zoo, if you've already put 
     zebras in the cage, you can put more 
     zebras in ­­ but don't put lions in.




                                                23
Multi­Column Constraints
... EXCLUDE USING gist
       (a WITH =,
        b 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.

                                               24
Demo



DEMO



       25
Extra Capabilities
●   Support for predicates (WHERE)
    ●   Constraint on a subset of the table
●   Support for arbitrary expressions:
        ... EXCLUDE ((t::circle)
             WITH &&) ...
●   Deferrable
●   Doesn't work with GIN, yet.


                                              26
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.
●   “Exclusion Constraints” in 9.0!
                                                  27

More Related Content

What's hot

What's hot (20)

Docker Swarm for Beginner
Docker Swarm for BeginnerDocker Swarm for Beginner
Docker Swarm for Beginner
 
今だからこそ知りたい Docker Compose/Swarm 入門
今だからこそ知りたい Docker Compose/Swarm 入門今だからこそ知りたい Docker Compose/Swarm 入門
今だからこそ知りたい Docker Compose/Swarm 入門
 
Kubernetes internals (Kubernetes 해부하기)
Kubernetes internals (Kubernetes 해부하기)Kubernetes internals (Kubernetes 해부하기)
Kubernetes internals (Kubernetes 해부하기)
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
プログラミング言語Clojureのニャンパスでの活用事例
プログラミング言語Clojureのニャンパスでの活用事例プログラミング言語Clojureのニャンパスでの活用事例
プログラミング言語Clojureのニャンパスでの活用事例
 
PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門
 
ガード節を使おう
ガード節を使おうガード節を使おう
ガード節を使おう
 
containerd summit - Deep Dive into containerd
containerd summit - Deep Dive into containerdcontainerd summit - Deep Dive into containerd
containerd summit - Deep Dive into containerd
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design Patterns
 
Introduction to docker
Introduction to dockerIntroduction to docker
Introduction to docker
 
Java8から17へ
Java8から17へJava8から17へ
Java8から17へ
 
Docker networking Tutorial 101
Docker networking Tutorial 101Docker networking Tutorial 101
Docker networking Tutorial 101
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
ClickHouse Monitoring 101: What to monitor and how
ClickHouse Monitoring 101: What to monitor and howClickHouse Monitoring 101: What to monitor and how
ClickHouse Monitoring 101: What to monitor and how
 
Docker swarm
Docker swarmDocker swarm
Docker swarm
 
Get Hands-On with NGINX and QUIC+HTTP/3
Get Hands-On with NGINX and QUIC+HTTP/3Get Hands-On with NGINX and QUIC+HTTP/3
Get Hands-On with NGINX and QUIC+HTTP/3
 
A Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in DepthA Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in Depth
 
Learned from KIND
Learned from KIND Learned from KIND
Learned from KIND
 
Introduction à docker.io
Introduction à docker.ioIntroduction à docker.io
Introduction à docker.io
 
Inside FastEnum
Inside FastEnumInside FastEnum
Inside FastEnum
 

Viewers also liked

Oracle Sandbox
Oracle SandboxOracle Sandbox
Oracle Sandbox
Datavail
 
The Renaissance of Stable Value: Capital Preservation in Defined Contribution
The Renaissance of Stable Value: Capital Preservation in Defined ContributionThe Renaissance of Stable Value: Capital Preservation in Defined Contribution
The Renaissance of Stable Value: Capital Preservation in Defined Contribution
Callan
 
The Process Of Portfolio Management
The Process Of Portfolio ManagementThe Process Of Portfolio Management
The Process Of Portfolio Management
Hitesh Kukreja
 
Investment alternative
Investment alternativeInvestment alternative
Investment alternative
Dharmik
 
Investment meaning nature
Investment meaning natureInvestment meaning nature
Investment meaning nature
reema21
 
Theory of constraints
Theory of constraintsTheory of constraints
Theory of constraints
MOHD ARISH
 

Viewers also liked (19)

Microsoft SQL Server Analysis Services Multidimensional
Microsoft SQL Server Analysis Services MultidimensionalMicrosoft SQL Server Analysis Services Multidimensional
Microsoft SQL Server Analysis Services Multidimensional
 
Oracle Sandbox
Oracle SandboxOracle Sandbox
Oracle Sandbox
 
The Renaissance of Stable Value: Capital Preservation in Defined Contribution
The Renaissance of Stable Value: Capital Preservation in Defined ContributionThe Renaissance of Stable Value: Capital Preservation in Defined Contribution
The Renaissance of Stable Value: Capital Preservation in Defined Contribution
 
Video 2.2 investment strategies
Video 2.2   investment strategiesVideo 2.2   investment strategies
Video 2.2 investment strategies
 
Investment strategies-to-grow-your-assets
Investment strategies-to-grow-your-assetsInvestment strategies-to-grow-your-assets
Investment strategies-to-grow-your-assets
 
The Process Of Portfolio Management
The Process Of Portfolio ManagementThe Process Of Portfolio Management
The Process Of Portfolio Management
 
Investment strategies-to-grow-your-income
Investment strategies-to-grow-your-incomeInvestment strategies-to-grow-your-income
Investment strategies-to-grow-your-income
 
Investment strategies of famous investment gurus
Investment strategies of famous investment gurusInvestment strategies of famous investment gurus
Investment strategies of famous investment gurus
 
Investment alternative
Investment alternativeInvestment alternative
Investment alternative
 
2015.01.21 ACG cup (M&A case competition)
2015.01.21 ACG cup (M&A case competition)2015.01.21 ACG cup (M&A case competition)
2015.01.21 ACG cup (M&A case competition)
 
ACG Cup 2nd round case competition final presentation
ACG Cup 2nd round case competition final presentationACG Cup 2nd round case competition final presentation
ACG Cup 2nd round case competition final presentation
 
Portfolio management process
Portfolio management processPortfolio management process
Portfolio management process
 
investment
investmentinvestment
investment
 
Global Marketing
Global MarketingGlobal Marketing
Global Marketing
 
Investment meaning nature
Investment meaning natureInvestment meaning nature
Investment meaning nature
 
Equality & Equity
Equality & EquityEquality & Equity
Equality & Equity
 
Project Portfolio Management
Project Portfolio ManagementProject Portfolio Management
Project Portfolio Management
 
Theory of constraints
Theory of constraintsTheory of constraints
Theory of constraints
 
Types of investment
Types of investmentTypes of investment
Types of investment
 

Similar to Not Just UNIQUE: Exclusion Constraints

The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
Bill Buchan
 
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
Justin Dorfman
 
Rules Programming tutorial
Rules Programming tutorialRules Programming tutorial
Rules Programming tutorial
Srinath Perera
 

Similar to Not Just UNIQUE: Exclusion Constraints (20)

Not Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index ConstraintsNot Just UNIQUE: Generalized Index Constraints
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 ?
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Modern Java Concurrency
Modern Java ConcurrencyModern Java Concurrency
Modern Java Concurrency
 
Cassandra in production
Cassandra in productionCassandra in production
Cassandra in production
 
Elephant grooming: quality with Hadoop
Elephant grooming: quality with HadoopElephant grooming: quality with Hadoop
Elephant grooming: quality with Hadoop
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
 
Mongo nyc nyt + mongodb
Mongo nyc nyt + mongodbMongo nyc nyt + mongodb
Mongo nyc nyt + mongodb
 
(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
 
TS 5341 Rethinking the ESB
TS 5341 Rethinking the ESBTS 5341 Rethinking the ESB
TS 5341 Rethinking the ESB
 
Series of Unfortunate Netflix Container Events - QConNYC17
Series of Unfortunate Netflix Container Events - QConNYC17Series of Unfortunate Netflix Container Events - QConNYC17
Series of Unfortunate Netflix Container Events - QConNYC17
 
The Future of zHeap
The Future of zHeapThe Future of zHeap
The Future of zHeap
 
Cassandra On EC2
Cassandra On EC2Cassandra On EC2
Cassandra On EC2
 
Lect04
Lect04Lect04
Lect04
 
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...
 
Parallel builds in Eclipse IDE workspace
Parallel builds in Eclipse IDE workspaceParallel builds in Eclipse IDE workspace
Parallel builds in Eclipse IDE workspace
 
Making Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF UsableMaking Strongly-typed NETCONF Usable
Making Strongly-typed NETCONF Usable
 
Rules Programming tutorial
Rules Programming tutorialRules Programming tutorial
Rules Programming tutorial
 

More from Command Prompt., Inc

Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
Command Prompt., Inc
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL Replicator
Command 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 PostgreSQL
Command Prompt., Inc
 

More from Command Prompt., Inc (20)

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
 
Backup and-recovery2
Backup and-recovery2Backup and-recovery2
Backup and-recovery2
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
Temporal Data
Temporal DataTemporal Data
Temporal Data
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL Replicator
 
Go replicator
Go replicatorGo replicator
Go replicator
 
Pg migrator
Pg migratorPg migrator
Pg migrator
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentation
 
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
 
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
 
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
 
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
 
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
 
Bucardo
BucardoBucardo
Bucardo
 
Basic Query Tuning Primer
Basic Query Tuning PrimerBasic Query Tuning Primer
Basic Query Tuning Primer
 
A Practical Multi-Tenant Cluster
A Practical Multi-Tenant ClusterA Practical Multi-Tenant Cluster
A Practical Multi-Tenant Cluster
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 
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
 

Not Just UNIQUE: Exclusion Constraints