UPDATE Partition Key Moves Rows
Updating a partition key value triggers row movement: the row vanishes from the old partition and appears in the new one automatically.
Code
-- UPDATE 改到 partition key 触发 row movement:rows 从旧分区移除、插入新分区
CREATE TABLE events_2026 (
id int PRIMARY KEY,
happened_on date NOT NULL
) PARTITION BY RANGE (happened_on);
CREATE TABLE events_2026_h1 PARTITION OF events_2026
FOR VALUES FROM ('2026-01-01') TO ('2026-07-01');
CREATE TABLE events_2026_h2 PARTITION OF events_2026
FOR VALUES FROM ('2026-07-01') TO ('2027-01-01');
INSERT INTO events_2026 VALUES
(1, '2026-02-14'),
(2, '2026-08-01');
-- 行 1 起初在 h1 分区
SELECT tableoid::regclass AS partition_name, id, happened_on
FROM events_2026 ORDER BY id;
-- 改日期跨过分区边界:行自动搬家,两张分区表一增一减
UPDATE events_2026 SET happened_on = '2026-09-09' WHERE id = 1;
SELECT tableoid::regclass AS partition_name, id, happened_on
FROM events_2026 ORDER BY id;
DROP TABLE events_2026;Two partitions and one crossing update
events_2026 is declared PARTITION BY RANGE (happened_on) with two halves: events_2026_h1 covers January through June 2026, events_2026_h2 covers July through December. Row 1 (‘2026-02-14’) lands in h1 and row 2 (‘2026-08-01’) in h2. The first inventory query reads tableoid::regclass — the physical table each row lives in — so it names the partition per row.
What moves and where
partition_name | id | happened_on
------------------+----+------------
events_2026_h1 | 1 | 2026-02-14
events_2026_h2 | 2 | 2026-08-01
partition_name | id | happened_on
------------------+----+------------
events_2026_h2 | 1 | 2026-09-09
events_2026_h2 | 2 | 2026-08-01
The single UPDATE events_2026 SET happened_on = '2026-09-09' WHERE id = 1 crosses the partition boundary: PostgreSQL performs row movement — the row is removed from h1 and inserted into h2 — so both physical partitions change with one statement and no manual copy step. The second query’s tableoid output proves row 1 now reports h2.
The line that triggers the move
UPDATE events_2026 SET happened_on = '2026-09-09' WHERE id = 1;
The new value decides the target partition automatically; the only rule is that it must fall inside some partition’s range, or the update fails instead of relocating.
Interactive Tools for This Topic
FAQ
- What objects does the SQL involve?
- `events_2026`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.