btree_gist: Let Scalar Columns Join GiST Exclusion Constraints

Last updated: 2026-11-04

btree_gist: Let Scalar Columns Join GiST Exclusion Constraints

GiST natively covers only a handful of types — geometries and ranges — so plain int or text columns cannot join an EXCLUDE USING gist constraint. btree_gist adds GiST operator classes for scalar types, which lets a rule such as "no overlapping time ranges within the same room number" be expressed as a single exclusion constraint instead of a hand-written trigger. It also makes scalar columns usable in GiST indexes over range types.

Kindextension
Example LanguageSQL

📝 Syntax

CREATE EXTENSION IF NOT EXISTS btree_gist;

CREATE TABLE t_booking (
    id     int PRIMARY KEY,
    room   int NOT NULL,
    during tstzrange NOT NULL,
    EXCLUDE USING gist (room WITH =, during WITH &&)
);

Returns:CREATE EXTENSION returns success on completion. The extension itself returns nothing useful: it installs a set of operator classes (int4_ops, text_ops, numeric_ops and friends), and its effect shows up as working GiST indexes and exclusion constraints.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

SELECT id, room, during, status FROM t_gist_attempt ORDER BY id
 id | room | during                                                  | status
----+------+----------------------------------------------------------+-------------------
  1 |  101 | ["2026-01-01 00:00:00+00","2026-02-01 00:00:00+00")      | OK
  2 |  202 | ["2026-01-01 00:00:00+00","2026-03-01 00:00:00+00")      | OK
  3 |  101 | ["2026-01-15 00:00:00+00","2026-02-15 00:00:00+00")      | rejected: overlaps id=1
(3 rows)

Note: row 3 shares room 101 with row 1 and overlaps its time range, so the exclusion constraint rejected it; a PL/pgSQL EXCEPTION block caught the violation and logged it to the audit table.

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QExclusion constraint or trigger?
APrefer an exclusion constraint wherever it can express the rule. PostgreSQL enforces it atomically and correctly under concurrency, whereas a trigger is easy to get subtly wrong and prone to missing concurrent conflicts. With btree_gist installed, a scalar column combined with a range column goes straight into EXCLUDE USING gist.
QCan btree_gist replace a regular btree index?
ANo. btree_gist merely adds GiST operator classes for scalar types so they can take part in GiST indexes and GiST exclusion constraints. Equality lookups, range scans and unique constraints should still use a plain btree index — it is smaller and faster.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏