AdvancedAdvanced PostgreSQL

Partitioning: list partition by status

The query

SQL
CREATE TABLE orders_partitioned (
  order_id   INT NOT NULL,
  cust_id    INT,
  order_date TIMESTAMP,
  status     VARCHAR(30) NOT NULL,
  total      NUMERIC
) PARTITION BY LIST (status);

-- Create partitions per status
CREATE TABLE orders_pending   PARTITION OF orders_partitioned FOR VALUES IN ('pending');
CREATE TABLE orders_shipped   PARTITION OF orders_partitioned FOR VALUES IN ('shipped');
CREATE TABLE orders_completed PARTITION OF orders_partitioned FOR VALUES IN ('completed');
CREATE TABLE orders_cancelled PARTITION OF orders_partitioned FOR VALUES IN ('cancelled');

-- DEFAULT partition for unmatched values
CREATE TABLE orders_other PARTITION OF orders_partitioned DEFAULT;

-- Query auto-routes to correct partition
SELECT COUNT(*) FROM orders_partitioned WHERE status = 'completed';
Tested against PostgreSQL 16

Note

LIST partitioning for categorical data. Query on status column only scans the matching partition.