I am trying to ALTER a table to use partitions LIST in postgres 11. I have been trying for hours but i keep getting errors.
I have a huge table, clients, with ( client_id, customer_id, value).
I have already created a new empty table, clients, by renaming the old table to clients_old and then created the new table with: CREATE TABLE clients( like clients_old including all)
.
And from here I am stuck when trying to add the LIST partition.
I have tried to:
ALTER TABLE Clients attach PARTITION BY LIST (client_id) --> fail;
ALTER TABLE Clients attach PARTITION LIST (client_id) --> fail;
ALTER TABLE Clients ADD PARTITION LIST (client_id) --> fail;
What syntax should I use to alter the table to use partitions?
Use the ALTER TABLE ADD PARTITION statement to add a new partition to the "high" end (the point after the last existing partition). To add a partition at the beginning or in the middle of a table, use the SPLIT PARTITION clause.
Postgres 12 – It can handle thousands of partitions. We tested it with 25,000 partitions and sub-partitions on a single table.
PostgreSQL allows you to declare that a table is divided into partitions. The table that is divided is referred to as a partitioned table. The declaration includes the partitioning method as described above, plus a list of columns or expressions to be used as the partition key.
Make sure no other process is writing to the table. Create new external table using partitioning. Insert into new table by selecting from the old table. Drop the new table (external), only table will be dropped but data will be there.
Quote from the manual
It is not possible to turn a regular table into a partitioned table or vice versa
So, you can not change an existing non-partitioned table to a partitioned table.
You need to create a new table (with a different name) that is partitioned, create all necessary partitions and then copy the data from the old table to the new, partitioned table.
Something like:
create table clients_partitioned
(
.... all columns ...
)
PARTITION BY LIST (client_id);
Then create the partitions:
create table clients_1
partition of clients_partioned
for values in (1,2,3);
create table clients_1
partition of clients_partioned
for values in (4,5,6);
Then copy the data:
insert into clients_partitioned
select *
from clients;
Once that is done, you can drop the old table and rename the new table:
drop table clients;
alter table clients_partitioned rename to clients;
Don't forget to re-create your foreign keys and indexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With