Since IPV6 requires 128 bits (16 bytes), why in postgres CIDR does the data type have a storage of 24 bytes (8.1) and 19 bytes (9.1)?

I work with ipv4 and ipv6 to store in postgres db.

since ipv4 needs 32 bits (4 bytes) and ipv6 requires 128 bits (16 bytes). Then why in postgres CIDR and the INET data type have storage of 12 bytes and 24 bytes, respectively, for IPV4 and IPV6 (8.1).

since 9.1, it has 7 bytes and 19 bytes respectively for IPV4 and IPV6.

I don’t understand why he needs an extra byte of more than 16 bytes to store IPV6 and 4 bytes for IPV4 ??

http://www.postgresql.org/docs/8.1/static/datatype-net-types.html

http://www.postgresql.org/docs/9.1/interactive/datatype-net-types.html

+5
source share
1 answer

IP :

typedef struct
{
    unsigned char family;       /* PGSQL_AF_INET or PGSQL_AF_INET6 */
    unsigned char bits;         /* number of bits in netmask */
    unsigned char ipaddr[16];   /* up to 128 bits of address */
} inet_struct;

, "" ipaddr (4 IP4, 16 IP6) ( IP4/6).

, varlena, :

/*
 * Both INET and CIDR addresses are represented within Postgres as varlena
 * objects, ie, there is a varlena header in front of the struct type
 * depicted above.  This struct depicts what we actually have in memory
 * in "uncompressed" cases.  Note that since the maximum data size is only
 * 18 bytes, INET/CIDR will invariably be stored into tuples using the
 * 1-byte-header varlena format.  However, we have to be prepared to cope
 * with the 4-byte-header format too, because various code may helpfully
 * try to "decompress" 1-byte-header datums.
 */
typedef struct
{
    char        vl_len_[4];     /* Do not touch this field directly! */
    inet_struct inet_data;
} inet;

, IP4 :

1 byte varlena
1 byte address family
1 byte netmask
4 raw bytes
=========== 
7 byte total

IP6 19 .

. PostgreSQL 4- varlena. 3 (IP4: 10, IP6: 22). , 4 . 2 , 12 24 .

.

+9

All Articles