ip4r

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

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3820
ip4r
ip4r
2.4.2
TYPE
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
Need By
geoip
See Also
pg_net
prefix
semver
unit
pgpdf
pglite_fusion
md5hash
asn1oid

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PGDG
2.4.2
18
17
16
15
14
ip4r-
RPM
PGDG
2.4.2
18
17
16
15
14
ip4r_$v-
DEB
PGDG
2.4.2
18
17
16
15
14
postgresql-$v-ip4r-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
el8.aarch64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
el9.x86_64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
el9.aarch64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
el10.x86_64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
el10.aarch64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
d12.x86_64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
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
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
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
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
u22.aarch64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
u24.x86_64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
u24.aarch64
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2
PGDG 2.4.2

Source

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install ip4r;		# install via package name, for the active PG version

pig install ip4r -v 18;   # install for PG 18
pig install ip4r -v 17;   # install for PG 17
pig install ip4r -v 16;   # install for PG 16
pig install ip4r -v 15;   # install for PG 15
pig install ip4r -v 14;   # install for PG 14

Create this extension with:

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 updated on