List Partitioning Category Pruning

Split a tickets table by region with PARTITION BY LIST: equality and IN predicates prune to the matching child tables and skip the rest.

SqlData Fetching

Code

snippet
-- LIST 分区:按键的离散值划分子表,条件命中即可裁剪
CREATE TABLE tickets (
    id        bigint GENERATED ALWAYS AS IDENTITY,
    region    text NOT NULL,
    title     text NOT NULL,
    opened_at timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY (id, region)
) PARTITION BY LIST (region);

CREATE TABLE tickets_emea PARTITION OF tickets
    FOR VALUES IN ('emea', 'uk');
CREATE TABLE tickets_apac PARTITION OF tickets
    FOR VALUES IN ('apac');
CREATE TABLE tickets_default PARTITION OF tickets DEFAULT;

-- WHERE region = 'emea' 只会打开 tickets_emea 一张子表
SELECT count(*)
FROM tickets
WHERE region = 'emea';

-- IN 列表同样可裁剪:两张子表,default 分区不参与
SELECT count(*)
FROM tickets
WHERE region IN ('emea', 'apac');

How it works

Several statements share this file — SELECT / CREATE, with tickets among the objects involved.

In terms of clauses, the statements use: a WHERE clause narrowing which rows one statement affects; COUNT aggregation; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; DEFAULT values filling omitted columns; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.

Using it

Run the statement against the tickets table.

Key statements

CREATE TABLE tickets (
id        bigint GENERATED ALWAYS AS IDENTITY,
region    text NOT NULL,
title     text NOT NULL,
opened_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id, region)
) PARTITION BY LIST (region);
CREATE TABLE tickets_emea PARTITION OF tickets
FOR VALUES IN ('emea', 'uk');
CREATE TABLE tickets_apac PARTITION OF tickets
FOR VALUES IN ('apac');

Interactive Tools for This Topic

FAQ

What are the relations in play here?
The references are `tickets`.