SP-GiST Index (Extensible Partition Tree, Approximately Ordered)
Last updated: 2026-11-04
SP-GiST Index (Extensible Partition Tree, Approximately Ordered)
SP-GiST (Space-partitioned GiST) is GiST's close relative: it partitions data into disjoint regions and backs each region with a locally ordered tree. Several implementations ship with PostgreSQL — point uses a KD-Tree for two-dimensional nearest-neighbour (KNN) ordering, inet a radix tree for prefix containment, text a radix tree for prefix matching. This example uses the built-in point type to demonstrate SP-GiST's distinctive KNN ordering.
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE INDEX <index_name> ON <table_name> USING spgist ( <column> )
⚙️ Parameters
| index_name | Name of the new index. |
|---|---|
| table_name | The table to index. |
| column_name | The indexed column; built-in support covers point (KD-Tree), inet (radix tree), text (radix tree), box, polygon and range types. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ 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:
CREATE TABLE
INSERT 0 5000
CREATE INDEX
ANALYZE
-- First EXPLAIN: the <-> distance ordering is served by the SP-GiST index, no sort node needed
Limit (cost=0.15..0.38 rows=3 width=19)
-> Index Scan using idx_spgist_pos on idx_demo_spgist (cost=0.15..388.15 rows=5000 width=19)
Order By: (pos <-> '(500,500)'::point)
Index Searches: 1
Buffers: shared hit=6
Planning Time: 0.213 ms
Execution Time: 0.486 ms
-- Second EXPLAIN: the << positional predicate hits the SP-GiST index
Bitmap Heap Scan on idx_demo_spgist (cost=20.02..63.27 rows=500 width=11)
Recheck Cond: (pos << '(900,900)'::point)
Heap Blocks: exact=37
Buffers: shared hit=123
-> Bitmap Index Scan on idx_spgist_pos (cost=0.00..19.90 rows=500 width=0)
Index Cond: (pos << '(900,900)'::point)
Index Searches: 1
Buffers: shared hit=86
Planning Time: 0.052 ms
Execution Time: 2.722 ms
-- Query result
label
-------------
sensor-500
sensor-1500
sensor-2500
(3 rows)
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.)
📌 Key Points
- The point operator class for SP-GiST is a KD-Tree (kd_point_ops); two-dimensional KNN ordering such as ORDER BY pos <-> point(...) is served straight from the index with no extra Sort node.
- Positional comparisons use << (left/below), >> (right/above), &< and &>. There is no <@ operator for point — point <@ point raises an error.
- inet has a native SP-GiST operator class (inet_ops) that supports prefix containment with <<=, without needing btree_gist.
- For text, SP-GiST uses a radix tree, which suits prefix matching. It provides no locale-aware collation, so it is not the right choice for multilingual sorting.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QBoth SP-GiST and GiST handle spatial queries — how do I choose?
AGiST is the general framework, pairing each type with an extensible set of predicates and covering many cases. SP-GiST is the space-partitioned implementation with purpose-built structures (KD-Tree for nearest neighbour, radix tree for prefixes). SP-GiST shines for KNN ordering; GiST is the safer general choice.
QWhy does the KNN plan contain no Sort node?
AThe KD-Tree stores data in spatial order, so the index scan emits rows nearest-first. The ORDER BY pos <-> point(...) is therefore satisfied by the index itself and the separate sort is avoided.