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

時間による範囲パーティショニング

大規模なテーブルを時間範囲でパーティショニングし、最近のデータへのアクセスを高速に保ちながら、古いデータを効率的にアーカイブする方法を学びます。

「時間による範囲パーティショニング」はCoddyKit上の無料Advanced PostgreSQL: Indexing, Partitioning, Replicationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「時間による範囲パーティショニング」レッスンは無料ですか?

はい。「時間による範囲パーティショニング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと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は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン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に戻る