pgpartman / pg_partman

Partition management extension for PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change partition key in existing table

geoemm opened this issue · comments

Hello,

I have a table using partition by range of a timestamp column and I would like to change that column for another of the same type. Is there any guide for that? Ideally I would like for this to happen in place without having to take offline the table or recreate another one and move data.

I'm not aware of any guides for this and unfortunately I don't believe this could be done easily without at least some outages. There is no ability to change the partition key that I'm aware of so you would need to create a new table then move the child tables from the old one to the new one using detach and then attach with the new partition boundaries for the new column(s) defined. So the length of the outage would be the time it takes to move those child tables. So it's not a table rewrite, but it may take some time to validate the boundary constraints when you reattach the child tables.

Actually, just realized the other problem here. The child tables are likely not going to have their data split up on clean boundaries for the new column and will also likely have their data duplicated across the child tables. So unless you lucked out in having them already sorted like that, this is likely going to require a rewrite of the table to move the data to the relevant children.

The only thing that's popping into my head at the moment to possibly reduce downtime would be to make a higher level table that inherits both the old and new table. You're not changing the column structure of the table as part of this, so all 3 tables in the inheritance set would be the same, so the top level table would be able to see all the data as it moves from one table to another. The caveat would be that any data that is in transit would likely be inaccessible due to the exclusive row locks required to move from one table to another.

Thank you very much for your input!