Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach partition LIST to existing table in postgres 11

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?

like image 306
Troels Avatar asked Jul 15 '19 11:07

Troels


People also ask

How do I add a partition to an existing table?

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.

How many partitions can a Postgres table have?

Postgres 12 – It can handle thousands of partitions. We tested it with 25,000 partitions and sub-partitions on a single table.

Can we create partition on existing table in PostgreSQL?

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.

Can we apply the partitioning on the already existing hive table?

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.


1 Answers

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.

like image 149
a_horse_with_no_name Avatar answered Oct 20 '22 14:10

a_horse_with_no_name