Postgresql – Getting rid of partitioning in Postgresql 9

partitioningpostgresql

it turns out that our approach with list partitioning was not the best idea. We have way to much small partitions and a lot of time is wasted on partition choosing by query optimizer.

What's the easiest way to merge all partitions back into one table? I don't expect any data clash problems.

<!-- language: lang-sql -->
PARENT_TABLE(id,col1,col2,col3,client_id)

CREATE TABLE CHILD_TABLE_1 (CHECK (client_id = '1')) INHERITS (PARENT_TABLE);
CREATE TABLE CHILD_TABLE_2 (CHECK (client_id = '2')) INHERITS (PARENT_TABLE);
CREATE TABLE CHILD_TABLE_3 (CHECK (client_id = '3')) INHERITS (PARENT_TABLE);

Best Answer

The easiest way is to first remove any triggers which direct new rows to the child tables, and then do this for each child table:

with t as (delete from child_table_1 returning *) 
    insert into parent_table select * from t;

The locking and performance implications may dictate against this method, but it is hard to beat it for simplicity.

You might want to wrap it in a transaction block that starts with locking the child table and ends with dropping it, to make sure no one can keep using it for new tuples while you are cleaning out the old ones.