A deep dive into a PostgreSQL patch that adds SortSupport abbreviated keys for the inet and cidr network types, more than doubling their sorting speed. The post explains how inet/cidr values are structured (family, netmask bits, subnet bits), the non-obvious sort semantics (network bits compared first, then netmask size, then subnet), and how an abbreviated key is designed to pack sorting-relevant information into a pointer-sized datum. On 64-bit machines with IPv4, all four sort criteria fit into the key, enabling nearly full sort resolution without heap lookups. The implementation uses bitmasking, byte-swapping for endianness, and bitwise OR assembly. The patch benefits all Postgres installations sorting or indexing on network types.

14m read timeFrom brandur.org
Post cover image
Table of contents
Network type anatomy, and inet vs. cidrSorting rulesDesigning an abbreviated keyBit gymnastics in CSpeed vs. sustainability
7 Impressions