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.
Code
-- 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
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SQL FormatterReformat SQL by uppercasing reserved words and breaking lines before SELECT, FROM, WHERE, and other clauses.
FAQ
- What are the relations in play here?
- The references are `tickets`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.