pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Enrico Thierbach <eno@open-lab.org>
To: pgsql-general@lists.postgresql.org
Subject: partitions vs indexes
Date: Wed, 02 Oct 2019 22:09:52 +0200
Message-ID: <F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org> (raw)

Hello list,

I run into some trouble with partitions:

I would like to convert a table with a primary key into a partitioned 
setup by a column which is not part of the primary key. Also, a column 
might hold a referenece to a parent row. So this is my current table 
setup, slimmed down:


     CREATE TYPE statuses AS ENUM ('ready', ‘processing’, 
‘done’);

     CREATE TABLE mytable (
       id          BIGSERIAL PRIMARY KEY NOT NULL,
       parent_id   BIGINT REFERENCES mytable(id) ON DELETE CASCADE,
       status statuses DEFAULT 'ready'
     );

Since entries in the table are often frequented when status is not 
‘done’ I would like to partition by state. However, if I want to do 
that:

     CREATE TABLE mytable (
       id          BIGSERIAL NOT NULL,
       parent_id   BIGINT REFERENCES mytable(id) ON DELETE CASCADE,
       status statuses DEFAULT 'ready'

       -- UNIQUE(id, status)        -- doesn’t work: can’t reference 
parent
       -- UNIQUE(id)                -- doesn’t work: can’t partition
     ) PARTITION BY LIST(status);

I need to add status to the primary key or another unique constraint. In 
that case, however, I can no longer have the foreign key constraint on 
parent_id.

Weirdly enough partitioning works fine if there is no unique constraint 
on that table:

     CREATE TABLE mytable (
       id          BIGSERIAL NOT NULL,
       status statuses DEFAULT 'ready'
     ) PARTITION BY LIST(status);


So partitioning seems to require the column being in a unique constraint 
if and only if a unique constraint exist on the table. Also I cannot 
create multiple unique constraints on the table.

Here comes my question:

- Do I miss something?
- ThI don’t understand the requirement the partition value to be part 
of a unique constraint if such a constraint exists, since partitioning 
seems to work fine if the table has no unique constraints at all. Can 
someone shed some light on that? Is that maybe an artificial limitation 
that will go away on the future?
- Any suggestions how I could proceed?

Thank you for any suggestion!

Best,
Eno

--
me on github: http://github.com/radiospiel

view thread (5+ messages)  latest in thread

Message-ID: <F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org>
Permalink:  ../F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org/
Also on:    postgresql.org/message-id/F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: eno@open-lab.org
  Subject: Re: partitions vs indexes
  In-Reply-To: <F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org>
  References: <F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox