Databases 17 min read

Installing and Using the IP4R Extension for IP Address Range Queries in PostgreSQL

This article explains how to install the IP4R PostgreSQL extension via PGXS, describes its custom IP address and range data types, demonstrates common queries, functions, operators, and GiST indexing techniques, and compares its performance against bigint‑based lookups for efficient IP range handling.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Installing and Using the IP4R Extension for IP Address Range Queries in PostgreSQL

IP4R is a PostgreSQL extension that adds lightweight IP address and range data types (ip4, ip4r, ip6, ip6r, ipaddress, iprange) with dedicated operators and functions, enabling fast containment and overlap queries that the built‑in inet/cidr types cannot efficiently support.

Installation (PGXS) can be performed with the following commands:

sudo -s
wget https://github.com/RhodiumToad/ip4r/archive/2.4.tar.gz
tar -zxvf 2.4.tar.gz
cd ip4r-2.4
env USE_PGXS=1 make
env USE_PGXS=1 make install

After installation, enable the extension in a database with CREATE EXTENSION ip4r; and verify it with \dx ip4r.

Data types and conversions are summarized in the table below, showing how each IP type can be cast to text, numeric, bigint, float8, varbit, bytea, and CIDR, e.g., ip4::bigint or bigint::ip4. The module also provides functions such as family(ipX), ipX_net_lower(ipX, n), and cidr_split(ipXr).

Operators include equality, ordering, containment ( >>=, <<=), intersection ( &&), size estimation ( @, @@), and range construction ( / n, / mask).

Indexing can be done with a standard B‑tree for primary‑key or ordering needs, but the real power comes from GiST indexes on IP ranges, e.g.: CREATE INDEX ip_range_idx ON ipranges USING gist (range); GiST indexes enable fast queries such as WHERE ip_range >>= '10.0.0.5' or WHERE ip_range && '10.0.0.0/8'. Function‑based GiST indexes can also be created on existing CIDR columns:

CREATE INDEX cidr_idx ON tablename USING gist (iprange(cidrcolumn));

Performance comparison demonstrates a 36× speedup when using IP4R with GiST indexes versus a bigint range scan on a real‑world IP dataset. The example shows an EXPLAIN ANALYZE of a lookup using ip_range << '156.80.8.88' that takes ~0.13 ms, compared to ~4.6 ms for the equivalent bigint condition.

Overall, IP4R provides a compact, index‑friendly way to store and query IPv4/IPv6 ranges, making it suitable for large‑scale IP‑based analytics and access‑log processing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLPostgreSQLExtensionIP addressGiST indexIP4R
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.