3

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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm concerned about renaming a table with millions of rows? or is that fine
The number of rows doesn't matter, it's just an update to the system tables. It does require an exclusive lock to do it, but the actual operation is very fast. Updating any dependencies in views or functions is far more problematic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.