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

PDF
Not Just UNIQUE: Exclusion Constraints
ODP
Basic Query Tuning Primer
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PDF
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
ODP
The PostgreSQL Query Planner
PDF
Mastering PostgreSQL Administration
PDF
PostgreSQL High Availability via SLONY and PG POOL II
Not Just UNIQUE: Exclusion Constraints
Basic Query Tuning Primer
pg_proctab: Accessing System Stats in PostgreSQL
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
The PostgreSQL Query Planner
Mastering PostgreSQL Administration
PostgreSQL High Availability via SLONY and PG POOL II

Viewers also liked (17)

PPT
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
PDF
configuring a warm standby, the easy way
PDF
PDF
Replication using PostgreSQL Replicator
PDF
Go replicator
PDF
Backup and-recovery2
PDF
Implementing the Future of PostgreSQL Clustering with Tungsten
ODP
Python utilities for data presentation
PDF
A Practical Multi-Tenant Cluster
PDF
Temporal Data
PDF
Elephant Roads: a tour of Postgres forks
PDF
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
ODP
Introduction to triggers
ODP
PostgreSQL Administration for System Administrators
PDF
Postgres in Production - Best Practices 2014
 
PDF
PostgreSQL - El camino de la disponibilidad
PDF
5 Steps to PostgreSQL Performance
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
configuring a warm standby, the easy way
Replication using PostgreSQL Replicator
Go replicator
Backup and-recovery2
Implementing the Future of PostgreSQL Clustering with Tungsten
Python utilities for data presentation
A Practical Multi-Tenant Cluster
Temporal Data
Elephant Roads: a tour of Postgres forks
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Introduction to triggers
PostgreSQL Administration for System Administrators
Postgres in Production - Best Practices 2014
 
PostgreSQL - El camino de la disponibilidad
5 Steps to PostgreSQL Performance
Ad

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

PPTX
Concurrency - Why it's hard ?
PPTX
Why Concurrency is hard ?
PDF
(3) c sharp introduction_basics_part_ii
KEY
Modern Java Concurrency
ODP
Parallel builds in Eclipse IDE workspace
PPTX
TDD in Go with Ginkgo and Gomega
ODP
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018
PDF
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
PDF
Spock pres
PDF
Performance optimization techniques for Java code
PDF
How we operate CEPH at scale by Digital Ocean
PDF
Benchmarks, performance, scalability, and capacity what s behind the numbers...
PDF
Benchmarks, performance, scalability, and capacity what's behind the numbers
PDF
Mongo nyc nyt + mongodb
PDF
Rails DB migrate SAFE.pdf
PDF
Universal job embedding in recommendation (public ver.)
PPTX
Distributed Model Validation with Epsilon
PPTX
presentation des google test dans un contexte de tdd
ODP
rsyslog meets docker
PPTX
Instant DBMS Homework Help
Concurrency - Why it's hard ?
Why Concurrency is hard ?
(3) c sharp introduction_basics_part_ii
Modern Java Concurrency
Parallel builds in Eclipse IDE workspace
TDD in Go with Ginkgo and Gomega
Parallel builds in Eclipse IDE workspace - EclispeCon Europe 2018
PostgreSQL Configuration for Humans / Alvaro Hernandez (OnGres)
Spock pres
Performance optimization techniques for Java code
How we operate CEPH at scale by Digital Ocean
Benchmarks, performance, scalability, and capacity what s behind the numbers...
Benchmarks, performance, scalability, and capacity what's behind the numbers
Mongo nyc nyt + mongodb
Rails DB migrate SAFE.pdf
Universal job embedding in recommendation (public ver.)
Distributed Model Validation with Epsilon
presentation des google test dans un contexte de tdd
rsyslog meets docker
Instant DBMS Homework Help
Ad

More from Command Prompt., Inc (10)

PPT
Normalization: A Workshop for Everybody Pt. 2
PPT
Normalization: A Workshop for Everybody Pt. 1
PDF
Integrating PostGIS in Web Applications
PDF
Postgres for MySQL (and other database) people
PDF
Building Grails applications with PostgreSQL
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PDF
Database Hardware Benchmarking
PDF
Vertically Challenged
PDF
Simpycity and Exceptable
Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 1
Integrating PostGIS in Web Applications
Postgres for MySQL (and other database) people
Building Grails applications with PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Database Hardware Benchmarking
Vertically Challenged
Simpycity and Exceptable

Not Just UNIQUE: Generalized Index Constraints