Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does exclusion constraint `EXCLUDE USING gist (c WITH &&)` mean?

People also ask

What distinguish an exclusion constraint from a check constraint?

Whereas a CHECK constraint evaluates an expression based on a single row of the table, an EXCLUDE constraint evaluates a comparison of two rows in the table.

What is constraint in PSQL?

Constraints are the rules enforced on data columns on table. These are used to prevent invalid data from being entered into the database. This ensures the accuracy and reliability of the data in the database. Constraints could be column level or table level.

How do I find unique constraints in PostgreSQL?

SELECT conname FROM pg_constraint WHERE conrelid = (SELECT oid FROM pg_class WHERE relname LIKE 'tableName'); Also you can get it from pgAdmin in objects tree.

How do I view constraints in PostgreSQL?

Constraints of the table can be retrieved from catalog-pg-constraint. using the SELECT query. Just a detail: If you don't have tables with the same name in multiple schemas, in PSQL just \d+ {TABLE_NAME} works too.


Whereas a CHECK constraint evaluates an expression based on a single row of the table, an EXCLUDE constraint evaluates a comparison of two rows in the table. Think of it like a generalised UNIQUE constraint: instead of "no two rows can be equal", you can say things like "no two rows overlap", or even "no two rows can be different".

In order to achieve this without checking every possible combination of values, it needs an appropriate index structure which allows it to find possible violations when you insert or update a row. This is what the gist part of the declaration refers to: a particular type of index which can be used to speed up operations other than equality.

The remainder of the declaration is the constraint itself: c is the column being tested, and && is the operator which must not return true for any pair of rows. In this case, && is the "overlaps" operator as listed on the geometric operators manual page.

So put together, the constraint EXCLUDE USING gist (c WITH &&) translates to "no two values of c must overlap each other (more precisely, A.c && B.c must return false or null for all distinct rows A and B), and please use a gist index to monitor this constraint".