add postgres partitioning to existing table

2.8k Views Asked by At

All examples show:

CREATE TABLE ... PARTITION BY ...

Which is kind of ridiculous, because the only time you would use partitioning is when a dataset has become too large, which by definition is not going to be a new table. If someone is making a new table with partitioning, i think almost anyone would criticize that as a premature optimization.

1

There are 1 best solutions below

2
On BEST ANSWER

Just create a partitioned table and attach the existing table as a partition:

create table test (a int);

insert into test select generate_series(1,10);

alter table test_parent attach partition test DEFAULT;

select * from test_parent;
 a
----
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
(10 rows)

You could also rename the table. However, if you do this, you will need to re-define any views that point at the original table.