PgPostgreSQL Lab
GitHub
Browser investigation

Divide a growing table

A February report should avoid January's rows. Use partition boundaries to remove whole relations from the search, then inspect the remaining work.

You will learn to separate pruning from index access, choose half-open time boundaries, and account for uniqueness and retention. Prerequisite: query plans and indexes.

The page loads a fresh order dataset automatically. Select Run SQL in each numbered editor in order. The editors share one database, so later experiments use changes from earlier ones.

Sections on this page

Build time partitions

A partitioned table routes rows to child tables according to a key. A range partition includes its lower bound and excludes its upper bound. Predict where a row at exactly February 1 belongs.

Enable JavaScript to start the browser database.
SQL 01
Ctrl / ⌘ + Enter

Find the January and February counts under partition. tableoid identifies which table stores a row. The fixture ends during February, so both partitions contain data. No default partition is present; an out-of-range insert fails until an appropriate partition exists.

Prune before scanning

Partition pruning eliminates children whose bounds cannot match the query. Predict which relations remain for the February range and inspect the plan.

SQL 02
Ctrl / ⌘ + Enter

The direct range exposes the partition bounds. Wrapping the key in a function can prevent that proof, so inspect whether both children appear in the second plan. A semantically equivalent predicate need not be equally useful to pruning. PostgreSQL can also prune during execution when parameter values become known; a static plan alone may not show all runtime pruning.

Index inside the surviving partition

Pruning does not make the remaining rows free to search. Predict what an index changes for a tenant-specific newest-first report bounded to February.

SQL 03
Ctrl / ⌘ + Enter

The parent index definition creates corresponding child indexes. Bounds can remove January, while the surviving child's index can narrow the tenant and supply time order. These are distinct savings. The composite primary key includes the partition key because uniqueness across partitions must be compatible with the partition scheme. It does not enforce globally unique id by itself.

Make retention a table operation

Predict whether detaching January deletes its rows, and whether parent queries still include them.

SQL 04
Ctrl / ⌘ + Enter

Compare still_in_detached_table and still_in_parent after detaching January. Detachment leaves an independent table. Archival or deletion can follow as a separate decision. The command needs locks; the browser does not demonstrate its blocking behavior on a busy server. More partitions add planning and maintenance overhead, so partitioning every small category is not a universal improvement.

Explain the boundaries

Use a table name from each plan to explain pruning. Which node describes how the surviving table finds the tenant's rows?

A hint for explaining the results

Separate three questions: which relations can contain a match, how a surviving relation finds rows, and what keys the database can prove unique.

Worked explanation

Half-open February bounds let PostgreSQL eliminate January. The tenant/time index can reduce work inside February. The function-wrapped predicate can leave more children in the plan. Detaching January changes the parent's membership without removing the detached data. The primary key protects (id, created_at) rather than id alone.

Go deeper: how the engine works

How does eliminating whole files before opening them compare with eliminating PostgreSQL partitions? Read Where work happens (opens in a new tab) in Query Engine Book. Its DuckDB examples and teaching engine explain the general principle; PostgreSQL's operators and implementation details can differ. This reading is optional.

Transfer the idea

After the original experiments, replace SQL 04 with the following and select Run SQL.

CREATE TABLE orders_mar PARTITION OF orders_by_month
FOR VALUES FROM ('2025-03-01') TO ('2025-04-01');
INSERT INTO orders_by_month (id, created_at, tenant_id)
VALUES (60001, '2025-03-01', 42) RETURNING tableoid::regclass AS partition;
EXPLAIN (COSTS OFF)
SELECT id FROM orders_by_month
WHERE created_at >= '2025-03-01' AND created_at < '2025-04-01';

Which partition accepts midnight on March 1? Find the table name in the plan to check whether PostgreSQL can skip February. What would you verify before dropping or archiving a detached table?

Next: follow a row version.

Primary source: Table partitioning.