0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · 课时

实现列表分区

了解如何设置列表分区,根据列中的特定离散值对数据进行分段。

实现列表分区 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What is List Partitioning?

Welcome! In this lesson, we'll dive into List Partitioning, a powerful way to organize your data in PostgreSQL.

List partitioning divides a table based on discrete values in a specified column. Think of it as categorizing your data into distinct groups.

List vs. Range: A Quick Look

You might recall Range Partitioning from a previous lesson, which divides data based on a *range* of values (e.g., dates, IDs).

List partitioning is different: it uses specific, distinct values. For example, you could partition by country_code ('US', 'CA', 'MX') or order_status ('PENDING', 'SHIPPED', 'CANCELLED').

Choosing Your Partition Key

The column you choose to partition by is called the partition key. For list partitioning, this column should have a finite and manageable set of distinct values.

  • Good candidates: Country codes, product categories, order statuses.
  • Poor candidates: Timestamps, large text fields, unique IDs (too many distinct values).

Setting Up the Parent Table

First, we create the main table, known as the parent table. We specify PARTITION BY LIST and indicate the column that will serve as our partition key.

Let's create a sales table partitioned by country_code:

CREATE TABLE sales (
    sale_id SERIAL,
    product_id INT,
    country_code CHAR(2),
    amount NUMERIC(10, 2),
    sale_date DATE
) PARTITION BY LIST (country_code);

Defining Your Partitions

Once the parent table is ready, we create individual child partitions. Each child partition is a separate table that stores rows for specific values of the partition key.

Here, we create partitions for sales in the 'US' and 'CA':

CREATE TABLE sales_us PARTITION OF sales
FOR VALUES IN ('US');

CREATE TABLE sales_ca PARTITION OF sales
FOR VALUES IN ('CA');

Data Insertion in Action

When you insert data into the parent sales table, PostgreSQL automatically directs each row to the correct child partition based on its country_code value.

Let's add some sales data and then check the individual partitions:

INSERT INTO sales (product_id, country_code, amount, sale_date) VALUES
(101, 'US', 150.75, '2023-01-05'),
(102, 'CA', 200.00, '2023-01-06'),
(103, 'US', 75.20, '2023-01-07');

SELECT * FROM sales_us;
SELECT * FROM sales_ca;

Inspecting Your Partitions

It's good practice to verify that your partitions are correctly linked to the parent table. You can query PostgreSQL's catalog tables to see the inheritance structure.

This query lists child tables for 'sales':

SELECT relnamespace::regnamespace AS parent_schema,
       relname AS parent_table,
       c.relname AS child_table
FROM pg_class p
JOIN pg_inherits i ON p.oid = i.inhparent
JOIN pg_class c ON i.inhrelid = c.oid
WHERE p.relname = 'sales';

Expanding Your Partition Scheme

As your business expands or new data categories emerge, you can easily add new partitions to your existing scheme without downtime.

Let's add a partition for Mexico ('MX') sales and insert some data:

CREATE TABLE sales_mx PARTITION OF sales
FOR VALUES IN ('MX');

INSERT INTO sales (product_id, country_code, amount, sale_date) VALUES
(104, 'MX', 120.50, '2023-01-08');

SELECT * FROM sales_mx;

Handling Unknown Values with DEFAULT

What happens if you insert a country_code that isn't explicitly covered by a partition (e.g., 'GB')?

You can create a default partition to catch all values that don't match any other defined list. This prevents errors for unexpected data.

CREATE TABLE sales_default PARTITION OF sales
DEFAULT;

INSERT INTO sales (product_id, country_code, amount, sale_date) VALUES
(105, 'GB', 99.99, '2023-01-09');

SELECT * FROM sales_default;

List Partitioning Quiz

Consider a table orders partitioned by list on the status column, which can be 'NEW', 'PROCESSING', 'SHIPPED', or 'CANCELLED'.

List Partitioning Summary

We've covered how to implement list partitioning in PostgreSQL. You learned to:

  • Define a parent table with PARTITION BY LIST.
  • Create child partitions using FOR VALUES IN.
  • Route data automatically upon insertion.
  • Add new partitions and handle unspecified values with a DEFAULT partition.

List partitioning is ideal for data with discrete, categorical values, helping organize and query your data more efficiently.

常见问题解答

「实现列表分区」课时是免费的吗?

是的 — 「实现列表分区」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「实现列表分区」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?

能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为什么要分区
  2. 设置范围分区
  3. 实现列表分区
  4. 哈希分区实现
← 返回 Advanced PostgreSQL: Indexing, Partitioning, Replication