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

按时间进行范围分区

学习如何按时间范围对大型表进行分区,从而加快近期数据的访问,并高效归档旧数据。

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

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

Why Range Partitioning

Range partitioning splits a table into chunks based on a continuous value, most often a date or timestamp.

It is the most common strategy for time-series data such as logs, events, and orders, because old data can be dropped or archived as a whole partition.

Declaring a Range-Partitioned Table

You declare partitioning with PARTITION BY RANGE on the parent table.

CREATE TABLE events (
    id bigserial,
    created_at timestamptz NOT NULL,
    payload jsonb
) PARTITION BY RANGE (created_at);

Creating Monthly Partitions

Each child partition covers a half-open interval: the lower bound is inclusive and the upper bound is exclusive.

CREATE TABLE events_2024_01 PARTITION OF events
    FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');

CREATE TABLE events_2024_02 PARTITION OF events
    FOR VALUES FROM ('2024-02-01') TO ('2024-03-01');

Half-Open Intervals

The exclusive upper bound prevents gaps and overlaps. A row at exactly 2024-02-01 00:00:00 lands in events_2024_02, never in January.

This makes consecutive partitions perfectly contiguous.

Inserting Routes Automatically

You insert into the parent table. PostgreSQL routes each row to the correct partition based on the range key.

INSERT INTO events (created_at, payload)
VALUES ('2024-01-15', '{"type":"login"}');
-- lands in events_2024_01

A DEFAULT Partition

A DEFAULT partition catches any row that does not match a defined range, avoiding insert errors.

CREATE TABLE events_default PARTITION OF events DEFAULT;

Indexes on Partitions

An index created on the parent is automatically propagated to all current and future partitions.

CREATE INDEX ON events (created_at);

Dropping Old Data Instantly

The killer feature: deleting old data is just DROP TABLE on a partition. No row-by-row DELETE, no bloat, no vacuum pressure.

DROP TABLE events_2024_01;

Querying with Pruning

When the planner sees a range predicate on the partition key it can skip entire partitions. This is called partition pruning.

SELECT count(*) FROM events
WHERE created_at >= '2024-02-10'
  AND created_at <  '2024-02-20';
-- only events_2024_02 is scanned

Automating Partition Creation

Tools like pg_partman create future partitions ahead of time on a schedule, so you never insert into the default partition by accident.

  • Define a retention window
  • Pre-create N future partitions
  • Detach or drop expired ones

Choosing the Range Width

Pick a width that keeps each partition in the tens of millions of rows. Too many tiny partitions hurt planning time; too few huge ones lose pruning benefits.

Quick Check

Why is dropping an old partition better than a bulk DELETE?

Recap

You learned range partitioning by time: declare with PARTITION BY RANGE, create half-open child intervals, rely on automatic routing and pruning, and archive by dropping whole partitions. Automation tools like pg_partman keep the partition set healthy.

常见问题解答

「按时间进行范围分区」课时是免费的吗?

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

「按时间进行范围分区」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 用于分布的哈希分区
  2. 子分区技术
  3. 管理分区表
  4. 按时间进行范围分区
← 返回 Advanced PostgreSQL: Indexing, Partitioning, Replication