ip4r

IPv4/v6 and IPv4/v6 range index type for PostgreSQL

Overview

PackageVersionCategoryLicenseLanguage
ip4r2.4.2TYPEPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
3820ip4rNoYesNoYesNoYes-
Relatedpg_net prefix semver unit pgpdf pglite_fusion md5hash asn1oid
Depended Bygeoip

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG2.4.21817161514ip4r-
RPMPGDG2.4.21817161514ip4r_$v-
DEBPGDG2.4.21817161514postgresql-$v-ip4r-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
d13.x86_64
d13.aarch64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

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

ip4r: IPv4/IPv6 address and range types with GiST indexing

The ip4r extension provides specialized data types for IPv4/IPv6 addresses and ranges with superior indexing for containment queries.

CREATE EXTENSION ip4r;

Data Types

TypeDescription
ip4Single IPv4 address (32-bit)
ip6Single IPv6 address (dual 64-bit)
ip4rIPv4 address range
ip6rIPv6 address range
ipaddressMixed IPv4/IPv6 address
iprangeMixed 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

OperatorDescription
>>=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;

Last Modified 2026-03-12: add pg extension catalog (95749bf)