0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · Lezione

Implementazione del partizionamento hash

Impari come il partizionamento hash di PostgreSQL distribuisca uniformemente le righe su un insieme fisso di partizioni usando un hash della chiave, ideale per bilanciare il carico in assenza di intervalli naturali.

Implementazione del partizionamento hash è una lezione Advanced PostgreSQL: Indexing, Partitioning, Replication gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Advanced PostgreSQL: Indexing, Partitioning, Replication, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Advanced PostgreSQL: Indexing, Partitioning, Replication include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Implementazione del partizionamento hash» è gratuita?

Sì — il testo completo di «Implementazione del partizionamento hash» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Advanced PostgreSQL: Indexing, Partitioning, Replication, passa a CoddyKit PRO. Il corso Advanced PostgreSQL: Indexing, Partitioning, Replication include 4 lezioni in totale.

Cosa imparerò in «Implementazione del partizionamento hash»?

Impari come il partizionamento hash di PostgreSQL distribuisca uniformemente le righe su un insieme fisso di partizioni usando un hash della chiave, ideale per bilanciare il carico in assenza di inte… Eserciti Advanced PostgreSQL: Indexing, Partitioning, Replication con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Advanced PostgreSQL: Indexing, Partitioning, Replication?

Non è richiesta alcuna esperienza precedente. Advanced PostgreSQL: Indexing, Partitioning, Replication su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Implementazione del partizionamento hash»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Advanced PostgreSQL: Indexing, Partitioning, Replication?

Sì. Ogni lezione Advanced PostgreSQL: Indexing, Partitioning, Replication include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Perché il partizionamento?
  2. Configurazione del partizionamento per intervallo
  3. Implementazione del partizionamento per elenco
  4. Implementazione del partizionamento hash
← Torna a Advanced PostgreSQL: Indexing, Partitioning, Replication