Re: HashTable KeySize

From: Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>
To: Natarajan R <nataraj3098(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: HashTable KeySize
Date: 2019-10-04 12:43:37
Message-ID: 20191004124337.y2n5arasydwlmnz2@development
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Oct 04, 2019 at 05:06:47PM +0530, Natarajan R wrote:
>typedef struct HashTableKey
>{
> Oid dbId; // 4 bytes
> int64 productid; // 8 bytes
>}HashTableKey; (total size - 12 bytes)
>
>typedef struct HashTableEntry
>{
> HashTableKey key;
> ProductInfo *pdt;
>}HashTableEntry;
>
>HASHCTL hashInfo;
>hashInfo.keysize = sizeof(HashTableKey);
>hashInfo.entrysize = sizeof(HashTableEntry);
>SampleHashTable = ShmemInitHash("productid vs product struct HashTable",
>size, size, &hashInfo, HASH_ELEM | HASH_SHARED_MEM | HASH_BLOBS);
>
>while printing keysize: elog(LOG,"Keysize = %d",sizeof(HashTableKey));
>
>I am getting Keysize = 16, How? what should i need to do inorder to have
>keysize = 12

That's likely due to alignment. The second field is a 64-bit value will
be aligned at 8-byte boundary, so in memory the struct will look like
this:

dbId -- 4 bytes
padding -- 4 bytes
productId -- 8 bytes

See

https://en.wikipedia.org/wiki/Data_structure_alignment

and there's also a tool to show the memory layout:

https://linux.die.net/man/1/pahole

regards

--
Tomas Vondra http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2019-10-04 13:14:57 Re: fairywren failures
Previous Message Robert Haas 2019-10-04 12:36:44 Re: Consider low startup cost in add_partial_path