Re: Creating a partition table

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: John Scalia <jayknowsunix(at)gmail(dot)com>, pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Creating a partition table
Date: 2020-10-30 17:01:35
Message-ID: 8cf7a01702a3dae1205e5dcf09b27e083dcd9511.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Fri, 2020-10-30 at 12:52 -0400, John Scalia wrote:
> I have a PostgreSQL server version 11.3 that my devs are asking for some partitioned tables. I can’t seem to get the syntax correct though. My SQL code is:
>
> CREATE TABLE schema.temp_table
> AS (SELECT * FROM schema.original_table)
> PARTITION BY LIST(attribute_name);
>
> As what I want to do is create a copy of the current table, create the partitions on the new temp table, and when everything works, drop the original table and do an alter table rename on the new
> one.
>
> The error is:
> ERROR: syntax error at or near “PARTITION”
> Line 3: PARTITION BY LIST(attribute_name);
>
> I’ve tried using the word RANGE as opposed to LIST, but the error remains. Does PostgreSQL not support a construct like this one or am I doing something wrong?

You cannot do that in a single step. For one, you didn't create partitions.

Do it like this:

CREATE TABLE temp_table (LIKE original_table INCLUDING ALL) PARTITION BY LIST(attribute_name);

CREATE TABLE temp_table_1 PARTITION OF temp_table FOR VALUES IN (1);
CREATE TABLE temp_table_2_3 PARTITION OF temp_table FOR VALUES IN (2, 3);

INSERT INTO temp_table SELECT * FROM original_table;

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2020-10-30 17:07:13 Re: Creating a partition table
Previous Message John Scalia 2020-10-30 16:52:49 Creating a partition table