ip4r
IPv4/v6 and IPv4/v6 range index type for PostgreSQL
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
ip4r | 2.4.2 | TYPE | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3820 | ip4r | No | Yes | No | Yes | No | Yes | - |
| Related | pg_net prefix semver unit pgpdf pglite_fusion md5hash asn1oid |
|---|---|
| Depended By | geoip |
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 2.4.2 | 1817161514 | ip4r | - |
| RPM | PGDG | 2.4.2 | 1817161514 | ip4r_$v | - |
| DEB | PGDG | 2.4.2 | 1817161514 | postgresql-$v-ip4r | - |
Install
You can install ip4r directly. First, make sure the PGDG repository is added and enabled:
pig repo add pgdg -u # Add PGDG repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install ip4r; # Install for current active PG version
pig ext install -y ip4r -v 18 # PG 18
pig ext install -y ip4r -v 17 # PG 17
pig ext install -y ip4r -v 16 # PG 16
pig ext install -y ip4r -v 15 # PG 15
pig ext install -y ip4r -v 14 # PG 14
dnf install -y ip4r_18 # PG 18
dnf install -y ip4r_17 # PG 17
dnf install -y ip4r_16 # PG 16
dnf install -y ip4r_15 # PG 15
dnf install -y ip4r_14 # PG 14
apt install -y postgresql-18-ip4r # PG 18
apt install -y postgresql-17-ip4r # PG 17
apt install -y postgresql-16-ip4r # PG 16
apt install -y postgresql-15-ip4r # PG 15
apt install -y postgresql-14-ip4r # PG 14
Create Extension:
CREATE EXTENSION ip4r;
Usage
The ip4r extension provides specialized data types for IPv4/IPv6 addresses and ranges with superior indexing for containment queries.
CREATE EXTENSION ip4r;
Data Types
| Type | Description |
|---|---|
ip4 | Single IPv4 address (32-bit) |
ip6 | Single IPv6 address (dual 64-bit) |
ip4r | IPv4 address range |
ip6r | IPv6 address range |
ipaddress | Mixed IPv4/IPv6 address |
iprange | Mixed IPv4/IPv6 range |
Address Input
SELECT '192.168.1.1'::ip4;
SELECT '2001:db8::1'::ip6;
SELECT '10.0.0.0/24'::ip4r; -- CIDR notation
SELECT '192.168.1.100-192.168.1.200'::ip4r; -- explicit range
Address Operators
- Comparison:
=,<>,<,>,<=,>= - Arithmetic:
+,-with integers - Bitwise:
&(AND),|(OR),#(XOR),~(NOT)
Address Functions
SELECT family('192.168.1.1'::ipaddress); -- 4
SELECT ip4_netmask(24); -- 255.255.255.0
Range Operators
| Operator | Description |
|---|---|
>>= | Contains or equal |
>> | Strictly contains |
<<= | Contained in or equal |
<< | Strictly contained in |
&& | Overlaps |
Range Functions
SELECT lower('10.0.0.0/24'::ip4r); -- 10.0.0.0
SELECT upper('10.0.0.0/24'::ip4r); -- 10.0.0.255
SELECT is_cidr('10.0.0.0/24'::ip4r); -- true
SELECT cidr_split('10.0.0.0-10.0.0.5'::ip4r); -- decompose to CIDRs
SELECT @ '10.0.0.0/24'::ip4r; -- approximate size
Indexing
-- GiST index for containment queries
CREATE INDEX idx ON ipranges USING gist (range);
-- Find ranges containing a specific IP
SELECT * FROM ipranges WHERE range >>= '10.0.1.15'::ip4;
-- Find most specific match
SELECT * FROM ipranges
WHERE range >>= '10.0.1.15'::ip4
ORDER BY @ range LIMIT 1;
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.