ip4r
ip4r
ip4r : IPv4/v6 and IPv4/v6 range index type for PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3820 | ip4r | ip4r | 2.4.2 | TYPE | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--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
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| 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 / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
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 cacheInstall 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 14Create this extension with:
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 rangeAddress 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.0Range 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 sizeIndexing
-- 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