Implementação do particionamento por hash
Aprenda como o particionamento por hash do PostgreSQL distribui as linhas uniformemente entre um conjunto fixo de partições usando um hash da chave, sendo ideal para equilibrar a carga sem intervalos naturais.
Implementação do particionamento por hash é uma aula grátis de Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Advanced PostgreSQL: Indexing, Partitioning, Replication, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
When Range and List Do Not Fit
Range partitioning suits dates; list partitioning suits known categories. But what if you just want even distribution with no natural boundaries?
That is where hash partitioning shines.
What Is Hash Partitioning?
Hash partitioning applies a hash function to the partition key and assigns each row to a partition based on the remainder, spreading rows evenly.
Declaring the Parent Table
Create the parent table partitioned BY HASH on a key column.
CREATE TABLE events (
id BIGINT,
user_id BIGINT,
payload TEXT
) PARTITION BY HASH (user_id);Creating Partitions
Each partition declares a modulus and remainder. With modulus 4 you create four partitions, remainders 0 to 3.
CREATE TABLE events_0 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 0);The Full Set
You must create partitions covering every remainder, otherwise some rows have nowhere to go.
CREATE TABLE events_1 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 1);
CREATE TABLE events_2 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 2);
CREATE TABLE events_3 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 3);How Rows Get Routed
On insert, PostgreSQL hashes user_id, takes the remainder modulo 4, and stores the row in the matching partition automatically.
Even Distribution
Because hashing scatters values, partitions stay roughly equal in size, avoiding the hot-partition problem that range partitioning can suffer.
Choosing the Modulus
The modulus equals your number of partitions. Pick it up front, since changing partition count later requires redistributing data.
Indexes per Partition
Create indexes on the parent and PostgreSQL propagates them to all partitions, keeping lookups fast within each shard.
CREATE INDEX ON events (user_id);Hash vs Range vs List
- Range: ordered boundaries (dates, numbers)
- List: discrete known values (regions)
- Hash: even spread when no natural grouping exists
Limitations
Hash partitioning gives no partition pruning for range queries on the key, since values are scattered. It helps point lookups and write distribution, not range scans.
Quick Check
Test your hash partitioning knowledge.
Recap
Hash partitioning spreads rows evenly across a fixed number of partitions using MODULUS and REMAINDER on a hashed key.
It is ideal for balancing writes and point lookups when there is no natural range or list, but it cannot prune partitions for range queries on the key.
Perguntas Frequentes
A aula “Implementação do particionamento por hash” é grátis?
Sim — o texto completo de “Implementação do particionamento por hash” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Advanced PostgreSQL: Indexing, Partitioning, Replication, atualize para CoddyKit PRO. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.
O que vou aprender em “Implementação do particionamento por hash”?
Aprenda como o particionamento por hash do PostgreSQL distribui as linhas uniformemente entre um conjunto fixo de partições usando um hash da chave, sendo ideal para equilibrar a carga sem intervalos… Você pratica Advanced PostgreSQL: Indexing, Partitioning, Replication com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Advanced PostgreSQL: Indexing, Partitioning, Replication?
Nenhuma experiência prévia é necessária. Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Implementação do particionamento por hash”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Advanced PostgreSQL: Indexing, Partitioning, Replication?
Sim. Cada aula de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Por que particionar
- Configuração do particionamento por intervalo
- Implementação do particionamento por lista
- Implementação do particionamento por hash