Advanced PostgreSQL: Indexing, Partitioning, Replication · 课时

用于分布的哈希分区

实现哈希分区,在各个分区之间均匀分布数据,有助于避免热点。

第 1 / 4 课11 个步骤

用于分布的哈希分区 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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.

免费开始

用 AI 导师学习 Advanced PostgreSQL: Indexing, Partitioning, Replication — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
11
课程
44

常见问题解答

「用于分布的哈希分区」课时是免费的吗?

是的 — 「用于分布的哈希分区」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

「用于分布的哈希分区」这节课中我会学到什么?

实现哈希分区,在各个分区之间均匀分布数据,有助于避免热点。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 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