2012-07-18 12 views
5

sto lavorando con ipv4 e ipv6 per archiviare in postgres db.Poiché IPV6 ha bisogno di 128 bit (16 byte), allora perché nel datatype CIDR postgres è presente una memoria di 24 byte (8.1) e 19byte (9.1)?

come ipv4 ha bisogno di 32 bit (4byte) e ipv6 ha bisogno di 128 bit (16byte). Quindi perché in postgres CIDR e INET datatype ha lo storage come 12 byte e 24 byte rispettivamente per IPV4 e IPV6 (8.1).

con 9.1, ha rispettivamente 7 byte e 19 byte per IPV4 e IPV6.

non capisco perché ha bisogno di un byte in più di 16 byte per la memorizzazione di IPV6 e 4 byte per 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

+0

Ha bisogno di almeno +1 byte per i bit maschera di rete, ad esempio "10.1.0.0/8" è valido per la maschera 10.1.0.0 255.0.0.0 –

risposta

9

Il sourcecode per i tipi di dati IP mostrarlo:

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; 

Ciò significa, che in aggiunta ai dati "grezzi" a ipaddr (4 byte per IP4, 16 byte per IP6) c'è un byte per la maschera di rete e un byte per la famiglia di indirizzi (fondamentalmente un interruttore per IP4/IP6).

Inoltre v'è la varlena spese generali di cui si parla nello stesso file:

/* 
* 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; 

Quindi l'equazione per IP4 è questo:

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

Per IP6 la stessa formula ti dà 19 byte.

EDIT Le versioni precedenti di PostgreSQL avevano solo una rappresentazione di varlena di 4 byte. Pertanto è possibile aggiungere 3 byte per ogni tipo (IP4: 10, IP6: 22). Inoltre, c'era un padding fino al prossimo bordo da 4 byte. Questo ti dà 2 byte per ogni tipo aggiungendo fino a 12 o 24 byte.

This mail getta luce sullo sviluppo della versione più corta.

+0

grazie ... @ A.H..it mi ha effettivamente aiutato .. –

Problemi correlati