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.

Kindindex
Example LanguageSQL

📝 Syntax

CREATE INDEX <index_name> ON <table_name> USING spgist ( <column> )

⚙️ Parameters

index_nameName of the new index.
table_nameThe table to index.
column_nameThe 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

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.
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%

🙏 帮我们做得更好

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

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