Continuing our series of PostgreSQL Data Types today we’re going to introduce network address types.

PostgreSQL includes support for both cidr, inet, and macaddr data types. Again, those types are bundled with indexing support and advanced functions and operator support.

Network Address Types

The PostgreSQL documentation chapters entitled Network Address Types and Network Address Functions and Operators cover network address types.

Web servers logs are a classic source of data to process where we find network address types and The Honeynet Project has some free samples for us to play with. This time we’re using the Scan 34 entry. Here’s how to load the sample data set, once cleaned into a proper CSV file:


drop table if exists access_log;

create table access_log
  ip      inet,
  ts      timestamptz,
  request text,
  status  integer

\copy access_log from 'access.csv' with csv delimiter ';'


The script used to cleanse the original data into a CSV that PostgreSQL is happy about implements a pretty simple transformation from - - [13/Mar/2005:04:10:18 -0500] "GET / HTTP/1.1" 403 2898 "-" "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)"


"";"2005-05-13 04:10:18 -0500";"GET / HTTP/1.1";"403"

Being mostly interested into network address types, the transformation from the Apache access log format to CSV is lossy here, we keep only some of the fields we might be interested into.

One of the things that’s possible to implement thanks to the PostgreSQL inet data type is an analysis of /24 networks that are to be found in the logs.

Network Address Masks, CIDR

To enable that analysis, we can use the set_masklen() function which allows us to transforms an IP address into an arbitrary CIDR network address:

select distinct on (ip)
       set_masklen(ip, 24) as inet_24,
       set_masklen(ip::cidr, 24) as cidr_24
  from access_log
 limit 10;

And we can see that if we keep the data type as inet, we still get the full IP address with the /24 network notation added. To have the .0/24 notation we need to be using cidr:

      ip       │     inet_24      │     cidr_24     
═══════════════╪══════════════════╪═════════════════  │  │ │ │ │ │ │ │  │  │   │   │   │   │ │ │  │  │  │  │
(10 rows)

Of course, note that you could be analyzing other networks than /24:

select distinct on (ip)
       set_masklen(ip::cidr, 27) as cidr_27,
       set_masklen(ip::cidr, 28) as cidr_28
  from access_log
 limit 10;

This computes for us the proper starting ip addresses for our CIDR notation for us, of course. After all, what’s the point of using proper data types if not for advanced processing?

      ip       │     cidr_27      │     cidr_28      
═══════════════╪══════════════════╪══════════════════  │  │ │  │ │ │ │ │  │  │   │   │   │   │ │ │  │  │  │   │
(10 rows)

Network Mask Based Reporting

Equipped with this set_masklen() function, it’s now easy to analyze our access logs using arbitrary CIDR network definitions.

  select set_masklen(ip::cidr, 24) as network,
         count(*) as requests,
         array_length(array_agg(distinct ip), 1) as ipcount
    from access_log
group by network
  having array_length(array_agg(distinct ip), 1) > 1
order by requests desc, ipcount desc;

In our case, we get the following result:

     network      │ requests │ ipcount 
══════════════════╪══════════╪═════════   │      140 │       2   │       59 │       2    │       32 │       2     │       25 │      25 │       25 │      24  │        7 │       3  │        6 │       4 │        5 │       5 │        3 │       3   │        2 │       2 │        2 │       2   │        2 │       2
(12 rows)


When analyzing logs containing IP addresses in ipv4 and ipv6 formats is something you need to do, then PostgreSQL has you covered here with its CIDR and INET datatypes. Not only will PostgreSQL make sure that the values managed actually are IP addresses, it knows how to do basic processing of the network addresses.

This article is an extract from my book The Art of PostgresQL, which teaches SQL to developers so that they may replace thousands of lines of code with very simple queries. The book has a full chapter about data types in PostgreSQL, check it out!