DBAtlas · Database operations atlas

Create / split / drop a partition

Oracle

-- Add a RANGE partition
ALTER TABLE sales ADD PARTITION p2026 VALUES LESS THAN (TO_DATE('2027-01-01','YYYY-MM-DD'));

-- Split an existing partition
ALTER TABLE sales SPLIT PARTITION pmax AT (TO_DATE('2027-01-01','YYYY-MM-DD')) INTO (PARTITION p2026, PARTITION pmax);

-- Drop a partition
ALTER TABLE sales DROP PARTITION p2020 UPDATE INDEXES;

Supports RANGE/LIST/HASH partitioning. DROP PARTITION deletes all rows in that partition; with UPDATE INDEXES, global indexes stay valid. Interval partitioning can add partitions automatically.

PostgreSQL

-- Declarative partitioning: parent table
CREATE TABLE sales (id bigint, created_at date, amount numeric) PARTITION BY RANGE (created_at);

-- Add a new partition
CREATE TABLE sales_2026 PARTITION OF sales FOR VALUES FROM ('2026-01-01') TO ('2027-01-01');

-- Attach a standalone (existing) table as a partition
CREATE TABLE sales_2027 (LIKE sales INCLUDING ALL);
ALTER TABLE sales ATTACH PARTITION sales_2027 FOR VALUES FROM ('2027-01-01') TO ('2028-01-01');

-- Detach a partition (the table is not dropped; it remains a standalone table)
ALTER TABLE sales DETACH PARTITION sales_2026;

Core PostgreSQL has no SPLIT/MERGE PARTITION (as of PG17); to split, DETACH the partition, create two partitions with the new bounds, move the data and ATTACH them. DETACH ... CONCURRENTLY (PG14+) reduces lock duration.

MySQL

-- Partitioned table
CREATE TABLE sales (id BIGINT, created_at DATE) PARTITION BY RANGE (YEAR(created_at)) (
  PARTITION p2024 VALUES LESS THAN (2025),
  PARTITION p2025 VALUES LESS THAN (2026),
  PARTITION pmax VALUES LESS THAN MAXVALUE
);

-- Add / split / drop a partition
ALTER TABLE sales REORGANIZE PARTITION pmax INTO (
  PARTITION p2026 VALUES LESS THAN (2027),
  PARTITION pmax VALUES LESS THAN MAXVALUE
);
ALTER TABLE sales DROP PARTITION p2024;

The partitioning column must be part of every PRIMARY KEY / UNIQUE key. For RANGE, splitting/merging is done with REORGANIZE PARTITION. DROP PARTITION also deletes the rows (RANGE/LIST).

SQL Server

-- 1) Partition function and scheme
CREATE PARTITION FUNCTION pf_date (date) AS RANGE RIGHT FOR VALUES ('2025-01-01','2026-01-01');
CREATE PARTITION SCHEME ps_date AS PARTITION pf_date ALL TO ([PRIMARY]);

-- 2) Add a new boundary (split)
ALTER PARTITION SCHEME ps_date NEXT USED [PRIMARY];
ALTER PARTITION FUNCTION pf_date() SPLIT RANGE ('2027-01-01');

-- 3) Remove a boundary (merge)
ALTER PARTITION FUNCTION pf_date() MERGE RANGE ('2025-01-01');

A partition function + scheme are defined, and the table is created on that scheme with CREATE TABLE ... ON ps_date(created_at). The NEXT USED filegroup must be set before SPLIT. To remove data, switch it out to a staging table with SWITCH PARTITION.

MongoDB

// MongoDB uses sharding instead of table partitioning
sh.shardCollection("db.sales", { created_at: 1 });

// Native time-series collection for time series
db.createCollection("measurements", { timeseries: { timeField: "ts", granularity: "hours" } });

There is no partitioning in the classic sense. Horizontal splitting is done via chunks with sharding; for time-based data, time-series collections provide automatic bucketing.

Redis

# Not applicable: Redis has no table/partition concept; scaling is done with Redis Cluster hash slots (16384).

Redis Cluster distributes keys across 16384 hash slots; this is application-level splitting, not table partitioning. There is no manual partition command.

Elasticsearch

# Time-based automatic splitting (rollover) with an ILM policy + data stream
PUT _ilm/policy/logs-policy
{ "policy": { "phases": { "hot": { "actions": { "rollover": { "max_size": "50gb", "max_age": "30d" } } } } } }

PUT _index_template/logs-tmpl
{ "index_patterns": ["logs*"], "data_stream": {}, "template": { "settings": { "index.lifecycle.name": "logs-policy" } } }

In Elasticsearch, splitting is achieved by dividing an index into shards and, for time series, by creating new backing indices with ILM + data streams (rollover). Old indices can be deleted with ILM.

Open in DBAtlas →