pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Enrico Thierbach <eno@open-lab.org>
To: pgsql-general@lists.postgresql.org
Subject: Re: partitions vs indexes
Date: Thu, 03 Oct 2019 13:34:18 +0200
Message-ID: <3f6a6a4fdc13d825c85f48e89e76757c972e1391.camel@cybertec.at> (raw)
In-Reply-To: <F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org>
References: <F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org>

On Wed, 2019-10-02 at 22:09 +0200, Enrico Thierbach wrote:
> 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.

Have you considered using partial indexes?

CREATE INDEX ..... WHERE status <> 'done'.

Such indexes will be smaller, and any query where "status" is in
the WHERE clause can use the index.

If partitoining is really what you need, you could create the primary
key on the partitions and not on the partitioned table.

That won't guarantee you global uniqueness, but you could rely on
a sequence to do that.

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






view thread (5+ messages)

Message-ID: <3f6a6a4fdc13d825c85f48e89e76757c972e1391.camel@cybertec.at>
Permalink:  ../3f6a6a4fdc13d825c85f48e89e76757c972e1391.camel@cybertec.at/
Also on:    postgresql.org/message-id/3f6a6a4fdc13d825c85f48e89e76757c972e1391.camel@cybertec.at

 · 

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: laurenz.albe@cybertec.at, eno@open-lab.org
  Subject: Re: partitions vs indexes
  In-Reply-To: <3f6a6a4fdc13d825c85f48e89e76757c972e1391.camel@cybertec.at>
  References: <F1986981-6A8F-4FF8-BAEC-D323DCCC3D37@open-lab.org> <3f6a6a4fdc13d825c85f48e89e76757c972e1391.camel@cybertec.at>

* 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