0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · レッスン

分散のためのハッシュパーティショニング

データをパーティション間で均等に分散するハッシュパーティショニングを実装します。ホットスポットの回避に役立つ手法です。

「分散のためのハッシュパーティショニング」はCoddyKit上の無料Advanced PostgreSQL: Indexing, Partitioning, Replicationレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAdvanced PostgreSQL: Indexing, Partitioning, Replication学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Advanced PostgreSQL: Indexing, Partitioning, Replicationコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What is Hash Partitioning?

Welcome to Hash Partitioning! This strategy helps distribute data evenly across multiple partitions. Instead of dividing data by ranges or specific values, it uses a hash function.

This approach is especially useful when you want to avoid 'hot spots' where one partition receives significantly more data or queries than others.

Why Use Hash Partitioning?

Hash partitioning is ideal for:

  • Even Distribution: It spreads data uniformly across partitions, preventing any single partition from becoming too large or slow.
  • Avoiding Hot Spots: Useful when your partition key doesn't have a natural order or predictable distribution (unlike time-series data for Range Partitioning).
  • Improved Concurrency: Queries can often operate on different partitions in parallel, boosting performance.

Hash Partitioning Syntax

To use hash partitioning, you define a parent table with PARTITION BY HASH (column_name). Then, you create child partitions using FOR VALUES WITH (MODULUS n, REMAINDER r).

  • MODULUS (n): The total number of partitions you plan to have.
  • REMAINDER (r): A unique number from 0 to n-1, indicating which 'bucket' of hashed values this partition will hold.

Creating a Hash-Partitioned Table

Let's create a table for user activity logs, partitioned by a user_id. We'll start with a parent table and two partitions. Note how the REMAINDER values cover the MODULUS.

CREATE TABLE user_activity (
  activity_id SERIAL,
  user_id INT NOT NULL,
  activity_type VARCHAR(50),
  activity_time TIMESTAMP DEFAULT NOW()
) PARTITION BY HASH (user_id);

CREATE TABLE user_activity_0
PARTITION OF user_activity
FOR VALUES WITH (MODULUS 2, REMAINDER 0);

CREATE TABLE user_activity_1
PARTITION OF user_activity
FOR VALUES WITH (MODULUS 2, REMAINDER 1);

How Data is Distributed

When you insert data, PostgreSQL calculates a hash value for the user_id. It then performs a modulo operation (hash_value % MODULUS). The result determines which partition the row belongs to.

For example, if MODULUS is 2:

  • If hash_value % 2 = 0, it goes to user_activity_0.
  • If hash_value % 2 = 1, it goes to user_activity_1.

Inserting Data Example

Let's insert some data into our user_activity table. PostgreSQL automatically directs each row to the correct hash partition based on the user_id.

Try inserting different user_id values and observe the distribution!

INSERT INTO user_activity (user_id, activity_type) VALUES
(101, 'login'),
(202, 'logout'),
(303, 'view_item'),
(404, 'add_to_cart'),
(101, 'browse_category'),
(505, 'purchase');

SELECT tableoid::regclass as partition_name, *
FROM user_activity
ORDER BY user_id;

Querying Hash Partitions

Querying a hash-partitioned table is just like querying a regular table. PostgreSQL's query planner is smart enough to use partition pruning.

If your query includes the partition key (e.g., WHERE user_id = 101), the planner will only scan the relevant partition, significantly speeding up queries.

SELECT * FROM user_activity WHERE user_id = 101;

-- To see pruning in action:
EXPLAIN SELECT * FROM user_activity WHERE user_id = 101;

Hash vs. Other Partition Types

Unlike Range partitioning (based on intervals) or List partitioning (based on specific values), Hash partitioning doesn't group data logically.

Its strength is in spreading data evenly, making it great for keys without natural ranges (like UUIDs or arbitrary IDs) and for preventing specific parts of your data from becoming bottlenecks.

Considerations & Maintenance

When using hash partitioning:

  • Choose your key wisely: The partition key should have a good distribution of hash values.
  • Modulus & Remainder: Ensure all REMAINDER values from 0 to MODULUS - 1 are covered by partitions.
  • Adding/Removing Partitions: Changing the MODULUS later requires re-hashing all data, which can be complex. Plan your partition count carefully.

Hash Partitioning Quiz

Which scenario is best suited for PostgreSQL hash partitioning?

Hash Partitioning Recap

In this lesson, you've learned about PostgreSQL hash partitioning:

  • It distributes data evenly based on a hash function of the partition key.
  • It uses MODULUS and REMAINDER to define partitions.
  • It's excellent for avoiding hot spots and improving performance for non-sequential or arbitrary keys.
  • Queries benefit from partition pruning when the partition key is used.

Hash partitioning is a powerful tool for scaling your database when even data spread is crucial.

よくある質問

「分散のためのハッシュパーティショニング」レッスンは無料ですか?

はい。「分散のためのハッシュパーティショニング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Advanced PostgreSQL: Indexing, Partitioning, Replicationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Advanced PostgreSQL: Indexing, Partitioning, Replicationコースには全4レッスンが含まれています。

「分散のためのハッシュパーティショニング」で何を学びますか?

データをパーティション間で均等に分散するハッシュパーティショニングを実装します。ホットスポットの回避に役立つ手法です。 ブラウザで直接実行するハンズオンコードでAdvanced PostgreSQL: Indexing, Partitioning, Replicationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Advanced PostgreSQL: Indexing, Partitioning, Replicationを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAdvanced PostgreSQL: Indexing, Partitioning, Replicationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「分散のためのハッシュパーティショニング」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAdvanced PostgreSQL: Indexing, Partitioning, Replicationレッスンでコードを書いて実行できますか?

はい。すべてのAdvanced PostgreSQL: Indexing, Partitioning, Replicationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 分散のためのハッシュパーティショニング
  2. サブパーティショニングの技法
  3. パーティションテーブルの管理
  4. 時間による範囲パーティショニング
← Advanced PostgreSQL: Indexing, Partitioning, Replicationに戻る