Senior
How did you perform sharding in PostgreSQL? What hashing algorithm did you use?
sobes.tech AI
Answer from AI
In PostgreSQL, sharding is usually implemented at the application level or using extensions, as there is no built-in full sharding support.
For application-level sharding, I used a hash function to distribute data across different databases or tables. The most common algorithms are consistent hashing or simple hash-based partitioning (e.g., by user_id) using standard hash functions like MurmurHash or CRC32.
An example of simple hashing for shard selection:
func getShard(key string, shardCount int) int {
h := fnv.New32a()
h.Write([]byte(key))
return int(h.Sum32()) % shardCount
}
This allows for even distribution of records across shards and easy system scaling by adding new shards.